1 56 package org.opencrx.kernel.layer.application; 57 58 import java.lang.reflect.Constructor ; 59 import java.lang.reflect.InvocationTargetException ; 60 import java.util.Date ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.opencrx.kernel.generic.OpenCrxException; 67 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0; 68 import org.openmdx.base.exception.ServiceException; 69 import org.openmdx.base.text.format.DateFormat; 70 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSelectors; 71 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSpecifier; 72 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject; 73 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0; 74 import org.openmdx.compatibility.base.dataprovider.cci.Dataprovider_1_0; 75 import org.openmdx.compatibility.base.dataprovider.cci.Orders; 76 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection; 77 import org.openmdx.compatibility.base.dataprovider.cci.ServiceHeader; 78 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes; 79 import org.openmdx.compatibility.base.naming.Path; 80 import org.openmdx.compatibility.kernel.application.cci.Classes; 81 import org.openmdx.kernel.exception.BasicException; 82 import org.openmdx.model1.accessor.basic.cci.Model_1_0; 83 84 public class Workflows { 85 86 public Workflows( 88 Model_1_0 model, 89 ServiceHeader header, 90 OpenCrxKernel_1 plugin, 91 Dataprovider_1_0 dataprovider, 92 RefPackage_1_0 rootPkg 93 ) throws ServiceException { 94 this.header = header; 95 this.delegation = new RequestCollection( 96 header, 97 dataprovider 98 ); 99 this.plugin = plugin; 100 this.rootPkg = rootPkg; 101 } 102 103 public DataproviderObject executeWorkflow( 105 DataproviderObject_1_0 userHome, 106 Path wfProcessIdentity, 107 Path targetObject, 108 String targetObjectXri, 109 String triggeredByEventId, 110 Path triggeredBySubscription, 111 Number triggeredByEventType 112 ) throws ServiceException { 113 114 if(wfProcessIdentity == null) { 116 throw new ServiceException( 117 OpenCrxException.DOMAIN, 118 OpenCrxException.WORKFLOW_MISSING_WORKFLOW, 119 null, 120 "Missing workflow" 121 ); 122 } 123 DataproviderObject_1_0 wfProcess = this.delegation.addGetRequest( 124 wfProcessIdentity, 125 AttributeSelectors.ALL_ATTRIBUTES, 126 new AttributeSpecifier[]{} 127 ); 128 String workflowName = (String )wfProcess.values("name").get(0); 129 boolean isSynchronous = 130 (wfProcess.values("isSynchronous").get(0) != null) && 131 ((Boolean )wfProcess.values("isSynchronous").get(0)).booleanValue(); 132 133 if((targetObject == null) && (targetObjectXri == null)) { 135 throw new ServiceException( 136 OpenCrxException.DOMAIN, 137 OpenCrxException.WORKFLOW_MISSING_TARGET, 138 null, 139 "Missing target object" 140 ); 141 } 142 Path targetObjectIdentity = targetObject != null 143 ? targetObject 144 : new Path(targetObjectXri); 145 146 DataproviderObject wfProcessInstance = new DataproviderObject( 148 userHome.path().getDescendant( 149 new String []{ 150 "wfProcessInstance", 151 this.plugin.getUidAsString() 152 } 153 ) 154 ); 155 wfProcessInstance.values(SystemAttributes.OBJECT_CLASS).add("org:opencrx:kernel:home1:WfProcessInstance"); 156 wfProcessInstance.values("stepCounter").add(new Integer (1)); 157 wfProcessInstance.values("process").add(wfProcess.path()); 158 wfProcessInstance.values("targetObject").add(targetObjectIdentity.toXri()); 159 if(isSynchronous) { 162 wfProcessInstance.values("startedOn").add(DateFormat.getInstance().format(new Date ())); 163 wfProcessInstance.values("lastActivityOn").add(DateFormat.getInstance().format(new Date ())); 164 } 165 try { 167 try { 168 return new DataproviderObject( 170 this.plugin.retrieveObjectFromDelegation( 171 wfProcessInstance.path() 172 ) 173 ); 174 } 175 catch(ServiceException e) { 176 this.delegation.addCreateRequest( 177 wfProcessInstance 178 ); 179 } 180 } 181 catch(ServiceException e) { 182 new ServiceException(e).log(); 183 throw new ServiceException( 184 OpenCrxException.DOMAIN, 185 OpenCrxException.WORKFLOW_CAN_NOT_CREATE_PROCESS_INSTANCE, 186 new BasicException.Parameter[]{ 187 new BasicException.Parameter("param0", workflowName), 188 new BasicException.Parameter("param1", e.getMessage()) 189 }, 190 "Can not create process instance" 191 ); 192 } 193 194 if(triggeredBySubscription != null) { 196 DataproviderObject property = new DataproviderObject( 197 wfProcessInstance.path().getDescendant(new String []{"property", this.plugin.getUidAsString()}) 198 ); 199 property.values(SystemAttributes.OBJECT_CLASS).add( 200 "org:opencrx:kernel:base:UriProperty" 201 ); 202 property.values("name").add( 203 "triggeredBySubscription" 204 ); 205 property.values("uriValue").add( 206 triggeredBySubscription.toXri() 207 ); 208 this.delegation.addCreateRequest( 209 property 210 ); 211 } 212 if(triggeredByEventType != null) { 213 DataproviderObject property = new DataproviderObject( 214 wfProcessInstance.path().getDescendant(new String []{"property", this.plugin.getUidAsString()}) 215 ); 216 property.values(SystemAttributes.OBJECT_CLASS).add( 217 "org:opencrx:kernel:base:IntegerProperty" 218 ); 219 property.values("name").add( 220 "triggeredByEventType" 221 ); 222 property.values("integerValue").add( 223 triggeredByEventType 224 ); 225 this.delegation.addCreateRequest( 226 property 227 ); 228 } 229 230 if(isSynchronous) { 232 SynchWorkflow_1_0 workflow = null; 234 Class workflowClass = null; 235 try { 236 workflowClass = Classes.getApplicationClass( 237 workflowName 238 ); 239 } 240 catch(ClassNotFoundException e) { 241 new ServiceException(e).log(); 242 throw new ServiceException( 243 OpenCrxException.DOMAIN, 244 OpenCrxException.WORKFLOW_NO_IMPLEMENTATION, 245 new BasicException.Parameter[]{ 246 new BasicException.Parameter("param0", workflowName), 247 new BasicException.Parameter("param1", e.getMessage()) 248 }, 249 "implementation not found" 250 ); 251 } 252 Constructor workflowConstructor = null; 254 try { 255 workflowConstructor = workflowClass.getConstructor(new Class []{}); 256 } 257 catch(NoSuchMethodException e) { 258 new ServiceException(e).log(); 259 throw new ServiceException( 260 OpenCrxException.DOMAIN, 261 OpenCrxException.WORKFLOW_MISSING_CONSTRUCTOR, 262 new BasicException.Parameter[]{ 263 new BasicException.Parameter("param0", workflowName), 264 new BasicException.Parameter("param1", e.getMessage()) 265 }, 266 "missing constructor" 267 ); 268 } 269 try { 271 workflow = (SynchWorkflow_1_0)workflowConstructor.newInstance(new Object []{}); 272 } 273 catch(InstantiationException e) { 274 new ServiceException(e).log(); 275 throw new ServiceException( 276 OpenCrxException.DOMAIN, 277 OpenCrxException.WORKFLOW_CAN_NOT_INSTANTIATE, 278 new BasicException.Parameter[]{ 279 new BasicException.Parameter("param0", workflowName), 280 new BasicException.Parameter("param1", e.getMessage()) 281 }, 282 "can not instantiate" 283 ); 284 } 285 catch(IllegalAccessException e) { 286 new ServiceException(e).log(); 287 throw new ServiceException( 288 OpenCrxException.DOMAIN, 289 OpenCrxException.WORKFLOW_ILLEGAL_ACCESS, 290 new BasicException.Parameter[]{ 291 new BasicException.Parameter("param0", workflowName), 292 new BasicException.Parameter("param1", e.getMessage()) 293 }, 294 "illegal access" 295 ); 296 } 297 catch(IllegalArgumentException e) { 298 new ServiceException(e).log(); 299 throw new ServiceException( 300 OpenCrxException.DOMAIN, 301 OpenCrxException.WORKFLOW_ILLEGAL_ARGUMENT, 302 new BasicException.Parameter[]{ 303 new BasicException.Parameter("param0", workflowName), 304 new BasicException.Parameter("param1", e.getMessage()) 305 }, 306 "illegal argument" 307 ); 308 } 309 catch(InvocationTargetException e) { 310 throw new ServiceException( 312 OpenCrxException.DOMAIN, 313 OpenCrxException.WORKFLOW_CAN_NOT_INVOKE, 314 new BasicException.Parameter[]{ 315 new BasicException.Parameter("param0", workflowName), 316 new BasicException.Parameter("param1", e.getTargetException().getMessage()) 317 }, 318 "can not invoke" 319 ); 320 } 321 List parameters = this.delegation.addFindRequest( 323 wfProcess.path().getChild("property"), 324 null, 325 AttributeSelectors.ALL_ATTRIBUTES, 326 null, 327 0, 328 Integer.MAX_VALUE, 329 Orders.ASCENDING 330 ); 331 Map params = new HashMap (); 332 if(triggeredBySubscription != null) { 334 params.put("triggeredBySubscription", triggeredBySubscription); 335 } 336 if(triggeredByEventType != null) { 337 params.put("triggeredByEventType", triggeredByEventType); 338 } 339 for( 340 Iterator i = parameters.iterator(); 341 i.hasNext(); 342 ) { 343 DataproviderObject_1_0 parameter = (DataproviderObject_1_0)i.next(); 344 String parameterType = (String )parameter.values(SystemAttributes.OBJECT_CLASS).get(0); 345 Object val = null; 346 if("org:opencrx:kernel:base:BooleanProperty".equals(parameterType)) { 347 val = parameter.values("booleanValue"); 348 } 349 else if("org:opencrx:kernel:base:IntegerProperty".equals(parameterType)) { 350 val = parameter.values("integerValue"); 351 } 352 else if("org:opencrx:kernel:base:DecimalProperty".equals(parameterType)) { 353 val = parameter.values("decimalValue"); 354 } 355 else if("org:opencrx:kernel:base:UriProperty".equals(parameterType)) { 356 val = parameter.values("uriValue"); 357 } 358 else { 359 val = parameter.values("stringValue"); 360 } 361 params.put( 362 parameter.values("name").get(0), 363 val 364 ); 365 } 366 try { 368 workflow.execute( 369 userHome, 370 targetObjectIdentity, 371 params, 372 wfProcessInstance.path(), 373 this.header, 374 this.delegation, 375 this.plugin 376 ); 377 } 378 catch(ServiceException e) { 379 e.log(); 380 throw e; 381 } 382 catch(Exception e) { 383 ServiceException e0 = new ServiceException(e); 384 e0.log(); 385 throw e0; 386 } 387 } 388 return wfProcessInstance; 389 } 390 391 public static final short STATUS_OK = 0; 395 public static final short STATUS_FAILED = 1; 396 397 private final ServiceHeader header; 398 private final RequestCollection delegation; 399 private final OpenCrxKernel_1 plugin; 400 private final RefPackage_1_0 rootPkg; 401 402 } 403 404 | Popular Tags |