KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > ecommerce > misc > ThirdPartyEvents


1 /*
2  * $Id: ThirdPartyEvents.java 5460 2005-08-05 18:10:06Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.ecommerce.misc;
25
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.UtilDateTime;
36 import org.ofbiz.base.util.UtilHttp;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilProperties;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.util.EntityUtil;
44
45
46 /**
47  * Third Party Events (Affiliate/Distributor)
48  *
49  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
50  * @version $Rev: 5460 $
51  * @since 2.0
52  */

53 public class ThirdPartyEvents {
54     
55     public static final String JavaDoc module = ThirdPartyEvents.class.getName();
56
57     public final static String JavaDoc DISTRIBUTOR_ID = "_DISTRIBUTOR_ID_";
58     public final static String JavaDoc AFFILIATE_ID = "_AFFILIATE_ID_";
59
60     /** Save the association id(s) specified in the request object into the session.
61      *@param request The HTTPRequest object for the current request
62      *@param response The HTTPResponse object for the current request
63      *@return String specifying the exit status of this event
64      */

65     public static String JavaDoc setAssociationId(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
66         Map JavaDoc requestParams = UtilHttp.getParameterMap(request);
67
68         // check distributor
69
String JavaDoc distriParam[] = { "distributor_id", "distributorid", "distributor" };
70         String JavaDoc distributorId = null;
71
72         for (int i = 0; i < distriParam.length; i++) {
73             String JavaDoc param = distriParam[i];
74
75             if (requestParams.containsKey(param)) {
76                 distributorId = (String JavaDoc) requestParams.get(param);
77                 break;
78             } else if (requestParams.containsKey(param.toUpperCase())) {
79                 distributorId = (String JavaDoc) requestParams.get(param.toUpperCase());
80                 break;
81             }
82         }
83
84         // check affiliate
85
String JavaDoc affiliParam[] = { "affiliate_id", "affiliateid", "affiliate", "affil" };
86         String JavaDoc affiliateId = null;
87
88         for (int i = 0; i < affiliParam.length; i++) {
89             String JavaDoc param = affiliParam[i];
90
91             if (requestParams.containsKey(param)) {
92                 affiliateId = (String JavaDoc) requestParams.get(param);
93                 break;
94             } else if (requestParams.containsKey(param.toUpperCase())) {
95                 affiliateId = (String JavaDoc) requestParams.get(param.toUpperCase());
96                 break;
97             }
98         }
99
100         if (UtilValidate.isNotEmpty(distributorId)) {
101             request.getSession().setAttribute(DISTRIBUTOR_ID, distributorId);
102             updateAssociatedDistributor(request, response);
103         }
104         if (UtilValidate.isNotEmpty(affiliateId)) {
105             request.getSession().setAttribute(AFFILIATE_ID, affiliateId);
106             updateAssociatedAffiliate(request, response);
107         }
108
109         return "success";
110     }
111
112     /** Update the distributor association for the logged in user, if possible.
113      *@param request The HTTPRequest object for the current request
114      *@param response The HTTPResponse object for the current request
115      *@return String specifying the exit status of this event
116      */

117     public static String JavaDoc updateAssociatedDistributor(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
118         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
119         GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
120         GenericValue party = null;
121
122         java.net.URL JavaDoc ecommercePropertiesUrl = null;
123
124         try {
125             ecommercePropertiesUrl = ((ServletContext JavaDoc) request.getAttribute("servletContext")).getResource("/WEB-INF/ecommerce.properties");
126         } catch (java.net.MalformedURLException JavaDoc e) {
127             Debug.logWarning(e, module);
128         }
129
130         String JavaDoc store = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "distributor.store.customer");
131
132         if (store == null || store.toUpperCase().startsWith("N")) {
133             return "success";
134         }
135
136         String JavaDoc storeOnClick = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "distributor.store.onclick");
137
138         if (storeOnClick == null || storeOnClick.toUpperCase().startsWith("N")) {
139             return "success";
140         }
141
142         try {
143             party = userLogin == null ? null : userLogin.getRelatedOne("Party");
144         } catch (GenericEntityException gee) {
145             Debug.logWarning(gee, module);
146         }
147
148         if (party != null) {
149             // if a distributorId is already associated, it will be used instead
150
String JavaDoc currentDistributorId = getId(party, "DISTRIBUTOR");
151
152             if (UtilValidate.isEmpty(currentDistributorId)) {
153                 String JavaDoc distributorId = (String JavaDoc) request.getSession().getAttribute(DISTRIBUTOR_ID);
154
155                 if (UtilValidate.isNotEmpty(distributorId)) {
156                     List JavaDoc toBeStored = new LinkedList JavaDoc();
157
158                     // create distributor Party ?? why?
159
// create distributor PartyRole ?? why?
160
// create PartyRelationship
161
GenericValue partyRelationship = delegator.makeValue("PartyRelationship", UtilMisc.toMap("partyIdFrom", party.getString("partyId"), "partyIdTo", distributorId, "roleTypeIdFrom", "CUSTOMER", "roleTypeIdTo", "DISTRIBUTOR"));
162
163                     partyRelationship.set("fromDate", UtilDateTime.nowTimestamp());
164                     partyRelationship.set("partyRelationshipTypeId", "DISTRIBUTION_CHANNEL");
165                     toBeStored.add(partyRelationship);
166
167                     toBeStored.add(delegator.makeValue("Party", UtilMisc.toMap("partyId", distributorId)));
168                     toBeStored.add(delegator.makeValue("PartyRole", UtilMisc.toMap("partyId", distributorId, "roleTypeId", "DISTRIBUTOR")));
169                     try {
170                         delegator.storeAll(toBeStored);
171                         if (Debug.infoOn()) Debug.logInfo("Distributor for user " + party.getString("partyId") + " set to " + distributorId, module);
172                     } catch (GenericEntityException gee) {
173                         Debug.logWarning(gee, module);
174                     }
175                 } else {
176                     // no distributorId is available
177
Debug.logInfo("No distributor in session or already associated with user " + userLogin.getString("partyId"), module);
178                     return "success";
179                 }
180             } else {
181                 request.getSession().setAttribute(DISTRIBUTOR_ID, currentDistributorId);
182             }
183
184             return "success";
185         } else {
186             // not logged in
187
Debug.logWarning("Cannot associate distributor since not logged in yet", module);
188             return "success";
189         }
190     }
191
192     /** Update the affiliate association for the logged in user, if possible.
193      *@param request The HTTPRequest object for the current request
194      *@param response The HTTPResponse object for the current request
195      *@return String specifying the exit status of this event
196      */

