All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.activitystream.model.aspects.ProductViewAspect Maven / Gradle / Ivy

Go to download

AS-SDK is a java library to allow easy interoperability with Activity Stream.

There is a newer version: 0.1.25
Show newest version
package com.activitystream.model.aspects;

import com.activitystream.model.ASConstants;
import com.activitystream.model.interfaces.*;
import com.activitystream.model.analytics.TimeSeriesEntry;
import com.activitystream.model.entities.EntityReference;
import com.activitystream.model.validation.AdjustedPropertyWarning;
import com.google.common.base.Splitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.function.Consumer;

public class ProductViewAspect extends AbstractMapAspect implements LinkedElement, AnalyticsElement {

    public static final AspectType ASPECT_TYPE = new AspectType(ASConstants.ASPECTS_PRODUCT_VIEW, ProductViewAspect::new) {
        /*
        @Override
        public boolean isActive(SavableElement element, BaseStreamElement root) {
            return ((OrientVertex) element).countEdges(Direction.OUT, "VIEWED") > 0;
        }
        */
    };

    protected static final Logger logger = LoggerFactory.getLogger(ProductViewAspect.class);

    final static List EXCLUDED_PARAMS = Arrays.asList(ASConstants.FIELD_TOKEN);

    public ProductViewAspect() {

    }

    @Override
    public void loadFromValue(Object value) {
        if (value instanceof String) {
            put("product", value);
        } else {
            super.loadFromValue(value);
        }
    }

    /************
     * Utility functions
     ************/

    @Override
    public void onEachEntityReference(Consumer action) {
        if (containsKey(ASConstants.FIELD_PRODUCT)) {
            action.accept(new EntityReference((String) get(ASConstants.FIELD_PRODUCT)));
        }
    }

    @Override
    public AspectType getAspectType() {
        return ASPECT_TYPE;
    }

    private String getQuery(String path) {
        try {
            URL aURL = new URL(path);
            return aURL.getQuery();
        } catch (MalformedURLException e) {
            logger.error("Bad URL", e);
        }
        return null;
    }

    private Map getQueryParameters(String query) {
        int pos = query.indexOf("?");
        if (pos > -1) query = query.substring(pos + 1);
        Map map = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(query);
        if (!map.isEmpty()) return map;
        return null;
    }

    /************  Analytical functions  ************/

    @Override
    public void addTimeSeriesDimensions(TimeSeriesEntry entry) {
        final String[] fields = new String[]{ASConstants.FIELD_PATH, ASConstants.FIELD_SECTION, ASConstants.FIELD_REFERRER, ASConstants.FIELD_SIZE,
                ASConstants.FIELD_METHOD,ASConstants.FIELD_DURATION, ASConstants.FIELD_STATUS};

        if (entry.getTimeSeriesType().equals("pageviews")) {
            Map pageView = new LinkedHashMap<>();
            for (String field : fields) {
                if (containsKey(field) && get(field) != null) pageView.put(field, get(field));
            }
            entry.put("pageview", pageView);
        }
    }

    @Override
    public void populateTimeSeriesEntry(TimeSeriesEntry entry, String context, long depth) {

    }

    /************ Assignment & Validation ************/

    @Override
    public Object put(Object key, Object value) {

        String theKey = key.toString();
        String theLCKey = theKey.toLowerCase();
        if (!theKey.equals(theLCKey)) {
            this.addProblem(new AdjustedPropertyWarning("The property name: '" + theKey + "' was converted to lower case"));
            theKey = theLCKey;
        }

        //todo - implement proper data processing

        switch (theKey) {
            case ASConstants.FIELD_PRODUCT:
                value = validator().processUtlString(theKey, value, false);
                break;
            case ASConstants.FIELD_VERSION:
                theKey = ASConstants.FIELD_VARIANT;
            case ASConstants.FIELD_VARIANT:
                value = validator().processUtlString(theKey, value, false);
                break;
            case ASConstants.FIELD_CONTEXT:
                value = validator().processUtlString(theKey, value, false);
                break;
            case ASConstants.FIELD_REFERRER:
                value = validator().processUtlString(theKey, value, false);
                break;
            default:
                logger.warn(theKey + " was not found for Product Viewed Aspect");
                //this.addException(new UnsupportedAspectError("The " +  theKey + " property is not supported for the Client IP Aspect"));
        }
        return super.put(theKey, value);
    }

    @Override
    public void verify() {

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy