001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.sequencer.jpdl3;
025    
026    import java.io.InputStream;
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Map;
030    import java.util.Set;
031    import org.jbpm.graph.def.Node;
032    import org.jbpm.graph.def.ProcessDefinition;
033    import org.jbpm.graph.def.Transition;
034    import org.jbpm.graph.node.EndState;
035    import org.jbpm.graph.node.StartState;
036    import org.jbpm.graph.node.TaskNode;
037    import org.jbpm.instantiation.Delegation;
038    import org.jbpm.taskmgmt.def.Swimlane;
039    import org.jbpm.taskmgmt.def.Task;
040    import org.jbpm.taskmgmt.def.TaskMgmtDefinition;
041    import static org.jboss.dna.sequencer.jpdl3.JPDL3MetadataConstants.*;
042    
043    /**
044     * The jBPM Process definition language meta data.
045     * 
046     * @author Serge Pagop
047     */
048    public class JPDL3Metadata {
049    
050        /**
051         * The process definition name.
052         */
053        private String pdName;
054    
055        /**
056         * The start node of the process definition.
057         */
058        private JPDL3StartStateMetadata jPDL3StartStateMetadata;
059    
060        /**
061         * The end node of the process definition.
062         */
063        private JPDL3EndStateMetadata jPDL3EndStateMetadata;
064    
065        /**
066         * The swimlanes of the process definitions
067         */
068        List<JPDL3SwimlaneMetadata> swimlanes = new ArrayList<JPDL3SwimlaneMetadata>();
069    
070        /**
071         * The task nodes of the process definitions.
072         */
073        private List<JPDL3TaskNodeMetadata> taskNodes = new ArrayList<JPDL3TaskNodeMetadata>();
074    
075        private JPDL3Metadata() {
076            // prevent construction
077        }
078    
079        /**
080         * Create an instance of {@link JPDL3Metadata} with all data of a specific jpdl xml document.
081         * 
082         * @param stream - the {@link InputStream}, that represents a stream of jpdl.
083         * @return a object of {@link JPDL3Metadata}.
084         */
085        @SuppressWarnings( {"unchecked", "cast"} )
086        public static JPDL3Metadata instance( InputStream stream ) {
087            ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(stream);
088            List<JPDL3SwimlaneMetadata> swimlaneContainer = new ArrayList<JPDL3SwimlaneMetadata>();
089            List<JPDL3TaskNodeMetadata> taskNodeContainer = new ArrayList<JPDL3TaskNodeMetadata>();
090    
091            if (processDefinition != null) {
092                JPDL3Metadata jplMetadata = new JPDL3Metadata();
093                if (processDefinition.getName() != null) {
094                    jplMetadata.setPdName(processDefinition.getName());
095                }
096    
097                TaskMgmtDefinition taskMgmtDefinition = processDefinition.getTaskMgmtDefinition();
098                if (taskMgmtDefinition != null) {
099                    // 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    }