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

com.activitystream.model.ASEvent 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;

import com.activitystream.model.aspects.AspectManager;
import com.activitystream.model.aspects.ItemsManager;
import com.activitystream.model.config.JacksonMapper;
import com.activitystream.model.core.AbstractMapElement;
import com.activitystream.model.entities.EntityReference;
import com.activitystream.model.interfaces.AspectInterface;
import com.activitystream.model.interfaces.BaseStreamElement;
import com.activitystream.model.relations.Relation;
import com.activitystream.model.relations.RelationsManager;
import com.activitystream.model.stream.CustomerEvent;
import com.activitystream.model.stream.ImportanceLevel;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.*;

public class ASEvent extends CustomerEvent {

    /**
     * Popular AS event types put here for convenience
     *
     */
    public static enum POP_TYPES {
        AS_COMMERCE_TRANSACTION_COMPLETED,
        AS_COMMERCE_CART_UPDATED,

        AS_ENTERTAINMENT_SHOW_STARTS,
        AS_ENTERTAINMENT_SHOW_ENDS,
        AS_ENTERTAINMENT_SHOW_ANNOUNCED,
        AS_ENTERTAINMENT_SHOW_PRESALE_STARTS,
        AS_ENTERTAINMENT_SHOW_ONSALE_STARTS,
        AS_ENTERTAINMENT_SEAT_ASSIGNED,
        AS_ENTERTAINMENT_SEAT_UNASSIGNED,
        AS_ENTERTAINMENT_TICKET_ISSUED,
        AS_ENTERTAINMENT_TICKET_INVALIDATED,
        AS_ENTERTAINMENT_TICKET_TRANSFERRED,
        AS_ENTERTAINMENT_TICKET_USED
    }

    public ASEvent() {
    }

    public ASEvent(POP_TYPES type, String origin, String description, ImportanceLevel importance, Object... involves) {
        this(type.toString().replaceAll("_",".").toLowerCase(), origin, description, importance, Arrays.asList(involves));
    }

    public ASEvent(String type, String origin, String description, ImportanceLevel importance, Object... involves) {
        this(type, origin, description, importance, Arrays.asList(involves));
    }

    public ASEvent(String type, String origin, String description, ImportanceLevel importance, Object involves) {
        this(new DateTime(), type, origin, description, importance, involves);
    }

    public ASEvent(String occurred_at, POP_TYPES type, String origin, String description, ImportanceLevel importance, Object... involves) {
        this(occurred_at, type.toString().replaceAll("_",".").toLowerCase(), origin, description, importance, Arrays.asList(involves));
    }

    public ASEvent(String occurred_at, String type, String origin, String description, ImportanceLevel importance, Object involves) {
        this(DateTime.parse(occurred_at), type, origin, description, importance, involves);
    }

    public ASEvent(DateTime occurred_at, String type, String origin, String description, ImportanceLevel importance, Object involves) {
        super();
        put(ASConstants.FIELD_OCCURRED_AT, occurred_at);
        put(ASConstants.FIELD_TYPE, type);
        put(ASConstants.FIELD_ORIGIN, origin);

        if (description != null) put(ASConstants.FIELD_DESCRIPTION, description);

        List allRelations = new LinkedList<>();
        if (involves != null) {
            if (involves instanceof String || involves instanceof ASEntity) {
                allRelations.add(new Relation("ACTOR", involves, this));
            } else if (involves instanceof List) {
                for (Object relation : (List) involves) {
                    if (relation instanceof Relation) {
                        ((Relation)relation).setRoot(this);
                        allRelations.add((Relation) relation);
                    } else if (relation instanceof String) {
                        allRelations.add(new Relation("ACTOR", relation, this));
                    }
                }
            } else if (involves instanceof Relation) {
                Relation relation = (Relation) involves;
                relation.setRoot(this);
                allRelations.add(relation);
            }
        }
        RelationsManager relationsManager = getRelationsManager(true);
        relationsManager.addAll(allRelations);

        if (importance != null) put(ASConstants.FIELD_IMPORTANCE, importance.ordinal());
        else put(ASConstants.FIELD_IMPORTANCE, ImportanceLevel.NOT_IMPORTANT);

        HashMap aspects = new LinkedHashMap<>();
        if (!aspects.isEmpty()) put(ASConstants.FIELD_ASPECTS, aspects);
    }

    public ASEvent(Map map, BaseStreamElement root) {
        super(map, root);
    }

    public ASEvent(Map map) {
        super(map);
    }

    /************  UtilityFunctions ************/

    public ASEvent addType(String type) {
        put(ASConstants.FIELD_TYPE, type);
        return this;
    }

    public ASEvent addOccurredAt(DateTime occurredAt) {
        put(ASConstants.FIELD_OCCURRED_AT, occurredAt);
        return this;
    }

