KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > contacts > ContactsServices


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

39 package com.opensourcestrategies.crmsfa.contacts;
40
41 import java.util.Map JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.sql.Date JavaDoc;
45 import java.sql.Timestamp JavaDoc;
46
47 import javolution.util.FastMap;
48
49 import org.ofbiz.base.util.Debug;
50 import org.ofbiz.base.util.UtilMisc;
51 import org.ofbiz.base.util.UtilProperties;
52 import org.ofbiz.base.util.UtilDateTime;
53 import org.ofbiz.entity.GenericDelegator;
54 import org.ofbiz.entity.GenericEntityException;
55 import org.ofbiz.entity.GenericValue;
56 import org.ofbiz.entity.condition.EntityConditionList;
57 import org.ofbiz.entity.condition.EntityExpr;
58 import org.ofbiz.entity.condition.EntityOperator;
59 import org.ofbiz.entity.util.EntityUtil;
60 import org.ofbiz.service.DispatchContext;
61 import org.ofbiz.service.GenericServiceException;
62 import org.ofbiz.service.LocalDispatcher;
63 import org.ofbiz.service.ServiceUtil;
64 import org.ofbiz.security.Security;
65
66 import com.opensourcestrategies.crmsfa.party.PartyHelper;
67 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;
68 import com.opensourcestrategies.crmsfa.util.UtilCommon;
69
70 /**
71  * Contacts services. The service documentation is in services_contacts.xml.
72  *
73  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
74  */

