1 24 package org.ofbiz.ecommerce.misc; 25 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 53 public class ThirdPartyEvents { 54 55 public static final String module = ThirdPartyEvents.class.getName(); 56 57 public final static String DISTRIBUTOR_ID = "_DISTRIBUTOR_ID_"; 58 public final static String AFFILIATE_ID = "_AFFILIATE_ID_"; 59 60 65 public static String setAssociationId(HttpServletRequest request, HttpServletResponse response) { 66 Map requestParams = UtilHttp.getParameterMap(request); 67 68 String distriParam[] = { "distributor_id", "distributorid", "distributor" }; 70 String distributorId = null; 71 72 for (int i = 0; i < distriParam.length; i++) { 73 String param = distriParam[i]; 74 75 if (requestParams.containsKey(param)) { 76 distributorId = (String ) requestParams.get(param); 77 break; 78 } else if (requestParams.containsKey(param.toUpperCase())) { 79 distributorId = (String ) requestParams.get(param.toUpperCase()); 80 break; 81 } 82 } 83 84 String affiliParam[] = { "affiliate_id", "affiliateid", "affiliate", "affil" }; 86 String affiliateId = null; 87 88 for (int i = 0; i < affiliParam.length; i++) { 89 String param = affiliParam[i]; 90 91 if (requestParams.containsKey(param)) { 92 affiliateId = (String ) requestParams.get(param); 93 break; 94 } else if (requestParams.containsKey(param.toUpperCase())) { 95 affiliateId = (String ) 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 117 public static String updateAssociatedDistributor(HttpServletRequest request, HttpServletResponse 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 ecommercePropertiesUrl = null; 123 124 try { 125 ecommercePropertiesUrl = ((ServletContext ) request.getAttribute("servletContext")).getResource("/WEB-INF/ecommerce.properties"); 126 } catch (java.net.MalformedURLException e) { 127 Debug.logWarning(e, module); 128 } 129 130 String store = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "distributor.store.customer"); 131 132 if (store == null || store.toUpperCase().startsWith("N")) { 133 return "success"; 134 } 135 136 String 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 String currentDistributorId = getId(party, "DISTRIBUTOR"); 151 152 if (UtilValidate.isEmpty(currentDistributorId)) { 153 String distributorId = (String ) request.getSession().getAttribute(DISTRIBUTOR_ID); 154 155 if (UtilValidate.isNotEmpty(distributorId)) { 156 List toBeStored = new LinkedList (); 157 158 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 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 Debug.logWarning("Cannot associate distributor since not logged in yet", module); 188 return "success"; 189 } 190 } 191 192 197 public static String updateAssociatedAffiliate(HttpServletRequest request, HttpServletResponse 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 ecommercePropertiesUrl = null; 203 204 try { 205 ecommercePropertiesUrl = ((ServletContext ) request.getAttribute("servletContext")).getResource("/WEB-INF/ecommerce.properties"); 206 } catch (java.net.MalformedURLException e) { 207 Debug.logWarning(e, module); 208 } 209 210 String store = UtilProperties.getPropertyValue(ecommercePropertiesUrl, "affiliate.store.customer"); 211 212 if (store == null || store.toUpperCase().startsWith("N")) 213 return "success"; 214 String 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 String currentAffiliateId = getId(party, "AFFILIATE"); 228 229 if (UtilValidate.isEmpty(currentAffiliateId)) { 230 String affiliateId = (String ) request.getSession().getAttribute(AFFILIATE_ID); 231 232 if (UtilValidate.isNotEmpty(affiliateId)) { 233 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 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 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 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 getId(GenericValue party, String roleTypeTo) { 271 GenericValue partyRelationship = getPartyRelationship(party, roleTypeTo); 272 273 return partyRelationship == null ? null : partyRelationship.getString("partyIdTo"); 274 } 275 276 } 277 | Popular Tags |