View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.jpdl3;
25  
26  import java.io.InputStream;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Set;
31  import org.jbpm.graph.def.Node;
32  import org.jbpm.graph.def.ProcessDefinition;
33  import org.jbpm.graph.def.Transition;
34  import org.jbpm.graph.node.EndState;
35  import org.jbpm.graph.node.StartState;
36  import org.jbpm.graph.node.TaskNode;
37  import org.jbpm.instantiation.Delegation;
38  import org.jbpm.taskmgmt.def.Swimlane;
39  import org.jbpm.taskmgmt.def.Task;
40  import org.jbpm.taskmgmt.def.TaskMgmtDefinition;
41  import static org.modeshape.sequencer.jpdl3.JPDL3MetadataConstants.*;
42  
43  /**
44   * The jBPM Process definition language meta data.
45   * 
46   * @author Serge Pagop
47   */
48  public class JPDL3Metadata {
49  
50      /**
51       * The process definition name.
52       */
53      private String pdName;
54  
55      /**
56       * The start node of the process definition.
57       */
58      private JPDL3StartStateMetadata jPDL3StartStateMetadata;
59  
60      /**
61       * The end node of the process definition.
62       */
63      private JPDL3EndStateMetadata jPDL3EndStateMetadata;
64  
65      /**
66       * The swimlanes of the process definitions
67       */
68      List<JPDL3SwimlaneMetadata> swimlanes = new ArrayList<JPDL3SwimlaneMetadata>();
69  
70      /**
71       * The task nodes of the process definitions.
72       */
73      private List<JPDL3TaskNodeMetadata> taskNodes = new ArrayList<JPDL3TaskNodeMetadata>();
74  
75      private JPDL3Metadata() {
76          // prevent construction
77      }
78  
79      /**
80       * Create an instance of {@link JPDL3Metadata} with all data of a specific jpdl xml document.
81       * 
82       * @param stream - the {@link InputStream}, that represents a stream of jpdl.
83       * @return a object of {@link JPDL3Metadata}.
84       */
85      @SuppressWarnings( {"unchecked", "cast"} )
86      public static JPDL3Metadata instance( InputStream stream ) {
87          ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(stream);
88          List<JPDL3SwimlaneMetadata> swimlaneContainer = new ArrayList<JPDL3SwimlaneMetadata>();
89          List<JPDL3TaskNodeMetadata> taskNodeContainer = new ArrayList<JPDL3TaskNodeMetadata>();
90  
91          if (processDefinition != null) {
92              JPDL3Metadata jplMetadata = new JPDL3Metadata();
93              if (processDefinition.getName() != null) {
94                  jplMetadata.setPdName(processDefinition.getName());
95              }
96  
97              TaskMgmtDefinition taskMgmtDefinition = processDefinition.getTaskMgmtDefinition();
98              if (taskMgmtDefinition != null) {
99                  // Get the swimlanes of the process definition, if there is one.
100                 Map<String, Swimlane> mapOfSwimlanes = taskMgmtDefinition.getSwimlanes();
101                 Set<String> swimlaneKeys = mapOfSwimlanes.keySet();
102                 for (String swimlaneKey : swimlaneKeys) {
103                     Swimlane swimlane = mapOfSwimlanes.get(swimlaneKey);
104                     JPDL3SwimlaneMetadata jPDL3SwimlaneMetadata = new JPDL3SwimlaneMetadata();
105                     jPDL3SwimlaneMetadata.setName(swimlane.getName());
106                     if (swimlane.getActorIdExpression() != null) jPDL3SwimlaneMetadata.setActorIdExpression(swimlane.getActorIdExpression());
107                     if (swimlane.getPooledActorsExpression() != null) jPDL3SwimlaneMetadata.setPooledActorsExpression(swimlane.getPooledActorsExpression());
108                     Delegation delegation = swimlane.getAssignmentDelegation();
109                     if (delegation != null) {
110                         JPDL3AssignmentMetadata jPDL3AssignmentMetadata = new JPDL3AssignmentMetadata();
111                         // full qualified class name.
112                         jPDL3AssignmentMetadata.setFqClassName(delegation.getClassName());
113                         // config type
114                         if (delegation.getConfigType() != null) jPDL3AssignmentMetadata.setConfigType(delegation.getConfigType());
115                         // expression assignment
116                         if (EXPRESSION_ASSIGNMENT_HANLDER_DELEGATION_CN.equals(delegation.getClassName())) jPDL3AssignmentMetadata.setExpression(delegation.getConfiguration());
117                         jPDL3SwimlaneMetadata.setAssignment(jPDL3AssignmentMetadata);
118                     }
119                     swimlaneContainer.add(jPDL3SwimlaneMetadata);
120                     // with expression
121                 }
122             }
123             jplMetadata.setSwimlanes(swimlaneContainer);
124 
125             List<Node> nodes = (List<Node>)processDefinition.getNodes();
126 
127             for (Node node : nodes) {
128                 if (node instanceof StartState) {
129                     StartState startState = (StartState)node;
130                     JPDL3StartStateMetadata jPDL3StartStateMetadata = new JPDL3StartStateMetadata();
131                     if (startState.getName() != null) {
132                         jPDL3StartStateMetadata.setName(startState.getName());
133                     }
134                     List<JPDL3TransitionMetadata> transitions = new ArrayList<JPDL3TransitionMetadata>();
135                     for (Transition transition : (List<Transition>)startState.getLeavingTransitions()) {
136                         JPDL3TransitionMetadata jPDL3TransitionMetadata = new JPDL3TransitionMetadata();
137                         if (transition.getName() != null) {
138                             jPDL3TransitionMetadata.setName(transition.getName());
139                         }
140                         Node toNode = transition.getTo();
141                         if (toNode != null) {
142                             jPDL3TransitionMetadata.setTo(toNode.getName());
143                         }
144                         transitions.add(jPDL3TransitionMetadata);
145                     }
146                     jPDL3StartStateMetadata.setTransitions(transitions);
147                     jplMetadata.setStartStateMetadata(jPDL3StartStateMetadata);
148                 }
149 
150                 if (node instanceof EndState) {
151                     EndState endState = (EndState)node;
152                     JPDL3EndStateMetadata jPDL3EndStateMetadata = new JPDL3EndStateMetadata();
153                     if (endState.getName() != null) {
154                         jPDL3EndStateMetadata.setName(endState.getName());
155                     }
156                     jplMetadata.setEndStateMetadata(jPDL3EndStateMetadata);
157                 }
158                 
159                 // TaskNode
160                 if (node instanceof TaskNode) {
161                     TaskNode taskNode = (TaskNode)node;
162                     JPDL3TaskNodeMetadata jPDL3TaskNodeMetadata = new JPDL3TaskNodeMetadata();
163                     
164                     if(taskNode.getName() != null) {
165                         jPDL3TaskNodeMetadata.setName(taskNode.getName()); 
166                     }
167                     
168                     Map<String, Task> tasks = taskNode.getTasksMap();
169                     List<JPDL3TaskMetadata> taskList = new ArrayList<JPDL3TaskMetadata>();
170                     
171                     if(!tasks.isEmpty()) {
172                         Set<String> keys = tasks.keySet();
173                         for (String key : keys) {
174                             Task task = tasks.get(key);
175                             JPDL3TaskMetadata jPDL3TaskMetadata = new JPDL3TaskMetadata();
176                             if(task.getName() != null)
177                                 jPDL3TaskMetadata.setName(task.getName());
178                             if(task.getDueDate() != null)
179                                 jPDL3TaskMetadata.setDueDate(task.getDueDate());
180                             taskList.add(jPDL3TaskMetadata);
181                             
182                             if(task.getSwimlane() != null) {
183                                 Swimlane swimlane = task.getSwimlane();
184                                 jPDL3TaskMetadata.setSwimlane(swimlane.getName());
185                             }
186                         }
187                     }
188                     jPDL3TaskNodeMetadata.setTasks(taskList);
189                     
190                     // transitions
191                     List<JPDL3TransitionMetadata> transitions = new ArrayList<JPDL3TransitionMetadata>();
192                     for (Transition transition : (List<Transition>)taskNode.getLeavingTransitions()) {
193                         JPDL3TransitionMetadata jPDL3TransitionMetadata = new JPDL3TransitionMetadata();
194                         if (transition.getName() != null) {
195                             jPDL3TransitionMetadata.setName(transition.getName());
196                         }
197                         Node toNode = transition.getTo();
198                         if (toNode != null) {
199                             jPDL3TransitionMetadata.setTo(toNode.getName());
200                         }
201                         transitions.add(jPDL3TransitionMetadata);
202                     }
203                     jPDL3TaskNodeMetadata.setTransitions(transitions);
204                     
205                     taskNodeContainer.add(jPDL3TaskNodeMetadata);
206                     jplMetadata.setTaskNodes(taskNodeContainer);
207                 }
208             }
209             return jplMetadata;
210         }
211         return null;
212     }
213 
214     /**
215      * Get the name of process definition.
216      * 
217      * @return the name of the process definition.
218      */
219     public String getPdName() {
220         return pdName;
221     }
222 
223     /**
224      * Set the name of process definition.
225      * 
226      * @param pdName - the name of process definition.
227      */
228     public void setPdName( String pdName ) {
229         this.pdName = pdName;
230     }
231 
232     /**
233      * @return the jPDL3StartStateMetadata.
234      */
235     public JPDL3StartStateMetadata getStartStateMetadata() {
236         return this.jPDL3StartStateMetadata;
237     }
238 
239     /**
240      * @return the jPDL3EndStateMetadata.
241      */
242     public JPDL3EndStateMetadata getEndStateMetadata() {
243         return this.jPDL3EndStateMetadata;
244     }
245 
246     /**
247      * @param jPDL3StartStateMetadata the jPDL3StartStateMetadata to set
248      */
249     public void setStartStateMetadata( JPDL3StartStateMetadata jPDL3StartStateMetadata ) {
250         this.jPDL3StartStateMetadata = jPDL3StartStateMetadata;
251     }
252 
253     /**
254      * @param jPDL3EndStateMetadata the jPDL3EndStateMetadata to set
255      */
256     public void setEndStateMetadata( JPDL3EndStateMetadata jPDL3EndStateMetadata ) {
257         this.jPDL3EndStateMetadata = jPDL3EndStateMetadata;
258     }
259 
260     /**
261      * Get a list of all swimlane of the process definition
262      * 
263      * @return a list of all swimlane of the process definition. this can also be a empty list.
264      */
265     public List<JPDL3SwimlaneMetadata> getSwimlanes() {
266         return this.swimlanes;
267     }
268 
269     /**
270      * Set a list with some swimlanes for the process definition.
271      * 
272      * @param swimlanes - the swimlanes.
273      */
274     public void setSwimlanes( List<JPDL3SwimlaneMetadata> swimlanes ) {
275         this.swimlanes = swimlanes;
276     }
277 
278     /**
279      * @return the task nodes
280      */
281     public List<JPDL3TaskNodeMetadata> getTaskNodes() {
282         return this.taskNodes;
283     }
284 
285     /**
286      * @param taskNodes Sets taskNodes to the specified value.
287      */
288     public void setTaskNodes( List<JPDL3TaskNodeMetadata> taskNodes ) {
289         this.taskNodes = taskNodes;
290     }
291 }