Search in sources :

Example 31 with App

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);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface)

Example 32 with App

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) SourceSearchAttribute(org.structr.core.graph.search.SourceSearchAttribute) AbstractNode(org.structr.core.entity.AbstractNode) EmptySearchAttribute(org.structr.core.graph.search.EmptySearchAttribute) GraphObject(org.structr.core.GraphObject) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 33 with App

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;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Iterator(java.util.Iterator) GraphObject(org.structr.core.GraphObject)

Example 34 with App

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;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) PropertiesNotFoundToken(org.structr.common.error.PropertiesNotFoundToken) PropertyMap(org.structr.core.property.PropertyMap) TypeToken(org.structr.common.error.TypeToken) PropertyMap(org.structr.core.property.PropertyMap)

Example 35 with App

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 "";
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SecurityContext(org.structr.common.SecurityContext) Folder(org.structr.web.entity.Folder)

Aggregations

App (org.structr.core.app.App)296 StructrApp (org.structr.core.app.StructrApp)294 Tx (org.structr.core.graph.Tx)201 FrameworkException (org.structr.common.error.FrameworkException)176 LinkedList (java.util.LinkedList)60 SecurityContext (org.structr.common.SecurityContext)56 PropertyMap (org.structr.core.property.PropertyMap)41 Folder (org.structr.web.entity.Folder)38 GraphObject (org.structr.core.GraphObject)35 Principal (org.structr.core.entity.Principal)31 IOException (java.io.IOException)30 AbstractFile (org.structr.web.entity.AbstractFile)27 AbstractNode (org.structr.core.entity.AbstractNode)26 Test (org.junit.Test)24 NodeAttribute (org.structr.core.graph.NodeAttribute)24 File (org.structr.web.entity.File)23 NodeInterface (org.structr.core.graph.NodeInterface)22 SchemaNode (org.structr.core.entity.SchemaNode)19 PropertyKey (org.structr.core.property.PropertyKey)17 Map (java.util.Map)16