📄 trackingcodeevents.java
字号:
/*
* $Id: TrackingCodeEvents.java,v 1.2 2003/10/23 06:33:02 jonesde Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.marketing.tracking;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.content.stats.VisitHandler;
import org.ofbiz.content.website.WebSiteWorker;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.product.category.CategoryWorker;
/**
* Events used for maintaining TrackingCode related information
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.2 $
* @since 2.0
*/
public class TrackingCodeEvents {
public static final String module = TrackingCodeEvents.class.getName();
/** If TrackingCode monitoring is desired this event should be added to the list
* of events that run on every request. This event looks for the parameter
* <code>autoTrackingCode</code> or a shortened version: <code>atc</code>.
*/
public static String checkTrackingCodeUrlParam(HttpServletRequest request, HttpServletResponse response) {
String trackingCodeId = request.getParameter("autoTrackingCode");
if (UtilValidate.isEmpty(trackingCodeId)) trackingCodeId = request.getParameter("atc");
if (UtilValidate.isNotEmpty(trackingCodeId)) {
//tracking code is specified on the request, get the TrackingCode value and handle accordingly
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
GenericValue trackingCode = null;
try {
trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId));
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module);
return "error";
}
if (trackingCode == null) {
Debug.logError("TrackingCode not found for trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId.", module);
//this return value will be ignored, but we'll designate this as an error anyway
return "error";
}
return processTrackingCode(trackingCode, request, response);
} else {
return "success";
}
}
/** If TrackingCode monitoring is desired this event should be added to the list
* of events that run on every request. This event looks for the parameter
* <code>ptc</code> and handles the value as a Partner Managed Tracking Code.
*
* If the specified trackingCodeId exists then it is used as is, otherwise a new one
* is created with the ptc value as the trackingCodeId. The values for the fields of
* the new TrackingCode can come from one of two places: if a <code>dtc</code> parameter
* is included the value will be used to lookup a TrackingCode with default values,
* otherwise the default trackingCodeId will be looked up in the <code>partner.trackingCodeId.default</code>
* in the <code>general.properties</code> file. If that is still not found just use an empty TrackingCode.
*/
public static String checkPartnerTrackingCodeUrlParam(HttpServletRequest request, HttpServletResponse response) {
String trackingCodeId = request.getParameter("ptc");
if (UtilValidate.isNotEmpty(trackingCodeId)) {
//partner managed tracking code is specified on the request
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
GenericValue trackingCode = null;
try {
trackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", trackingCodeId));
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module);
return "error";
}
if (trackingCode == null) {
//create new TrackingCode with default values from a "dtc" parameter or from a properties file
String dtc = request.getParameter("dtc");
if (UtilValidate.isEmpty(dtc)) {
dtc = UtilProperties.getPropertyValue("general", "partner.trackingCodeId.default");
}
if (UtilValidate.isNotEmpty(dtc)) {
GenericValue defaultTrackingCode = null;
try {
defaultTrackingCode = delegator.findByPrimaryKeyCache("TrackingCode", UtilMisc.toMap("trackingCodeId", dtc));
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up Default values TrackingCode with trackingCodeId [" + dtc + "], not using the dtc value for new TrackingCode defaults", module);
}
if (defaultTrackingCode != null) {
defaultTrackingCode.set("trackingCodeId", trackingCodeId);
defaultTrackingCode.set("trackingCodeTypeId", "PARTNER_MGD");
//null out userLogin fields, no use tracking to customer, or is there?; set dates to current
defaultTrackingCode.set("createdDate", UtilDateTime.nowTimestamp());
defaultTrackingCode.set("createdByUserLogin", null);
defaultTrackingCode.set("lastModifiedDate", UtilDateTime.nowTimestamp());
defaultTrackingCode.set("lastModifiedByUserLogin", null);
trackingCode = defaultTrackingCode;
try {
trackingCode.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Error creating new Partner TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module);
return "error";
}
}
}
//if trackingCode is still null then the defaultTrackingCode thing didn't work out, use empty TrackingCode
if (trackingCode == null) {
trackingCode = delegator.makeValue("TrackingCode", null);
trackingCode.set("trackingCodeId", trackingCodeId);
trackingCode.set("trackingCodeTypeId", "PARTNER_MGD");
//leave userLogin fields empty, no use tracking to customer, or is there?; set dates to current
trackingCode.set("createdDate", UtilDateTime.nowTimestamp());
trackingCode.set("lastModifiedDate", UtilDateTime.nowTimestamp());
//use nearly unlimited trackable lifetime: 10 billion seconds, 310 years
trackingCode.set("trackableLifetime", new Long(10000000000L));
//use 2592000 seconds as billable lifetime: equals 1 month
trackingCode.set("billableLifetime", new Long(2592000));
trackingCode.set("comments", "This TrackingCode has default values because no default TrackingCode could be found.");
Debug.logWarning("No default TrackingCode record was found, using a TrackingCode with hard coded default values: " + trackingCode, module);
try {
trackingCode.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Error creating new Partner TrackingCode with trackingCodeId [" + trackingCodeId + "], ignoring this trackingCodeId", module);
return "error";
}
}
}
return processTrackingCode(trackingCode, request, response);
} else {
return "success";
}
}
private static String processTrackingCode(GenericValue trackingCode, HttpServletRequest request, HttpServletResponse response) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
String trackingCodeId = trackingCode.getString("trackingCodeId");
//check effective dates
java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp();
if (trackingCode.get("fromDate") != null && nowStamp.before(trackingCode.getTimestamp("fromDate"))) {
if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has not yet gone into effect, ignoring this trackingCodeId", module);
return "success";
}
if (trackingCode.get("thruDate") != null && nowStamp.after(trackingCode.getTimestamp("thruDate"))) {
if (Debug.infoOn()) Debug.logInfo("The TrackingCode with ID [" + trackingCodeId + "] has expired, ignoring this trackingCodeId", module);
return "success";
}
//persist that info by associating with the current visit
GenericValue visit = VisitHandler.getVisit(request.getSession());
if (visit == null) {
Debug.logWarning("Could not get visit, not associating trackingCode [" + trackingCodeId + "] with visit", module);
} else {
GenericValue trackingCodeVisit = delegator.makeValue("TrackingCodeVisit",
UtilMisc.toMap("trackingCodeId", trackingCodeId, "visitId", visit.get("visitId"),
"fromDate", UtilDateTime.nowTimestamp(), "sourceEnumId", "TKCDSRC_URL_PARAM"));
try {
trackingCodeVisit.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Error while saving TrackingCodeVisit", module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -