1 56 package org.opencrx.kernel.layer.application; 57 58 import java.util.ArrayList ; 59 import java.util.Date ; 60 import java.util.HashSet ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Set ; 64 65 import org.openmdx.application.log.AppLog; 66 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0; 67 import org.openmdx.base.exception.ServiceException; 68 import org.openmdx.base.text.format.DateFormat; 69 import org.openmdx.compatibility.base.dataprovider.cci.AttributeSelectors; 70 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject; 71 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0; 72 import org.openmdx.compatibility.base.dataprovider.cci.Directions; 73 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection; 74 import org.openmdx.compatibility.base.dataprovider.cci.ServiceHeader; 75 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes; 76 import org.openmdx.compatibility.base.naming.Path; 77 import org.openmdx.compatibility.base.query.FilterOperators; 78 import org.openmdx.compatibility.base.query.FilterProperty; 79 import org.openmdx.compatibility.base.query.Quantors; 80 import org.openmdx.model1.accessor.basic.cci.Model_1_0; 81 82 public class Accounts { 83 84 public Accounts( 86 Model_1_0 model, 87 OpenCrxKernel_1 plugin, 88 RequestCollection delegation, 89 RefPackage_1_0 rootPkg 90 ) { 91 this.model = model; 92 this.plugin = plugin; 93 this.delegation = delegation; 94 this.rootPkg = rootPkg; 95 } 96 97 void completeOuMembership( 99 DataproviderObject_1_0 contact 100 ) { 101 try { 102 List ouMemberships = new ArrayList (); 104 105 ouMemberships.addAll( 107 this.delegation.addFindRequest( 108 contact.path().getPrefix(5).getChild("extent"), 109 new FilterProperty[]{ 110 new FilterProperty( 111 Quantors.THERE_EXISTS, 112 "identity", 113 FilterOperators.IS_LIKE, 114 new String []{ 115 "xri:@openmdx:org.opencrx.kernel.account1/provider/:*/segment/:*/organization/:*/contactMembership/:*" 116 } 117 ), 118 new FilterProperty( 119 Quantors.THERE_EXISTS, 120 "contact", 121 FilterOperators.IS_IN, 122 new Object []{ 123 contact.path() 124 } 125 ) 126 } 127 ) 128 ); 129 130 ouMemberships.addAll( 132 this.delegation.addFindRequest( 133 contact.path().getPrefix(5).getChild("extent"), 134 new FilterProperty[]{ 135 new FilterProperty( 136 Quantors.THERE_EXISTS, 137 "identity", 138 FilterOperators.IS_LIKE, 139 new String []{ 140 "xri:@openmdx:org.opencrx.kernel.account1/provider/:*/segment/:*/organization/:*/organizationalUnit/:*/contactMembership/:*" 141 } 142 ), 143 new FilterProperty( 144 Quantors.THERE_EXISTS, 145 "contact", 146 FilterOperators.IS_IN, 147 new Object []{ 148 contact.path() 149 } 150 ) 151 } 152 ) 153 ); 154 155 Set memberships = new HashSet (); 156 for(Iterator i = ouMemberships.iterator(); i.hasNext(); ) { 157 memberships.add( 158 ((DataproviderObject_1_0)i.next()).path().getParent().getParent() 159 ); 160 } 161 contact.clearValues("ouMembership").addAll(memberships); 162 } 163 catch(ServiceException e) { 164 AppLog.info(e.getMessage(), e.getCause(), 1); 165 } 166 } 167 168 public DataproviderObject createContract( 170 ServiceHeader header, 171 DataproviderObject_1_0 account, 172 DataproviderObject param, 173 String contractType, 174 String referenceName 175 ) { 176 DataproviderObject contract = null; 177 try { 178 if((param.getValues("basedOn") != null) && (param.values("basedOn").size() > 0)) { 180 DataproviderObject_1_0 contractBase = this.plugin.retrieveObjectFromDelegation( 181 (Path)param.values("basedOn").get(0) 182 ); 183 Path contractIdentity = null; 184 if((contractBase.values("isTemplate").size() > 0) && ((Boolean )contractBase.values("isTemplate").get(0)).booleanValue()) { 185 contractIdentity = this.plugin.createObjectFromTemplate( 186 header, 187 contractBase, 188 (String )param.values("name").get(0), 189 (String )contractBase.values("templateReferenceFilter").get(0) 190 ); 191 } 192 else { 193 contractIdentity = this.plugin.cloneable.cloneAndUpdateReferences( 194 header, 195 contractBase, 196 contractBase.path().getParent(), 197 null, 198 DEFAULT_REFERENCE_FILTER, 199 false 200 ).path(); 201 } 202 contract = this.plugin.retrieveObjectForModification(contractIdentity); 203 } 204 else { 206 Path contractIdentity = new Path("xri:@openmdx:org.opencrx.kernel.contract1/provider").getDescendant( 207 new String []{account.path().get(2), "segment", account.path().get(4), referenceName, this.plugin.getUidAsString()} 208 ); 209 contract = new DataproviderObject(contractIdentity); 210 contract.values(SystemAttributes.OBJECT_CLASS).add(contractType); 211 this.delegation.addCreateRequest( 212 contract 213 ); 214 contract = this.plugin.retrieveObjectForModification(contractIdentity); 215 } 216 217 if(param.values("name").size() > 0) { 219 contract.clearValues("name").addAll(param.values("name")); 220 } 221 if(param.values("description").size() > 0) { 222 contract.clearValues("description").addAll(param.values("description")); 223 } 224 if(param.values("nextStep").size() > 0) { 225 contract.clearValues("nextStep").addAll(param.values("nextStep")); 226 } 227 contract.clearValues("customer").add(account.path()); 229 contract.clearValues("activeOn").add( 231 DateFormat.getInstance().format(new Date ()) 232 ); 233 234 this.plugin.assignToMe( 236 header, 237 contract, 238 null, 239 true, 240 null 241 ); 242 } 243 catch(ServiceException e) { 244 AppLog.info(e.getMessage(), e.getCause(), 1); 245 } 246 return contract; 247 } 248 249 public DataproviderObject createLead( 251 ServiceHeader header, 252 DataproviderObject_1_0 account, 253 DataproviderObject param 254 ) { 255 return this.createContract( 256 header, 257 account, 258 param, 259 "org:opencrx:kernel:contract1:Lead", 260 "lead" 261 ); 262 } 263 264 public DataproviderObject createOpportunity( 266 ServiceHeader header, 267 DataproviderObject_1_0 account, 268 DataproviderObject param 269 ) { 270 return this.createContract( 271 header, 272 account, 273 param, 274 "org:opencrx:kernel:contract1:Opportunity", 275 "opportunity" 276 ); 277 } 278 279 public DataproviderObject createQuote( 281 ServiceHeader header, 282 DataproviderObject_1_0 account, 283 DataproviderObject param 284 ) { 285 return this.createContract( 286 header, 287 account, 288 param, 289 "org:opencrx:kernel:contract1:Quote", 290 "quote" 291 ); 292 } 293 294 public DataproviderObject createSalesOrder( 296 ServiceHeader header, 297 DataproviderObject_1_0 account, 298 DataproviderObject param 299 ) { 300 return this.createContract( 301 header, 302 account, 303 param, 304 "org:opencrx:kernel:contract1:SalesOrder", 305 "salesOrder" 306 ); 307 } 308 309 public DataproviderObject createInvoice( 311 ServiceHeader header, 312 DataproviderObject_1_0 account, 313 DataproviderObject param 314 ) { 315 return this.createContract( 316 header, 317 account, 318 param, 319 "org:opencrx:kernel:contract1:Invoice", 320 "invoice" 321 ); 322 } 323 324 public void setAccountFullName( 326 DataproviderObject obj, 327 DataproviderObject_1_0 oldValues 328 ) throws ServiceException { 329 String objectClass = (String )obj.values( 330 SystemAttributes.OBJECT_CLASS 331 ).get(0); 332 333 if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:Contact")) { 335 List lastName = this.plugin.getNewValue("lastName", obj, oldValues); 336 List firstName = this.plugin.getNewValue("firstName", obj, oldValues); 337 List middleName = this.plugin.getNewValue("middleName", obj, oldValues); 338 obj.clearValues("fullName").add( 339 ( (lastName.size() == 0 ? "" : lastName.get(0)) + ", " 340 + (firstName.size() == 0 ? "" : firstName.get(0) + " ") 341 + (middleName.size() == 0 ? "" : middleName.get(0) + "") ).trim() 342 ); 343 } 344 345 else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:LegalEntity")) { 347 List name = this.plugin.getNewValue("name", obj, oldValues); 348 obj.clearValues("fullName").add( 349 name.size() == 0 ? "" : name.get(0) 350 ); 351 } 352 353 else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:UnspecifiedAccount")) { 355 List name = this.plugin.getNewValue("name", obj, oldValues); 356 obj.clearValues("fullName").add( 357 name.size() == 0 ? "" : name.get(0) 358 ); 359 } 360 361 else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:Group")) { 363 List name = this.plugin.getNewValue("name", obj, oldValues); 364 obj.clearValues("fullName").add( 365 name.size() == 0 ? "" : name.get(0) 366 ); 367 } 368 } 369 370 public FilterProperty[] getAccountFilterProperties( 372 Path activityFilterIdentity 373 ) throws ServiceException { 374 List filterProperties = this.delegation.addFindRequest( 375 activityFilterIdentity.getChild("accountFilterProperty"), 376 null, 377 AttributeSelectors.ALL_ATTRIBUTES, 378 null, 379 0, 380 Integer.MAX_VALUE, 381 Directions.ASCENDING 382 ); 383 List filter = new ArrayList (); 384 for( 385 Iterator i = filterProperties.iterator(); 386 i.hasNext(); 387 ) { 388 DataproviderObject_1_0 filterProperty = (DataproviderObject_1_0)i.next(); 389 String filterPropertyClass = (String )filterProperty.values(SystemAttributes.OBJECT_CLASS).get(0); 390 391 Boolean isActive = (Boolean )filterProperty.values("isActive").get(0); 392 393 if((isActive != null) && isActive.booleanValue()) { 394 short filterOperator = filterProperty.values("filterOperator").size() == 0 396 ? FilterOperators.IS_IN 397 : ((Number )filterProperty.values("filterOperator").get(0)).shortValue(); 398 filterOperator = filterOperator == 0 399 ? FilterOperators.IS_IN 400 : filterOperator; 401 short filterQuantor = filterProperty.values("filterQuantor").size() == 0 402 ? Quantors.THERE_EXISTS 403 : ((Number )filterProperty.values("filterQuantor").get(0)).shortValue(); 404 filterQuantor = filterQuantor == 0 405 ? Quantors.THERE_EXISTS 406 : filterQuantor; 407 408 if("org:opencrx:kernel:account1:AccountTypeFilterProperty".equals(filterPropertyClass)) { 409 filter.add( 410 new FilterProperty( 411 filterQuantor, 412 "accountType", 413 filterOperator, 414 filterProperty.values("accountType").toArray() 415 ) 416 ); 417 } 418 else if("org:opencrx:kernel:account1:AccountCategoryFilterProperty".equals(filterPropertyClass)) { 419 filter.add( 420 new FilterProperty( 421 filterQuantor, 422 "accountCategory", 423 filterOperator, 424 filterProperty.values("accountCategory").toArray() 425 ) 426 ); 427 } 428 else if("org:opencrx:kernel:account1:CategoryFilterProperty".equals(filterPropertyClass)) { 429 filter.add( 430 new FilterProperty( 431 filterQuantor, 432 "category", 433 filterOperator, 434 filterProperty.values("category").toArray() 435 ) 436 ); 437 } 438 } 439 } 440 return (FilterProperty[])filter.toArray(new FilterProperty[filter.size()]); 441 } 442 443 public static final String DEFAULT_REFERENCE_FILTER = ":*, :*/:*/:*, :*/:*/:*/:*/:*"; 447 448 private final Model_1_0 model; 449 private final OpenCrxKernel_1 plugin; 450 private final RequestCollection delegation; 451 private final RefPackage_1_0 rootPkg; 452 453 } 454 455 | Popular Tags |