1 24 package org.ofbiz.marketing.tracking; 25 26 import java.util.ArrayList ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 import javax.servlet.http.Cookie ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilDateTime; 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.webapp.stats.VisitHandler; 41 import org.ofbiz.webapp.website.WebSiteWorker; 42 import org.ofbiz.entity.GenericDelegator; 43 import org.ofbiz.entity.GenericEntityException; 44 import org.ofbiz.entity.GenericValue; 45 import org.ofbiz.product.category.CategoryWorker; 46 47 54 public class TrackingCodeEvents { 55 56 public static final String module = TrackingCodeEvents.class.getName(); 57 58 62 public static String checkTrackingCodeUrlParam(HttpServletRequest request, HttpServletResponse response) { 63 String trackingCodeId = request.getParameter("autoTrackingCode"); 64 if (UtilValidate.isEmpty(trackingCodeId)) trackingCodeId = request.getParameter("atc"); 65 66 if (UtilValidate.isNotEmpty(trackingCodeId)) { 67 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 69 GenericValue trackingCode = null; 70 try { 71 trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId)); 72 } catch (GenericEntityException e) { 73 Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 74 return "error"; 75 } 76 77 if (trackingCode == null) { 78 Debug.logError("TrackingCode not found for trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId.", module); 79 return "error"; 81 } 82 83 return processTrackingCode(trackingCode, request, response); 84 } else { 85 return "success"; 86 } 87 } 88 89 100 public static String checkPartnerTrackingCodeUrlParam(HttpServletRequest request, HttpServletResponse response) { 101 String trackingCodeId = request.getParameter("ptc"); 102 103 if (UtilValidate.isNotEmpty(trackingCodeId)) { 104 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 106 GenericValue trackingCode = null; 107 try { 108 trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId)); 109 } catch (GenericEntityException e) { 110 Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 111 return "error"; 112 } 113 114 if (trackingCode == null) { 115 117 String dtc = request.getParameter("dtc"); 118 if (UtilValidate.isEmpty(dtc)) { 119 dtc = UtilProperties.getPropertyValue("general", "partner.trackingCodeId.default"); 120 } 121 if (UtilValidate.isNotEmpty(dtc)) { 122 GenericValue defaultTrackingCode = null; 123 try { 124 defaultTrackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", dtc)); 125 } catch (GenericEntityException e) { 126 Debug.logError(e, "Error looking up Default values TrackingCode with trackingCodeId [" + dtc + "], not using the dtc value for new TrackingCode defaults", module); 127 } 128 129 if (defaultTrackingCode != null) { 130 defaultTrackingCode.set("trackingCodeId", trackingCodeId); 131 defaultTrackingCode.set("trackingCodeTypeId", "PARTNER_MGD"); 132 defaultTrackingCode.set("createdDate", UtilDateTime.nowTimestamp()); 134 defaultTrackingCode.set("createdByUserLogin", null); 135 defaultTrackingCode.set("lastModifiedDate", UtilDateTime.nowTimestamp()); 136 defaultTrackingCode.set("lastModifiedByUserLogin", null); 137 138 trackingCode = defaultTrackingCode; 139 try { 140 trackingCode.create(); 141 } catch (GenericEntityException e) { 142 Debug.logError(e, "Error creating new Partner TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 143 return "error"; 144 } 145 } 146 } 147 148 if (trackingCode == null) { 150 trackingCode = delegator.makeValue("TrackingCode", null); 151 trackingCode.set("trackingCodeId", trackingCodeId); 152 trackingCode.set("trackingCodeTypeId", "PARTNER_MGD"); 153 trackingCode.set("createdDate", UtilDateTime.nowTimestamp()); 155 trackingCode.set("lastModifiedDate", UtilDateTime.nowTimestamp()); 156 157 trackingCode.set("trackableLifetime", new Long (10000000000L)); 159 trackingCode.set("billableLifetime", new Long (2592000)); 161 162 trackingCode.set("comments", "This TrackingCode has default values because no default TrackingCode could be found."); 163 164 Debug.logWarning("No default TrackingCode record was found, using a TrackingCode with hard coded default values: " + trackingCode, module); 165 166 try { 167 trackingCode.create(); 168 } catch (GenericEntityException e) { 169 Debug.logError(e, "Error creating new Partner TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 170 return "error"; 171 } 172 } 173 } 174 175 return processTrackingCode(trackingCode, request, response); 176 } else { 177 return "success"; 178 } 179 } 180 181 private static String processTrackingCode(GenericValue trackingCode, HttpServletRequest request, HttpServletResponse response) { 182 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 183 String trackingCodeId = trackingCode.getString("trackingCodeId"); 184 185 java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp(); 187 if (trackingCode.get("fromDate") != null && nowStamp.before(trackingCode.getTimestamp("fromDate"))) { 188 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has not yet gone into effect, ignoring this trackingCodeId", module); 189 return "success"; 190 } 191 if (trackingCode.get("thruDate") != null && nowStamp.after(trackingCode.getTimestamp("thruDate"))) { 192 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has expired, ignoring this trackingCodeId", module); 193 return "success"; 194 } 195 196 GenericValue visit = VisitHandler.getVisit(request.getSession()); 198 if (visit == null) { 199 Debug.logWarning("Could not get visit, not associating trackingCode [" + trackingCodeId + "] with visit", module); 200 } else { 201 GenericValue trackingCodeVisit = delegator.makeValue("TrackingCodeVisit", 202 UtilMisc.toMap("trackingCodeId", trackingCodeId, "visitId", visit.get("visitId"), 203 "fromDate", UtilDateTime.nowTimestamp(), "sourceEnumId", "TKCDSRC_URL_PARAM")); 204 try { 205 trackingCodeVisit.create(); 206 } catch (GenericEntityException e) { 207 Debug.logError(e, "Error while saving TrackingCodeVisit", module); 208 } 209 } 210 211 212 215 String cookieDomain = null; 217 218 String webSiteId = WebSiteWorker.getWebSiteId(request); 219 if (webSiteId != null) { 220 try { 221 GenericValue webSite = delegator.findByPrimaryKeyCache("WebSite", UtilMisc.toMap("webSiteId", webSiteId)); 222 if (webSite != null) { 223 cookieDomain = webSite.getString("cookieDomain"); 224 } 225 } catch (GenericEntityException e) { 226 Debug.logWarning(e, "Problems with WebSite entity; using global default cookie domain", module); 227 } 228 } 229 230 if (cookieDomain == null) { 231 cookieDomain = UtilProperties.getPropertyValue("url", "cookie.domain", ""); 232 } 233 234 Long trackableLifetime = trackingCode.getLong("trackableLifetime"); 236 if (trackableLifetime != null && trackableLifetime.longValue() > 0) { 237 Cookie trackableCookie = new Cookie ("TKCDT_" + trackingCode.getString("trackingCodeTypeId"), trackingCode.getString("trackingCodeId")); 238 trackableCookie.setMaxAge(trackableLifetime.intValue()); 239 trackableCookie.setPath("/"); 240 trackableCookie.setVersion(1); 241 if (cookieDomain.length() > 0) trackableCookie.setDomain(cookieDomain); 242 response.addCookie(trackableCookie); 243 } 244 245 Long billableLifetime = trackingCode.getLong("billableLifetime"); 247 if (billableLifetime != null && billableLifetime.longValue() > 0) { 248 Cookie billableCookie = new Cookie ("TKCDB_" + trackingCode.getString("trackingCodeTypeId"), trackingCode.getString("trackingCodeId")); 249 billableCookie.setMaxAge(billableLifetime.intValue()); 250 billableCookie.setPath("/"); 251 billableCookie.setVersion(1); 252 if (cookieDomain.length() > 0) billableCookie.setDomain(cookieDomain); 253 response.addCookie(billableCookie); 254 } 255 256 HttpSession session = request.getSession(); 258 String overrideLogo = trackingCode.getString("overrideLogo"); 259 if (overrideLogo != null) 260 session.setAttribute("overrideLogo", overrideLogo); 261 String overrideCss = trackingCode.getString("overrideCss"); 262 if (overrideCss != null) 263 session.setAttribute("overrideCss", overrideCss); 264 String prodCatalogId = trackingCode.getString("prodCatalogId"); 265 if (prodCatalogId != null && prodCatalogId.length() > 0) { 266 session.setAttribute("CURRENT_CATALOG_ID", prodCatalogId); 267 CategoryWorker.setTrail(request, new ArrayList ()); 268 } 269 270 String redirectUrl = trackingCode.getString("redirectUrl"); 272 if (UtilValidate.isNotEmpty(redirectUrl)) { 273 try { 274 response.sendRedirect(redirectUrl); 275 } catch (java.io.IOException e) { 276 Debug.logError(e, "Could not redirect as requested in the trackingCode to: " + redirectUrl, module); 277 } 278 return null; 279 } 280 281 return "success"; 282 } 283 284 287 public static String checkTrackingCodeCookies(HttpServletRequest request, HttpServletResponse response) { 288 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 289 java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp(); 290 GenericValue visit = VisitHandler.getVisit(request.getSession()); 291 if (visit == null) { 292 Debug.logWarning("Could not get visit, not checking trackingCode cookies to associate with visit", module); 293 } else { 294 Cookie [] cookies = request.getCookies(); 296 297 if (cookies != null && cookies.length > 0) { 298 for (int i = 0; i < cookies.length; i++) { 299 if (cookies[i].getName().startsWith("TKCDT_")) { 300 String trackingCodeId = cookies[i].getValue(); 301 GenericValue trackingCode = null; 302 try { 303 trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId)); 304 } catch (GenericEntityException e) { 305 Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 306 continue; 307 } 308 309 if (trackingCode == null) { 310 Debug.logError("TrackingCode not found for trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId.", module); 311 continue; 313 } 314 315 if (trackingCode.get("fromDate") != null && nowStamp.before(trackingCode.getTimestamp("fromDate"))) { 317 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has not yet gone into effect, ignoring this trackingCodeId", module); 318 continue; 319 } 320 if (trackingCode.get("thruDate") != null && nowStamp.after(trackingCode.getTimestamp("thruDate"))) { 321 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has expired, ignoring this trackingCodeId", module); 322 continue; 323 } 324 325 GenericValue trackingCodeVisit = delegator.makeValue("TrackingCodeVisit", 327 UtilMisc.toMap("trackingCodeId", trackingCodeId, "visitId", visit.get("visitId"), 328 "fromDate", nowStamp, "sourceEnumId", "TKCDSRC_COOKIE")); 329 try { 330 trackingCodeVisit.create(); 332 } catch (GenericEntityException e) { 333 Debug.logError(e, "Error while saving TrackingCodeVisit", module); 334 } 336 } 337 } 338 } 339 } 340 341 return "success"; 342 } 343 344 345 public static List makeTrackingCodeOrders(HttpServletRequest request) { 346 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 347 java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp(); 348 List trackingCodeOrders = new LinkedList (); 349 350 Cookie [] cookies = request.getCookies(); 351 if (cookies != null && cookies.length > 0) { 352 for (int i = 0; i < cookies.length; i++) { 353 356 String isBillable = null; 357 String cookieName = cookies[i].getName(); 358 if (cookieName.startsWith("TKCDB_")) { 359 isBillable = "Y"; 360 } else if (cookieName.startsWith("TKCDT_")) { 361 isBillable = "N"; 362 } 363 364 if (isBillable != null) { 365 String trackingCodeId = cookies[i].getValue(); 366 GenericValue trackingCode = null; 367 try { 368 trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId)); 369 } catch (GenericEntityException e) { 370 Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module); 371 continue; 372 } 373 374 if (trackingCode == null) { 375 Debug.logError("TrackingCode not found for trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId.", module); 376 continue; 378 } 379 380 if (trackingCode.get("fromDate") != null && nowStamp.before(trackingCode.getTimestamp("fromDate"))) { 382 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has not yet gone into effect, ignoring this trackingCodeId", module); 383 continue; 384 } 385 if (trackingCode.get("thruDate") != null && nowStamp.after(trackingCode.getTimestamp("thruDate"))) { 386 if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has expired, ignoring this trackingCodeId", module); 387 continue; 388 } 389 390 392 String cookieTrackingCodeTypeId = cookieName.substring("TKCDB_".length()); 394 if (cookieTrackingCodeTypeId == null || cookieTrackingCodeTypeId.length() == 0) { 395 Debug.logWarning("The trackingCodeTypeId as part of the cookie name was null or empty", module); 396 } else if (!cookieTrackingCodeTypeId.equals(trackingCode.getString("trackingCodeTypeId"))) { 397 Debug.logWarning("The trackingCodeTypeId [" + cookieTrackingCodeTypeId + "] as part of the cookie name was equal to the current trackingCodeTypeId [" + trackingCode.getString("trackingCodeTypeId") + "] associated with the trackingCodeId [" + trackingCodeId + "]", module); 398 } 399 400 GenericValue trackingCodeOrder = delegator.makeValue("TrackingCodeOrder", 402 UtilMisc.toMap("trackingCodeTypeId", trackingCode.get("trackingCodeTypeId"), 403 "trackingCodeId", trackingCodeId, "isBillable", isBillable)); 404 405 trackingCodeOrders.add(trackingCodeOrder); 406 } 407 } 408 } 409 410 return trackingCodeOrders; 411 } 412 } 413 | Popular Tags |