1 package org.enhydra.shark.corbaclient; 2 3 import java.text.SimpleDateFormat ; 4 import java.util.ArrayList ; 5 import java.util.Calendar ; 6 import java.util.Date ; 7 import java.util.GregorianCalendar ; 8 import java.util.HashMap ; 9 import java.util.LinkedHashMap ; 10 import java.util.List ; 11 12 import org.omg.CORBA.Any ; 13 import org.omg.CORBA.BAD_OPERATION ; 14 import org.omg.CORBA.ORB ; 15 import org.omg.CORBA.TCKind ; 16 import org.omg.CORBA.TypeCode ; 17 import org.omg.DynamicAny.DynAnyFactory ; 18 import org.omg.DynamicAny.DynAnyFactoryHelper ; 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 28 public class WorkflowUtilities { 29 30 public static final String BOOLEAN_KEY="BooleanKey"; 31 public static final String STRING_KEY="StringKey"; 32 public static final String INTEGER_KEY="IntegerKey"; 33 public static final String DOUBLE_KEY="DoubleKey"; 34 public static final String DATE_KEY="DateKey"; 35 public static final String ENUMERATION_KEY="EnumerationKey"; 36 public static final String ARRAY_KEY="ArrayKey"; 37 public static final String UNKNOWN_KEY="UnknownKey"; 38 public static final String MAP_KEY="MapKey"; 39 40 public static final String VARIABLE_TO_PROCESS_UPDATE="VariableToProcess_UPDATE"; 41 public static final String VARIABLE_TO_PROCESS_VIEW="VariableToProcess_VIEW"; 42 public static final String 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 convertNameValueArrayToTheListOfNameValueString (NameValue[] nva) { 50 ArrayList l=new ArrayList (); 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 makeLinkedHashMap(NameValueInfo[] nvi) { 60 LinkedHashMap lhm=new LinkedHashMap (); 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 getTypeKeyOfAnyObject (Any theValueHolder) { 70 try { 71 int kind=theValueHolder.type().kind().value(); 72 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 obj=theValueHolder.extract_Value(); 88 if (obj instanceof Date ) { 89 return DATE_KEY; 90 } 91 if (obj instanceof HashMap ) { 92 return MAP_KEY; 93 } 94 } catch (Exception ex) { 95 } 96 return UNKNOWN_KEY; 97 } 98 } catch (BAD_OPERATION bo) { 99 return UNKNOWN_KEY; 100 } 101 } 102 103 public static String getTypeKeyOfAnyObject (String javaClassName) { 104 try { 105 if (javaClassName.equals(Boolean .class.getName())) { 107 return BOOLEAN_KEY; 108 } else if (javaClassName.equals(String .class.getName())) { 109 return STRING_KEY; 110 } else if (javaClassName.equals(Date .class.getName())) { 111 return DATE_KEY; 112 } else if (javaClassName.equals(Long .class.getName())) { 113 return INTEGER_KEY; 114 } else if (javaClassName.equals(Double .class.getName())) { 115 return DOUBLE_KEY; 116 } else if (javaClassName.equals(HashMap .class.getName())) { 117 return MAP_KEY; 118 } else { 123 return UNKNOWN_KEY; 124 } 125 } catch (Exception bo) { 126 return UNKNOWN_KEY; 127 } 128 } 129 130 public static java.lang.Object exstractValueOfAnyObject (Any theValueHolder,String typeKey) { 131 try { 132 if (typeKey.equals(BOOLEAN_KEY)) { 133 return new Boolean (theValueHolder.extract_boolean()); 134 } else if (typeKey.equals(STRING_KEY)) { 135 return new String (theValueHolder.extract_wstring()); 136 } else if (typeKey.equals(INTEGER_KEY)) { 137 return new Long (theValueHolder.extract_longlong()); 138 } else if (typeKey.equals(DOUBLE_KEY)) { 139 return new Double (theValueHolder.extract_double()); 140 } else if (typeKey.equals(ENUMERATION_KEY)) { 141 ORB orb=SharkClient.getORB(); 142 org.omg.CORBA.Object obj = null; 143 obj = orb.resolve_initial_references ("DynAnyFactory"); 144 DynAnyFactory daf = DynAnyFactoryHelper.narrow (obj); 146 org.omg.DynamicAny.DynEnum de=(org.omg.DynamicAny.DynEnum )daf.create_dyn_any(theValueHolder); 147 return de.get_as_string(); 148 } else { 149 try { 150 java.lang.Object obj=theValueHolder.extract_Value(); 151 return obj; 152 } catch (Exception ex) { 153 return null; 154 } 155 } 156 } catch (Exception ex) { 157 return null; 158 } 159 } 160 161 162 public static java.lang.Object [] extractChoicesFromDynEnum (Any any) { 163 TypeCode tc=any.type(); 165 ArrayList chc=new ArrayList (); 166 if (tc.kind().value()==TCKind._tk_enum) { 167 try { 168 for (int i=0; i<tc.member_count(); i++) { 169 chc.add(tc.member_name(i)); 171 } 172 } catch (Exception ex) { 173 return null; 174 } 175 } 176 return chc.toArray(); 177 } 178 179 public static Any insertValueIntoAnyObject (Any theValueHolder,String txtVal,String typeKey) { 180 try { 181 if (typeKey.equals(BOOLEAN_KEY)) { 182 if (txtVal.equalsIgnoreCase("true") || txtVal.equalsIgnoreCase("false")) { 183 Boolean val=new Boolean (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 sdf=new SimpleDateFormat ("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 orb=SharkClient.getORB(); 200 org.omg.CORBA.Object obj = null; 201 obj=orb.resolve_initial_references ("DynAnyFactory"); 202 DynAnyFactory daf = DynAnyFactoryHelper.narrow (obj); 204 org.omg.DynamicAny.DynEnum de=(org.omg.DynamicAny.DynEnum )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 ex) { 212 System.out.println("Wrong value"); 213 ex.printStackTrace(); 214 return null; 215 } 216 return theValueHolder; 217 } 218 219 224 public static NameValue[] getActivityContext (NameValue[] contextToSearch, 225 WfActivity act,String type) throws BaseException { 226 NameValue[] extAttribs=null; 227 try { 228 extAttribs=SharkClient.getAdminMiscUtilities().getActivitiesExtendedAttributeNameValuePairs(act.container().key(),act.key()); 229 } catch (Exception ex) {} 230 List nvl=new ArrayList (); 231 if (extAttribs!=null && extAttribs.length>0) { 232 for (int i=0; i<extAttribs.length; i++) { 233 String 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 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 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 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 274 public static String getDateFromUTC (UtcT utct) { 275 String dateSeparator="-"; 278 String timeSeparator=":"; 279 Calendar cal=new GregorianCalendar (); 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 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 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 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 { 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 createParticipantOrApplicationKey (String packageId,String processDefinitionId, 384 String participantOrAppDefId) { 385 if (processDefinitionId==null) processDefinitionId=""; 386 String key=""; 387 if( processDefinitionId == null ) processDefinitionId = ""; 388 key+=packageId+";"+processDefinitionId+";"+participantOrAppDefId; 389 return key; 390 } 391 392 433 434 } 435 436 | Popular Tags |