KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale 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.GenericEntityException;
54 import org.ofbiz.entity.GenericValue;
55 import org.ofbiz.service.DispatchContext;
56 import org.ofbiz.service.GenericServiceException;
57 import org.ofbiz.service.LocalDispatcher;
58 import org.ofbiz.service.ServiceUtil;
59 import org.ofbiz.service.ModelService;
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 import com.opensourcestrategies.crmsfa.cases.UtilCase;
66
67 /**
68  * Cases services. The service documentation is in services_cases.xml.
69  *
70  * @author <a HREF="mailto:leon@opensourcestrategies.com">Leon Torres</a>
71  * @version $Rev: 312 $
72  */

73
74 public class CasesServices {
75
76     public static final String JavaDoc module = CasesServices.class.getName();
77
78     public static Map JavaDoc createCase(DispatchContext dctx, Map JavaDoc context) {
79         GenericDelegator delegator = dctx.getDelegator();
80         LocalDispatcher dispatcher = dctx.getDispatcher();
81         Security security = dctx.getSecurity();
82         GenericValue userLogin = (GenericValue) context.get("userLogin");
83         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
84
85         // verify that a partyId of some sort was supplied
86
String JavaDoc accountPartyId = (String JavaDoc) context.get("accountPartyId");
87         String JavaDoc contactPartyId = (String JavaDoc) context.get("contactPartyId");
88         if (accountPartyId == null && contactPartyId == null) {
89             return UtilCommon.createAndLogServiceError("Must specify an account or contact for the case.", "CrmErrorCreateCaseFail", locale, module);
90         }
91         try {
92             // create the cust request
93
context.put("statusId", "CRQ_SUBMITTED");
94             ModelService modelService = dctx.getModelService("createCustRequest");
95             Map JavaDoc caseParams = modelService.makeValid(context, "IN");
96
97             // CustRequest.fromPartyId is not used by the CRM/SFA application, which is designed to handle multiple parties
98
// but we'll fill it for consistency with OFBiz and use contactPartyId first then accountPartyId
99
if (contactPartyId != null) {
100                 caseParams.put("fromPartyId", contactPartyId);
101             } else {
102                 caseParams.put("fromPartyId", accountPartyId);
103             }
104             
105             Map JavaDoc serviceResults = dispatcher.runSync("createCustRequest", caseParams);
106             if (ServiceUtil.isError(serviceResults)) {
107                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
108             }
109             String JavaDoc custRequestId = (String JavaDoc) serviceResults.get("custRequestId");
110
111             // create the account role if an account is supplied, but only if user has CRMSFA_CREATE_CASE permission on that account
112
if (accountPartyId != null) {
113                 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CASE", "_CREATE", userLogin, accountPartyId)) {
114                     return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
115                 }
116                 serviceResults = dispatcher.runSync("createCustRequestRole",
117                         UtilMisc.toMap("custRequestId", custRequestId, "partyId", accountPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin));
118                 if (ServiceUtil.isError(serviceResults)) {
119                     return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
120                 }
121             }
122
123             // create the contact role if a contact is supplied, but only if user has CRMSFA_CASE_CREATE permission on that contact
124
if (contactPartyId != null) {
125                 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CASE", "_CREATE", userLogin, contactPartyId)) {
126                     return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
127                 }
128                 serviceResults = dispatcher.runSync("createCustRequestRole",
129                         UtilMisc.toMap("custRequestId", custRequestId, "partyId", contactPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin));
130                 if (ServiceUtil.isError(serviceResults)) {
131                     return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
132                 }
133             }
134
135             // create the note if a note is supplied
136
String JavaDoc note = (String JavaDoc) context.get("note");
137             if (note != null) {
138                 serviceResults = dispatcher.runSync("createCustRequestNote", UtilMisc.toMap("custRequestId", custRequestId, "note", note, "userLogin", userLogin));
139                 if (ServiceUtil.isError(serviceResults)) {
140                     return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
141                 }
142                 String JavaDoc noteId = (String JavaDoc) serviceResults.get("noteId");
143
144                 // create a note association with the account and contact parties
145
if (accountPartyId != null) {
146                     serviceResults = dispatcher.runSync("createPartyNote", UtilMisc.toMap("partyId", accountPartyId, "noteId", noteId, "userLogin", userLogin));
147                     if (ServiceUtil.isError(serviceResults)) {
148                         return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
149                     }
150                 }
151                 if (contactPartyId != null) {
152                     serviceResults = dispatcher.runSync("createPartyNote", UtilMisc.toMap("partyId", contactPartyId, "noteId", noteId, "userLogin", userLogin));
153                     if (ServiceUtil.isError(serviceResults)) {
154                         return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module);
155                     }
156                 }
157             }
158             
159             // return the custRequestId
160
Map JavaDoc result = ServiceUtil.returnSuccess();
161             result.put("custRequestId", custRequestId);
162             return result;
163         } catch (GenericServiceException e) {
164             return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateCaseFail", locale, module);
165         }
166     }
167
168     public static Map JavaDoc updateCase(DispatchContext dctx, Map JavaDoc context) {
169         GenericDelegator delegator = dctx.getDelegator();
170         LocalDispatcher dispatcher = dctx.getDispatcher();
171         Security security = dctx.getSecurity();
172         GenericValue userLogin = (GenericValue) context.get("userLogin");
173         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
174
175         String JavaDoc custRequestId = (String JavaDoc) context.get("custRequestId");
176
177         try {
178             // first make sure userLogin has update permission
179
if (!CrmsfaSecurity.hasCasePermission(security, "_UPDATE", userLogin, custRequestId)) {
180                 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
181             }
182
183             // if the status is being set to close, check if user has permission to
184
String JavaDoc statusId = (String JavaDoc) context.get("statusId");
185             if (statusId.equals("CRQ_CANCELLED") || statusId.equals("CRQ_COMPLETED") || statusId.equals("CRQ_REJECTED")) {
186                 if (!CrmsfaSecurity.hasCasePermission(security, "_CLOSE", userLogin, custRequestId)) {
187                     return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
188                 }
189             }
190
191             // update the cust request
192
ModelService modelService = dctx.getModelService("updateCustRequest");
193             Map JavaDoc serviceResults = dispatcher.runSync("updateCustRequest", modelService.makeValid(context, "IN"));
194             if (ServiceUtil.isError(serviceResults)) {
195                 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateCaseFail", locale, module);
196             }
197
198             // create a note if a note is supplied
199
String JavaDoc note = (String JavaDoc) context.get("note");
200             if (note != null) {
201                 serviceResults = dispatcher.runSync("createCustRequestNote", UtilMisc.toMap("custRequestId", custRequestId, "note", note, "userLogin", userLogin));
202                 if (ServiceUtil.isError(serviceResults)) {
203                     return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateCaseFail", locale, module);
204                 }
205                 String JavaDoc noteId = (String JavaDoc) serviceResults.get("noteId");
206
207                 // associate this note with each case account and contact
208
List JavaDoc parties = UtilCase.getCaseAccountsAndContacts(delegator, custRequestId);
209                 for (Iterator JavaDoc iter = parties.iterator(); iter.hasNext(); ) {
210                     GenericValue party = (GenericValue) iter.next();
211                     serviceResults = dispatcher.runSync("createPartyNote", UtilMisc.toMap("partyId", party.get("partyId"), "noteId", noteId, "userLogin", userLogin));
212                     if (ServiceUtil.isError(serviceResults)) {
213                         return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateCaseFail", locale, module);
214                     }
215                 }
216             }
217             return ServiceUtil.returnSuccess();
218         } catch (GenericServiceException e) {
219             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateCaseFail", locale, module);
220         } catch (GenericEntityException e) {
221             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateCaseFail", locale, module);
222         }
223     }
224
225     public static Map JavaDoc closeCase(DispatchContext dctx, Map JavaDoc context) {
226         GenericDelegator delegator = dctx.getDelegator();
227         LocalDispatcher dispatcher = dctx.getDispatcher();
228         Security security = dctx.getSecurity();
229         GenericValue userLogin = (GenericValue) context.get("userLogin");
230         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
231
232         String JavaDoc custRequestId = (String JavaDoc) context.get("custRequestId");
233
234         try {
235             // check if user has permission to close
236
if (!CrmsfaSecurity.hasCasePermission(security, "_CLOSE", userLogin, custRequestId)) {
237                 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module);
238             }
239             // close by setting status to CRQ_COMPLETED
240
Map JavaDoc serviceResults = dispatcher.runSync("updateCustRequest",
241                     UtilMisc.toMap("custRequestId", custRequestId, "statusId", "CRQ_COMPLETED", "userLogin", userLogin));
242             if (ServiceUtil.isError(serviceResults)) {
243                 return serviceResults;
244             }
245             return ServiceUtil.returnSuccess();
246         } catch (GenericServiceException e) {
247             return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateCaseFail", locale, module);
248         }
249     }
250 }
251
Popular Tags