75
76 public class ContactsServices {
77
78     public static final String JavaDoc module = ContactsServices.class.getName();
79
80     public static Map JavaDoc createContact(DispatchContext dctx, Map JavaDoc context) {
81         GenericDelegator delegator = dctx.getDelegator();
82         LocalDispatcher dispatcher = dctx.getDispatcher();
83         Security security = dctx.getSecurity();
84         GenericValue userLogin = (GenericValue) context.get("userLogin");
85         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
86
87         if (!security.hasPermission("CRMSFA_CONTACT_CREATE", userLogin)) {
88             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
89         }
90
91         // the net result of creating an contact is the generation of a Contact partyId
92
String JavaDoc contactPartyId = null;
93         try {
94             // create the Party and Person, which results in a partyId
95
Map JavaDoc input = UtilMisc.toMap("firstName", context.get("firstName"), "lastName", context.get("lastName"));
96             input.put("firstNameLocal", context.get("firstNameLocal"));
97             input.put("lastNameLocal", context.get("lastNameLocal"));
98             input.put("personalTitle", context.get("personalTitle"));
99             input.put("preferredCurrencyUomId", context.get("preferredCurrencyUomId"));
100             input.put("description", context.get("description"));
101             input.put("birthDate", context.get("birthDate"));
102             Map JavaDoc serviceResults = dispatcher.runSync("createPerson", input);
103             if (ServiceUtil.isError(serviceResults)) {
104                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateContactFail", locale, module);
105             }
106             contactPartyId = (String JavaDoc) serviceResults.get("partyId");
107
108             // create a PartyRole for the resulting Contact partyId with roleTypeId = CONTACT
109
serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", contactPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin));
110             if (ServiceUtil.isError(serviceResults)) {
111                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateContactFail", locale, module);
112             }
113
114             // create PartySupplementalData
115
GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId));
116             partyData.setNonPKFields(context);
117             partyData.create();
118
119             // create a party relationship between the userLogin and the Contact with partyRelationshipTypeId RESPONSIBLE_FOR
120
createResponsibleContactRelationshipForParty(userLogin.getString("partyId"), contactPartyId, userLogin, delegator, dispatcher);
121
122         } catch (GenericServiceException e) {
123             return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, module);
124         } catch (GenericEntityException e) {
125             return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, module);
126         }
127
128         // return the partyId of the newly created Contact
129
Map JavaDoc results = ServiceUtil.returnSuccess();
130         results.put("partyId", contactPartyId);
131         results.put("contactPartyId", contactPartyId);
132         return results;
133     }
134
135     public static Map JavaDoc updateContact(DispatchContext dctx, Map JavaDoc context) {
136         GenericDelegator delegator = dctx.getDelegator();
137         LocalDispatcher dispatcher = dctx.getDispatcher();
138         Security security = dctx.getSecurity();
139         GenericValue userLogin = (GenericValue) context.get("userLogin");
140         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
141
142         String JavaDoc contactPartyId = (String JavaDoc) context.get("partyId");
143
144         // make sure userLogin has CRMSFA_CONTACT_UPDATE permission for this contact
145
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_UPDATE", userLogin, contactPartyId)) {
146             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
147         }
148         try {
149             // update the Party and Person
150
Map JavaDoc input = UtilMisc.toMap("partyId", contactPartyId, "firstName", context.get("firstName"), "lastName", context.get("lastName"));
151             input.put("firstNameLocal", context.get("firstNameLocal"));
152             input.put("lastNameLocal", context.get("lastNameLocal"));
153             input.put("personalTitle", context.get("personalTitle"));
154             input.put("preferredCurrencyUomId", context.get("preferredCurrencyUomId"));
155             input.put("description", context.get("description"));
156             input.put("birthDate", context.get("birthDate"));
157             input.put("userLogin", userLogin);
158             Map JavaDoc serviceResults = dispatcher.runSync("updatePerson", input);
159             if (ServiceUtil.isError(serviceResults)) {
160                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateContactFail", locale, module);
161             }
162
163             // update PartySupplementalData
164
GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId));
165             if (partyData == null) {
166                 // create a new one
167
partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId));
168                 partyData.create();
169             }
170             partyData.setNonPKFields(context);
171             partyData.store();
172
173         } catch (GenericServiceException e) {
174             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateContactFail", locale, module);
175         } catch (GenericEntityException e) {
176             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateContactFail", locale, module);
177         }
178         return ServiceUtil.returnSuccess();
179     }
180
181     public static Map JavaDoc assignContactToAccount(DispatchContext dctx, Map JavaDoc context) {
182         GenericDelegator delegator = dctx.getDelegator();
183         LocalDispatcher dispatcher = dctx.getDispatcher();
184         Security security = dctx.getSecurity();
185         GenericValue userLogin = (GenericValue) context.get("userLogin");
186         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
187
188         String JavaDoc contactPartyId = (String JavaDoc) context.get("contactPartyId");
189         String JavaDoc accountPartyId = (String JavaDoc) context.get("accountPartyId");
190
191         try {
192             // check if this contact is already a contact of this account
193
EntityConditionList searchConditions = new EntityConditionList(UtilMisc.toList(
194                     new EntityExpr("partyIdFrom", EntityOperator.EQUALS, contactPartyId),
195                     new EntityExpr("partyIdTo", EntityOperator.EQUALS, accountPartyId),
196                     new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "CONTACT"),
197                     new EntityExpr("roleTypeIdTo", EntityOperator.EQUALS, "ACCOUNT"),
198                     new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "CONTACT_REL_INV"),
199                     EntityUtil.getFilterByDateExpr()),
200                     EntityOperator.AND);
201             List JavaDoc existingRelationships = delegator.findByCondition("PartyRelationship", searchConditions, null, null);
202             if (existingRelationships.size() > 0) {
203                 return UtilCommon.createAndLogServiceError("CrmErrorContactAlreadyAssociatedToAccount", locale, module);
204             }
205             
206             // check if userLogin has CRMSFA_ACCOUNT_UPDATE permission for this account
207
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) {
208                 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
209             }
210             // create the party relationship between the Contact and the Account
211
PartyHelper.createNewPartyToRelationship(accountPartyId, contactPartyId, "CONTACT", "CONTACT_REL_INV",
212                     null, UtilMisc.toList("ACCOUNT"), false, userLogin, delegator, dispatcher);
213                 
214         }
215          catch (GenericServiceException e) {
216             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignContactToAccountFail", locale, module);
217         } catch (GenericEntityException e) {
218             return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignContactToAccountFail", locale, module);
219         }
220         return ServiceUtil.returnSuccess();
221     }
222
223     public static Map JavaDoc reassignContactResponsibleParty(DispatchContext dctx, Map JavaDoc context) {
224         GenericDelegator delegator = dctx.getDelegator();
225         LocalDispatcher dispatcher = dctx.getDispatcher();
226         Security security = dctx.getSecurity();
227         GenericValue userLogin = (GenericValue) context.get("userLogin");
228         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
229
230         String JavaDoc contactPartyId = (String JavaDoc) context.get("contactPartyId");
231         String JavaDoc newPartyId = (String JavaDoc) context.get("newPartyId");
232
233         // ensure reassign permission on this contact
234
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_REASSIGN", userLogin, contactPartyId)) {
235             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
236         }
237         try {
238             // reassign relationship using a helper method
239
boolean result = createResponsibleContactRelationshipForParty(newPartyId, contactPartyId, userLogin, delegator, dispatcher);
240             if (result == false) {
241                 return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module);
242             }
243         } catch (GenericServiceException e) {
244             return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module);
245         } catch (GenericEntityException e) {
246             return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module);
247         }
248         return ServiceUtil.returnSuccess();
249     }
250
251     public static Map JavaDoc removeContactFromAccount(DispatchContext dctx, Map JavaDoc context) {
252         GenericDelegator delegator = dctx.getDelegator();
253         LocalDispatcher dispatcher = dctx.getDispatcher();
254         Security security = dctx.getSecurity();
255         GenericValue userLogin = (GenericValue) context.get("userLogin");
256         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
257
258         String JavaDoc contactPartyId = (String JavaDoc) context.get("contactPartyId");
259         String JavaDoc accountPartyId = (String JavaDoc) context.get("accountPartyId");
260
261         // ensure update permission on account
262
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) {
263             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
264         }
265         try {
266             // find and expire all contact relationships between the contact and account
267
List JavaDoc relations = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdTo", accountPartyId,
268                         "partyIdFrom", contactPartyId, "partyRelationshipTypeId", "CONTACT_REL_INV"));
269             PartyHelper.expirePartyRelationships(relations, UtilDateTime.nowTimestamp(), dispatcher, userLogin);
270         } catch (GenericServiceException e) {
271             return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFail", locale, module);
272         } catch (GenericEntityException e) {
273             return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFail", locale, module);
274         }
275         return ServiceUtil.returnSuccess();
276     }
277
278     public static Map JavaDoc deactivateContact(DispatchContext dctx, Map JavaDoc context) {
279         GenericDelegator delegator = dctx.getDelegator();
280         LocalDispatcher dispatcher = dctx.getDispatcher();
281         Security security = dctx.getSecurity();
282         GenericValue userLogin = (GenericValue) context.get("userLogin");
283         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
284
285         // what contact we're expiring
286
String JavaDoc contactPartyId = (String JavaDoc) context.get("partyId");
287
288         // check that userLogin has CRMSFA_CONTACT_DEACTIVATE permission for this contact
289
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_DEACTIVATE", userLogin, contactPartyId)) {
290             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
291         }
292
293         // when to expire the contact
294
Timestamp JavaDoc expireDate = (Timestamp JavaDoc) context.get("expireDate");
295         if (expireDate == null) {
296             expireDate = UtilDateTime.nowTimestamp();
297         }
298
299         // in order to deactivate a contact, we expire all party relationships on the expire date
300
try {
301             List JavaDoc partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", contactPartyId, "roleTypeIdFrom", "CONTACT"));
302             PartyHelper.expirePartyRelationships(partyRelationships, expireDate, dispatcher, userLogin);
303         } catch (GenericEntityException e) {
304             return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateContactFail", locale, module);
305         } catch (GenericServiceException e) {
306             return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateContactFail", locale, module);
307         }
308         return ServiceUtil.returnSuccess();
309     }
310
311
312     /**************************************************************************/
313     /** Helper Methods ***/
314     /**************************************************************************/
315
316     /**
317      * Creates an contact relationship of a given type for the given party and removes all previous relationships of that type.
318      * This method helps avoid semantic mistakes and typos from the repeated use of this code pattern.
319      */

320     public static boolean createResponsibleContactRelationshipForParty(String JavaDoc partyId, String JavaDoc contactPartyId,
321             GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher)
322         throws GenericServiceException, GenericEntityException {
323         return PartyHelper.createNewPartyToRelationship(partyId, contactPartyId, "CONTACT", "RESPONSIBLE_FOR",
324                 "CONTACT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher);
325     }
326 }
327
Popular Tags