package uk.org.sappho.jira.workflow.approvals;

import java.util.List;

import org.apache.log4j.Logger;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.MutableIssue;
import com.opensymphony.workflow.WorkflowException;

public class WorkflowConfiguration {

    private int transitionActionId;
    private String requiredStatus;
    private static final Logger log = Logger.getLogger(WorkflowConfiguration.class);

    public WorkflowConfiguration(String project, String approvalType, MutableIssue parentIssue)
            throws WorkflowException {

        ApprovalsConfiguration approvalsConfiguration = ApprovalsConfiguration.getInstance();
        List<String> workflowNames =
                approvalsConfiguration.getPropertyList(project, "parent.issue.workflow.name." + approvalType);
        List<String> workflowActions =
                approvalsConfiguration.getPropertyList(project, "parent.issue.workflow.auto.transition.action.id."
                        + approvalType);
        List<String> workflowStatuses =
                approvalsConfiguration.getPropertyList(project, "parent.issue.workflow.status." + approvalType);
        String issueWorkflowName =
                ComponentManager.getInstance().getWorkflowManager().getWorkflow(parentIssue).getName();
        log.warn("Found workflow " + issueWorkflowName + " on " + parentIssue.getKey());
        int index = 0;
        boolean workflowFound = false;
        for (String workflowName : workflowNames) {
            if (workflowName.equals(issueWorkflowName)) {
                try {
                    transitionActionId = Integer.parseInt(workflowActions.get(index));
                } catch (Throwable t) {
                    throw new WorkflowException("Auto-transition action ID for workflow " + issueWorkflowName
                            + " and approval type " + approvalType + " is incorrect!", t);
                }
                try {
                    requiredStatus = workflowStatuses.get(index);
                } catch (Throwable t) {
                    throw new WorkflowException("Auto-transition action ID for workflow " + issueWorkflowName
                            + " and approval type " + approvalType + " is incorrect!", t);
                }
                workflowFound = true;
                break;
            }
            index++;
        }
        if (!workflowFound)
            throw new WorkflowException("Configuration for workflow " + issueWorkflowName + " and approval type "
                    + approvalType + " is incorrect!");
    }

    public int getTransitionActionId() {

        return transitionActionId;
    }

    public String getRequiredStatus() {

        return requiredStatus;
    }
}

@@ -1582,80 +1582,33 @@
     
-try %7B%0A                    transitionActionId = Integer.parseInt(
+requiredStatus = 
 workflow
 Acti
@@ -1603,22 +1603,23 @@
 workflow
-Action
+Statuse
 s.get(in
@@ -1622,17 +1622,16 @@
 t(index)
-)
 ;%0A      
@@ -1640,39 +1640,43 @@
         
-%7D catch (Throwable t) %7B
+if (requiredStatus == null)
 %0A       
@@ -1721,33 +1721,23 @@
 on(%22
-Auto-transition action ID
+Required status
  for
@@ -1846,43 +1846,20 @@
  is 
-incorrect!%22, t
+missing!%22
 );%0A
-                %7D%0A
     
@@ -1900,33 +1900,54 @@
     
-requiredStatus = 
+transitionActionId = Integer.parseInt(
 workflow
 Stat
@@ -1934,39 +1934,38 @@
 arseInt(workflow
-Statuse
+Action
 s.get(index);%0A  
@@ -1952,32 +1952,33 @@
 tions.get(index)
+)
 ;%0A              
@@ -2522,24 +2522,107 @@
 correct!%22);%0A
+        log.warn(%22Status/ActionId: %22 + requiredStatus + %22/%22 + transitionActionId);%0A
     %7D%0A%0A    p

Clean up workflow configuration.