KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > activities > UtilActivity


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.activities;
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 import java.sql.Timestamp JavaDoc;
48
49 import org.ofbiz.base.util.Debug;
50 import org.ofbiz.base.util.UtilMisc;
51 import org.ofbiz.base.util.UtilDateTime;
52 import org.ofbiz.entity.GenericDelegator;
53 import org.ofbiz.entity.GenericValue;
54 import org.ofbiz.entity.GenericEntityException;
55 import org.ofbiz.entity.condition.EntityConditionList;
56 import org.ofbiz.entity.condition.EntityExpr;
57 import org.ofbiz.entity.condition.EntityOperator;
58 import org.ofbiz.entity.util.EntityFindOptions;
59 import org.ofbiz.entity.util.EntityUtil;
60 import org.ofbiz.entity.util.EntityListIterator;
61
62 import com.opensourcestrategies.crmsfa.party.PartyHelper;
63
64 /**
65  * Activity utility methods.
66  *
67  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
68  * @version $Rev: 312 $
69  */

70 public class UtilActivity {
71
72     public static final String JavaDoc module = UtilActivity.class.getName();
73
74     // list the activity statuses in one place in case the model changes
75
public static List JavaDoc ACT_STATUSES_PENDING = null;
76     public static List JavaDoc ACT_STATUSES_COMPLETED = null;
77     static {
78         ACT_STATUSES_COMPLETED = UtilMisc.toList("TASK_COMPLETED", "TASK_CANCELLED", "TASK_REJECTED", "EVENT_COMPLETED", "EVENT_CANCELLED", "EVENT_REJECTED");
79         ACT_STATUSES_PENDING = UtilMisc.toList("TASK_SCHEDULED", "TASK_CONFIRMED", "TASK_ON_HOLD", "EVENT_SCHEDULED", "EVENT_CONFIRMED", "EVENT_ON_HOLD");
80         ACT_STATUSES_PENDING.add("TASK_STARTED");
81         ACT_STATUSES_PENDING.add("EVENT_STARTED");
82     }
83
84     /**
85      * gets all unexpired parties related to the work effort. The result is a list of WorkEffortPartyAssignments containing
86      * the partyIds we need.
87      */

88     public static List JavaDoc getActivityParties(GenericDelegator delegator, String JavaDoc workEffortId) throws GenericEntityException {
89         // add each role type id (ACCOUNT, CONTACT, etc) to an OR condition list
90
List JavaDoc roleCondList = new ArrayList JavaDoc();
91         for (Iterator JavaDoc iter = PartyHelper.CLIENT_PARTY_ROLES.iterator(); iter.hasNext(); ) {
92             String JavaDoc roleTypeId = (String JavaDoc) iter.next();
93             roleCondList.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, roleTypeId));
94         }
95         EntityConditionList roleEntityCondList = new EntityConditionList(roleCondList, EntityOperator.OR);
96
97         // roleEntityCondList AND workEffortId = ${workEffortId} AND filterByDateExpr
98
EntityConditionList mainCondList = new EntityConditionList(UtilMisc.toList(
99                     roleEntityCondList,
100                     new EntityExpr("workEffortId", EntityOperator.EQUALS, workEffortId),
101                     EntityUtil.getFilterByDateExpr()
102                     ), EntityOperator.AND);
103
104         EntityListIterator partiesIt = delegator.findListIteratorByCondition("WorkEffortPartyAssignment", mainCondList, null,
105                 UtilMisc.toList("partyId"), // fields to select (right now we just want the partyId)
106
null, // fields to order by (unimportant here)
107
new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true));
108         return partiesIt.getCompleteList();
109     }
110
111     /**
112      * Helper method to delete all the associations related to a work effort.
113      * TODO: make this more intelligent so it doesn't delete if the association has not changed
114      * and then rename this to removeUpdatedAssociationsForWorkEffort
115      */

