1 16 17 18 41 package com.opensourcestrategies.crmsfa.cases; 42 43 import java.util.Map ; 44 import java.util.List ; 45 import java.util.Locale ; 46 import java.util.Iterator ; 47 import java.sql.Timestamp ; 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 73 74 public class CasesServices { 75 76 public static final String module = CasesServices.class.getName(); 77 78 public static Map createCase(DispatchContext dctx, Map 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 locale = (Locale ) context.get("locale"); 84 85 String accountPartyId = (String ) context.get("accountPartyId"); 87 String contactPartyId = (String ) 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 context.put("statusId", "CRQ_SUBMITTED"); 94 ModelService modelService = dctx.getModelService("createCustRequest"); 95 Map caseParams = modelService.makeValid(context, "IN"); 96 97 if (contactPartyId != null) { 100 caseParams.put("fromPartyId", contactPartyId); 101 } else { 102 caseParams.put("fromPartyId", accountPartyId); 103 } 104 105 Map serviceResults = dispatcher.runSync("createCustRequest", caseParams); 106 if (ServiceUtil.isError(serviceResults)) { 107 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateCaseFail", locale, module); 108 } 109 String custRequestId = (String ) serviceResults.get("custRequestId"); 110 111 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 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 String note = (String ) 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 noteId = (String ) serviceResults.get("noteId"); 143 144 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 Map 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 updateCase(DispatchContext dctx, Map 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 locale = (Locale ) context.get("locale"); 174 175 String custRequestId = (String ) context.get("custRequestId"); 176 177 try { 178 if (!CrmsfaSecurity.hasCasePermission(security, "_UPDATE", userLogin, custRequestId)) { 180 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 181 } 182 183 String statusId = (String ) 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 ModelService modelService = dctx.getModelService("updateCustRequest"); 193 Map serviceResults = dispatcher.runSync("updateCustRequest", modelService.makeValid(context, "IN")); 194 if (ServiceUtil.isError(serviceResults)) { 195 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateCaseFail", locale, module); 196 } 197 198 String note = (String ) 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 noteId = (String ) serviceResults.get("noteId"); 206 207 List parties = UtilCase.getCaseAccountsAndContacts(delegator, custRequestId); 209 for (Iterator 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 closeCase(DispatchContext dctx, Map 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 locale = (Locale ) context.get("locale"); 231 232 String custRequestId = (String ) context.get("custRequestId"); 233 234 try { 235 if (!CrmsfaSecurity.hasCasePermission(security, "_CLOSE", userLogin, custRequestId)) { 237 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 238 } 239 Map 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 |