001: /*
002: * $Id: ThirdPartyEvents.java,v 1.1 2003/08/18 19:44:45 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.ecommerce.misc;
025:
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilDateTime;
036: import org.ofbiz.base.util.UtilHttp;
037: import org.ofbiz.base.util.UtilMisc;
038: import org.ofbiz.base.util.UtilProperties;
039: import org.ofbiz.base.util.UtilValidate;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.entity.GenericEntityException;
042: import org.ofbiz.entity.GenericValue;
043: import org.ofbiz.entity.util.EntityUtil;
044:
045: /**
046: * Third Party Events (Affiliate/Distributor)
047: *
048: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
049: * @version $Revision: 1.1 $
050: * @since 2.0
051: */
052: public class ThirdPartyEvents {
053:
054: public static final String module = ThirdPartyEvents.class
055: .getName();
056:
057: public final static String DISTRIBUTOR_ID = "_DISTRIBUTOR_ID_";
058: public final static String AFFILIATE_ID = "_AFFILIATE_ID_";
059:
060: /** Save the association id(s) specified in the request object into the session.
061: *@param request The HTTPRequest object for the current request
062: *@param response The HTTPResponse object for the current request
063: *@return String specifying the exit status of this event
064: */
065: public static String setAssociationId(HttpServletRequest request,
066: HttpServletResponse response) {
067: Map requestParams = UtilHttp.getParameterMap(request);
068:
069: // check distributor
070: String distriParam[] = { "distributor_id", "distributorid",
071: "distributor" };
072: String distributorId = null;
073:
074: for (int i = 0; i < distriParam.length; i++) {
075: String param = distriParam[i];
076:
077: if (requestParams.containsKey(param)) {
078: distributorId = (String) requestParams.get(param);
079: break;
080: } else if (requestParams.containsKey(param.toUpperCase())) {
081: distributorId = (String) requestParams.get(param
082: .toUpperCase());
083: break;
084: }
085: }
086:
087: // check affiliate
088: String affiliParam[] = { "affiliate_id", "affiliateid",
089: "affiliate", "affil" };
090: String affiliateId = null;
091:
092: for (int i = 0; i < affiliParam.length; i++) {
093: String param = affiliParam[i];
094:
095: if (requestParams.containsKey(param)) {
096: affiliateId = (String) requestParams.get(param);
097: break;
098: } else if (requestParams.containsKey(param.toUpperCase())) {
099: affiliateId = (String) requestParams.get(param
100: .toUpperCase());
101: break;
102: }
103: }
104:
105: if (UtilValidate.isNotEmpty(distributorId)) {
106: request.getSession().setAttribute(DISTRIBUTOR_ID,
107: distributorId);
108: updateAssociatedDistributor(request, response);
109: }
110: if (UtilValidate.isNotEmpty(affiliateId)) {
111: request.getSession()
112: .setAttribute(AFFILIATE_ID, affiliateId);
113: updateAssociatedAffiliate(request, response);
114: }
115:
116: return "success";
117: }
118:
119: /** Update the distributor association for the logged in user, if possible.
120: *@param request The HTTPRequest object for the current request
121: *@param response The HTTPResponse object for the current request
122: *@return String specifying the exit status of this event
123: */
124: public static String updateAssociatedDistributor(
125: HttpServletRequest request, HttpServletResponse response) {
126: GenericDelegator delegator = (GenericDelegator) request
127: .getAttribute("delegator");
128: GenericValue userLogin = (GenericValue) request.getSession()
129: .getAttribute("userLogin");
130: GenericValue party = null;
131:
132: java.net.URL ecommercePropertiesUrl = null;
133:
134: try {
135: ecommercePropertiesUrl = ((ServletContext) request
136: .getAttribute("servletContext"))
137: .getResource("/WEB-INF/ecommerce.properties");
138: } catch (java.net.MalformedURLException e) {
139: Debug.logWarning(e, module);
140: }
141:
142: String store = UtilProperties.getPropertyValue(
143: ecommercePropertiesUrl, "distributor.store.customer");
144:
145: if (store == null || store.toUpperCase().startsWith("N"))
146: return "success";
147: String storeOnClick = UtilProperties.getPropertyValue(
148: ecommercePropertiesUrl, "distributor.store.onclick");
149:
150: if (storeOnClick == null
151: || storeOnClick.toUpperCase().startsWith("N"))
152: return "success";
153:
154: try {
155: party = userLogin == null ? null : userLogin
156: .getRelatedOne("Party");
157: } catch (GenericEntityException gee) {
158: Debug.logWarning(gee, module);
159: }
160:
161: if (party != null) {
162: // if a distributorId is already associated, it will be used instead
163: String currentDistributorId = getId(party, "DISTRIBUTOR");
164:
165: if (UtilValidate.isEmpty(currentDistributorId)) {
166: String distributorId = (String) request.getSession()
167: .getAttribute(DISTRIBUTOR_ID);
168:
169: if (UtilValidate.isNotEmpty(distributorId)) {
170: List toBeStored = new LinkedList();
171:
172: // create distributor Party ?? why?
173: // create distributor PartyRole ?? why?
174: // create PartyRelationship
175: GenericValue partyRelationship = delegator
176: .makeValue("PartyRelationship", UtilMisc
177: .toMap("partyIdFrom", party
178: .getString("partyId"),
179: "partyIdTo", distributorId,
180: "roleTypeIdFrom",
181: "CUSTOMER", "roleTypeIdTo",
182: "DISTRIBUTOR"));
183:
184: partyRelationship.set("fromDate", UtilDateTime
185: .nowTimestamp());
186: partyRelationship.set("partyRelationshipTypeId",
187: "DISTRIBUTION_CHANNEL");
188: toBeStored.add(partyRelationship);
189:
190: toBeStored.add(delegator.makeValue("Party",
191: UtilMisc.toMap("partyId", distributorId)));
192: toBeStored.add(delegator.makeValue("PartyRole",
193: UtilMisc.toMap("partyId", distributorId,
194: "roleTypeId", "DISTRIBUTOR")));
195: try {
196: delegator.storeAll(toBeStored);
197: if (Debug.infoOn())
198: Debug.logInfo("Distributor for user "
199: + party.getString("partyId")
200: + " set to " + distributorId,
201: module);
202: } catch (GenericEntityException gee) {
203: Debug.logWarning(gee, module);
204: }
205: } else {
206: // no distributorId is available
207: Debug
208: .log("No distributor in session or already associated with user "
209: + userLogin.getString("partyId"));
210: return "success";
211: }
212: } else {
213: request.getSession().setAttribute(DISTRIBUTOR_ID,
214: currentDistributorId);
215: }
216:
217: return "success";
218: } else {
219: // not logged in
220: Debug
221: .log("Cannot associate distributor since not logged in yet");
222: return "success";
223: }
224: }
225:
226: /** Update the affiliate association for the logged in user, if possible.
227: *@param request The HTTPRequest object for the current request
228: *@param response The HTTPResponse object for the current request
229: *@return String specifying the exit status of this event
230: */
231: public static String updateAssociatedAffiliate(
232: HttpServletRequest request, HttpServletResponse response) {
233: GenericDelegator delegator = (GenericDelegator) request
234: .getAttribute("delegator");
235: GenericValue userLogin = (GenericValue) request.getSession()
236: .getAttribute("userLogin");
237: GenericValue party = null;
238:
239: java.net.URL ecommercePropertiesUrl = null;
240:
241: try {
242: ecommercePropertiesUrl = ((ServletContext) request
243: .getAttribute("servletContext"))
244: .getResource("/WEB-INF/ecommerce.properties");
245: } catch (java.net.MalformedURLException e) {
246: Debug.logWarning(e, module);
247: }
248:
249: String store = UtilProperties.getPropertyValue(
250: ecommercePropertiesUrl, "affiliate.store.customer");
251:
252: if (store == null || store.toUpperCase().startsWith("N"))
253: return "success";
254: String storeOnClick = UtilProperties.getPropertyValue(
255: ecommercePropertiesUrl, "affiliate.store.onclick");
256:
257: if (storeOnClick == null
258: || storeOnClick.toUpperCase().startsWith("N"))
259: return "success";
260:
261: try {
262: party = userLogin == null ? null : userLogin
263: .getRelatedOne("Party");
264: } catch (GenericEntityException gee) {
265: Debug.logWarning(gee, module);
266: }
267:
268: if (party != null) {
269: // if a distributorId is already associated, it will be used instead
270: String currentAffiliateId = getId(party, "AFFILIATE");
271:
272: if (UtilValidate.isEmpty(currentAffiliateId)) {
273: String affiliateId = (String) request.getSession()
274: .getAttribute(AFFILIATE_ID);
275:
276: if (UtilValidate.isNotEmpty(affiliateId)) {
277: // create PartyRelationship
278: GenericValue partyRelationship = delegator
279: .makeValue("PartyRelationship", UtilMisc
280: .toMap("partyIdFrom", party
281: .getString("partyId"),
282: "partyIdTo", affiliateId,
283: "roleTypeIdFrom",
284: "CUSTOMER", "roleTypeIdTo",
285: "AFFILIATE"));
286:
287: partyRelationship.set("fromDate", UtilDateTime
288: .nowTimestamp());
289: partyRelationship.set("partyRelationshipTypeId",
290: "SALES_AFFILIATE");
291: try {
292: delegator.create(partyRelationship);
293: if (Debug.infoOn())
294: Debug.logInfo("Affiliate for user "
295: + party.getString("partyId")
296: + " set to " + affiliateId, module);
297: } catch (GenericEntityException gee) {
298: Debug.logWarning(gee, module);
299: }
300: } else {
301: // no distributorId is available
302: Debug
303: .log("No affiliate in session or already associated with user "
304: + userLogin.getString("partyId"));
305: return "success";
306: }
307: } else {
308: request.getSession().setAttribute(AFFILIATE_ID,
309: currentAffiliateId);
310: }
311:
312: return "success";
313: } else {
314: // not logged in
315: Debug
316: .log("Cannot associate affiliate since not logged in yet");
317: return "success";
318: }
319: }
320:
321: private static GenericValue getPartyRelationship(
322: GenericValue party, String roleTypeTo) {
323: try {
324: return EntityUtil.getFirst(EntityUtil.filterByDate(party
325: .getRelatedByAnd("FromPartyRelationship", UtilMisc
326: .toMap("roleTypeIdTo", roleTypeTo)), true));
327: } catch (GenericEntityException gee) {
328: Debug.logWarning(gee, module);
329: }
330: return null;
331: }
332:
333: private static String getId(GenericValue party, String roleTypeTo) {
334: GenericValue partyRelationship = getPartyRelationship(party,
335: roleTypeTo);
336:
337: return partyRelationship == null ? null : partyRelationship
338: .getString("partyIdTo");
339: }
340:
341: }
|