View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes;
9   
10  import org.apache.log4j.Logger;
11  
12  /**
13   * Status of Workflow
14   * @author Andreas Hoheisel
15   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
16   * @version $Id: WorkflowStatus.java 1484 2011-02-14 17:02:26Z hoheisel $
17   */
18  public class WorkflowStatus {
19  
20      /**
21       * log4j logger
22       */
23      private static Logger logger = Logger.getLogger(WorkflowStatus.class);
24  
25      /**
26       * Status <B>UNDEFINED = 0</B>. The status remains <CODE>UNDEFINED</CODE> from the construction of the workflow
27       * until the method <CODE>initiateWorkflow()</CODE> has been called.
28       *
29       * @see #STATUS_INITIATED
30       * @see #STATUS_RUNNING
31       * @see #STATUS_SUSPENDED
32       * @see #STATUS_ACTIVE
33       * @see #STATUS_TERMINATED
34       * @see #STATUS_COMPLETED
35       */
36      public static final int STATUS_UNDEFINED = 0;
37      public static final String STATUS_UNDEFINED_STR = "UNDEFINED";
38  
39      /**
40       * Status <B>INITIATED = 1</B>. The workflow has been created and initialized but not yet been started or aborted.
41       *
42       * @see #STATUS_UNDEFINED
43       * @see #STATUS_RUNNING
44       * @see #STATUS_SUSPENDED
45       * @see #STATUS_ACTIVE
46       * @see #STATUS_TERMINATED
47       * @see #STATUS_COMPLETED
48       */
49      public static final int STATUS_INITIATED = 1;
50      public static final String STATUS_INITIATED_STR = "INITIATED";
51  
52      /**
53       * Status <B>RUNNING = 2</B>. The workflow has been started but it is not yet active or complete and it has not
54       * been suspended or aborted.
55       *
56       * @see #STATUS_UNDEFINED
57       * @see #STATUS_INITIATED
58       * @see #STATUS_SUSPENDED
59       * @see #STATUS_ACTIVE
60       * @see #STATUS_TERMINATED
61       * @see #STATUS_COMPLETED
62       */
63      public static final int STATUS_RUNNING = 2;
64      public static final String STATUS_RUNNING_STR = "RUNNING";
65  
66      /**
67       * Status <B>SUSPENDED = 3</B>. The workflow is quiescent and can be returned
68       * to the running state via <CODE>resumeWorkflow()</CODE>.
69       *
70       * @see #STATUS_UNDEFINED
71       * @see #STATUS_INITIATED
72       * @see #STATUS_RUNNING
73       * @see #STATUS_ACTIVE
74       * @see #STATUS_TERMINATED
75       * @see #STATUS_COMPLETED
76       */
77      public static final int STATUS_SUSPENDED = 3;
78      public static final String STATUS_SUSPENDED_STR = "SUSPENDED";
79  
80      /**
81       * Status <B>ACTIVE = 4</B>. One ore more activity instances of this workflow are allocated.
82       *
83       * @see net.kwfgrid.gwes.Activity
84       * @see #STATUS_UNDEFINED
85       * @see #STATUS_INITIATED
86       * @see #STATUS_RUNNING
87       * @see #STATUS_SUSPENDED
88       * @see #STATUS_TERMINATED
89       * @see #STATUS_COMPLETED
90       */
91      public static final int STATUS_ACTIVE = 4;
92      public static final String STATUS_ACTIVE_STR = "ACTIVE";
93  
94      /**
95       * Status <B>TERMINATED = 5</B>. The execution of this workflow instance has aborted before its normal completion.
96       * This can be due to an workflow exception (workflow failed) or via the method <CODE>abortWorkflow()</CODE>.
97       *
98       * @see #STATUS_UNDEFINED
99       * @see #STATUS_INITIATED
100      * @see #STATUS_RUNNING
101      * @see #STATUS_SUSPENDED
102      * @see #STATUS_ACTIVE
103      * @see #STATUS_COMPLETED
104      */
105     public static final int STATUS_TERMINATED = 5;
106     public static final String STATUS_TERMINATED_STR = "TERMINATED";
107 
108     /**
109      * Status <B>COMPLETED = 6</B>. The workflow instance has fulfilled the conditions for completion. The workflow
110      * instance will be destroyed.
111      *
112      * @see #STATUS_UNDEFINED
113      * @see #STATUS_INITIATED
114      * @see #STATUS_RUNNING
115      * @see #STATUS_SUSPENDED
116      * @see #STATUS_ACTIVE
117      * @see #STATUS_TERMINATED
118      */
119     public static final int STATUS_COMPLETED = 6;
120     public static final String STATUS_COMPLETED_STR = "COMPLETED";
121 
122     /**
123      * Status <B>FAILED = 7</B>. One or more activities have failed. They will be retried.
124      *
125      * @see #STATUS_UNDEFINED
126      * @see #STATUS_INITIATED
127      * @see #STATUS_RUNNING
128      * @see #STATUS_SUSPENDED
129      * @see #STATUS_ACTIVE
130      * @see #STATUS_TERMINATED
131      * @see #STATUS_COMPLETED
132      */
133     public static final int STATUS_FAILED = 7;
134     public static final String STATUS_FAILED_STR = "FAILED";
135 
136     /**
137      * Convert the status of a workflow from an integer to a string.
138      *
139      * @param status The status code with should be converted into a string.
140      * @return string representing the status that corresponds to the status code.
141      */
142     public static String getStatusAsString(int status) {
143         if (status == STATUS_UNDEFINED) {
144             return STATUS_UNDEFINED_STR;
145         } else if (status == STATUS_INITIATED) {
146             return STATUS_INITIATED_STR;
147         } else if (status == STATUS_RUNNING) {
148             return STATUS_RUNNING_STR;
149         } else if (status == STATUS_ACTIVE) {
150             return STATUS_ACTIVE_STR;
151         } else if (status == STATUS_COMPLETED) {
152             return STATUS_COMPLETED_STR;
153         } else if (status == STATUS_TERMINATED) {
154             return STATUS_TERMINATED_STR;
155         } else if (status == STATUS_SUSPENDED) {
156             return STATUS_SUSPENDED_STR;
157         } else if (status == STATUS_FAILED) {
158         	return STATUS_FAILED_STR;
159         }
160         logger.error("Unknown status code: " + status);
161         return "NN";
162     }
163 
164     /**
165      * Convert the status of a workflow from an string to int.
166      *
167      * @param statusStr The status string with should be converted into int.
168      * @return status code.
169      */
170     public static int getStatusAsInt(String statusStr) {
171         if (statusStr.equals(STATUS_UNDEFINED_STR)) {
172             return STATUS_UNDEFINED;
173         } else if (statusStr.equals(STATUS_INITIATED_STR)) {
174             return STATUS_INITIATED;
175         } else if (statusStr.equals(STATUS_RUNNING_STR)) {
176             return STATUS_RUNNING;
177         } else if (statusStr.equals(STATUS_ACTIVE_STR)) {
178             return STATUS_ACTIVE;
179         } else if (statusStr.equals(STATUS_COMPLETED_STR)) {
180             return STATUS_COMPLETED;
181         } else if (statusStr.equals(STATUS_TERMINATED_STR)) {
182             return STATUS_TERMINATED;
183         } else if (statusStr.equals(STATUS_SUSPENDED_STR)) {
184             return STATUS_SUSPENDED;
185         } else if (statusStr.equals(STATUS_FAILED_STR)) {
186             return STATUS_FAILED;
187         } else if (statusStr.equals(""))
188         logger.error("Unknown status string: " + statusStr);
189         return -1;
190     }
191 }