KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > entity > AndEdgeState


1 package hero.entity;
2 import hero.interfaces.Constants;
3 import hero.interfaces.BnEdgeLocal;
4 import hero.interfaces.BnNodeLocal;
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 import org.apache.log4j.Category;
14
15
16 /*
17 * 02/01/2002 - 15:24:07
18 *
19 * AndEdgeState.java -
20 * Copyright (C) 2002 Ecoo Team
21 * charoy@loria.fr
22 *
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 */

38
39 public class AndEdgeState extends EdgeState implements java.io.Serializable JavaDoc {
40
41     private static AndEdgeState nd;
42
43     public AndEdgeState() {
44     }
45
46     public static AndEdgeState getInstance() {
47     if (nd==null) {
48         nd=new AndEdgeState();
49     }
50     return nd;
51     }
52
53     public int state(BnNodeLocal node) {
54     BonitaProjectValue currentProject=null;
55     Collection edges = new ArrayList();
56     Collection edgesModel = new ArrayList();
57     
58     try{
59         edges = BonitaServiceValue.getNode(node.getBnProject(),node.getName()).getInEdges();
60     }catch (Exception JavaDoc e) {e.printStackTrace();}
61         
62     // Instances consistency (may be some activities are not yet created in the instance)
63
String JavaDoc nodeName = node.getName();
64     if (isInstance(node.getName()))
65         nodeName = getModel(nodeName);
66         
67     try{
68         edgesModel = BonitaServiceValue.getNode(node.getBnProject().getModel().getName(),nodeName).getInEdges();
69     }catch (Exception JavaDoc e) {edgesModel=new ArrayList();}
70     if (edges.size() != edgesModel.size() && !edgesModel.isEmpty())
71         edges = concatEdges(edges, edgesModel);
72
73     Iterator i = edges.iterator();
74     int result=Constants.Nd.ACTIVE;
75     while (i.hasNext()) {
76         BonitaEdgeValue edge = (BonitaEdgeValue)i.next();
77         if (edge.getState()==Constants.Ed.INITIAL) {
78         result=Constants.Nd.EDGEINITIAL;
79         break;
80         }
81         if ((edge.getState() == Constants.Ed.ANTICIPATING))
82         result=Constants.Nd.ANTACTIVE;
83         if ((edge.getState() == Constants.Ed.DEAD))
84         {
85             result=Constants.Nd.CANCEL;
86             break;
87         }
88     }
89     return result;
90     }
91     
92     private String JavaDoc getModel(String JavaDoc instanceName) {
93         int i = instanceName.indexOf("_instance");
94         return (instanceName.substring(0, i));
95     }
96
97     private boolean isInstance(String JavaDoc name) {
98         return (name.matches(".*_instance.*"));
99     }
100     
101     private Collection concatEdges(Collection edges, Collection edgesModel) {
102         Collection result = new ArrayList();
103         Iterator ed = edgesModel.iterator();
104         while (ed.hasNext())
105         {
106             boolean found = false;
107             BonitaEdgeValue el = (BonitaEdgeValue)ed.next();
108             String JavaDoc nodeName = el.getInNode();
109             Iterator instEdges = edges.iterator();
110             while(instEdges.hasNext())
111             {
112                 BonitaEdgeValue insEd = (BonitaEdgeValue)instEdges.next();
113                 String JavaDoc node = insEd.getInNode();
114                 if (this.isInstance(node))
115                     node = this.getModel(node);
116                 if (nodeName.equals(node))
117                 {
118                     result.add(insEd);
119                     found = true;
120                     break;
121                 }
122             }
123             if (!found)
124                 result.add(el);
125         }
126         return result;
127     }
128 }
129
130
131
132
133
134
135
136
137
138
139
140
Popular Tags