001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.marketing.marketing;
019:
020: import java.util.Locale;
021: import java.util.Map;
022: import java.sql.Timestamp;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.base.util.UtilDateTime;
026: import org.ofbiz.base.util.UtilMisc;
027: import org.ofbiz.base.util.UtilProperties;
028: import org.ofbiz.base.util.UtilValidate;
029: import org.ofbiz.entity.GenericDelegator;
030: import org.ofbiz.entity.GenericEntityException;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.service.*;
033:
034: /**
035: * MarketingServices contains static service methods for Marketing Campaigns and Contact Lists.
036: * See the documentation in marketing/servicedef/services.xml and use the service reference in
037: * webtools. Comments in this file are implemntation notes and technical details.
038: */
039: public class MarketingServices {
040:
041: public static final String module = MarketingServices.class
042: .getName();
043: public static final String resourceMarketing = "MarketingUiLabels";
044: public static final String resourceOrder = "OrderUiLabels";
045:
046: public static Map signUpForContactList(DispatchContext dctx,
047: Map context) {
048: LocalDispatcher dispatcher = dctx.getDispatcher();
049: GenericDelegator delegator = dctx.getDelegator();
050: Locale locale = (Locale) context.get("locale");
051:
052: Timestamp fromDate = UtilDateTime.nowTimestamp();
053: String contactListId = (String) context.get("contactListId");
054: String email = (String) context.get("email");
055:
056: if (!UtilValidate.isEmail(email)) {
057: String error = UtilProperties.getMessage(resourceMarketing,
058: "MarketingInvalidEmailInput", locale);
059: return ServiceUtil.returnError(error);
060: }
061:
062: try {
063: // locate the contact list
064: Map input = UtilMisc.toMap("contactListId", contactListId);
065: GenericValue contactList = delegator.findByPrimaryKey(
066: "ContactList", input);
067: if (contactList == null) {
068: String error = UtilProperties.getMessage(
069: resourceMarketing, "ContactListNotFound",
070: input, locale);
071: return ServiceUtil.returnError(error);
072: }
073:
074: // perform actions as the system user
075: GenericValue userLogin = delegator.findByPrimaryKeyCache(
076: "UserLogin", UtilMisc
077: .toMap("userLoginId", "system"));
078:
079: // associate the email with anonymous user TODO: do we need a custom contact mech purpose type, say MARKETING_EMAIL?
080: input = UtilMisc.toMap("userLogin", userLogin,
081: "emailAddress", email, "partyId", "_NA_",
082: "fromDate", fromDate, "contactMechPurposeTypeId",
083: "OTHER_EMAIL");
084: Map serviceResults = dispatcher.runSync(
085: "createPartyEmailAddress", input);
086: if (ServiceUtil.isError(serviceResults)) {
087: throw new GenericServiceException(ServiceUtil
088: .getErrorMessage(serviceResults));
089: }
090: String contactMechId = (String) serviceResults
091: .get("contactMechId");
092:
093: // create a new association at this fromDate to the anonymous party with status accepted
094: input = UtilMisc.toMap("userLogin", userLogin,
095: "contactListId", contactList.get("contactListId"),
096: "partyId", "_NA_", "fromDate", fromDate,
097: "statusId", "CLPT_ACCEPTED",
098: "preferredContactMechId", contactMechId);
099: serviceResults = dispatcher.runSync(
100: "createContactListParty", input);
101: if (ServiceUtil.isError(serviceResults)) {
102: throw new GenericServiceException(ServiceUtil
103: .getErrorMessage(serviceResults));
104: }
105: } catch (GenericEntityException e) {
106: String error = UtilProperties.getMessage(resourceOrder,
107: "checkhelper.problems_reading_database", locale);
108: Debug.log(e, error + e.getMessage(), module);
109: return ServiceUtil.returnError(error);
110: } catch (GenericServiceException e) {
111: String error = UtilProperties.getMessage(resourceMarketing,
112: "MarketingServiceError", locale);
113: Debug.log(e, error + e.getMessage(), module);
114: return ServiceUtil.returnError(error);
115: }
116: return ServiceUtil.returnSuccess();
117: }
118: }
|