116     public static void removeAllAssociationsForWorkEffort(String JavaDoc workEffortId, GenericDelegator delegator) throws GenericEntityException {
117
118         // delete any existing opportunity relationships
119
List JavaDoc oldAssocs = delegator.findByAnd("SalesOpportunityWorkEffort", UtilMisc.toMap("workEffortId", workEffortId));
120         for (Iterator JavaDoc iter = oldAssocs.iterator(); iter.hasNext(); ) {
121             GenericValue old = (GenericValue) iter.next();
122             // TODO: deleted by hand because we don't have a service yet
123
old.remove();
124         }
125
126         // delete any existing case relationships
127
oldAssocs = delegator.findByAnd("CustRequestWorkEffort", UtilMisc.toMap("workEffortId", workEffortId));
128         for (Iterator JavaDoc iter = oldAssocs.iterator(); iter.hasNext(); ) {
129             GenericValue old = (GenericValue) iter.next();
130             // TODO: add permissions to deleteWorkEffortRequest and use that service, otherwise it's exactly identical to the following line
131
old.remove();
132         }
133
134         // delete existing party relationships with roles like ACCOUNT, CONTACT, PROSPECT
135
for (Iterator JavaDoc iter = PartyHelper.CLIENT_PARTY_ROLES.iterator(); iter.hasNext(); ) {
136             oldAssocs = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment",
137                         UtilMisc.toMap("workEffortId", workEffortId, "roleTypeId", iter.next())));
138             for (Iterator JavaDoc inner = oldAssocs.iterator(); inner.hasNext(); ) {
139                 GenericValue assoc = (GenericValue) inner.next();
140                 assoc.set("thruDate", UtilDateTime.nowTimestamp());
141                 assoc.store();
142             }
143         }
144     }
145
146     /**
147      * Helper method to check if a userLogin has any TASK or EVENT conflicts between two timestamps.
148      * It will search the WorkEffortAndPartyAssign view for all EVENT_SCHEDULED events
149      * that the user marked as availabilityStatusId=WEPT_AV_BUSY or WEPT_AV_AWAY, and
150      * statusId=PRTYASGN_ASSIGNED, and filter out those that fall outside the specified duration.
151      *
152      * @param workEffortId if specified, filters out the work effort ID
153      * @return List of events/tasks that the user conflicts with
154      */

155     public static List JavaDoc getActivityConflicts(GenericValue userLogin, Timestamp JavaDoc start, Timestamp JavaDoc end, String JavaDoc workEffortId) throws GenericEntityException {
156         GenericDelegator delegator = userLogin.getDelegator();
157         List JavaDoc conditions = UtilMisc.toList(
158                 new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.getString("partyId")),
159                 new EntityConditionList(
160                     UtilMisc.toList(
161                         new EntityExpr("currentStatusId", EntityOperator.EQUALS, "EVENT_SCHEDULED"),
162                         new EntityExpr("currentStatusId", EntityOperator.EQUALS, "TASK_SCHEDULED")
163                         ), EntityOperator.OR
164                     ),
165                 new EntityExpr("statusId", EntityOperator.EQUALS, "PRTYASGN_ASSIGNED"),
166                 new EntityConditionList(
167                     UtilMisc.toList(
168                         new EntityExpr("availabilityStatusId", EntityOperator.EQUALS, "WEPA_AV_BUSY"),
169                         new EntityExpr("availabilityStatusId", EntityOperator.EQUALS, "WEPA_AV_AWAY")
170                         ), EntityOperator.OR
171                     ),
172                 new EntityConditionList(
173                     UtilMisc.toList(
174                         EntityUtil.getFilterByDateExpr(start, "estimatedStartDate", "estimatedCompletionDate"),
175                         EntityUtil.getFilterByDateExpr(end, "estimatedStartDate", "estimatedCompletionDate")
176                         ), EntityOperator.OR
177                     )
178                 );
179         if (workEffortId != null) conditions.add(new EntityExpr("workEffortId", EntityOperator.NOT_EQUAL, workEffortId));
180         EntityConditionList cond = new EntityConditionList(conditions, EntityOperator.AND);
181         return delegator.findByCondition("WorkEffortAndPartyAssign", cond, UtilMisc.toList("workEffortId"), null);
182     }
183
184     /**
185      * As above, but for all work efforts.
186      */

187     public static List JavaDoc getActivityConflicts(GenericValue userLogin, Timestamp JavaDoc start, Timestamp JavaDoc end) throws GenericEntityException {
188         return getActivityConflicts(userLogin, start, end, null);
189     }
190
191     /**
192      * Determine if the activity has a status which should be considered "finished" or "history" or "done with".
193      * It is recommended to use this instead of checking ACT_STATUSES_COMPLETED directly or testing the statuses by hand
194      */

195     public static boolean activityIsInactive(GenericValue activity) {
196         for (Iterator JavaDoc iter = ACT_STATUSES_COMPLETED.iterator(); iter.hasNext(); ) {
197             if (iter.next().equals(activity.getString("currentStatusId"))) {
198                 return true;
199             }
200         }
201         return false;
202     }
203 }
204
Popular Tags