KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.corbaclient;
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.LinkedHashMap JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.omg.CORBA.Any JavaDoc;
13 import org.omg.CORBA.BAD_OPERATION JavaDoc;
14 import org.omg.CORBA.ORB JavaDoc;
15 import org.omg.CORBA.TCKind JavaDoc;
16 import org.omg.CORBA.TypeCode JavaDoc;
17 import org.omg.DynamicAny.DynAnyFactory JavaDoc;
18 import org.omg.DynamicAny.DynAnyFactoryHelper JavaDoc;
19 import org.omg.TimeBase.UtcT;
20 import org.omg.WfBase.BaseException;
21 import org.omg.WfBase.NameValue;
22 import org.omg.WfBase.NameValueInfo;
23 import org.omg.WorkflowModel.WfActivity;
24
25 /**
26  * Implements various usefull static methods.
27  */

28 public class WorkflowUtilities {
29
30    public static final String JavaDoc BOOLEAN_KEY="BooleanKey";
31    public static final String JavaDoc STRING_KEY="StringKey";
32    public static final String JavaDoc INTEGER_KEY="IntegerKey";
33    public static final String JavaDoc DOUBLE_KEY="DoubleKey";
34    public static final String JavaDoc DATE_KEY="DateKey";
35    public static final String JavaDoc ENUMERATION_KEY="EnumerationKey";
36    public static final String JavaDoc ARRAY_KEY="ArrayKey";
37    public static final String JavaDoc UNKNOWN_KEY="UnknownKey";
38    public static final String JavaDoc MAP_KEY="MapKey";
39
40    public static final String JavaDoc VARIABLE_TO_PROCESS_UPDATE="VariableToProcess_UPDATE";
41    public static final String JavaDoc VARIABLE_TO_PROCESS_VIEW="VariableToProcess_VIEW";
42    public static final String JavaDoc VARIABLE_TO_PROCESS_ALL="VariableToProcess_ALL";
43
44
45    public static NameValueStringMap convertNameValueToNameValueString (NameValue nv) {
46       return new NameValueStringMap(nv.the_name,nv.the_value.extract_wstring());
47    }
48
49    public static ArrayList JavaDoc convertNameValueArrayToTheListOfNameValueString (NameValue[] nva) {
50       ArrayList JavaDoc l=new ArrayList JavaDoc();
51       if (nva!=null) {
52          for (int i=0; i<nva.length; i++) {
53             l.add(convertNameValueToNameValueString(nva[i]));
54          }
55       }
56       return l;
57    }
58
59    public static LinkedHashMap JavaDoc makeLinkedHashMap(NameValueInfo[] nvi) {
60       LinkedHashMap JavaDoc lhm=new LinkedHashMap JavaDoc();
61       if (nvi!=null) {
62          for (int i=0; i<nvi.length; i++) {
63             lhm.put(nvi[i].attribute_name,nvi[i].type_name);
64          }
65       }
66       return lhm;
67    }
68
69    public static String JavaDoc getTypeKeyOfAnyObject (Any JavaDoc theValueHolder) {
70       try {
71          int kind=theValueHolder.type().kind().value();
72          //System.out.println("Kind no="+kind);
73
if (kind==TCKind._tk_boolean) {
74             return BOOLEAN_KEY;
75          } else if (kind==TCKind._tk_wstring) {
76             return STRING_KEY;
77          } else if (kind==TCKind._tk_longlong) {
78             return INTEGER_KEY;
79          } else if (kind==TCKind._tk_double) {
80             return DOUBLE_KEY;
81          } else if (kind==TCKind._tk_enum) {
82             return ENUMERATION_KEY;
83          } else if (kind==TCKind._tk_array) {
84             return ARRAY_KEY;
85          } else {
86             try {
87                java.lang.Object JavaDoc obj=theValueHolder.extract_Value();
88                if (obj instanceof Date JavaDoc) {
89                   return DATE_KEY;
90                }
91                if (obj instanceof HashMap JavaDoc) {
92                   return MAP_KEY;
93                }
94             } catch (Exception JavaDoc ex) {
95             }
96             return UNKNOWN_KEY;
97          }
98       } catch (BAD_OPERATION JavaDoc bo) {
99          return UNKNOWN_KEY;
100       }
101    }
102
103    public static String JavaDoc getTypeKeyOfAnyObject (String JavaDoc javaClassName) {
104       try {
105          //System.out.println("Kind no="+kind);
106
if (javaClassName.equals(Boolean JavaDoc.class.getName())) {
107             return BOOLEAN_KEY;
108          } else if (javaClassName.equals(String JavaDoc.class.getName())) {
109             return STRING_KEY;
110          } else if (javaClassName.equals(Date JavaDoc.class.getName())) {
111             return DATE_KEY;
112          } else if (javaClassName.equals(Long JavaDoc.class.getName())) {
113             return INTEGER_KEY;
114          } else if (javaClassName.equals(Double JavaDoc.class.getName())) {
115             return DOUBLE_KEY;
116          } else if (javaClassName.equals(HashMap JavaDoc.class.getName())) {
117             return MAP_KEY;
118          }/* else if (kind==TCKind._tk_enum) {
119              return ENUMERATION_KEY;
120              } else if (kind==TCKind._tk_array) {
121              return ARRAY_KEY;
122           }*/
else {
123             return UNKNOWN_KEY;
124          }
125       } catch (Exception JavaDoc bo) {
126          return UNKNOWN_KEY;
127       }
128    }
129
130    public static java.lang.Object JavaDoc exstractValueOfAnyObject (Any JavaDoc theValueHolder,String JavaDoc typeKey) {
131       try {
132          if (typeKey.equals(BOOLEAN_KEY)) {
133             return new Boolean JavaDoc(theValueHolder.extract_boolean());
134          } else if (typeKey.equals(STRING_KEY)) {
135             return new String JavaDoc(theValueHolder.extract_wstring());
136          } else if (typeKey.equals(INTEGER_KEY)) {
137             return new Long JavaDoc(theValueHolder.extract_longlong());
138          } else if (typeKey.equals(DOUBLE_KEY)) {
139             return new Double JavaDoc(theValueHolder.extract_double());
140          } else if (typeKey.equals(ENUMERATION_KEY)) {
141             ORB JavaDoc orb=SharkClient.getORB();
142             org.omg.CORBA.Object JavaDoc obj = null;
143             obj = orb.resolve_initial_references ("DynAnyFactory");
144             // narrow the reference to the correct type
145
DynAnyFactory JavaDoc daf = DynAnyFactoryHelper.narrow (obj);
146             org.omg.DynamicAny.DynEnum JavaDoc de=(org.omg.DynamicAny.DynEnum JavaDoc)daf.create_dyn_any(theValueHolder);
147             return de.get_as_string();
148          } else {
149             try {
150                java.lang.Object JavaDoc obj=theValueHolder.extract_Value();
151                return obj;
152             } catch (Exception JavaDoc ex) {
153                return null;
154             }
155          }
156       } catch (Exception JavaDoc ex) {
157          return null;
158       }
159    }
160
161
162    public static java.lang.Object JavaDoc[] extractChoicesFromDynEnum (Any JavaDoc any) {
163       //System.out.println("Extracting from enum...");
164
TypeCode JavaDoc tc=any.type();
165       ArrayList JavaDoc chc=new ArrayList JavaDoc();
166       if (tc.kind().value()==TCKind._tk_enum) {
167          try {
168             for (int i=0; i<tc.member_count(); i++) {
169                //System.out.println("Choice "+i+" = "+tc.member_name(i));
170
chc.add(tc.member_name(i));
171             }
172          } catch (Exception JavaDoc ex) {
173             return null;
174          }
175       }
176       return chc.toArray();
177    }
178
179    public static Any JavaDoc insertValueIntoAnyObject (Any JavaDoc theValueHolder,String JavaDoc txtVal,String JavaDoc typeKey) {
180       try {
181          if (typeKey.equals(BOOLEAN_KEY)) {
182             if (txtVal.equalsIgnoreCase("true") || txtVal.equalsIgnoreCase("false")) {
183                Boolean JavaDoc val=new Boolean JavaDoc(txtVal);
184                theValueHolder.insert_boolean(val.booleanValue());
185             } else {
186                return null;
187             }
188          } else if (typeKey.equals(STRING_KEY)) {
189             theValueHolder.insert_wstring(txtVal);
190          } else if (typeKey.equals(DATE_KEY)) {
191             System.out.println("dte="+txtVal);
192             SimpleDateFormat JavaDoc sdf=new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
193             theValueHolder.insert_Value(sdf.parse(txtVal));
194          } else if (typeKey.equals(INTEGER_KEY)) {
195             theValueHolder.insert_longlong(Long.parseLong(txtVal));
196          } else if (typeKey.equals(DOUBLE_KEY)) {
197             theValueHolder.insert_double(Double.parseDouble(txtVal));
198          } else if (typeKey.equals(ENUMERATION_KEY)) {
199             ORB JavaDoc orb=SharkClient.getORB();
200             org.omg.CORBA.Object JavaDoc obj = null;
201             obj=orb.resolve_initial_references ("DynAnyFactory");
202             // narrow the reference to the correct type
203
DynAnyFactory JavaDoc daf = DynAnyFactoryHelper.narrow (obj);
204             org.omg.DynamicAny.DynEnum JavaDoc de=(org.omg.DynamicAny.DynEnum JavaDoc)daf.create_dyn_any(theValueHolder);
205             de.set_as_string(txtVal);
206             theValueHolder=de.to_any();
207          } else {
208             System.out.println("Unknown type");
209             return null;
210          }
211       } catch (Exception JavaDoc ex) {
212          System.out.println("Wrong value");
213          ex.printStackTrace();
214          return null;
215       }
216       return theValueHolder;
217    }
218
219    /**
220     * Gets the context for the given activity that could be updated, viewed
221     * by the user, or all activity context.
222     * The context is determined based on activities extended attributes.
223     */

224    public static NameValue[] getActivityContext (NameValue[] contextToSearch,
225                                                  WfActivity act,String JavaDoc type) throws BaseException {
226       NameValue[] extAttribs=null;
227       try {
228          extAttribs=SharkClient.getAdminMiscUtilities().getActivitiesExtendedAttributeNameValuePairs(act.container().key(),act.key());
229       } catch (Exception JavaDoc ex) {}
230       List JavaDoc nvl=new ArrayList JavaDoc();
231       if (extAttribs!=null && extAttribs.length>0) {
232          for (int i=0; i<extAttribs.length; i++) {
233             String JavaDoc eaName=extAttribs[i].the_name;
234             if (type.equals(VARIABLE_TO_PROCESS_ALL)) {
235                if (eaName.equalsIgnoreCase(VARIABLE_TO_PROCESS_UPDATE) ||
236                    eaName.equalsIgnoreCase(VARIABLE_TO_PROCESS_VIEW)) {
237                   String JavaDoc variableId=extAttribs[i].the_value.extract_wstring();
238                   NameValue nv=getNameValue(contextToSearch,variableId);
239                   if (nv!=null) {
240                      nvl.add(nv);
241                   }
242                }
243             } else {
244                if (eaName.equalsIgnoreCase(type)) {
245                   String JavaDoc variableId=extAttribs[i].the_value.extract_wstring();
246                   NameValue nv=getNameValue(contextToSearch,variableId);
247                   if (nv!=null) {
248                      nvl.add(nv);
249                   }
250                }
251             }
252          }
253       }
254       NameValue[] updateContext=new NameValue[nvl.size()];
255       nvl.toArray(updateContext);
256
257       return updateContext;
258    }
259
260    public static NameValue getNameValue (NameValue[] nvs,String JavaDoc name) {
261       if (nvs==null) return null;
262       for (int i=0; i<nvs.length; i++) {
263          if (nvs[i].the_name.equals(name)) {
264             return nvs[i];
265          }
266       }
267       return null;
268    }
269
270    /**
271     * Converts the given UTC time into the time
272     * string in format YYYY-MM-DD HH:mm:SS:mmm
273     */

274    public static String JavaDoc getDateFromUTC (UtcT utct) {
275       // UTC is temporary realized to hold the time in miliss passed from .. 1970
276
//return new Date(utct.time).toString();
277
String JavaDoc dateSeparator="-";
278       String JavaDoc timeSeparator=":";
279       Calendar JavaDoc cal=new GregorianCalendar JavaDoc();
280       cal.setTimeInMillis(utct.time);
281
282       int YYYY=cal.get(Calendar.YEAR);
283       int MM=cal.get(Calendar.MONTH)+1;
284       int DD=cal.get(Calendar.DAY_OF_MONTH);
285       int HH=cal.get(Calendar.HOUR_OF_DAY);
286       int mm=cal.get(Calendar.MINUTE);
287       int ss=cal.get(Calendar.SECOND);
288       int mmm=cal.get(Calendar.MILLISECOND);
289
290       String JavaDoc dateTime="";
291
292       dateTime=dateTime+String.valueOf(YYYY)+dateSeparator;
293       if (MM<10) {
294          dateTime=dateTime+"0";
295       }
296       dateTime=dateTime+String.valueOf(MM)+dateSeparator;
297       if (DD<10) {
298          dateTime=dateTime+"0";
299       }
300       dateTime=dateTime+String.valueOf(DD)+" ";
301
302       if (cal.get(Calendar.AM_PM)==Calendar.PM && HH<12) {
303          HH+=12;
304       }
305       if (HH<10) {
306          dateTime=dateTime+"0";
307       }
308       dateTime=dateTime+String.valueOf(HH)+timeSeparator;
309
310       if (mm<10) {
311          dateTime=dateTime+"0";
312       }
313       dateTime=dateTime+String.valueOf(mm)+timeSeparator;
314
315       if (ss<10) {
316          dateTime=dateTime+"0";
317       }
318       dateTime=dateTime+String.valueOf(ss)+timeSeparator;
319
320       if (mmm<10) {
321          dateTime=dateTime+"00";
322       } else if (mmm<100) {
323          dateTime=dateTime+"0";
324       } else {
325          dateTime=dateTime;
326       }
327       dateTime=dateTime+String.valueOf(mmm);
328
329       return dateTime;
330    }
331
332    public static String JavaDoc getDuration (UtcT started) {
333       long sec=1000;
334       long min=sec*60;
335       long hour=min*60;
336       long day=hour*24;
337       long month=day*30;
338       long year=365*day;
339
340       // UTC is temporary realized to hold the time in miliss passed from .. 1970
341
long diffInMills=System.currentTimeMillis()-started.time;
342       if (diffInMills<min) {
343          return String.valueOf(diffInMills/sec)+" [s]";
344       } else if (diffInMills<hour) {
345          long m=diffInMills/min;
346          long s=(diffInMills-m*min)/sec;
347          return String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
348       } else if (diffInMills<day) {
349          long h=diffInMills/hour;
350          long m=(diffInMills-h*hour)/min;
351          long s=(diffInMills-h*hour-m*min)/sec;
352          return String.valueOf(h)+" [h] "+String.valueOf(m)+" [min] "+
353             String.valueOf(s)+" [s]";
354       } else if (diffInMills<month) {
355          long d=diffInMills/day;
356          long h=(diffInMills-d*day)/hour;
357          long m=(diffInMills-d*day-h*hour)/min;
358          long s=(diffInMills-d*day-h*hour-m*min)/sec;
359          return String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+
360             String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
361       } else if (diffInMills<year) {
362          long mn=diffInMills/month;
363          long d=(diffInMills-mn*month)/day;
364          long h=(diffInMills-mn*month-d*day)/hour;
365          long m=(diffInMills-mn*month-d*day-h*hour)/min;
366          long s=(diffInMills-mn*month-d*day-h*hour-m*min)/sec;
367          return String.valueOf(mn)+" [m] "+String.valueOf(d)+" [d] "+
368             String.valueOf(h)+" [h] "+String.valueOf(m)+
369             " [min] "+String.valueOf(s)+" [s]";
370       } else { //if (diffInMills>=year)
371
long y=diffInMills/year;
372          long mn=(diffInMills-y*year)/month;
373          long d=(diffInMills-y*year-mn*month)/day;
374          long h=(diffInMills-y*year-mn*month-d*day)/hour;
375          long m=(diffInMills-y*year-mn*month-d*day-h*hour)/min;
376          long s=(diffInMills-y*year-mn*month-d*day-h*hour-m*min)/sec;
377          return String.valueOf(y)+" [y] "+String.valueOf(mn)+" [m] "+
378             String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+
379             String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]";
380       }
381    }
382
383    public static String JavaDoc createParticipantOrApplicationKey (String JavaDoc packageId,String JavaDoc processDefinitionId,
384                                                            String JavaDoc participantOrAppDefId) {
385       if (processDefinitionId==null) processDefinitionId="";
386       String JavaDoc key="";
387       if( processDefinitionId == null ) processDefinitionId = "";
388       key+=packageId+";"+processDefinitionId+";"+participantOrAppDefId;
389       return key;
390    }
391
392    /*public static Node parseExtAttributesString (String toParse) throws Exception {
393     String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
394     String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
395     String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
396
397
398     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
399     factory.setNamespaceAware(true);
400     //factory.setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);
401
402     DocumentBuilder parser = factory.newDocumentBuilder();
403
404     Document document=null;
405     document=parser.parse(new InputSource(new StringReader(toParse)));
406     if (document!=null) {
407     return document.getDocumentElement();
408     } else {
409     return null;
410     }
411     }
412
413     public static NameValue[] getExtendedAttributesNameValuePairs (String extAttribs) throws Exception {
414     if (extAttribs==null || extAttribs.trim().length()==0) return null;
415     org.w3c.dom.Node easn=WorkflowUtilities.parseExtAttributesString(extAttribs);
416     ExtendedAttributes eas=new ExtendedAttributes(null);
417     eas.fromXML(easn);
418
419     NameValue[] nvs=new NameValue[eas.size()];
420     int i=0;
421     Iterator easIt=eas.toCollection().iterator();
422     ORB orb=SharkClient.getORB();
423     while (easIt.hasNext()) {
424     ExtendedAttribute ea=(ExtendedAttribute)easIt.next();
425     String nm=ea.get("Name").toValue().toString();
426     String val=ea.get("Value").toValue().toString();
427     Any any=orb.create_any();
428     any.insert_wstring(val);
429     nvs[i++]=new NameValue(nm,any);
430     }
431     return nvs;
432     }*/

433    
434 }
435
436
Popular Tags