0001: /*
0002: * $Id: ContactMechServices.java,v 1.2 2004/02/26 09:10:47 jonesde Exp $
0003: *
0004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.party.contact;
0025:
0026: import java.sql.Timestamp;
0027: import java.util.HashMap;
0028: import java.util.Iterator;
0029: import java.util.LinkedList;
0030: import java.util.List;
0031: import java.util.Map;
0032: import java.util.Locale;
0033:
0034: import org.ofbiz.base.util.Debug;
0035: import org.ofbiz.base.util.UtilDateTime;
0036: import org.ofbiz.base.util.UtilMisc;
0037: import org.ofbiz.base.util.UtilProperties;
0038: import org.ofbiz.entity.GenericDelegator;
0039: import org.ofbiz.entity.GenericEntityException;
0040: import org.ofbiz.entity.GenericValue;
0041: import org.ofbiz.entity.util.EntityUtil;
0042: import org.ofbiz.security.Security;
0043: import org.ofbiz.service.DispatchContext;
0044: import org.ofbiz.service.ModelService;
0045: import org.ofbiz.service.ServiceUtil;
0046:
0047: /**
0048: * Services for Contact Mechanism maintenance
0049: *
0050: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0051: * @version $Revision: 1.2 $
0052: * @since 2.0
0053: */
0054: public class ContactMechServices {
0055:
0056: public static final String module = ContactMechServices.class
0057: .getName();
0058: public static final String resource = "PartyUiLabels";
0059:
0060: /**
0061: * Creates a ContactMech
0062: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
0063: *@param ctx The DispatchContext that this service is operating in
0064: *@param context Map containing the input parameters
0065: *@return Map with the result of the service, the output parameters
0066: */
0067: public static Map createContactMech(DispatchContext ctx, Map context) {
0068: Map result = new HashMap();
0069: GenericDelegator delegator = ctx.getDelegator();
0070: Security security = ctx.getSecurity();
0071: GenericValue userLogin = (GenericValue) context
0072: .get("userLogin");
0073: Timestamp now = UtilDateTime.nowTimestamp();
0074: List toBeStored = new LinkedList();
0075:
0076: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0077: security, context, result, "PARTYMGR", "_CREATE");
0078: String errMsg = null;
0079: Locale locale = (Locale) context.get("locale");
0080:
0081: if (result.size() > 0)
0082: return result;
0083:
0084: String contactMechTypeId = (String) context
0085: .get("contactMechTypeId");
0086:
0087: Long newCmId = delegator.getNextSeqId("ContactMech");
0088:
0089: if (newCmId == null) {
0090: errMsg = UtilProperties
0091: .getMessage(
0092: resource,
0093: "contactmechservices.could_not_create_contact_info_id_generation_failure",
0094: locale);
0095: return ServiceUtil.returnError(errMsg);
0096: }
0097:
0098: GenericValue tempContactMech = delegator.makeValue(
0099: "ContactMech", UtilMisc.toMap("contactMechId", newCmId
0100: .toString(), "contactMechTypeId",
0101: contactMechTypeId));
0102:
0103: toBeStored.add(tempContactMech);
0104:
0105: if (!partyId.equals("_NA_")) {
0106: toBeStored.add(delegator.makeValue("PartyContactMech",
0107: UtilMisc.toMap("partyId", partyId, "contactMechId",
0108: newCmId.toString(), "fromDate", now,
0109: "roleTypeId", context.get("roleTypeId"),
0110: "allowSolicitation", context
0111: .get("allowSolicitation"),
0112: "extension", context.get("extension"))));
0113: }
0114:
0115: if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
0116: errMsg = UtilProperties
0117: .getMessage(
0118: resource,
0119: "contactmechservices.service_createContactMech_not_be_used_for_POSTAL_ADDRESS",
0120: locale);
0121: return ServiceUtil.returnError(errMsg);
0122: } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
0123: errMsg = UtilProperties
0124: .getMessage(
0125: resource,
0126: "contactmechservices.service_createContactMech_not_be_used_for_TELECOM_NUMBER",
0127: locale);
0128: return ServiceUtil.returnError(errMsg);
0129: } else {
0130: tempContactMech
0131: .set("infoString", context.get("infoString"));
0132: }
0133:
0134: try {
0135: delegator.storeAll(toBeStored);
0136: } catch (GenericEntityException e) {
0137: Debug.logWarning(e.toString(), module);
0138: Map messageMap = UtilMisc.toMap("errMessage", e
0139: .getMessage());
0140: errMsg = UtilProperties
0141: .getMessage(
0142: resource,
0143: "contactmechservices.could_not_create_contact_info_write",
0144: messageMap, locale);
0145: return ServiceUtil.returnError(errMsg);
0146: }
0147:
0148: result.put("contactMechId", newCmId.toString());
0149: result.put(ModelService.RESPONSE_MESSAGE,
0150: ModelService.RESPOND_SUCCESS);
0151: return result;
0152: }
0153:
0154: /**
0155: * Updates a ContactMech
0156: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_UPDATE permission
0157: *@param ctx The DispatchContext that this service is operating in
0158: *@param context Map containing the input parameters
0159: *@return Map with the result of the service, the output parameters
0160: */
0161: public static Map updateContactMech(DispatchContext ctx, Map context) {
0162: Map result = new HashMap();
0163: GenericDelegator delegator = ctx.getDelegator();
0164: Security security = ctx.getSecurity();
0165: GenericValue userLogin = (GenericValue) context
0166: .get("userLogin");
0167: Timestamp now = UtilDateTime.nowTimestamp();
0168: List toBeStored = new LinkedList();
0169: boolean isModified = false;
0170:
0171: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0172: security, context, result, "PARTYMGR", "_UPDATE");
0173: String errMsg = null;
0174: Locale locale = (Locale) context.get("locale");
0175:
0176: if (result.size() > 0)
0177: return result;
0178:
0179: Long newCmId = delegator.getNextSeqId("ContactMech");
0180:
0181: if (newCmId == null) {
0182: errMsg = UtilProperties
0183: .getMessage(
0184: resource,
0185: "contactmechservices.could_not_change_contact_info_id_generation_failure",
0186: locale);
0187: return ServiceUtil.returnError(errMsg);
0188: }
0189:
0190: String contactMechId = (String) context.get("contactMechId");
0191: GenericValue contactMech = null;
0192: GenericValue partyContactMech = null;
0193:
0194: try {
0195: contactMech = delegator.findByPrimaryKey("ContactMech",
0196: UtilMisc.toMap("contactMechId", contactMechId));
0197: } catch (GenericEntityException e) {
0198: Debug.logWarning(e.getMessage(), module);
0199: contactMech = null;
0200: }
0201:
0202: if (!partyId.equals("_NA_")) {
0203: // try to find a PartyContactMech with a valid date range
0204: try {
0205: List partyContactMechs = EntityUtil.filterByDate(
0206: delegator
0207: .findByAnd("PartyContactMech", UtilMisc
0208: .toMap("partyId", partyId,
0209: "contactMechId",
0210: contactMechId),
0211: UtilMisc.toList("fromDate")),
0212: true);
0213: partyContactMech = EntityUtil
0214: .getFirst(partyContactMechs);
0215: if (partyContactMech == null) {
0216: errMsg = UtilProperties
0217: .getMessage(
0218: resource,
0219: "contactmechservices.cannot_update_specified_contact_info_not_corresponds",
0220: locale);
0221: return ServiceUtil.returnError(errMsg);
0222: } else {
0223: toBeStored.add(partyContactMech);
0224: }
0225: } catch (GenericEntityException e) {
0226: Debug.logWarning(e.getMessage(), module);
0227: contactMech = null;
0228: }
0229: }
0230: if (contactMech == null) {
0231: errMsg = UtilProperties
0232: .getMessage(
0233: resource,
0234: "contactmechservices.could_not_find_specified_contact_info_read",
0235: locale);
0236: return ServiceUtil.returnError(errMsg);
0237: }
0238:
0239: String contactMechTypeId = contactMech
0240: .getString("contactMechTypeId");
0241:
0242: // never change a contact mech, just create a new one with the changes
0243: GenericValue newContactMech = new GenericValue(contactMech);
0244: GenericValue newPartyContactMech = new GenericValue(
0245: partyContactMech);
0246: GenericValue relatedEntityToSet = null;
0247:
0248: if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
0249: errMsg = UtilProperties
0250: .getMessage(
0251: resource,
0252: "contactmechservices.service_updateContactMech_not_be_used_for_POSTAL_ADDRESS",
0253: locale);
0254: return ServiceUtil.returnError(errMsg);
0255: } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
0256: errMsg = UtilProperties
0257: .getMessage(
0258: resource,
0259: "contactmechservices.service_updateContactMech_not_be_used_for_TELECOM_NUMBER",
0260: locale);
0261: return ServiceUtil.returnError(errMsg);
0262: } else {
0263: newContactMech.set("infoString", context.get("infoString"));
0264: }
0265:
0266: newPartyContactMech
0267: .set("roleTypeId", context.get("roleTypeId"));
0268: newPartyContactMech.set("allowSolicitation", context
0269: .get("allowSolicitation"));
0270:
0271: if (!newContactMech.equals(contactMech))
0272: isModified = true;
0273: if (!newPartyContactMech.equals(partyContactMech))
0274: isModified = true;
0275:
0276: toBeStored.add(newContactMech);
0277: toBeStored.add(newPartyContactMech);
0278:
0279: if (isModified) {
0280: if (relatedEntityToSet != null)
0281: toBeStored.add(relatedEntityToSet);
0282:
0283: newContactMech.set("contactMechId", newCmId.toString());
0284: newPartyContactMech
0285: .set("contactMechId", newCmId.toString());
0286: newPartyContactMech.set("fromDate", now);
0287: newPartyContactMech.set("thruDate", null);
0288:
0289: try {
0290: Iterator partyContactMechPurposes = UtilMisc
0291: .toIterator(partyContactMech
0292: .getRelated("PartyContactMechPurpose"));
0293:
0294: while (partyContactMechPurposes != null
0295: && partyContactMechPurposes.hasNext()) {
0296: GenericValue tempVal = new GenericValue(
0297: (GenericValue) partyContactMechPurposes
0298: .next());
0299:
0300: tempVal.set("contactMechId", newCmId.toString());
0301: toBeStored.add(tempVal);
0302: }
0303: } catch (GenericEntityException e) {
0304: Debug.logWarning(e.toString(), module);
0305: Map messageMap = UtilMisc.toMap("errMessage", e
0306: .getMessage());
0307: errMsg = UtilProperties
0308: .getMessage(
0309: resource,
0310: "contactmechservices.could_not_change_contact_info_read",
0311: messageMap, locale);
0312: return ServiceUtil.returnError(errMsg);
0313: }
0314:
0315: partyContactMech.set("thruDate", now);
0316: try {
0317: delegator.storeAll(toBeStored);
0318: } catch (GenericEntityException e) {
0319: Debug.logWarning(e.toString(), module);
0320: Map messageMap = UtilMisc.toMap("errMessage", e
0321: .getMessage());
0322: errMsg = UtilProperties
0323: .getMessage(
0324: resource,
0325: "contactmechservices.could_not_change_contact_info_write",
0326: messageMap, locale);
0327: return ServiceUtil.returnError(errMsg);
0328: }
0329: } else {
0330: String sucMsg = UtilProperties.getMessage(resource,
0331: "contactmechservices.no_changes_made_not_updating",
0332: locale);
0333: result.put("newContactMechId", contactMechId);
0334: result.put(ModelService.RESPONSE_MESSAGE,
0335: ModelService.RESPOND_SUCCESS);
0336: result.put(ModelService.SUCCESS_MESSAGE, sucMsg);
0337: return result;
0338: }
0339:
0340: result.put("newContactMechId", newCmId.toString());
0341: result.put(ModelService.RESPONSE_MESSAGE,
0342: ModelService.RESPOND_SUCCESS);
0343: return result;
0344: }
0345:
0346: /**
0347: * Deletes a ContactMech
0348: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_DELETE permission
0349: *@param ctx The DispatchContext that this service is operating in
0350: *@param context Map containing the input parameters
0351: *@return Map with the result of the service, the output parameters
0352: */
0353: public static Map deleteContactMech(DispatchContext ctx, Map context) {
0354: Map result = new HashMap();
0355: GenericDelegator delegator = ctx.getDelegator();
0356: Security security = ctx.getSecurity();
0357: GenericValue userLogin = (GenericValue) context
0358: .get("userLogin");
0359: Timestamp now = UtilDateTime.nowTimestamp();
0360:
0361: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0362: security, context, result, "PARTYMGR", "_DELETE");
0363: String errMsg = null;
0364: Locale locale = (Locale) context.get("locale");
0365:
0366: if (result.size() > 0)
0367: return result;
0368:
0369: // never delete a contact mechanism, just put a to date on the link to the party
0370: String contactMechId = (String) context.get("contactMechId");
0371: GenericValue partyContactMech = null;
0372:
0373: try {
0374: // try to find a PartyContactMech with a valid date range
0375: List partyContactMechs = EntityUtil.filterByDate(
0376: delegator.findByAnd("PartyContactMech", UtilMisc
0377: .toMap("partyId", partyId, "contactMechId",
0378: contactMechId), UtilMisc
0379: .toList("fromDate")), true);
0380:
0381: partyContactMech = EntityUtil.getFirst(partyContactMechs);
0382: } catch (GenericEntityException e) {
0383: Debug.logWarning(e.toString(), module);
0384: Map messageMap = UtilMisc.toMap("errMessage", e
0385: .getMessage());
0386: errMsg = UtilProperties
0387: .getMessage(
0388: resource,
0389: "contactmechservices.could_not_delete_contact_info_read",
0390: messageMap, locale);
0391: return ServiceUtil.returnError(errMsg);
0392: }
0393:
0394: if (partyContactMech == null) {
0395: errMsg = UtilProperties
0396: .getMessage(
0397: resource,
0398: "contactmechservices.could_not_delete_contact_info_no_contact_found",
0399: locale);
0400: return ServiceUtil.returnError(errMsg);
0401: }
0402:
0403: partyContactMech.set("thruDate", UtilDateTime.nowTimestamp());
0404: try {
0405: partyContactMech.store();
0406: } catch (GenericEntityException e) {
0407: Debug.logWarning(e.toString(), module);
0408: errMsg = UtilProperties
0409: .getMessage(
0410: resource,
0411: "contactmechservices.could_not_delete_contact_info_write",
0412: locale);
0413: return ServiceUtil.returnError(errMsg);
0414: }
0415:
0416: result.put(ModelService.RESPONSE_MESSAGE,
0417: ModelService.RESPOND_SUCCESS);
0418: return result;
0419: }
0420:
0421: // ============================================================================
0422: // ============================================================================
0423:
0424: /**
0425: * Creates a PostalAddress
0426: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
0427: *@param ctx The DispatchContext that this service is operating in
0428: *@param context Map containing the input parameters
0429: *@return Map with the result of the service, the output parameters
0430: */
0431: public static Map createPostalAddress(DispatchContext ctx,
0432: Map context) {
0433: Map result = new HashMap();
0434: GenericDelegator delegator = ctx.getDelegator();
0435: Security security = ctx.getSecurity();
0436: GenericValue userLogin = (GenericValue) context
0437: .get("userLogin");
0438: Timestamp now = UtilDateTime.nowTimestamp();
0439: List toBeStored = new LinkedList();
0440:
0441: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0442: security, context, result, "PARTYMGR", "_CREATE");
0443: String errMsg = null;
0444: Locale locale = (Locale) context.get("locale");
0445:
0446: if (result.size() > 0)
0447: return result;
0448:
0449: String contactMechTypeId = "POSTAL_ADDRESS";
0450:
0451: Long newCmId = delegator.getNextSeqId("ContactMech");
0452:
0453: if (newCmId == null) {
0454: errMsg = UtilProperties
0455: .getMessage(
0456: resource,
0457: "contactmechservices.could_not_create_contact_info_id_generation_failure",
0458: locale);
0459: return ServiceUtil.returnError(errMsg);
0460: }
0461:
0462: GenericValue tempContactMech = delegator.makeValue(
0463: "ContactMech", UtilMisc.toMap("contactMechId", newCmId
0464: .toString(), "contactMechTypeId",
0465: contactMechTypeId));
0466:
0467: toBeStored.add(tempContactMech);
0468:
0469: // don't create a PartyContactMech if there is no party; we define no party as sending _NA_ as partyId
0470: if (!partyId.equals("_NA_")) {
0471: toBeStored.add(delegator.makeValue("PartyContactMech",
0472: UtilMisc.toMap("partyId", partyId, "contactMechId",
0473: newCmId.toString(), "fromDate", now,
0474: "roleTypeId", context.get("roleTypeId"),
0475: "allowSolicitation", context
0476: .get("allowSolicitation"),
0477: "extension", context.get("extension"))));
0478: }
0479:
0480: GenericValue newAddr = delegator.makeValue("PostalAddress",
0481: null);
0482:
0483: newAddr.set("contactMechId", newCmId.toString());
0484: newAddr.set("toName", context.get("toName"));
0485: newAddr.set("attnName", context.get("attnName"));
0486: newAddr.set("address1", context.get("address1"));
0487: newAddr.set("address2", context.get("address2"));
0488: newAddr.set("directions", context.get("directions"));
0489: newAddr.set("city", context.get("city"));
0490: newAddr.set("postalCode", context.get("postalCode"));
0491: newAddr.set("stateProvinceGeoId", context
0492: .get("stateProvinceGeoId"));
0493: newAddr.set("countryGeoId", context.get("countryGeoId"));
0494: newAddr.set("postalCodeGeoId", context.get("postalCodeGeoId"));
0495: toBeStored.add(newAddr);
0496:
0497: try {
0498: delegator.storeAll(toBeStored);
0499: } catch (GenericEntityException e) {
0500: Debug.logWarning(e.toString(), module);
0501: Map messageMap = UtilMisc.toMap("errMessage", e
0502: .getMessage());
0503: errMsg = UtilProperties
0504: .getMessage(
0505: resource,
0506: "contactmechservices.could_not_create_contact_info_write",
0507: messageMap, locale);
0508: return ServiceUtil.returnError(errMsg);
0509: }
0510:
0511: result.put("contactMechId", newCmId.toString());
0512: result.put(ModelService.RESPONSE_MESSAGE,
0513: ModelService.RESPOND_SUCCESS);
0514: return result;
0515: }
0516:
0517: /**
0518: * Updates a PostalAddress
0519: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_UPDATE permission
0520: *@param ctx The DispatchContext that this service is operating in
0521: *@param context Map containing the input parameters
0522: *@return Map with the result of the service, the output parameters
0523: */
0524: public static Map updatePostalAddress(DispatchContext ctx,
0525: Map context) {
0526: Map result = new HashMap();
0527: GenericDelegator delegator = ctx.getDelegator();
0528: Security security = ctx.getSecurity();
0529: GenericValue userLogin = (GenericValue) context
0530: .get("userLogin");
0531: Timestamp now = UtilDateTime.nowTimestamp();
0532: List toBeStored = new LinkedList();
0533: boolean isModified = false;
0534:
0535: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0536: security, context, result, "PARTYMGR", "_UPDATE");
0537: String errMsg = null;
0538: Locale locale = (Locale) context.get("locale");
0539:
0540: if (result.size() > 0)
0541: return result;
0542:
0543: Long newCmId = delegator.getNextSeqId("ContactMech");
0544:
0545: if (newCmId == null) {
0546: errMsg = UtilProperties
0547: .getMessage(
0548: resource,
0549: "contactmechservices.could_not_change_contact_info_id_generation_failure",
0550: locale);
0551: return ServiceUtil.returnError(errMsg);
0552: }
0553:
0554: String contactMechId = (String) context.get("contactMechId");
0555: GenericValue contactMech = null;
0556: GenericValue partyContactMech = null;
0557:
0558: try {
0559: contactMech = delegator.findByPrimaryKey("ContactMech",
0560: UtilMisc.toMap("contactMechId", contactMechId));
0561: } catch (GenericEntityException e) {
0562: Debug.logWarning(e.getMessage(), module);
0563: contactMech = null;
0564: }
0565:
0566: if (!partyId.equals("_NA_")) {
0567: // try to find a PartyContactMech with a valid date range
0568: try {
0569: List partyContactMechs = EntityUtil.filterByDate(
0570: delegator
0571: .findByAnd("PartyContactMech", UtilMisc
0572: .toMap("partyId", partyId,
0573: "contactMechId",
0574: contactMechId),
0575: UtilMisc.toList("fromDate")),
0576: true);
0577: partyContactMech = EntityUtil
0578: .getFirst(partyContactMechs);
0579: if (partyContactMech == null) {
0580: errMsg = UtilProperties
0581: .getMessage(
0582: resource,
0583: "contactmechservices.cannot_update_specified_contact_info_not_corresponds",
0584: locale);
0585: return ServiceUtil.returnError(errMsg);
0586: } else {
0587: toBeStored.add(partyContactMech);
0588: }
0589: } catch (GenericEntityException e) {
0590: Debug.logWarning(e.getMessage(), module);
0591: contactMech = null;
0592: }
0593: }
0594: if (contactMech == null) {
0595: errMsg = UtilProperties
0596: .getMessage(
0597: resource,
0598: "contactmechservices.could_not_find_specified_contact_info_read",
0599: locale);
0600: return ServiceUtil.returnError(errMsg);
0601: }
0602:
0603: // never change a contact mech, just create a new one with the changes
0604: GenericValue newContactMech = new GenericValue(contactMech);
0605: GenericValue newPartyContactMech = null;
0606: if (partyContactMech != null)
0607: newPartyContactMech = new GenericValue(partyContactMech);
0608: GenericValue relatedEntityToSet = null;
0609:
0610: if ("POSTAL_ADDRESS".equals(contactMech
0611: .getString("contactMechTypeId"))) {
0612: GenericValue addr = null;
0613:
0614: try {
0615: addr = delegator.findByPrimaryKey("PostalAddress",
0616: UtilMisc.toMap("contactMechId", contactMechId));
0617: } catch (GenericEntityException e) {
0618: Debug.logWarning(e.toString(), module);
0619: addr = null;
0620: }
0621: relatedEntityToSet = new GenericValue(addr);
0622: relatedEntityToSet.set("toName", context.get("toName"));
0623: relatedEntityToSet.set("attnName", context.get("attnName"));
0624: relatedEntityToSet.set("address1", context.get("address1"));
0625: relatedEntityToSet.set("address2", context.get("address2"));
0626: relatedEntityToSet.set("directions", context
0627: .get("directions"));
0628: relatedEntityToSet.set("city", context.get("city"));
0629: relatedEntityToSet.set("postalCode", context
0630: .get("postalCode"));
0631: relatedEntityToSet.set("stateProvinceGeoId", context
0632: .get("stateProvinceGeoId"));
0633: relatedEntityToSet.set("countryGeoId", context
0634: .get("countryGeoId"));
0635: relatedEntityToSet.set("postalCodeGeoId", context
0636: .get("postalCodeGeoId"));
0637: if (addr == null || !relatedEntityToSet.equals(addr)) {
0638: isModified = true;
0639: }
0640: relatedEntityToSet.set("contactMechId", newCmId.toString());
0641: } else {
0642: Map messageMap = UtilMisc.toMap("contactMechTypeId",
0643: contactMech.getString("contactMechTypeId"));
0644: errMsg = UtilProperties
0645: .getMessage(
0646: resource,
0647: "contactmechservices.could_not_update_contact_as_POSTAL_ADDRESS_specified",
0648: messageMap, locale);
0649: return ServiceUtil.returnError(errMsg);
0650: }
0651:
0652: if (newPartyContactMech != null) {
0653: newPartyContactMech.set("roleTypeId", context
0654: .get("roleTypeId"));
0655: newPartyContactMech.set("allowSolicitation", context
0656: .get("allowSolicitation"));
0657: }
0658:
0659: if (!newContactMech.equals(contactMech))
0660: isModified = true;
0661: if (newPartyContactMech != null
0662: && !newPartyContactMech.equals(partyContactMech))
0663: isModified = true;
0664:
0665: toBeStored.add(newContactMech);
0666: if (newPartyContactMech != null)
0667: toBeStored.add(newPartyContactMech);
0668:
0669: if (isModified) {
0670: if (relatedEntityToSet != null)
0671: toBeStored.add(relatedEntityToSet);
0672:
0673: newContactMech.set("contactMechId", newCmId.toString());
0674: if (newPartyContactMech != null) {
0675: newPartyContactMech.set("contactMechId", newCmId
0676: .toString());
0677: newPartyContactMech.set("fromDate", now);
0678: newPartyContactMech.set("thruDate", null);
0679:
0680: try {
0681: Iterator partyContactMechPurposes = UtilMisc
0682: .toIterator(partyContactMech
0683: .getRelated("PartyContactMechPurpose"));
0684:
0685: while (partyContactMechPurposes != null
0686: && partyContactMechPurposes.hasNext()) {
0687: GenericValue tempVal = new GenericValue(
0688: (GenericValue) partyContactMechPurposes
0689: .next());
0690:
0691: tempVal
0692: .set("contactMechId", newCmId
0693: .toString());
0694: toBeStored.add(tempVal);
0695: }
0696: } catch (GenericEntityException e) {
0697: Debug.logWarning(e.toString(), module);
0698: Map messageMap = UtilMisc.toMap("errMessage", e
0699: .getMessage());
0700: errMsg = UtilProperties
0701: .getMessage(
0702: resource,
0703: "contactmechservices.could_not_change_contact_info_read",
0704: messageMap, locale);
0705: return ServiceUtil.returnError(errMsg);
0706: }
0707:
0708: partyContactMech.set("thruDate", now);
0709: }
0710:
0711: try {
0712: delegator.storeAll(toBeStored);
0713: } catch (GenericEntityException e) {
0714: Debug.logWarning(e.toString(), module);
0715: Map messageMap = UtilMisc.toMap("errMessage", e
0716: .getMessage());
0717: errMsg = UtilProperties
0718: .getMessage(
0719: resource,
0720: "contactmechservices.could_not_change_contact_info_write",
0721: messageMap, locale);
0722: return ServiceUtil.returnError(errMsg);
0723: }
0724: } else {
0725: String sucMsg = UtilProperties.getMessage(resource,
0726: "contactmechservices.no_changes_made_not_updating",
0727: locale);
0728: result.put("newContactMechId", contactMechId);
0729: result.put(ModelService.RESPONSE_MESSAGE,
0730: ModelService.RESPOND_SUCCESS);
0731: result.put(ModelService.SUCCESS_MESSAGE, sucMsg);
0732: return result;
0733: }
0734:
0735: result.put("newContactMechId", newCmId.toString());
0736: result.put(ModelService.RESPONSE_MESSAGE,
0737: ModelService.RESPOND_SUCCESS);
0738: return result;
0739: }
0740:
0741: // ============================================================================
0742: // ============================================================================
0743:
0744: /**
0745: * Creates a TelecomNumber
0746: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
0747: *@param ctx The DispatchContext that this service is operating in
0748: *@param context Map containing the input parameters
0749: *@return Map with the result of the service, the output parameters
0750: */
0751: public static Map createTelecomNumber(DispatchContext ctx,
0752: Map context) {
0753: Map result = new HashMap();
0754: GenericDelegator delegator = ctx.getDelegator();
0755: Security security = ctx.getSecurity();
0756: GenericValue userLogin = (GenericValue) context
0757: .get("userLogin");
0758: Timestamp now = UtilDateTime.nowTimestamp();
0759: List toBeStored = new LinkedList();
0760:
0761: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0762: security, context, result, "PARTYMGR", "_CREATE");
0763: String errMsg = null;
0764: Locale locale = (Locale) context.get("locale");
0765:
0766: if (result.size() > 0)
0767: return result;
0768:
0769: String contactMechTypeId = "TELECOM_NUMBER";
0770:
0771: Long newCmId = delegator.getNextSeqId("ContactMech");
0772:
0773: if (newCmId == null) {
0774: errMsg = UtilProperties
0775: .getMessage(
0776: resource,
0777: "contactmechservices.could_not_create_contact_info_id_generation_failure",
0778: locale);
0779: return ServiceUtil.returnError(errMsg);
0780: }
0781:
0782: GenericValue tempContactMech = delegator.makeValue(
0783: "ContactMech", UtilMisc.toMap("contactMechId", newCmId
0784: .toString(), "contactMechTypeId",
0785: contactMechTypeId));
0786:
0787: toBeStored.add(tempContactMech);
0788:
0789: toBeStored.add(delegator.makeValue("PartyContactMech", UtilMisc
0790: .toMap("partyId", partyId, "contactMechId", newCmId
0791: .toString(), "fromDate", now, "roleTypeId",
0792: context.get("roleTypeId"), "allowSolicitation",
0793: context.get("allowSolicitation"), "extension",
0794: context.get("extension"))));
0795:
0796: toBeStored
0797: .add(delegator.makeValue("TelecomNumber", UtilMisc
0798: .toMap("contactMechId", newCmId.toString(),
0799: "countryCode", context
0800: .get("countryCode"),
0801: "areaCode", context.get("areaCode"),
0802: "contactNumber", context
0803: .get("contactNumber"))));
0804:
0805: try {
0806: delegator.storeAll(toBeStored);
0807: } catch (GenericEntityException e) {
0808: Debug.logWarning(e.toString(), module);
0809: Map messageMap = UtilMisc.toMap("errMessage", e
0810: .getMessage());
0811: errMsg = UtilProperties
0812: .getMessage(
0813: resource,
0814: "contactmechservices.could_not_create_contact_info_write",
0815: messageMap, locale);
0816: return ServiceUtil.returnError(errMsg);
0817: }
0818:
0819: result.put("contactMechId", newCmId.toString());
0820: result.put(ModelService.RESPONSE_MESSAGE,
0821: ModelService.RESPOND_SUCCESS);
0822: return result;
0823: }
0824:
0825: /**
0826: * Updates a TelecomNumber
0827: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_UPDATE permission
0828: *@param ctx The DispatchContext that this service is operating in
0829: *@param context Map containing the input parameters
0830: *@return Map with the result of the service, the output parameters
0831: */
0832: public static Map updateTelecomNumber(DispatchContext ctx,
0833: Map context) {
0834: Map result = new HashMap();
0835: GenericDelegator delegator = ctx.getDelegator();
0836: Security security = ctx.getSecurity();
0837: GenericValue userLogin = (GenericValue) context
0838: .get("userLogin");
0839: Timestamp now = UtilDateTime.nowTimestamp();
0840: List toBeStored = new LinkedList();
0841: boolean isModified = false;
0842:
0843: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
0844: security, context, result, "PARTYMGR", "_UPDATE");
0845: String errMsg = null;
0846: Locale locale = (Locale) context.get("locale");
0847:
0848: if (result.size() > 0)
0849: return result;
0850:
0851: Long newCmId = delegator.getNextSeqId("ContactMech");
0852:
0853: if (newCmId == null) {
0854: errMsg = UtilProperties
0855: .getMessage(
0856: resource,
0857: "contactmechservices.could_not_change_contact_info_id_generation_failure",
0858: locale);
0859: return ServiceUtil.returnError(errMsg);
0860: }
0861:
0862: String contactMechId = (String) context.get("contactMechId");
0863: GenericValue contactMech = null;
0864: GenericValue partyContactMech = null;
0865:
0866: try {
0867: contactMech = delegator.findByPrimaryKey("ContactMech",
0868: UtilMisc.toMap("contactMechId", contactMechId));
0869: // try to find a PartyContactMech with a valid date range
0870: List partyContactMechs = EntityUtil.filterByDate(
0871: delegator.findByAnd("PartyContactMech", UtilMisc
0872: .toMap("partyId", partyId, "contactMechId",
0873: contactMechId), UtilMisc
0874: .toList("fromDate")), true);
0875:
0876: partyContactMech = EntityUtil.getFirst(partyContactMechs);
0877: } catch (GenericEntityException e) {
0878: Debug.logWarning(e.getMessage(), module);
0879: contactMech = null;
0880: partyContactMech = null;
0881: }
0882: if (contactMech == null) {
0883: errMsg = UtilProperties
0884: .getMessage(
0885: resource,
0886: "contactmechservices.could_not_find_specified_contact_info_read",
0887: locale);
0888: return ServiceUtil.returnError(errMsg);
0889: }
0890: if (partyContactMech == null) {
0891: errMsg = UtilProperties
0892: .getMessage(
0893: resource,
0894: "contactmechservices.cannot_update_specified_contact_info_not_corresponds",
0895: locale);
0896: return ServiceUtil.returnError(errMsg);
0897: }
0898: toBeStored.add(partyContactMech);
0899:
0900: // never change a contact mech, just create a new one with the changes
0901: GenericValue newContactMech = new GenericValue(contactMech);
0902: GenericValue newPartyContactMech = new GenericValue(
0903: partyContactMech);
0904: GenericValue relatedEntityToSet = null;
0905:
0906: if ("TELECOM_NUMBER".equals(contactMech
0907: .getString("contactMechTypeId"))) {
0908: GenericValue telNum = null;
0909:
0910: try {
0911: telNum = delegator.findByPrimaryKey("TelecomNumber",
0912: UtilMisc.toMap("contactMechId", contactMechId));
0913: } catch (GenericEntityException e) {
0914: Debug.logWarning(e.toString(), module);
0915: telNum = null;
0916: }
0917: relatedEntityToSet = new GenericValue(telNum);
0918: relatedEntityToSet.set("countryCode", context
0919: .get("countryCode"));
0920: relatedEntityToSet.set("areaCode", context.get("areaCode"));
0921: relatedEntityToSet.set("contactNumber", context
0922: .get("contactNumber"));
0923:
0924: if (telNum == null || !relatedEntityToSet.equals(telNum)) {
0925: isModified = true;
0926: }
0927: relatedEntityToSet.set("contactMechId", newCmId.toString());
0928: newPartyContactMech.set("extension", context
0929: .get("extension"));
0930: } else {
0931: Map messageMap = UtilMisc.toMap("contactMechTypeId",
0932: contactMech.getString("contactMechTypeId"));
0933: errMsg = UtilProperties
0934: .getMessage(
0935: resource,
0936: "contactmechservices.could_not_update_contact_as_TELECOM_NUMBER_specified",
0937: messageMap, locale);
0938: return ServiceUtil.returnError(errMsg);
0939: }
0940:
0941: newPartyContactMech
0942: .set("roleTypeId", context.get("roleTypeId"));
0943: newPartyContactMech.set("allowSolicitation", context
0944: .get("allowSolicitation"));
0945:
0946: if (!newContactMech.equals(contactMech))
0947: isModified = true;
0948: if (!newPartyContactMech.equals(partyContactMech))
0949: isModified = true;
0950:
0951: toBeStored.add(newContactMech);
0952: toBeStored.add(newPartyContactMech);
0953:
0954: if (isModified) {
0955: if (relatedEntityToSet != null)
0956: toBeStored.add(relatedEntityToSet);
0957:
0958: newContactMech.set("contactMechId", newCmId.toString());
0959: newPartyContactMech
0960: .set("contactMechId", newCmId.toString());
0961: newPartyContactMech.set("fromDate", now);
0962: newPartyContactMech.set("thruDate", null);
0963:
0964: try {
0965: Iterator partyContactMechPurposes = UtilMisc
0966: .toIterator(partyContactMech
0967: .getRelated("PartyContactMechPurpose"));
0968:
0969: while (partyContactMechPurposes != null
0970: && partyContactMechPurposes.hasNext()) {
0971: GenericValue tempVal = new GenericValue(
0972: (GenericValue) partyContactMechPurposes
0973: .next());
0974:
0975: tempVal.set("contactMechId", newCmId.toString());
0976: toBeStored.add(tempVal);
0977: }
0978: } catch (GenericEntityException e) {
0979: Debug.logWarning(e.toString(), module);
0980: Map messageMap = UtilMisc.toMap("errMessage", e
0981: .getMessage());
0982: errMsg = UtilProperties
0983: .getMessage(
0984: resource,
0985: "contactmechservices.could_not_change_contact_info_read",
0986: messageMap, locale);
0987: return ServiceUtil.returnError(errMsg);
0988: }
0989:
0990: partyContactMech.set("thruDate", now);
0991: try {
0992: delegator.storeAll(toBeStored);
0993: } catch (GenericEntityException e) {
0994: Debug.logWarning(e.toString(), module);
0995: Map messageMap = UtilMisc.toMap("errMessage", e
0996: .getMessage());
0997: errMsg = UtilProperties
0998: .getMessage(
0999: resource,
1000: "contactmechservices.could_not_change_contact_info_write",
1001: messageMap, locale);
1002: return ServiceUtil.returnError(errMsg);
1003: }
1004: } else {
1005: String sucMsg = UtilProperties.getMessage(resource,
1006: "contactmechservices.no_changes_made_not_updating",
1007: locale);
1008: result.put("newContactMechId", contactMechId);
1009: result.put(ModelService.RESPONSE_MESSAGE,
1010: ModelService.RESPOND_SUCCESS);
1011: result.put(ModelService.SUCCESS_MESSAGE, sucMsg);
1012: return result;
1013: }
1014:
1015: result.put("newContactMechId", newCmId.toString());
1016: result.put(ModelService.RESPONSE_MESSAGE,
1017: ModelService.RESPOND_SUCCESS);
1018: return result;
1019: }
1020:
1021: // ============================================================================
1022: // ============================================================================
1023:
1024: /**
1025: * Creates a EmailAddress
1026: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
1027: *@param ctx The DispatchContext that this service is operating in
1028: *@param context Map containing the input parameters
1029: *@return Map with the result of the service, the output parameters
1030: */
1031: public static Map createEmailAddress(DispatchContext ctx,
1032: Map context) {
1033: Map newContext = new HashMap(context);
1034:
1035: newContext.put("infoString", newContext.get("emailAddress"));
1036: newContext.remove("emailAddress");
1037: newContext.put("contactMechTypeId", "EMAIL_ADDRESS");
1038:
1039: return createContactMech(ctx, newContext);
1040: }
1041:
1042: /**
1043: * Updates a EmailAddress
1044: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_UPDATE permission
1045: *@param ctx The DispatchContext that this service is operating in
1046: *@param context Map containing the input parameters
1047: *@return Map with the result of the service, the output parameters
1048: */
1049: public static Map updateEmailAddress(DispatchContext ctx,
1050: Map context) {
1051: Map newContext = new HashMap(context);
1052:
1053: newContext.put("infoString", newContext.get("emailAddress"));
1054: newContext.remove("emailAddress");
1055: return updateContactMech(ctx, newContext);
1056: }
1057:
1058: // ============================================================================
1059: // ============================================================================
1060:
1061: /**
1062: * Creates a PartyContactMechPurpose
1063: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
1064: *@param ctx The DispatchContext that this service is operating in
1065: *@param context Map containing the input parameters
1066: *@return Map with the result of the service, the output parameters
1067: */
1068: public static Map createPartyContactMechPurpose(
1069: DispatchContext ctx, Map context) {
1070: Map result = new HashMap();
1071: GenericDelegator delegator = ctx.getDelegator();
1072: Security security = ctx.getSecurity();
1073: GenericValue userLogin = (GenericValue) context
1074: .get("userLogin");
1075:
1076: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
1077: security, context, result, "PARTYMGR", "_CREATE");
1078: String errMsg = null;
1079: Locale locale = (Locale) context.get("locale");
1080:
1081: if (result.size() > 0)
1082: return result;
1083:
1084: // required parameters
1085: String contactMechId = (String) context.get("contactMechId");
1086: String contactMechPurposeTypeId = (String) context
1087: .get("contactMechPurposeTypeId");
1088:
1089: GenericValue tempVal = null;
1090:
1091: try {
1092: List allPCMPs = EntityUtil.filterByDate(delegator
1093: .findByAnd("PartyContactMechPurpose", UtilMisc
1094: .toMap("partyId", partyId, "contactMechId",
1095: contactMechId,
1096: "contactMechPurposeTypeId",
1097: contactMechPurposeTypeId), null),
1098: true);
1099:
1100: tempVal = EntityUtil.getFirst(allPCMPs);
1101: } catch (GenericEntityException e) {
1102: Debug.logWarning(e.getMessage(), module);
1103: tempVal = null;
1104: }
1105:
1106: Timestamp fromDate = UtilDateTime.nowTimestamp();
1107:
1108: if (tempVal != null) {
1109: // exists already with valid date, show warning
1110: errMsg = UtilProperties
1111: .getMessage(
1112: resource,
1113: "contactmechservices.could_not_create_new_purpose_already_exists",
1114: locale);
1115: return ServiceUtil.returnError(errMsg);
1116: } else {
1117: // no entry with a valid date range exists, create new with open thruDate
1118: GenericValue newPartyContactMechPurpose = delegator
1119: .makeValue("PartyContactMechPurpose", UtilMisc
1120: .toMap("partyId", partyId, "contactMechId",
1121: contactMechId,
1122: "contactMechPurposeTypeId",
1123: contactMechPurposeTypeId,
1124: "fromDate", fromDate));
1125:
1126: try {
1127: delegator.create(newPartyContactMechPurpose);
1128: } catch (GenericEntityException e) {
1129: Debug.logWarning(e.getMessage(), module);
1130: Map messageMap = UtilMisc.toMap("errMessage", e
1131: .getMessage());
1132: errMsg = UtilProperties
1133: .getMessage(
1134: resource,
1135: "contactmechservices.could_not_add_purpose_write",
1136: messageMap, locale);
1137: return ServiceUtil.returnError(errMsg);
1138: }
1139: }
1140:
1141: result.put("fromDate", fromDate);
1142: result.put(ModelService.RESPONSE_MESSAGE,
1143: ModelService.RESPOND_SUCCESS);
1144: return result;
1145: }
1146:
1147: /**
1148: * Deletes the PartyContactMechPurpose corresponding to the parameters in the context
1149: * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_DELETE permission
1150: *@param ctx The DispatchContext that this service is operating in
1151: *@param context Map containing the input parameters
1152: *@return Map with the result of the service, the output parameters
1153: */
1154: public static Map deletePartyContactMechPurpose(
1155: DispatchContext ctx, Map context) {
1156: Map result = new HashMap();
1157: GenericDelegator delegator = ctx.getDelegator();
1158: Security security = ctx.getSecurity();
1159: GenericValue userLogin = (GenericValue) context
1160: .get("userLogin");
1161:
1162: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
1163: security, context, result, "PARTYMGR", "_DELETE");
1164: String errMsg = null;
1165: Locale locale = (Locale) context.get("locale");
1166:
1167: if (result.size() > 0)
1168: return result;
1169:
1170: // required parameters
1171: String contactMechId = (String) context.get("contactMechId");
1172: String contactMechPurposeTypeId = (String) context
1173: .get("contactMechPurposeTypeId");
1174: Timestamp fromDate = (Timestamp) context.get("fromDate");
1175:
1176: GenericValue pcmp = null;
1177:
1178: try {
1179: pcmp = delegator.findByPrimaryKey(
1180: "PartyContactMechPurpose", UtilMisc.toMap(
1181: "partyId", partyId, "contactMechId",
1182: contactMechId, "contactMechPurposeTypeId",
1183: contactMechPurposeTypeId, "fromDate",
1184: fromDate));
1185: if (pcmp == null) {
1186: errMsg = UtilProperties
1187: .getMessage(
1188: resource,
1189: "contactmechservices.could_not_delete_purpose_from_contact_mechanism_not_found",
1190: locale);
1191: return ServiceUtil.returnError(errMsg);
1192: }
1193: } catch (GenericEntityException e) {
1194: Debug.logWarning(e.getMessage(), module);
1195: Map messageMap = UtilMisc.toMap("errMessage", e
1196: .getMessage());
1197: errMsg = UtilProperties
1198: .getMessage(
1199: resource,
1200: "contactmechservices.could_not_delete_purpose_from_contact_mechanism_read",
1201: messageMap, locale);
1202: return ServiceUtil.returnError(errMsg);
1203: }
1204:
1205: pcmp.set("thruDate", UtilDateTime.nowTimestamp());
1206: try {
1207: pcmp.store();
1208: } catch (GenericEntityException e) {
1209: Debug.logWarning(e.getMessage(), module);
1210: Map messageMap = UtilMisc.toMap("errMessage", e
1211: .getMessage());
1212: errMsg = UtilProperties
1213: .getMessage(
1214: resource,
1215: "contactmechservices.could_not_delete_purpose_from_contact_mechanism_write",
1216: messageMap, locale);
1217: return ServiceUtil.returnError(errMsg);
1218: }
1219:
1220: result.put(ModelService.RESPONSE_MESSAGE,
1221: ModelService.RESPOND_SUCCESS);
1222: return result;
1223: }
1224: }
|