KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > WorkflowUtilities


1 package org.enhydra.shark.swingclient;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.GregorianCalendar JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.LinkedHashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.enhydra.shark.Shark;
14 import org.enhydra.shark.api.client.timebase.UtcT;
15 import org.enhydra.shark.api.client.wfbase.BaseException;
16 import org.enhydra.shark.api.client.wfmodel.WfActivity;
17
18 /**
19  * Implements various usefull static methods.
20  */

21 public class WorkflowUtilities {
22
23    public static final String JavaDoc BOOLEAN_KEY="BooleanKey";
24    public static final String JavaDoc STRING_KEY="StringKey";
25    public static final String JavaDoc INTEGER_KEY="IntegerKey";
26    public static final String JavaDoc DOUBLE_KEY="DoubleKey";
27    public static final String JavaDoc DATE_KEY="DateKey";
28    public static final String JavaDoc ENUMERATION_KEY="EnumerationKey";
29    public static final String JavaDoc ARRAY_KEY="ArrayKey";
30    public static final String JavaDoc UNKNOWN_KEY="UnknownKey";
31    public static final String JavaDoc MAP_KEY="MapKey";
32
33    public static final String JavaDoc VARIABLE_TO_PROCESS_UPDATE="VariableToProcess_UPDATE";
34    public static final String JavaDoc VARIABLE_TO_PROCESS_VIEW="VariableToProcess_VIEW";
35    public static final String JavaDoc VARIABLE_TO_PROCESS_ALL="VariableToProcess_ALL";
36
37    public static ArrayList JavaDoc convertMapToTheListOfNameValueString (Map JavaDoc m) {
38       ArrayList JavaDoc l=new ArrayList JavaDoc();
39       if (m!=null) {
40          for (Iterator JavaDoc i=m.entrySet().iterator(); i.hasNext();) {
41             Map.Entry JavaDoc me=(Map.Entry JavaDoc)i.next();
42             l.add(new NameValueStringMap((String JavaDoc)me.getKey(),(String JavaDoc)me.getValue()));
43          }
44       }
45       return l;
46    }
47
48    /*public static LinkedHashMap makeLinkedHashMap(NameValueInfo[] nvi) {
49     LinkedHashMap lhm=new LinkedHashMap();
50     if (nvi!=null) {
51     for (int i=0; i<nvi.length; i++) {
52     lhm.put(nvi[i].attribute_name,nvi[i].type_name);
53     }
54     }
55     return lhm;
56     }*/

57
58    public static String JavaDoc getTypeKeyOfAnyObject (Object JavaDoc theValueHolder) {
59       try {
60          //System.out.println("Kind no="+kind);
61
if (theValueHolder instanceof Boolean JavaDoc) {
62             return BOOLEAN_KEY;
63          } else if (theValueHolder instanceof String JavaDoc) {
64             return STRING_KEY;
65          } else if (theValueHolder instanceof Date JavaDoc) {
66             return DATE_KEY;
67          } else if (theValueHolder instanceof Long JavaDoc) {
68             return INTEGER_KEY;
69          } else if (theValueHolder instanceof Double JavaDoc) {
70             return DOUBLE_KEY;
71          } else if (theValueHolder instanceof HashMap JavaDoc) {
72             return MAP_KEY;
73          }/* else if (kind==TCKind._tk_enum) {
74              return ENUMERATION_KEY;
75              } else if (kind==TCKind._tk_array) {
76              return ARRAY_KEY;
77           }*/
else {
78             return UNKNOWN_KEY;
79          }
80       } catch (Exception JavaDoc bo) {
81          return UNKNOWN_KEY;
82       }
83    }
84
85    public static String JavaDoc getTypeKeyOfAnyObject (String JavaDoc javaClassName) {
86       try {
87          //System.out.println("Kind no="+kind);
88
if (javaClassName.equals(Boolean JavaDoc.class.getName())) {
89             return BOOLEAN_KEY;
90          } else if (javaClassName.equals(String JavaDoc.class.getName())) {
91             return STRING_KEY;
92          } else if (javaClassName.equals(Date JavaDoc.class.getName())) {
93             return DATE_KEY;
94          } else if (javaClassName.equals(Long JavaDoc.class.getName())) {
95             return INTEGER_KEY;
96          } else if (javaClassName.equals(Double JavaDoc.class.getName())) {
97             return DOUBLE_KEY;
98          } else if (javaClassName.equals(HashMap JavaDoc.class.getName())) {
99             return MAP_KEY;
100          }/* else if (kind==TCKind._tk_enum) {
101              return ENUMERATION_KEY;
102              } else if (kind==TCKind._tk_array) {
103              return ARRAY_KEY;
104           }*/
else {
105             return UNKNOWN_KEY;
106          }
107       } catch (Exception JavaDoc bo) {
108          return UNKNOWN_KEY;
109       }
110    }
111
112    public static Object JavaDoc insertValueIntoAnyObject (Object JavaDoc theValueHolder,String JavaDoc txtVal,String JavaDoc typeKey) {
113       try {
114          if (typeKey.equals(BOOLEAN_KEY)) {
115             if (txtVal.equalsIgnoreCase("true") || txtVal.equalsIgnoreCase("false")) {
116                Boolean JavaDoc val=new Boolean JavaDoc(txtVal);
117                theValueHolder=val;
118             } else {
119                return null;
120             }
121          } else if (typeKey.equals(STRING_KEY)) {
122             theValueHolder=txtVal;
123          } else if (typeKey.equals(DATE_KEY)) {
124             System.out.println("dte="+txtVal);
125             SimpleDateFormat JavaDoc sdf=new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
126             theValueHolder=sdf.parse(txtVal);
127             System.out.println("dtec="+theValueHolder);
128          } else if (typeKey.equals(INTEGER_KEY)) {
129             theValueHolder=new Long JavaDoc(txtVal);
130          } else if (typeKey.equals(DOUBLE_KEY)) {
131             theValueHolder=new Double JavaDoc(txtVal);
132          }/* else if (typeKey.equals(ENUMERATION_KEY)) {
133              ORB orb=SharkClient.getORB();
134              org.omg.CORBA.Object obj = null;
135              obj=orb.resolve_initial_references ("DynAnyFactory");
136              // narrow the reference to the correct type
137              DynAnyFactory daf = DynAnyFactoryHelper.narrow (obj);
138              org.omg.DynamicAny.DynEnum de=(org.omg.DynamicAny.DynEnum)daf.create_dyn_any(theValueHolder);
139              de.set_as_string(txtVal);
140              theValueHolder=de.to_any();
141           }*/
else {
142             System.out.println("Unknown type");
143             return null;
144          }
145       } catch (Exception JavaDoc ex) {
146          System.out.println("Wrong value");
147          ex.printStackTrace();
148          return null;
149       }
150       return theValueHolder;
151    }
152
153    /**
154     * Gets the context for the given activity that could be updated, viewed
155     * by the user, or all activity context.
156     * The context is determined based on activities extended attributes.
157     */

158    public static Map JavaDoc getActivityContext (Map JavaDoc contextToSearch,
159                                          WfActivity act,String JavaDoc type) throws BaseException {
160       /*String extAttrs="";
161        try {
162        extAttrs=Shark.getInstance().getAdminInterface().getAdminMisc().
163        getActivitiesExtendedAttributes(act.container().key(),act.key());
164        } catch (Exception ex) {}
165        NameValue[] extAttribs=null;
166        try {
167        extAttribs=getExtendedAttributesNameValuePairs(extAttrs);
168        } catch (Exception ex) {}*/

169       String JavaDoc[][] extAttribs=null;
170       try {
171          extAttribs=Shark.getInstance().getAdminInterface().getAdminMisc().
172             getActivitiesExtendedAttributeNameValuePairs(act.container().key(),act.key());
173       } catch (Exception JavaDoc ex) {}
174
175       Map JavaDoc updateContext=new LinkedHashMap JavaDoc();
176       if (extAttribs!=null) {
177          for (int i=0; i<extAttribs.length; i++) {
178             String JavaDoc eaName=extAttribs[i][0];
179             if (type.equals(VARIABLE_TO_PROCESS_ALL)) {
180                if (eaName.equalsIgnoreCase(VARIABLE_TO_PROCESS_UPDATE) ||
181                    eaName.equalsIgnoreCase(VARIABLE_TO_PROCESS_VIEW)) {
182                   String JavaDoc variableId=extAttribs[i][1];
183                   if (contextToSearch.containsKey(variableId)) {
184                      updateContext.put(variableId,contextToSearch.get(variableId));
185                   }
186                }
187             } else {
188                if (eaName.equalsIgnoreCase(type)) {
189                   String JavaDoc variableId=extAttribs[i][1];
190                   if (contextToSearch.containsKey(variableId)) {
191                      updateContext.put(variableId,contextToSearch.get(variableId));
192                   }
193                }
194             }
195          }
196       }
197
198       return updateContext;
199    }
200
201    /**
202     * Converts the given UTC time into the time
203     * string in format YYYY-MM-DD HH:mm:SS:mmm
204     */

205    public static String JavaDoc getDateFromUTC (UtcT utct) {
206       // UTC is temporary realized to hold the time in miliss passed from .. 1970
207
//return new Date(utct.time).toString();
208
String JavaDoc dateSeparator="-";
209       String JavaDoc timeSeparator=":";
210       Calendar JavaDoc cal=new GregorianCalendar JavaDoc();
211       cal.setTimeInMillis(utct.time);
212
213       int YYYY=cal.get(Calendar.YEAR);
214       int MM=cal.get(Calendar.MONTH)+1;
215       int DD=cal.get(Calendar.DAY_OF_MONTH);
216       int HH=cal.get(Calendar.HOUR_OF_DAY);
217       int mm=cal.get(Calendar.MINUTE);
218       int ss=cal.get(Calendar.SECOND);
219       int mmm=cal.get(Calendar.MILLISECOND);
220
221       String JavaDoc dateTime="";
222
223       dateTime=dateTime+String.valueOf(YYYY)+dateSeparator;
224       if (MM<10) {
225          dateTime=dateTime+"0";
226       }
227       dateTime=dateTime+String.valueOf(MM)+dateSeparator;
228       if (DD<10) {
229          dateTime=dateTime+"0";
230       }
231       dateTime=dateTime+String.valueOf(DD)+" ";
232
233       if (cal.get(Calendar.AM_PM)==Calendar.PM && HH<12) {
234          HH+=12;
235       }
236       if (HH<10) {
237          dateTime=dateTime+"0";
238       }
239       dateTime=dateTime+String.valueOf(HH)+timeSeparator;
240
241       if (mm<10) {
242          dateTime=dateTime+"0";
243       }
244       dateTime=dateTime+String.valueOf(mm)+timeSeparator;
245
246       if (ss<10) {
247          dateTime=dateTime+"0";
248       }
249       dateTime=dateTime+String.valueOf(ss)+timeSeparator;
250
251       if (mmm<10) {
252          dateTime=dateTime+"00";
253       } else if (mmm<100) {
254          dateTime=dateTime+"0";
255       } else {
256          dateTime=dateTime;
257       }
258       dateTime=dateTime+String.valueOf(mmm);
259
260       return dateTime;
261    }
262
263    public static String JavaDoc getDuration (UtcT started) {
264       long sec=1000;
265       long min=sec*60;
266       long hour=min*60;
267       long day=hour*24;
268       long month=day*30;
269       long year=365*day;
270
271       // UTC is temporary realized to hold the time in miliss passed from .. 1970
272
long diffInMills=System.currentTimeMillis()-started.time;
273       if (diffInMills<min) {
274          return String.valueOf(diffInMills/sec)+" [s]";
275       } else if (diffInMills<hour) {
276          long m=diffInMills/min;
277          long s=(diffInMills-m*min)/sec;
278          return String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
279       } else if (diffInMills<day) {
280          long h=diffInMills/hour;
281          long m=(diffInMills-h*hour)/min;
282          long s=(diffInMills-h*hour-m*min)/sec;
283          return String.valueOf(h)+" [h] "+String.valueOf(m)+" [min] "+
284             String.valueOf(s)+" [s]";
285       } else if (diffInMills<month) {
286          long d=diffInMills/day;
287          long h=(diffInMills-d*day)/hour;
288          long m=(diffInMills-d*day-h*hour)/min;
289          long s=(diffInMills-d*day-h*hour-m*min)/sec;
290          return String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+
291             String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
292       } else if (diffInMills<year) {
293          long mn=diffInMills/month;
294          long d=(diffInMills-mn*month)/day;
295          long h=(diffInMills-mn*month-d*day)/hour;
296          long m=(diffInMills-mn*month-d*day-h*hour)/min;
297          long s=(diffInMills-mn*month-d*day-h*hour-m*min)/sec;
298          return String.valueOf(mn)+" [m] "+String.valueOf(d)+" [d] "+
299             String.valueOf(h)+" [h] "+String.valueOf(m)+
300             " [min] "+String.valueOf(s)+" [s]";
301       } else { //if (diffInMills>=year)
302
long y=diffInMills/year;
303          long mn=(diffInMills-y*year)/month;
304          long d=(diffInMills-y*year-mn*month)/day;
305          long h=(diffInMills-y*year-mn*month-d*day)/hour;
306          long m=(diffInMills-y*year-mn*month-d*day-h*hour)/min;
307          long s=(diffInMills-y*year-mn*month-d*day-h*hour-m*min)/sec;
308          return String.valueOf(y)+" [y] "+String.valueOf(mn)+" [m] "+
309             String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+
310             String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
311       }
312    }
313
314    public static String JavaDoc createParticipantOrApplicationKey (String JavaDoc packageId,String JavaDoc processDefinitionId,
315                                                            String JavaDoc participantOrAppDefId) {
316       if (processDefinitionId==null) processDefinitionId="";
317       String JavaDoc key="";
318       if( processDefinitionId == null ) processDefinitionId = "";
319       key+=packageId+";"+processDefinitionId+";"+participantOrAppDefId;
320       return key;
321    }
322
323    /*public static Node parseExtAttributesString (String toParse) throws Exception {
324     String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
325     String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
326     String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
327
328
329     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
330     factory.setNamespaceAware(true);
331     //factory.setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);
332
333     DocumentBuilder parser = factory.newDocumentBuilder();
334
335     Document document=null;
336     document=parser.parse(new InputSource(new StringReader(toParse)));
337
338     if (document!=null) {
339     return document.getDocumentElement();
340     } else {
341     return null;
342     }
343     }
344
345     public static NameValue[] getExtendedAttributesNameValuePairs (String extAttribs) throws Exception {
346     if (extAttribs==null || extAttribs.trim().length()==0) return null;
347     org.w3c.dom.Node easn=WorkflowUtilities.parseExtAttributesString(extAttribs);
348     ExtendedAttributes eas=new ExtendedAttributes(null);
349     eas.fromXML(easn);
350
351     NameValue[] nvs=new NameValue[eas.size()];
352     int i=0;
353     Iterator easIt=eas.toCollection().iterator();
354     while (easIt.hasNext()) {
355     ExtendedAttribute ea=(ExtendedAttribute)easIt.next();
356     String nm=ea.get("Name").toValue().toString();
357     String val=ea.get("Value").toValue().toString();
358     nvs[i++]=new NameValue(nm,val);
359     }
360     return nvs;
361     }
362
363     static class NameValue {
364     public String the_name;
365     public String the_value;
366
367     public NameValue (String name,String value) {
368     this.the_name=name;
369     this.the_value=value;
370     }
371     }*/

372
373 }
374
Popular Tags