KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > layer > application > Accounts


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: Accounts.java,v 1.19 2006/03/18 00:32:17 wfro Exp $
5  * Description: openCRX application plugin
6  * Revision: $Revision: 1.19 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/18 00:32:17 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2005, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.layer.application;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.HashSet JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Set JavaDoc;
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     //-----------------------------------------------------------------------
85
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     //-------------------------------------------------------------------------
98
void completeOuMembership(
99         DataproviderObject_1_0 contact
100     ) {
101         try {
102             // find on ContactMembership extent
103
List JavaDoc ouMemberships = new ArrayList JavaDoc();
104             
105             // organization memberships
106
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 JavaDoc[]{
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 JavaDoc[]{
123                                 contact.path()
124                             }
125                         )
126                     }
127                 )
128             );
129
130             // organizational unit memberships
131
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 JavaDoc[]{
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 JavaDoc[]{
148                                 contact.path()
149                             }
150                         )
151                     }
152                 )
153             );
154
155             Set JavaDoc memberships = new HashSet JavaDoc();
156             for(Iterator JavaDoc 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     //-------------------------------------------------------------------------
169
public DataproviderObject createContract(
170         ServiceHeader header,
171         DataproviderObject_1_0 account,
172         DataproviderObject param,
173         String JavaDoc contractType,
174         String JavaDoc referenceName
175     ) {
176         DataproviderObject contract = null;
177         try {
178             // Create new contract based on existing contract or contract template
179
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 JavaDoc)contractBase.values("isTemplate").get(0)).booleanValue()) {
185                     contractIdentity = this.plugin.createObjectFromTemplate(
186                         header,
187                         contractBase,
188                         (String JavaDoc)param.values("name").get(0),
189                         (String JavaDoc)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             // Create new contract
205
else {
206                 Path contractIdentity = new Path("xri:@openmdx:org.opencrx.kernel.contract1/provider").getDescendant(
207                     new String JavaDoc[]{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             // Update supplied contract attributes
218
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             // customer
228
contract.clearValues("customer").add(account.path());
229             // activeOn
230
contract.clearValues("activeOn").add(
231                 DateFormat.getInstance().format(new Date JavaDoc())
232             );
233             
234             // assign to current user
235
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     //-------------------------------------------------------------------------
250
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     //-------------------------------------------------------------------------
265
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     //-------------------------------------------------------------------------
280
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     //-------------------------------------------------------------------------
295
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     //-------------------------------------------------------------------------
310
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     //-------------------------------------------------------------------------
325
public void setAccountFullName(
326         DataproviderObject obj,
327         DataproviderObject_1_0 oldValues
328     ) throws ServiceException {
329         String JavaDoc objectClass = (String JavaDoc)obj.values(
330             SystemAttributes.OBJECT_CLASS
331         ).get(0);
332
333         // org:opencrx:kernel:account1:Contact:fullName
334
if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:Contact")) {
335             List JavaDoc lastName = this.plugin.getNewValue("lastName", obj, oldValues);
336             List JavaDoc firstName = this.plugin.getNewValue("firstName", obj, oldValues);
337             List JavaDoc 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         // org:opencrx:kernel:account1:LegalEntity:fullName
346
else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:LegalEntity")) {
347             List JavaDoc name = this.plugin.getNewValue("name", obj, oldValues);
348             obj.clearValues("fullName").add(
349                 name.size() == 0 ? "" : name.get(0)
350             );
351         }
352
353         // org:opencrx:kernel:account1:UnspecifiedAccount:fullName
354
else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:UnspecifiedAccount")) {
355             List JavaDoc name = this.plugin.getNewValue("name", obj, oldValues);
356             obj.clearValues("fullName").add(
357                 name.size() == 0 ? "" : name.get(0)
358             );
359         }
360
361         // org:opencrx:kernel:account1:Group:fullName
362
else if(this.model.isSubtypeOf(objectClass, "org:opencrx:kernel:account1:Group")) {
363             List JavaDoc name = this.plugin.getNewValue("name", obj, oldValues);
364             obj.clearValues("fullName").add(
365                 name.size() == 0 ? "" : name.get(0)
366             );
367         }
368     }
369     
370     //-------------------------------------------------------------------------
371
public FilterProperty[] getAccountFilterProperties(
372         Path activityFilterIdentity
373     ) throws ServiceException {
374         List JavaDoc 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 JavaDoc filter = new ArrayList JavaDoc();
384         for(
385             Iterator JavaDoc i = filterProperties.iterator();
386             i.hasNext();
387         ) {
388             DataproviderObject_1_0 filterProperty = (DataproviderObject_1_0)i.next();
389             String JavaDoc filterPropertyClass = (String JavaDoc)filterProperty.values(SystemAttributes.OBJECT_CLASS).get(0);
390
391             Boolean JavaDoc isActive = (Boolean JavaDoc)filterProperty.values("isActive").get(0);
392             
393             if((isActive != null) && isActive.booleanValue()) {
394                 // Get filterOperator, filterQuantor
395
short filterOperator = filterProperty.values("filterOperator").size() == 0
396                     ? FilterOperators.IS_IN
397                     : ((Number JavaDoc)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 JavaDoc)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     //-------------------------------------------------------------------------
444
// Members
445
//-------------------------------------------------------------------------
446
public static final String JavaDoc 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 //--- End of File -----------------------------------------------------------
456
Popular Tags