use of org.structr.core.GraphObject in project structr by structr.
the class NoneExpression method evaluate.
@Override
public Object evaluate(final ActionContext ctx, final GraphObject entity) throws FrameworkException, UnlicensedException {
if (listExpression == null) {
return ERROR_MESSAGE_ALL;
}
final Object listSource = listExpression.evaluate(ctx, entity);
if (listSource != null && listSource instanceof List) {
final List source = (List) listSource;
final Object oldDataValue = ctx.getConstant("data");
for (Object obj : source) {
ctx.setConstant("data", obj);
final Object resultObject = noneExpression.evaluate(ctx, entity);
if (resultObject != null) {
if (resultObject instanceof Boolean) {
if ((Boolean) resultObject) {
return false;
}
} else {
if (Boolean.valueOf(resultObject.toString())) {
return false;
}
}
}
}
ctx.setConstant("data", oldDataValue);
}
return true;
}
use of org.structr.core.GraphObject in project structr by structr.
the class SliceExpression method evaluate.
@Override
public Object evaluate(final ActionContext ctx, final GraphObject entity) throws FrameworkException, UnlicensedException {
if (listExpression == null || startExpression == null || endExpression == null) {
return ERROR_MESSAGE_SLICE;
}
// evaluate start and end bounds
final Object startObject = startExpression.evaluate(ctx, entity);
final Object endObject = endExpression.evaluate(ctx, entity);
if (startObject == null) {
throw new FrameworkException(422, "Error in slice(): invalid start of range: null");
}
if (endObject == null) {
throw new FrameworkException(422, "Error in slice(): invalid end of range: null");
}
final Integer start = toNumber(startObject);
Integer end = toNumber(endObject);
boolean valid = true;
// null => number format parsing error or invalid source data
if (start == null || end == null) {
return null;
}
// check bounds BEFORE evaluating list expression
if (start < 0) {
valid = false;
logger.warn("Error in slice(): start index must be >= 0.");
}
if (end < 0) {
valid = false;
logger.warn("Error in slice(): end index must be > 0.");
}
if (start >= end) {
valid = false;
logger.warn("Error in slice(): start index must be < end index.");
}
if (valid) {
if (listExpression instanceof QueryFunction) {
final QueryFunction queryFunction = (QueryFunction) listExpression;
queryFunction.setRangeStart(start);
queryFunction.setRangeEnd(end);
return listExpression.evaluate(ctx, entity);
} else if (listExpression instanceof FunctionExpression && ((FunctionExpression) listExpression).getFunction() instanceof QueryFunction) {
final QueryFunction queryFunction = (QueryFunction) ((FunctionExpression) listExpression).getFunction();
queryFunction.setRangeStart(start);
queryFunction.setRangeEnd(end);
return listExpression.evaluate(ctx, entity);
} else {
final Object src = listExpression.evaluate(ctx, entity);
List list = null;
// handle list argument
if (src instanceof List) {
list = (List) src;
// handle array argument
} else if (isArray(src)) {
list = toList((Object[]) src);
// handle collection argument
} else if (src != null) {
list = new LinkedList((Collection) src);
} else {
return null;
}
if (start > list.size()) {
valid = false;
logger.warn("Error in slice(): start index is out of range.");
}
if (end > list.size()) {
end = list.size();
}
if (valid) {
return list.subList(start, end);
}
}
}
return null;
}
use of org.structr.core.GraphObject in project structr by structr.
the class AbstractPrimitiveProperty method setProperty.
@Override
public Object setProperty(final SecurityContext securityContext, final GraphObject obj, final T value) throws FrameworkException {
final PropertyConverter converter = databaseConverter(securityContext, PropertyMap.unwrap(obj));
Object convertedValue = value;
if (converter != null) {
convertedValue = converter.convert(value);
}
// use transformators from property
for (final String fqcn : transformators) {
// first test, use caching here later..
final Transformer transformator = getTransformator(fqcn);
if (transformator != null) {
convertedValue = transformator.setProperty(PropertyMap.unwrap(obj), this, convertedValue);
}
}
final PropertyContainer propertyContainer = obj.getPropertyContainer();
if (propertyContainer != null) {
if (!TransactionCommand.inTransaction()) {
throw new NotInTransactionException("setProperty outside of transaction");
}
boolean internalSystemPropertiesUnlocked = (obj instanceof CreationContainer);
// collect modified properties
if (obj instanceof AbstractNode) {
if (!unvalidated) {
TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
}
internalSystemPropertiesUnlocked = ((AbstractNode) obj).internalSystemPropertiesUnlocked;
} else if (obj instanceof AbstractRelationship) {
if (!unvalidated) {
TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
}
internalSystemPropertiesUnlocked = ((AbstractRelationship) obj).internalSystemPropertiesUnlocked;
}
// catch all sorts of errors and wrap them in a FrameworkException
try {
// save space
if (convertedValue == null) {
propertyContainer.removeProperty(dbName());
} else {
if (!isSystemInternal() || internalSystemPropertiesUnlocked) {
propertyContainer.setProperty(dbName(), convertedValue);
} else {
logger.warn("Tried to set internal system property {} to {}. Action was denied.", new Object[] { dbName(), convertedValue });
}
}
updateAccessInformation(securityContext, propertyContainer);
} catch (final RetryException rex) {
// don't catch RetryException here
throw rex;
} catch (Throwable t) {
// throw FrameworkException with the given cause
final FrameworkException fex = new FrameworkException(500, "Unable to set property " + jsonName() + " on entity with ID " + obj.getUuid() + ": " + t.toString());
fex.initCause(t);
throw fex;
}
if (isIndexed()) {
// work
if (!isPassivelyIndexed()) {
index(obj, convertedValue);
}
}
}
return null;
}
use of org.structr.core.GraphObject in project structr by structr.
the class AbstractPrimitiveProperty method getProperty.
@Override
public T getProperty(final SecurityContext securityContext, final GraphObject obj, final boolean applyConverter, final Predicate<GraphObject> predicate) {
Object value = null;
final PropertyContainer propertyContainer = obj.getPropertyContainer();
if (propertyContainer != null) {
// this may throw a java.lang.IllegalStateException: Relationship[<id>] has been deleted in this tx
if (propertyContainer.hasProperty(dbName())) {
value = propertyContainer.getProperty(dbName());
}
}
if (applyConverter) {
// apply property converters
PropertyConverter converter = databaseConverter(securityContext, PropertyMap.unwrap(obj));
if (converter != null) {
try {
value = converter.revert(value);
} catch (Throwable t) {
logger.warn("Unable to convert property {} of type {}: {}", new Object[] { dbName(), getClass().getSimpleName(), t });
logger.warn("", t);
}
}
}
// use transformators from property
for (final String fqcn : transformators) {
// first test, use caching here later..
final Transformer transformator = getTransformator(fqcn);
if (transformator != null) {
value = transformator.getProperty(PropertyMap.unwrap(obj), this, value);
}
}
// no value found, use schema default
if (value == null) {
value = defaultValue();
}
return (T) value;
}
use of org.structr.core.GraphObject in project structr by structr.
the class AggregatorProperty method getProperty.
@Override
public List<T> getProperty(SecurityContext securityContext, GraphObject currentObject, boolean applyConverter, final Predicate<GraphObject> predicate) {
if (currentObject != null && currentObject instanceof AbstractNode) {
NodeInterface sourceNode = (NodeInterface) currentObject;
List<NodeInterface> nodes = new LinkedList<>();
// 1. step: add all nodes
for (Property property : aggregation.getAggregationProperties()) {
Object obj = sourceNode.getProperty(property);
if (obj != null && obj instanceof Collection) {
nodes.addAll((Collection) obj);
}
}
// 2. step: sort nodes according to comparator
Comparator<NodeInterface> comparator = aggregation.getComparator();
if (nodes.isEmpty() && comparator != null) {
Collections.sort(nodes, comparator);
}
// 3. step: apply notions depending on type
List results = new LinkedList();
try {
for (NodeInterface node : nodes) {
Notion notion = aggregation.getNotionForType(node.getClass());
if (notion != null) {
results.add(notion.getAdapterForGetter(securityContext).adapt(node));
} else {
results.add(node);
}
}
} catch (Throwable t) {
logger.warn("", t);
}
return results;
}
return Collections.emptyList();
}
Aggregations