    public ASEvent addOccurredAt(String occurredAt) {
        put(ASConstants.FIELD_OCCURRED_AT, occurredAt);
        return this;
    }

    public ASEvent addOrigin(String origin) {
        put(ASConstants.FIELD_ORIGIN, origin);
        return this;
    }

    public ASEvent addPartition(String partition) {
        put(ASConstants.FIELD_PARTITION, partition);
        return this;
    }

    public ASEvent addImportance(ImportanceLevel importance) {
        put(ASConstants.FIELD_IMPORTANCE, importance.ordinal());
        return this;
    }

    public ASEvent addImportance(Integer importance) {
        put(ASConstants.FIELD_IMPORTANCE, importance);
        return this;
    }

    public ASEvent addAspect(AspectInterface aspect) {
        if (!aspect.isEmpty()) super.addAspect(aspect, this);
        return this;
    }

    public ASEvent addRelation(String type, Object value) {
        this.getRelationsManager(true).addRelation(new Relation(type, value));
        return this;
    }

    public ASEvent addRelation(Relation relation) {
        this.getRelationsManager(true).addRelation(relation);
        return this;
    }

    public ASEvent addRelation(Relation relation, ASEntity entity) {
        if (entity != null) this.getRelationsManager(true).addRelation(relation);
        return this;
    }

    public ASEvent addDimensions(Map dimensionsMap) {
        super.addDimensions(dimensionsMap, this);
        return this;
    }

    public ASEvent addDimensions(String... dimensions) {
        super.addDimensions(this, dimensions);
        return this;
    }

    public ASEvent addDimension(String dimension, String value) {
        if (value != null && !value.isEmpty()) super.addDimension(dimension, value, this);
        return this;
    }

    @Override
    public ASEvent addMetrics(Map metricsMap, AbstractMapElement root) {
        super.addMetrics(metricsMap, root);
        return this;
    }

    @Override
    public ASEvent addMetric(AbstractMapElement root, Object... metrics) {
        super.addMetric(root, metrics);
        return this;
    }

    public ASEvent addMetric(String metric, double value) {
        super.addMetric(metric, value, this);
        return this;
    }

    @Override
    public ASEvent addMetric(String metric, double value, AbstractMapElement root) {
        super.addMetric(metric, value, root);
        return this;
    }

    public ASEvent addRelationIfValid(String type, String entityType, String entityId) {
        return addRelationIfValid(type, entityType, entityId, (Map) null, "Some");
    }

    public ASEvent addRelationIfValid(String type, String entityType, String entityId, Map relationsProperties) {
        return addRelationIfValid(type, entityType, entityId, relationsProperties, "Some");
    }

    public ASEvent addRelationIfValid(String type, String entityType, String entityId, String mustBeValue) {
        return addRelationIfValid(type, entityType, entityId, (Map) null, "Some");
    }

    public ASEvent addRelationIfValid(String type, String entityType, String entityId, Map relationsProperties, String mustBeValue) {
        //todo - mustBeValue should be better implemented
        if (!entityType.isEmpty() && entityId != null && !entityId.isEmpty() && mustBeValue != null && !mustBeValue.isEmpty()) {
            Relation newRelation = new Relation(type, new EntityReference(entityType, entityId, this), this);
            if (relationsProperties != null) {
                newRelation.directPut("properties", relationsProperties);
            }
            this.getRelationsManager(true).addRelation(newRelation);
        }
        return this;
    }

    public ASEvent addRelationIfValid(String type, EntityReference entityRef) {
        if (entityRef != null) {
            this.getRelationsManager(true).addRelation(new Relation(type, entityRef));
        }
        return this;
    }


    @Override
    public ASEvent addProperties(Object... properties) {
        super.addProperties(properties);
        return this;
    }

    @Override
    public ASEvent addProperties(String property, Object value) {
        super.addProperties(property, value);
        return this;
    }

    /*
    Utilities
     */

    public ASEvent addLineItem(ASLineItem itemLine) {
        AspectManager aspectManager = getAspectManager(true);
        ItemsManager itemsManager = (ItemsManager) aspectManager.getOrCreateAspect(ItemsManager.ASPECT_TYPE.getAspectSignature());
        itemsManager.add(itemLine);
        return this;
    }

    public ASEvent mergeLineItem(ASLineItem itemLine) {
        AspectManager aspectManager = getAspectManager(true);
        ItemsManager itemsManager = (ItemsManager) aspectManager.getOrCreateAspect(ItemsManager.ASPECT_TYPE.getAspectSignature());
        itemsManager.mergeItemLine(itemLine);
        return this;
    }

    /*
    Serialization utils
     */

    public String toJSON() throws JsonProcessingException {
        return JacksonMapper.getMapper().writeValueAsString(this);
    }

    public static ASEvent fromJSON(String json) throws IOException {
        return JacksonMapper.getMapper().readValue(json, ASEvent.class);
    }




}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy