use of org.structr.core.app.App in project structr by structr.
the class GraphMergeHelper method merge.
/**
* Merge new nodes into original nodes including all relationships and
* properties
*
* @param <T>
* @param newNodes
* @param origNodes
* @param shadowIdPropertyKey
*/
public static <T extends NodeInterface> void merge(final Set<T> origNodes, final Set<T> newNodes, final PropertyKey shadowIdPropertyKey) {
final App app = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
// and mark all original nodes as deleted which are not contained in new nodes list anymore
for (final NodeInterface origNode : origNodes) {
origNode.setProperty(NodeInterface.deleted, true);
for (final NodeInterface newNode : newNodes) {
final String shadowId = (String) newNode.getProperty(shadowIdPropertyKey);
logger.info("New node shadow id: {}", shadowId);
if (origNode.getUuid().equals(shadowId)) {
origNode.setProperty(NodeInterface.deleted, false);
}
}
}
// Delete all original nodes which are marked as deleted
for (final NodeInterface origNode : origNodes) {
if (origNode.getProperty(NodeInterface.deleted)) {
app.delete(origNode);
}
}
// Delete all new nodes
for (final NodeInterface newNode : newNodes) {
app.delete(newNode);
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
}
use of org.structr.core.app.App in project structr by structr.
the class EntityNotionProperty method getSearchAttribute.
@Override
public SearchAttribute getSearchAttribute(SecurityContext securityContext, Occurrence occur, T searchValue, boolean exactMatch, final Query query) {
final Predicate<GraphObject> predicate = query != null ? query.toPredicate() : null;
final SourceSearchAttribute attr = new SourceSearchAttribute(occur);
final Set<GraphObject> intersectionResult = new LinkedHashSet<>();
boolean alreadyAdded = false;
try {
if (searchValue != null && !StringUtils.isBlank(searchValue.toString())) {
final App app = StructrApp.getInstance(securityContext);
final PropertyKey key = notion.getPrimaryPropertyKey();
final PropertyConverter inputConverter = key != null ? key.inputConverter(securityContext) : null;
// transform search values using input convert of notion property
final Object transformedValue = inputConverter != null ? inputConverter.convert(searchValue) : searchValue;
if (exactMatch) {
Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType()).and(key, transformedValue).getResult();
for (AbstractNode node : result.getResults()) {
switch(occur) {
case REQUIRED:
if (!alreadyAdded) {
// the first result is the basis of all subsequent intersections
intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
// the next additions are intersected with this one
alreadyAdded = true;
} else {
intersectionResult.retainAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
}
break;
case OPTIONAL:
intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
break;
case FORBIDDEN:
break;
}
}
} else {
Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType()).and(key, transformedValue, false).getResult();
// loose search behaves differently, all results must be combined
for (AbstractNode node : result.getResults()) {
intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
}
}
attr.setResult(intersectionResult);
} else {
// value in the given field
return new EmptySearchAttribute(this, null);
}
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return attr;
}
use of org.structr.core.app.App in project structr by structr.
the class EachExpression method evaluate.
@Override
public Object evaluate(final ActionContext ctx, final GraphObject entity) throws FrameworkException, UnlicensedException {
if (listExpression == null) {
return ERROR_MESSAGE_EACH;
}
Object listSource = listExpression.evaluate(ctx, entity);
if (listSource != null && listSource.getClass().isArray()) {
listSource = Arrays.asList((Object[]) listSource);
}
if (listSource != null && listSource instanceof Iterable) {
final Iterable source = (Iterable) listSource;
final Object oldDataValue = ctx.getConstant("data");
if (isBatched()) {
final App app = StructrApp.getInstance(ctx.getSecurityContext());
final Iterator iterator = source.iterator();
int count = 0;
while (iterator.hasNext()) {
try (final Tx tx = app.tx()) {
while (iterator.hasNext()) {
ctx.setConstant("data", iterator.next());
eachExpression.evaluate(ctx, entity);
if ((++count % getBatchSize()) == 0) {
break;
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn(fex.getMessage());
logger.warn(fex.toString());
fex.printStackTrace();
}
logger.debug("Committing batch after {} objects", count);
// reset count
count = 0;
}
} else {
for (Object obj : source) {
ctx.setConstant("data", obj);
eachExpression.evaluate(ctx, entity);
}
}
// restore previous value of data keyword
ctx.setConstant("data", oldDataValue);
}
return null;
}
use of org.structr.core.app.App in project structr by structr.
the class TypeAndValueDeserializationStrategy method deserialize.
@Override
public T deserialize(final SecurityContext securityContext, Class<T> type, S source, final Object context) throws FrameworkException {
final App app = StructrApp.getInstance(securityContext);
Result<T> result = Result.EMPTY_RESULT;
// default to UUID
if (propertyKey == null) {
propertyKey = GraphObject.id;
}
// create and fill input map with source object
Map<String, Object> sourceMap = new LinkedHashMap<>();
sourceMap.put(propertyKey.jsonName(), source);
// try to convert input type to java type in order to create object correctly
PropertyMap convertedSourceMap = PropertyMap.inputTypeToJavaType(securityContext, type, sourceMap);
Object convertedSource = convertedSourceMap.get(propertyKey);
if (convertedSource != null) {
// FIXME: use uuid only here?
if (convertedSource instanceof Map) {
Object value = ((Map<String, Object>) convertedSource).get(propertyKey.jsonName());
if (value != null) {
result = app.nodeQuery(type).and(propertyKey, value.toString()).getResult();
}
} else if (convertedSource instanceof GraphObject) {
final GraphObject obj = (GraphObject) convertedSource;
result = app.nodeQuery(type).and(propertyKey, obj.getProperty(propertyKey)).getResult();
} else {
result = app.nodeQuery(type).and(propertyKey, convertedSource).getResult();
}
}
// just check for existance
int resultCount = result.size();
switch(resultCount) {
case 0:
if ((convertedSource != null) && createIfNotExisting) {
// create node and return it
T newNode = app.create(type);
if (newNode != null) {
newNode.setProperty(propertyKey, convertedSource);
return newNode;
}
} else {
logger.warn("Unable to create node of type {} for property {}", new Object[] { type.getSimpleName(), propertyKey.jsonName() });
}
break;
case 1:
T obj = result.get(0);
// if(!type.getSimpleName().equals(node.getType())) {
if (!type.isAssignableFrom(obj.getClass())) {
throw new FrameworkException(422, "Node type mismatch", new TypeToken(obj.getClass(), propertyKey, type.getSimpleName()));
}
if (!convertedSourceMap.isEmpty()) {
// set properties on related node?
setProperties(securityContext, obj, convertedSourceMap);
}
return obj;
}
if (convertedSource != null) {
PropertyMap attributes = new PropertyMap();
attributes.put(propertyKey, convertedSource);
attributes.put(AbstractNode.type, type.getSimpleName());
throw new FrameworkException(404, "No node found for given properties", new PropertiesNotFoundToken(type.getSimpleName(), null, attributes));
}
return null;
}
use of org.structr.core.app.App in project structr by structr.
the class AnalyzeSourceTreeFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (!(arrayHasLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof String)) {
return null;
}
final SecurityContext securityContext = ctx.getSecurityContext();
final App app = StructrApp.getInstance(securityContext);
new JavaParserModule().analyzeSourceTree(app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), (String) sources[0]).getFirst());
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
return "";
}
Aggregations