package com.ttv.acs.stub.adi;

import com.tandbergtv.workflow.message.WorkflowPayload;

/**
 * Represents an work order request. An implementor of this interface should know be able populate a message payload
 * 
 * @author carlos
 * 
 */
public abstract class AbstractWorkOrderRequest {

	protected static final int MAX_PARAMETER_SIZE = 4000;

	private String messageUID;

	/**
	 * 
	 * @param messageUID
	 *            The UID of the message that will start the workorder
	 */
	public AbstractWorkOrderRequest(String messageUID) {
		this.messageUID = messageUID;
	}

	/**
	 * Returns true if the provision succeeded
	 * 
	 * @return
	 */
	public abstract boolean succeeded();

	/**
	 * Returns PConstants.SUCCESS or PConstants.FAILURE
	 * 
	 * @return
	 */
	public abstract String getSuccess();

	/**
	 * Returns the error message or "" if the provision succeeded
	 * 
	 * @return
	 */
	public abstract String getMessage();

	/**
	 * Populates the workflow message payload
	 * 
	 * @param payload
	 */
	public abstract void populatePayload(WorkflowPayload payload);

	/**
	 * Returns the UID of the message that will start the work order
	 * 
	 * @return
	 */
	public String getMessageUID() {
		return messageUID;
	}

	/**
	 * Puts the parameter in the provided payload. The string value of the parameter is truncated at MAX_PARAMETER_SIZE
	 * characters.
	 * 
	 * @param payload
	 * @param parameterName
	 * @param value
	 */
	protected void putValue(WorkflowPayload payload, String parameterName, Object value) {
		String strValue;

		if (value == null) {
			strValue = "";
		} else if (value.toString().length() > MAX_PARAMETER_SIZE) {
			strValue = value.toString().substring(0, MAX_PARAMETER_SIZE);
		} else {
			strValue = value.toString();
		}

		payload.putValue(parameterName, strValue);
	}

}
