KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > accounts > AccountsServices


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.accounts;
42
43 import java.util.Map JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Locale JavaDoc;
46 import java.sql.Timestamp JavaDoc;
47
48 import javolution.util.FastMap;
49
50 import org.ofbiz.base.util.Debug;
51 import org.ofbiz.base.util.UtilMisc;
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.service.DispatchContext;
57 import org.ofbiz.service.GenericServiceException;
58 import org.ofbiz.service.LocalDispatcher;
59 import org.ofbiz.service.ServiceUtil;
60 import org.ofbiz.security.Security;
61
62 import com.opensourcestrategies.crmsfa.party.PartyHelper;
63 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;
64 import com.opensourcestrategies.crmsfa.util.UtilCommon;
65
66 /**
67  * Accounts services. The service documentation is in services_accounts.xml.
68  *
69  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
70  * @version $Rev: 312 $
71  */

72
73 public class AccountsServices {
74
75     public static final String JavaDoc module = AccountsServices.class.getName();
76
77     public static Map JavaDoc createAccount(DispatchContext dctx, Map JavaDoc context) {
78         GenericDelegator delegator = dctx.getDelegator();
79         LocalDispatcher dispatcher = dctx.getDispatcher();
80         Security security = dctx.getSecurity();
81         GenericValue userLogin = (GenericValue) context.get("userLogin");
82         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
83
84         if (!security.hasPermission("CRMSFA_ACCOUNT_CREATE", userLogin)) {
85             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
86         }
87
88         // the net result of creating an account is the generation of an Account partyId
89
String JavaDoc accountPartyId = null;
90         try {
91             // create the Party and PartyGroup, which results in a partyId
92
Map JavaDoc input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"),
93                     "officeSiteName", context.get("officeSiteName"), "description", context.get("description"));
94             Map JavaDoc serviceResults = dispatcher.runSync("createPartyGroup", input);
95             if (ServiceUtil.isError(serviceResults)) {
96                 return serviceResults;
97             }
98             accountPartyId = (String JavaDoc) serviceResults.get("partyId");
99
100             // create a PartyRole for the resulting Account partyId with roleTypeId = ACCOUNT
101
serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", accountPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin));
102             if (ServiceUtil.isError(serviceResults)) {
103                 return serviceResults;
104             }
105
106             // create PartySupplementalData
107
GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId));
108             partyData.setNonPKFields(context);
109             partyData.create();
110
111             // create a unique party relationship between the userLogin and the Account with partyRelationshipTypeId RESPONSIBLE_FOR
112
createResponsibleAccountRelationshipForParty(userLogin.getString("partyId"), accountPartyId, userLogin, delegator, dispatcher);
113
114             // if there's an initialTeamPartyId, assign the team to the account
115
String JavaDoc initialTeamPartyId = (String JavaDoc) context.get("initialTeamPartyId");
116             if (initialTeamPartyId != null) {
117                 serviceResults = dispatcher.runSync("crmsfa.assignTeamToAccount", UtilMisc.toMap("accountPartyId", accountPartyId,
118                             "teamPartyId", initialTeamPartyId, "userLogin", userLogin));
119                 if (ServiceUtil.isError(serviceResults)) {
120                     return serviceResults;
121                 }
122             }
123         } catch (GenericServiceException e) {
124             return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module);
125         } catch (GenericEntityException e) {
126             return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module);
127         }
128
129         // return the partyId of the newly created Account
130
Map JavaDoc results = ServiceUtil.returnSuccess();
131         results.put("partyId", accountPartyId);
132         return results;
133     }
134
135     public static Map JavaDoc updateAccount(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 accountPartyId = (String JavaDoc) context.get("partyId");
143
144         // make sure userLogin has CRMSFA_ACCOUNT_UPDATE permission for this account
145
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) {
146             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
147         }
148         try {
149             // update the Party and PartyGroup
150
Map JavaDoc input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"),
151                     "officeSiteName", context.get("officeSiteName"), "description", context.get("description"));
152             input.put("partyId", accountPartyId);
153             input.put("userLogin", userLogin);
154             Map JavaDoc serviceResults = dispatcher.runSync("updatePartyGroup", input);
155             if (ServiceUtil.isError(serviceResults)) {
156                 return serviceResults;
157             }
158
159             // update PartySupplementalData
160
GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId));
161             if (partyData == null) {
162                 // create a new one
163
partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId));
164                 partyData.create();
165             }
166             partyData.setNonPKFields(context);
167             partyData.store();
168
169         } catch (GenericServiceException e) {
170             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module);
171         } catch (GenericEntityException e) {
172             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module);
173         }
174         return ServiceUtil.returnSuccess();
175     }
176
177
178     public static Map JavaDoc deactivateAccount(DispatchContext dctx, Map JavaDoc context) {
179         GenericDelegator delegator = dctx.getDelegator();
180         LocalDispatcher dispatcher = dctx.getDispatcher();
181         Security security = dctx.getSecurity();
182         GenericValue userLogin = (GenericValue) context.get("userLogin");
183         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
184
185         // what account we're expiring
186
String JavaDoc accountPartyId = (String JavaDoc) context.get("partyId");
187
188         // check that userLogin has CRMSFA_ACCOUNT_DEACTIVATE permission for this account
189
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_DEACTIVATE", userLogin, accountPartyId)) {
190             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
191         }
192
193         // when to expire the account
194
Timestamp JavaDoc expireDate = (Timestamp JavaDoc) context.get("expireDate");
195         if (expireDate == null) {
196             expireDate = UtilDateTime.nowTimestamp();
197         }
198
199         // in order to deactivate an acocunt, we expire all party relationships on the expire date
200
try {
201             List JavaDoc partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountPartyId, "roleTypeIdFrom", "ACCOUNT"));
202             PartyHelper.expirePartyRelationships(partyRelationships, expireDate, dispatcher, userLogin);
203         } catch (GenericEntityException e) {
204             return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module);
205         } catch (GenericServiceException e) {
206             return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module);
207         }
208         return ServiceUtil.returnSuccess();
209     }
210
211     public static Map JavaDoc reassignAccountResponsibleParty(DispatchContext dctx, Map JavaDoc context) {
212         GenericDelegator delegator = dctx.getDelegator();
213         LocalDispatcher dispatcher = dctx.getDispatcher();
214         Security security = dctx.getSecurity();
215         GenericValue userLogin = (GenericValue) context.get("userLogin");
216         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
217
218         String JavaDoc accountPartyId = (String JavaDoc) context.get("accountPartyId");
219         String JavaDoc newPartyId = (String JavaDoc) context.get("newPartyId");
220
221         // ensure reassign permission on this account
222
if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_REASSIGN", userLogin, accountPartyId)) {
223             return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
224         }
225         try {
226             // reassign relationship using a helper method
227
boolean result = createResponsibleAccountRelationshipForParty(newPartyId, accountPartyId, userLogin, delegator, dispatcher);
228             if (result == false) {
229                 return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module);
230             }
231         } catch (GenericServiceException e) {
232             return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module);
233         } catch (GenericEntityException e) {
234             return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module);
235         }
236         return ServiceUtil.returnSuccess();
237     }
238
239     /**************************************************************************/
240     /** Helper Methods ***/
241     /**************************************************************************/
242
243     /**
244      * Creates an account relationship of a given type for the given party and removes all previous relationships of that type.
245      * This method helps avoid semantic mistakes and typos from the repeated use of this code pattern.
246      */

247     public static boolean createResponsibleAccountRelationshipForParty(String JavaDoc partyId, String JavaDoc accountPartyId,
248             GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher)
249         throws GenericServiceException, GenericEntityException {
250         return PartyHelper.createNewPartyToRelationship(partyId, accountPartyId, "ACCOUNT", "RESPONSIBLE_FOR",
251                 "ACCOUNT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher);
252     }
253
254 }
255
Popular Tags