package com.ngp.commerce.reporting;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import com.ngp.commerce.catalog.NGPCatalogService;
import com.ngp.commerce.reporting.omniture.OmnitureContentTagger;
import atg.core.util.StringUtils;
import atg.nucleus.GenericService;
import atg.repository.RepositoryItem;
/**
* The report tagger class is used to collect various reporting information from different sections of the site
* this data is then made available to omniture, or other pixel reporting software, via a droplet that renders
* the information in an easly consumed format.
* @author cboyer
*
*/
public class ReportTagger extends GenericService {
HashMap<String, String> dataMap = new HashMap<String, String>();
Set<String> events = new TreeSet<String>();
ProductDataObserver pDataObserver = new ProductDataObserver();
ReportTagger additionalTagger;
OmnitureContentTagger contentTagger;
NGPCatalogService catalogService;
/**
* Stores the requested key/value pair in the internal data Map.
* @param dataKey
* @param dataValue
*/
public void storeData(String dataKey, String dataValue ) {
if (isLoggingDebug()) {
logDebug("Request to store data " + dataKey + " with value " + dataValue);
}
dataMap.put(dataKey, dataValue);
}
public String getDataValue(String dataKey) {
if (isLoggingDebug())
logDebug("Request to return data for key " + dataKey );
return dataMap.get(dataKey);
}
/**
* Records the past event string in an internal set.
* @param event
*/
public void registerEvent (String event) {
if (isLoggingDebug()) {
logDebug("Adding event " + event);
}
events.add(event);
}
public Set<String> getEventData () {
return events;
}
public void addProduct(String productID, Integer qty, Float totalLinePrice, Map<String, String> eventAmounts, Map<String, String> merchantVars ) {
ProductReportData data = new ProductReportData(productID,qty,totalLinePrice,eventAmounts,merchantVars);
pDataObserver.addData(data);
/* Any event logged on the product line also needs to be added to the events set. */
if (eventAmounts != null)
events.addAll(eventAmounts.keySet());
}
/**
* Returns the internal data as a Map. Then clears old data.
* @return
*/
public Map getData() {
if (isLoggingDebug()) {
logDebug("Returning data values from storage");
}
HashMap<String, String> data = new HashMap<String, String>();
Map additionalData = new HashMap<String, String>();
boolean usesLongForm = false;
if (additionalTagger != null) {
usesLongForm = additionalTagger.productUsesLongForm();
additionalData = additionalTagger.getData();
data.putAll(additionalData);
}
data.putAll(dataMap);
if (contentTagger != null ) {
Map<String, String> contentInfo = contentTagger.getContentTagInfo();
data.putAll(contentInfo);
}
if (events.size() > 0) {
String eventString = StringUtils.makeList(events.toArray(), ",");
if (data.containsKey("events")) {
if (isLoggingDebug()) {
logDebug(" Event sring from session " + data.get("events"));
}
eventString = StringUtils.makeList(new String[]{data.get("events") , eventString}, ",");
}
data.put("events", eventString);
}
if (this.pDataObserver.size() > 0) {
if (isLoggingDebug())
logDebug("Product list contains " + pDataObserver.size() + " elements ");
StringBuilder productString = pDataObserver.getProductString();
if (data.containsKey("products")) {
if (productString.length() > 0 )
productString.append(",");
productString.append(data.get("products"));
}
data.put("products", productString.toString());
}
if (isLoggingDebug()) {
logDebug("Return data is: ");
for (Map.Entry<String, String> entry : data.entrySet()) {
logDebug(entry.getKey() + " = " + entry.getValue());
}
}
dataMap = new HashMap<String, String>();
events = new TreeSet<String>();
pDataObserver = new ProductDataObserver();
if (contentTagger != null ) {
contentTagger.initTagger();
}
return data;
}
private boolean productUsesLongForm() {
if (pDataObserver != null){
return pDataObserver.isLongProductFormRequired();
}
return false;
}
class ProductReportData {
String productIdentifier;
Integer quantity;
Float totalPrice;
Map<String, String> eventAmountInfo;
Map<String, String> merchVars;
boolean longFormRequired = false;
public ProductReportData(String productIdentifier) {
this.productIdentifier = productIdentifier;
}
public ProductReportData(String productIdentifier, Integer qty, Float totalPrice, Map<String, String> eventAmount, Map<String, String> merchVars) {
this.productIdentifier = productIdentifier;
if (qty != null ) {
quantity = qty;
longFormRequired = true;
}
if (totalPrice != null) {
this.totalPrice = totalPrice;
longFormRequired = true;
}
if (eventAmount != null ) {
this.eventAmountInfo = eventAmount;
longFormRequired = true;
}
if (merchVars != null) {
this.merchVars = merchVars;
longFormRequired = true;
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ProductReportData ) {
ProductReportData otherData = (ProductReportData) obj;
return this.productIdentifier.equals(otherData.getProductIdentifier() );
}
return super.equals(obj);
}
private Object getProductIdentifier() {
return productIdentifier;
}
public String getProductString(boolean longProductFormRequired) {
StringBuilder productString = new StringBuilder();
productString.append(";").append(productIdentifier);
if (!longProductFormRequired)
return productString.toString();
productString.append(";");
if(quantity != null)
productString.append(quantity);
productString.append(";");
if(totalPrice != null)
productString.append(totalPrice);
productString.append(";");
if (this.eventAmountInfo != null && this.eventAmountInfo.size() > 0) {
productString.append(stringifyMap(eventAmountInfo));
}
productString.append(";");
if (this.merchVars != null && this.merchVars.size() > 0) {
productString.append(stringifyMap(merchVars));
}
productString.append(";");
return productString.toString();
}
private String stringifyMap (Map<String, String> data) {
StringBuilder dataString = new StringBuilder();
Iterator<Map.Entry<String, String>> entryIterator = data.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, String> dataEntry = entryIterator.next();
dataString.append(dataEntry.getKey()).append("=").append(dataEntry.getValue());
if (entryIterator.hasNext())
dataString.append("|");
}
return dataString.toString();
}
public boolean isLongProductFormRequired() {
return longFormRequired;
}
}
class ProductDataObserver {
private LinkedHashSet<ProductReportData> productData;
private boolean longProductFormRequired = false;
ProductDataObserver () {
productData = new LinkedHashSet<ProductReportData>();
}
public StringBuilder getProductString() {
StringBuilder productString = new StringBuilder();
Iterator<ProductReportData> iProductData = productData.iterator();
while (iProductData.hasNext()) {
ProductReportData prod = iProductData.next();
productString.append(prod.getProductString(longProductFormRequired));
if (iProductData.hasNext())
productString.append(",");
}
return productString;
}
public boolean isLongProductFormRequired() {
return longProductFormRequired;
}
public void setLongProductFormRequired(boolean longFormRequired) {
longProductFormRequired = longFormRequired;
}
public boolean addData(ProductReportData pData) {
longProductFormRequired |= pData.isLongProductFormRequired();
return productData.add(pData);
}
public int size() {
return productData.size();
}
}
public void addProductBySku(String itemId) {
RepositoryItem product = catalogService.getParentProduct(itemId);
String enterpriseProductId = (String) product.getPropertyValue("enterpriseProductId");
this.addProduct(enterpriseProductId, null, null, null, null);
}
public ReportTagger getAdditionalTagger() {
return additionalTagger;
}
public void setAdditionalTagger(ReportTagger additionalTagger) {
this.additionalTagger = additionalTagger;
}
public OmnitureContentTagger getContentTagger() {
return contentTagger;
}
public void setContentTagger(OmnitureContentTagger contentTagger) {
this.contentTagger = contentTagger;
}
public NGPCatalogService getCatalogService() {
return catalogService;
}
public void setCatalogService(NGPCatalogService catalogService) {
this.catalogService = catalogService;
}
public void addProduct(String itemId) {
this.addProduct(itemId, null, null, null, null);
}
}
本文出自 “格外精致” 博客,谢绝转载!