197     public static String JavaDoc updateAssociatedAffiliate(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
198         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
199         GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
200         GenericValue party = null;
201
202         java.net.URL JavaDoc ecommercePropertiesUrl = null;
203
204         try {
205             ecommercePropertiesUrl = ((ServletContext JavaDoc) request.getAttribute("servletContext")).getResource("/WEB-INF/ecommerce.properties");
206         } catch (java.net.MalformedURLException JavaDoc e) {
207             Debug.logWarning(e, module);
208         }
209
210         String JavaDoc store = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "affiliate.store.customer");
211
212         if (store == null || store.toUpperCase().startsWith("N"))
213             return "success";
214         String JavaDoc storeOnClick = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "affiliate.store.onclick");
215
216         if (storeOnClick == null || storeOnClick.toUpperCase().startsWith("N"))
217             return "success";
218
219         try {
220             party = userLogin == null ? null : userLogin.getRelatedOne("Party");
221         } catch (GenericEntityException gee) {
222             Debug.logWarning(gee, module);
223         }
224
225         if (party != null) {
226             // if a distributorId is already associated, it will be used instead
227
String JavaDoc currentAffiliateId = getId(party, "AFFILIATE");
228
229             if (UtilValidate.isEmpty(currentAffiliateId)) {
230                 String JavaDoc affiliateId = (String JavaDoc) request.getSession().getAttribute(AFFILIATE_ID);
231
232                 if (UtilValidate.isNotEmpty(affiliateId)) {
233                     // create PartyRelationship
234
GenericValue partyRelationship = delegator.makeValue("PartyRelationship", UtilMisc.toMap("partyIdFrom", party.getString("partyId"), "partyIdTo", affiliateId, "roleTypeIdFrom", "CUSTOMER", "roleTypeIdTo", "AFFILIATE"));
235
236                     partyRelationship.set("fromDate", UtilDateTime.nowTimestamp());
237                     partyRelationship.set("partyRelationshipTypeId", "SALES_AFFILIATE");
238                     try {
239                         delegator.create(partyRelationship);
240                         if (Debug.infoOn()) Debug.logInfo("Affiliate for user " + party.getString("partyId") + " set to " + affiliateId, module);
241                     } catch (GenericEntityException gee) {
242                         Debug.logWarning(gee, module);
243                     }
244                 } else {
245                     // no distributorId is available
246
Debug.logInfo("No affiliate in session or already associated with user " + userLogin.getString("partyId"), module);
247                     return "success";
248                 }
249             } else {
250                 request.getSession().setAttribute(AFFILIATE_ID, currentAffiliateId);
251             }
252
253             return "success";
254         } else {
255             // not logged in
256
Debug.logWarning("Cannot associate affiliate since not logged in yet", module);
257             return "success";
258         }
259     }
260
261     private static GenericValue getPartyRelationship(GenericValue party, String JavaDoc roleTypeTo) {
262         try {
263             return EntityUtil.getFirst(EntityUtil.filterByDate(party.getRelatedByAnd("FromPartyRelationship", UtilMisc.toMap("roleTypeIdTo", roleTypeTo)), true));
264         } catch (GenericEntityException gee) {
265             Debug.logWarning(gee, module);
266         }
267         return null;
268     }
269
270     private static String JavaDoc getId(GenericValue party, String JavaDoc roleTypeTo) {
271         GenericValue partyRelationship = getPartyRelationship(party, roleTypeTo);
272
273         return partyRelationship == null ? null : partyRelationship.getString("partyIdTo");
274     }
275
276 }
277
Popular Tags