📄 trackingcodeevents.java
字号:
}
}
// write trackingCode cookies with the value set to the trackingCodeId
// NOTE: just write these cookies and if others exist from other tracking codes they will be overwritten, ie only keep the newest
// load the properties from the website entity
String cookieDomain = null;
String webSiteId = WebSiteWorker.getWebSiteId(request);
if (webSiteId != null) {
try {
GenericValue webSite = delegator.findByPrimaryKeyCache("WebSite", UtilMisc.toMap("webSiteId", webSiteId));
if (webSite != null) {
cookieDomain = webSite.getString("cookieDomain");
}
} catch (GenericEntityException e) {
Debug.logWarning(e, "Problems with WebSite entity; using global default cookie domain", module);
}
}
if (cookieDomain == null) {
cookieDomain = UtilProperties.getPropertyValue("url", "cookie.domain", "");
}
// if trackingCode.trackableLifetime not null and is > 0 write a trackable cookie with name in the form: TKCDT_{trackingCode.trackingCodeTypeId} and timeout will be trackingCode.trackableLifetime
Long trackableLifetime = trackingCode.getLong("trackableLifetime");
if (trackableLifetime != null && trackableLifetime.longValue() > 0) {
Cookie trackableCookie = new Cookie("TKCDT_" + trackingCode.getString("trackingCodeTypeId"), trackingCode.getString("trackingCodeId"));
trackableCookie.setMaxAge(trackableLifetime.intValue());
trackableCookie.setPath("/");
trackableCookie.setVersion(1);
if (cookieDomain.length() > 0) trackableCookie.setDomain(cookieDomain);
response.addCookie(trackableCookie);
}
// if trackingCode.billableLifetime not null and is > 0 write a billable cookie with name in the form: TKCDB_{trackingCode.trackingCodeTypeId} and timeout will be trackingCode.billableLifetime
Long billableLifetime = trackingCode.getLong("billableLifetime");
if (billableLifetime != null && billableLifetime.longValue() > 0) {
Cookie billableCookie = new Cookie("TKCDB_" + trackingCode.getString("trackingCodeTypeId"), trackingCode.getString("trackingCodeId"));
billableCookie.setMaxAge(billableLifetime.intValue());
billableCookie.setPath("/");
billableCookie.setVersion(1);
if (cookieDomain.length() > 0) billableCookie.setDomain(cookieDomain);
response.addCookie(billableCookie);
}
// if we have overridden logo, css and/or catalogId set some session attributes
HttpSession session = request.getSession();
String overrideLogo = trackingCode.getString("overrideLogo");
if (overrideLogo != null)
session.setAttribute("overrideLogo", overrideLogo);
String overrideCss = trackingCode.getString("overrideCss");
if (overrideCss != null)
session.setAttribute("overrideCss", overrideCss);
String prodCatalogId = trackingCode.getString("prodCatalogId");
if (prodCatalogId != null && prodCatalogId.length() > 0) {
session.setAttribute("CURRENT_CATALOG_ID", prodCatalogId);
CategoryWorker.setTrail(request, new ArrayList());
}
// if forward/redirect is needed, do a response.sendRedirect and return null to tell the control servlet to not do any other requests/views
String redirectUrl = trackingCode.getString("redirectUrl");
if (UtilValidate.isNotEmpty(redirectUrl)) {
try {
response.sendRedirect(redirectUrl);
} catch (java.io.IOException e) {
Debug.logError(e, "Could not redirect as requested in the trackingCode to: " + redirectUrl, module);
}
return null;
}
return "success";
}
/** If attaching TrackingCode Cookies to the visit is desired this event should be added to the list
* of events that run on the first hit in a visit.
*/
public static String checkTrackingCodeCookies(HttpServletRequest request, HttpServletResponse response) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp();
GenericValue visit = VisitHandler.getVisit(request.getSession());
if (visit == null) {
Debug.logWarning("Could not get visit, not checking trackingCode cookies to associate with visit", module);
} else {
// loop through cookies and look for ones with a name that starts with TKCDT_ for trackable cookies
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().startsWith("TKCDT_")) {
String trackingCodeId = cookies[i].getValue();
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);
continue;
}
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
continue;
}
//check effective dates
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);
continue;
}
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);
continue;
}
// for each trackingCodeId found in this way attach to the visit with the TKCDSRC_COOKIE sourceEnumId
GenericValue trackingCodeVisit = delegator.makeValue("TrackingCodeVisit",
UtilMisc.toMap("trackingCodeId", trackingCodeId, "visitId", visit.get("visitId"),
"fromDate", nowStamp, "sourceEnumId", "TKCDSRC_COOKIE"));
try {
//not doing this inside a transaction, want each one possible to go in
trackingCodeVisit.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Error while saving TrackingCodeVisit", module);
//don't return error, want to get as many as possible: return "error";
}
}
}
}
}
return "success";
}
/** Makes a list of TrackingCodeOrder entities to be attached to the current order; called by the createOrder event; the values in the returned List will not have the orderId set */
public static List makeTrackingCodeOrders(HttpServletRequest request) {
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
java.sql.Timestamp nowStamp = UtilDateTime.nowTimestamp();
List trackingCodeOrders = new LinkedList();
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
// find any that start with TKCDB_ for billable tracking code cookies with isBillable=Y
// also and for each TKCDT_ cookie that doesn't have a corresponding billable code add it to the list with isBillable=N
String isBillable = null;
String cookieName = cookies[i].getName();
if (cookieName.startsWith("TKCDB_")) {
isBillable = "Y";
} else if (cookieName.startsWith("TKCDT_")) {
isBillable = "N";
}
if (isBillable != null) {
String trackingCodeId = cookies[i].getValue();
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);
continue;
}
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
continue;
}
//check effective dates
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);
continue;
}
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);
continue;
}
// a quick sanity check here on the trackingCodeTypeId, will just display a warning if this happens but not do anythin about it for now
// note: using TKCDB_ only for length because both TKCDB_ and TKCDT_ are the same length
String cookieTrackingCodeTypeId = cookieName.substring("TKCDB_".length());
if (cookieTrackingCodeTypeId == null || cookieTrackingCodeTypeId.length() == 0) {
Debug.logWarning("The trackingCodeTypeId as part of the cookie name was null or empty", module);
} else if (!cookieTrackingCodeTypeId.equals(trackingCode.getString("trackingCodeTypeId"))) {
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);
}
// this will have everything except the orderId set, that will be set by the createOrder service
GenericValue trackingCodeOrder = delegator.makeValue("TrackingCodeOrder",
UtilMisc.toMap("trackingCodeTypeId", trackingCode.get("trackingCodeTypeId"),
"trackingCodeId", trackingCodeId, "isBillable", isBillable));
trackingCodeOrders.add(trackingCodeOrder);
}
}
}
return trackingCodeOrders;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -