/*
 * Created on Jul 9, 2008 (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.watchpoint.pmm.action.schedule.creation;

import java.io.IOException;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.action.ActionException;
import com.tandbergtv.watchpoint.pmm.util.TemplateProperties;
import com.tandbergtv.workflow.util.ApplicationProperties;

/**
 * Generates the URLs for the Schedule Approval and Edit actions.
 * 
 * @author Vijay Silva
 */
public class ConstructScheduleURL implements ActionHandler {

	/* Serialization UID */
	private static final long serialVersionUID = 7449377000572797225L;

	/* Property for the URL path relative to Workflow Application URL for editing a schedule */
	private static final String EDIT_URL = "ScheduleCreation.ScheduleEditURL";

	/* Property for the URL path relative to Workflow Application URL for approving a schedule */
	private static final String APPROVAL_URL = "ScheduleCreation.ScheduleApprovalURL";

	/**
	 * Build the URLs required for editing and approving a Schedule
	 * 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws ActionException {
		try {
			this.buildURLs(context);
		} catch (ActionException e) {
			throw e;
		} catch (Exception e) {
			String msg = "Failed to build URLs required to edit and approve a schedule, error: "
					+ e.getMessage();
			throw new ActionException(msg, e);
		}
	}

	/* Build the Edit and Approval URLs for the Schedule */
	private void buildURLs(ExecutionContext context) throws ActionException {
		/* Get the Schedule Id */
		String scheduleIdValue = this.getStringValue(context, Variables.SCHEDULE_ID);
		Long scheduleId = new Long(scheduleIdValue);

		/* Get the workflow application URL */
		String applicationURL = this.getApplicationProperties().getProperty("application.url");
		if (!applicationURL.endsWith("/"))
			applicationURL += "/";

		/* build the URLs and store the variables */
		String editURL = applicationURL + TemplateProperties.getString(EDIT_URL) + scheduleId;
		String approvalURL = applicationURL + TemplateProperties.getString(APPROVAL_URL)
				+ scheduleId;
		context.setVariable(Variables.SCHEDULE_EDIT_URL, editURL);
		context.setVariable(Variables.SCHEDULE_APPROVAL_URL, approvalURL);
	}

	/* Get a String value for a variable in the context */
	private String getStringValue(ExecutionContext context, String name) {
		Object value = context.getVariable(name);
		return (value != null) ? value.toString() : null;
	}

	/* Get the Application Properties for the Workflow Application */
	private ApplicationProperties getApplicationProperties() throws ActionException {
		try {
			return ApplicationProperties.getInstance();
		} catch (IOException e) {
			String msg = "Failed to the get Workflow Application Properties, error: "
					+ e.getMessage();
			throw new ActionException(msg, e);
		}
	}
}
