Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.activitystream.model.core.AbstractMapElement Maven / Gradle / Ivy
Go to download
AS-SDK is a java library to allow easy interoperability with Activity Stream.
package com.activitystream.model.core;
import com.activitystream.model.ASConstants;
import com.activitystream.model.validation.MessageValidator;
import com.activitystream.model.aspects.*;
import com.activitystream.model.entities.BusinessEntity;
import com.activitystream.model.interfaces.*;
import com.activitystream.model.security.SecurityScope;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public abstract class AbstractMapElement extends LinkedHashMap implements BaseStreamElement {
private static final Logger logger = LoggerFactory.getLogger(AbstractMapElement.class);
public BaseStreamElement root = null;
private MessageValidator messageValidator;
protected SecurityScope securityScope = null;
boolean isNew = false;
public AbstractMapElement() {
}
public AbstractMapElement(Map values, BaseStreamElement root) {
this.root = root;
if (values != null) setMapValues(values);
}
/************ Utility Functions ************/
@Override
public void setSecurityScope(SecurityScope scope) {
this.securityScope = scope;
}
public void setMapValues(Map values) {
if (values != null) {
values.forEach((key, value) -> {
if (!key.equals("relations")) put(key, value);
});
//Load the relations after everything else
values.forEach((key, value) -> {
if (key.equals("relations")) put(key, value);
});
verify();
}
}
@Override
public boolean traverse(ElementVisitor visitor) {
if (!visitor.visit(this))
return false;
for (Object entry : values()) {
if (entry instanceof LinkedElement) {
if (!((LinkedElement) entry).traverse(visitor))
return false;
}
}
return true;
}
@Override
public void addEntityReferences(BaseStreamElement baseStreamElement) {
}
/**
* Calculate a footprint for the Stream Item
* The Footprint is used to calculate a unique Stream ID
*/
public String getFootprint() {
//Todo - this should be overwritten
return null;
}
/************ Validation and Error Handling ************/
@Override
public void verify() {
validator().markAsVerified();
}
@Override
public boolean isValid() {
if (validator().getAllMessageExceptions() == null) this.verify();
if (validator().hasErrors() > 0) super.put("_warnings", validator().getAllMessageExceptions());
return validator().hasErrors() < 1;
}
public boolean isValid(boolean rerun) {
if (rerun) {
validator().reset();
remove("_warnings");
}
return isValid();
}
@Override
public MessageValidator validator() {
if (messageValidator == null) {
if (root != null && root != this)
messageValidator = root.validator();
else
messageValidator = new MessageValidator();
}
return messageValidator;
}
/************ Access ************/
protected CharSequence getCharSequence(String property) {
return StringUtils.defaultString((String) get(property));
}
public boolean isNew() {
return isNew;
}
public void setNew(boolean aNew) {
isNew = aNew;
}
@Override
public void setRoot(BaseStreamElement root) {
this.root = root;
}
@Override
public BaseStreamElement getRoot() {
return root;
}
@Override
public BusinessEntity getRootBusinessEntity() {
BusinessEntity topMostEntity = null;
BaseStreamElement topMost = (root != null) ? root : this;
while (topMost != null) {
if (topMost instanceof BusinessEntity) {
topMostEntity = (BusinessEntity) topMost;
}
topMost = topMost.getRoot();
}
return topMostEntity;
}
/************ Persistence ************/
/*
public SavableElement save(Tenant tenant, PersistenceCache cache) {
return null;
}
*/
protected Map getMapChanges(Map previousAspectMap, Map newAspectMap) {
if (previousAspectMap != null && !previousAspectMap.isEmpty()) {
Set removedKeys = new LinkedHashSet<>(previousAspectMap.keySet());
removedKeys.removeAll(newAspectMap.keySet());
Set addedKeys = new LinkedHashSet<>(newAspectMap.keySet());
addedKeys.removeAll(previousAspectMap.keySet());
Set> changedEntries = new HashSet<>(newAspectMap.entrySet());
changedEntries.removeAll(previousAspectMap.entrySet());
if (!addedKeys.isEmpty() || !removedKeys.isEmpty() || !changedEntries.isEmpty()) {
return new LinkedHashMap() {{
put("added", addedKeys);
put("removed", removedKeys);
put("changed", changedEntries);
put("was", previousAspectMap.entrySet());
}};
}
} else if (newAspectMap != null && !newAspectMap.isEmpty()) { //Everything is new
return new LinkedHashMap() {{
put("added", newAspectMap.keySet());
put("was", null);
}};
}
return null;
}
/************ Utility Functions ************/
public boolean hasAspects() {
return containsKey(ASConstants.FIELD_ASPECTS);
}
public AspectManager getAspectManager(boolean store, AbstractMapElement root) {
AspectManager aspectManager = (AspectManager) get(ASConstants.FIELD_ASPECTS);
if (aspectManager == null) {
aspectManager = new AspectManager((Map) null, root);
if (store) putIfAbsent(ASConstants.FIELD_ASPECTS, aspectManager);
}
return aspectManager;
}
public AspectManager getAspectManager(boolean store) {
return getAspectManager(store, this);
}
public AspectManager getAspectManager() {
return getAspectManager(false, null);
}
protected Object addMetrics(Map metricsMap, AbstractMapElement root) {
metricsMap.forEach((metric, value) -> addMetric(metric, value, root));
return this;
}
protected Object addMetric(AbstractMapElement root, Object... metrics) {
for (int i = 0; i < metrics.length; i = i + 2) addMetric((String) metrics[i], (Double) metrics[i + 1], root);
return this;
}
protected Object addMetric(String metric, double value, AbstractMapElement root) {
MetricsAspect metricAspect = getAspectManager(true, root).getMetrics();
if (metricAspect == null) {
metricAspect = new MetricsAspect() {{
put(metric, value);
}};
getAspectManager().put(ASConstants.ASPECTS_METRICS, metricAspect);
} else {
metricAspect.put(metric, value);
}
return this;
}
protected Object addDimensions(Map dimensionsMap, AbstractMapElement root) {
dimensionsMap.forEach((dimension, value) -> {
if (value instanceof String) addDimension(dimension, (String) value, root);
});
return this;
}
protected Object addDimensions(AbstractMapElement root, String... dimensions) {
for (int i = 0; i < dimensions.length; i = i + 2) {
if (dimensions[i + 1] != null && !dimensions[i + 1].isEmpty()) addDimension(dimensions[i], dimensions[i + 1], root);
}
return this;
}
protected Object addDimension(String dimension, String value, AbstractMapElement root) {
DimensionsAspect dimensionsAspect = getAspectManager(true, root).getDimensions();
if (dimensionsAspect == null) {
dimensionsAspect = new DimensionsAspect() {{
put(dimension, value);
}};
getAspectManager().put(ASConstants.ASPECTS_DIMENSIONS, dimensionsAspect);
} else {
dimensionsAspect.put(dimension, value);
}
return this;
}
protected Object addAspect(AspectInterface aspect, AbstractMapElement root) {
AspectManager aspectManager = getAspectManager(true, root);
aspect.setRoot(root);
aspectManager.putAspect(aspect);
return this;
}
protected Object addPresentation(String label, String thumbnail, String icon, String description, String detailsUrl, AbstractMapElement root) {
PresentationAspect presentationAspect = getAspectManager(true, root).getPresentation();
if (presentationAspect == null) {
presentationAspect = new PresentationAspect(label, thumbnail, icon, description, detailsUrl, null);
getAspectManager().put(ASConstants.ASPECTS_PRESENTATION, presentationAspect);
} else {
presentationAspect.put(ASConstants.FIELD_LABEL, label);
presentationAspect.put(ASConstants.FIELD_THUMBNAIL, thumbnail);
presentationAspect.put(ASConstants.FIELD_ICON, icon);
presentationAspect.put(ASConstants.FIELD_DESCRIPTION, description);
presentationAspect.put(ASConstants.FIELD_DETAILS_URL, detailsUrl);
}
return this;
}
public Object addProperties(Object... properties) {
for (int i = 0; i < properties.length; i = i + 2) addProperties((String) properties[i], properties[i + 1]);
return this;
}
public Object addProperties(String property, Object value) {
Map properties = getProperties();
if (properties == null) {
properties = new LinkedHashMap() {{
put(property, value);
}};
put(ASConstants.FIELD_PROPERTIES, properties);
} else {
properties.put(property, value);
}
return this;
}
public Map getProperties() {
return (Map) get(ASConstants.FIELD_PROPERTIES);
}
public Map getPropertiesWithJsonValues() {
return null; //todo - impliment with serializer //(Map) get(ASConstants.FIELD_PROPERTIES);
}
public Map getProperties(boolean create) {
Map properties = (Map) get(ASConstants.FIELD_PROPERTIES);
if (create && properties == null) {
properties = new LinkedHashMap();
put(ASConstants.FIELD_PROPERTIES, properties);
}
return properties;
}
}