KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > cases > UtilCase


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16 /* Copyright (c) 2005-2006 Open Source Strategies, Inc. */
17
18 /*
19  * $Id:$
20  *
21  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining a
24  * copy of this software and associated documentation files (the "Software"),
25  * to deal in the Software without restriction, including without limitation
26  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
27  * and/or sell copies of the Software, and to permit persons to whom the
28  * Software is furnished to do so, subject to the following conditions:
29  *
30  * The above copyright notice and this permission notice shall be included
31  * in all copies or substantial portions of the Software.
32  *
33  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
34  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
36  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
37  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
38  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
39  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40  */

41 package com.opensourcestrategies.crmsfa.cases;
42
43 import java.util.Map JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 import org.ofbiz.base.util.Debug;
49 import org.ofbiz.base.util.UtilMisc;
50 import org.ofbiz.entity.GenericDelegator;
51 import org.ofbiz.entity.GenericValue;
52 import org.ofbiz.entity.GenericEntityException;
53 import org.ofbiz.entity.condition.EntityConditionList;
54 import org.ofbiz.entity.condition.EntityExpr;
55 import org.ofbiz.entity.condition.EntityOperator;
56 import org.ofbiz.entity.util.EntityFindOptions;
57 import org.ofbiz.entity.util.EntityUtil;
58 import org.ofbiz.entity.util.EntityListIterator;
59
60 /**
61  * Case utility methods.
62  *
63  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
64  * @version $Rev: 312 $
65  */

66 public class UtilCase {
67
68     public static final String JavaDoc module = UtilCase.class.getName();
69
70     // list the case statuses in one place in case the model changes
71
public static List JavaDoc CASE_STATUSES_COMPLETED = null;
72     static {
73         CASE_STATUSES_COMPLETED = UtilMisc.toList("CRQ_COMPLETED", "CRQ_CANCELLED", "CRQ_REJECTED");
74     }
75
76     /**
77      * Get the active account and contacts for a case.
78      * @return List of PartyRelationshipAndCaseRoles with the requested partyId's.
79      */

80     public static List JavaDoc getCaseAccountsAndContacts(GenericDelegator delegator, String JavaDoc custRequestId) throws GenericEntityException {
81         return getCasePartiesByRole(delegator, custRequestId, UtilMisc.toList("ACCOUNT", "CONTACT"));
82     }
83
84     /**
85      * Gets the first active contact party ID for a case.
86      * @return partyId of contact or null
87      */

88     public static String JavaDoc getCasePrimaryContactPartyId(GenericDelegator delegator, String JavaDoc custRequestId) throws GenericEntityException {
89         List JavaDoc candidates = getCasePartiesByRole(delegator, custRequestId, UtilMisc.toList("CONTACT"));
90         if (candidates.size() > 0) {
91             return ((GenericValue) candidates.get(0)).getString("partyId");
92         }
93         return null;
94     }
95
96     /**
97      * Gets the first active account party ID for a case.
98      * @return partyId of account or null
99      */

100     public static String JavaDoc getCasePrimaryAccountPartyId(GenericDelegator delegator, String JavaDoc custRequestId) throws GenericEntityException {
101         List JavaDoc candidates = getCasePartiesByRole(delegator, custRequestId, UtilMisc.toList("ACCOUNT"));
102         if (candidates.size() > 0) {
103             return ((GenericValue) candidates.get(0)).getString("partyId");
104         }
105         return null;
106     }
107
108     /**
109      * Helper method to get active party relationships related to a given case via the cust request roles. This is used, for instance, to
110      * get unexpired ACCOUNTS or CONTACTS related to the case. This method is to be used for logic, not presentation. Presentation
111      * requires ordering and fields from other joined entities. Don't use this directly, use one of the more convenient helper methods.
112      * @return A list of PartyRelationshipAndCaseRole with partyId's of the requested parties
113      */

114     public static List JavaDoc getCasePartiesByRole(GenericDelegator delegator, String JavaDoc custRequestId, List JavaDoc roleTypeIds)
115         throws GenericEntityException {
116
117         // add each role type id to an OR condition list
118
List JavaDoc roleCondList = new ArrayList JavaDoc();
119         for (Iterator JavaDoc iter = roleTypeIds.iterator(); iter.hasNext(); ) {
120             String JavaDoc roleTypeId = (String JavaDoc) iter.next();
121             roleCondList.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, roleTypeId));
122         }
123         EntityConditionList roleEntityCondList = new EntityConditionList(roleCondList, EntityOperator.OR);
124
125         // roleEntityCondList AND custRequestId = ${custRequestID} AND filterByDateExpr
126
EntityConditionList mainCondList = new EntityConditionList(UtilMisc.toList(
127                     roleEntityCondList,
128                     new EntityExpr("custRequestId", EntityOperator.EQUALS, custRequestId),
129                     EntityUtil.getFilterByDateExpr()
130                     ), EntityOperator.AND);
131
132         EntityListIterator partiesIt = delegator.findListIteratorByCondition("PartyRelationshipAndCaseRole", mainCondList, null,
133                 UtilMisc.toList("partyId"), // fields to select (right now we just want the partyId)
134
null, // fields to order by (unimportant here)
135
new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true));
136         return partiesIt.getCompleteList();
137     }
138     
139     /**
140      * Finds all CustRequest which are open for a given party and role combination. CustRequest must not be CANCELLED, REJECTED, or COMPLETED.
141      * @param delegator
142      * @param partyId
143      * @param roleTypeId
144      * @param casesOrderBy field to order by. Defaults to "priority DESC"
145      * @return list iterator of the cases
146      * @throws GenericEntityException
147      */

148     public static EntityListIterator getCasesForParty(GenericDelegator delegator, String JavaDoc partyId, String JavaDoc roleTypeId, String JavaDoc casesOrderBy) throws GenericEntityException {
149         if (casesOrderBy == null) {
150             casesOrderBy = "priority DESC";
151         }
152         EntityConditionList casesCond = new EntityConditionList(UtilMisc.toList(
153                 new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CRQ_COMPLETED"),
154                 new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CRQ_REJECTED"),
155                 new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CRQ_CANCELLED"),
156                 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, roleTypeId),
157                 new EntityExpr("partyIdFrom", EntityOperator.EQUALS, partyId)
158                 ), EntityOperator.AND);
159         
160         EntityListIterator myCases = delegator.findListIteratorByCondition("PartyRelationshipAndCaseRole", casesCond, null,
161                 UtilMisc.toList("custRequestId", "custRequestName", "priority", "statusId", "custRequestTypeId", "custRequestCategoryId"), // fields to select
162
UtilMisc.toList(casesOrderBy), // fields to order by
163
// the first true here is for "specifyTypeAndConcur"
164
// the second true is for a distinct select. Apparently this is the only way the entity engine can do a distinct query
165
new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true));
166         
167         return myCases;
168     }
169     
170     /**
171      * Determine if the case has a status which should be considered "finished" or "history" or "done with".
172      * It is recommended to use this instead of checking statuses by hand, because those can change.
173      */

174     public static boolean caseIsInactive(GenericValue custRequest) {
175         for (Iterator JavaDoc iter = CASE_STATUSES_COMPLETED.iterator(); iter.hasNext(); ) {
176             if (iter.next().equals(custRequest.getString("statusId"))) {
177                 return true;
178             }
179         }
180         return false;
181     }
182 }
183
Popular Tags