KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > entity > OrEdgeState


1 package hero.entity;
2 import hero.interfaces.Constants;
3 import hero.interfaces.BnNodeLocal;
4 import hero.interfaces.BnEdgeLocal;
5 import hero.util.BonitaProjectLocator;
6 import hero.util.BonitaServiceException;
7 import hero.util.BonitaServiceValue;
8 import hero.util.HeroException;
9 import hero.util.values.BonitaEdgeValue;
10 import hero.util.values.BonitaProjectValue;
11
12 import java.util.*;
13
14 public class OrEdgeState extends EdgeState {
15
16     private static OrEdgeState nd;
17
18     public OrEdgeState() {
19     }
20
21     public static OrEdgeState getInstance() {
22         if (nd == null) {
23             nd = new OrEdgeState();
24         }
25         return nd;
26     }
27
28     public int state(BnNodeLocal node) {
29         //Collection edges = node.getInBnEdges();
30
BonitaProjectValue currentProject=null;
31         Collection edges = new ArrayList();
32         Collection edgesModel = new ArrayList();
33         
34         try{
35             edges = BonitaServiceValue.getNode(node.getBnProject(),node.getName()).getInEdges();
36         }catch (Exception JavaDoc e) {e.printStackTrace();}
37         
38         // Instances consistency (may be some activities are not yet created in the instance)
39
String JavaDoc nodeName = node.getName();
40         if (isInstance(node.getName()))
41             nodeName = getModel(nodeName);
42                                 
43         try{
44             edgesModel = BonitaServiceValue.getNode(node.getBnProject().getModel().getName(),nodeName).getInEdges();
45         }catch (Exception JavaDoc e) {edgesModel=new ArrayList();}
46         if (edges.size() != edgesModel.size() && !edgesModel.isEmpty())
47             edges = concatEdges(edges, edgesModel);
48         
49         Iterator i = edges.iterator();
50         int result = Constants.Nd.EDGEINITIAL;
51         boolean active = false;
52         while (i.hasNext()) {
53             BonitaEdgeValue edge = (BonitaEdgeValue) i.next();
54
55             if (edge.getState() == Constants.Ed.ACTIVE) {
56                 result = Constants.Nd.ACTIVE;
57                 active = true;
58                 break;
59             }
60             if (edge.getState() == Constants.Ed.INITIAL) {
61                 result = Constants.Nd.EDGEINITIAL;
62                 active = true;
63             }
64             if ((edge.getState() == Constants.Ed.ANTICIPATING)) {
65                 result = Constants.Nd.ANTACTIVE;
66                 active = true;
67                 break;
68             }
69             if ((edge.getState() == Constants.Ed.DEAD) && !active)
70                 result = Constants.Nd.CANCEL;
71         }
72         return result;
73
74     }
75     
76     private String JavaDoc getModel(String JavaDoc instanceName) {
77         int i = instanceName.indexOf("_instance");
78         return (instanceName.substring(0, i));
79     }
80
81     private boolean isInstance(String JavaDoc name) {
82         return (name.matches(".*_instance.*"));
83     }
84     
85     private Collection concatEdges(Collection edges, Collection edgesModel) {
86         Collection result = new ArrayList();
87         Iterator ed = edgesModel.iterator();
88         while (ed.hasNext())
89         {
90             boolean found = false;
91             BonitaEdgeValue el = (BonitaEdgeValue)ed.next();
92             String JavaDoc nodeName = el.getInNode();
93             Iterator instEdges = edges.iterator();
94             while(instEdges.hasNext())
95             {
96                 BonitaEdgeValue insEd = (BonitaEdgeValue)instEdges.next();
97                 String JavaDoc node = insEd.getInNode();
98                 if (this.isInstance(node))
99                     node = this.getModel(node);
100                 if (nodeName.equals(node))
101                 {
102                     result.add(insEd);
103                     found = true;
104                     break;
105                 }
106             }
107             if (!found)
108                 result.add(el);
109         }
110         return result;
111     }
112
113 }
114
Popular Tags