Search in sources :

Example 96 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class GraphObject method setProperties.

/**
 * Sets the given properties.
 *
 * @param securityContext
 * @param properties
 * @throws FrameworkException
 */
default void setProperties(final SecurityContext securityContext, final PropertyMap properties) throws FrameworkException {
    final CreationContainer container = new CreationContainer(this);
    boolean atLeastOnePropertyChanged = false;
    for (final Entry<PropertyKey, Object> attr : properties.entrySet()) {
        final PropertyKey key = attr.getKey();
        final Object value = attr.getValue();
        if (key.indexable(value)) {
            final Object oldValue = getProperty(key);
            if (!value.equals(oldValue)) {
                atLeastOnePropertyChanged = true;
                // bulk set possible, store in container
                key.setProperty(securityContext, container, value);
                if (isNode()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                } else if (isRelationship()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                }
            }
        } else {
            // bulk set NOT possible, set on entity
            if (key.isSystemInternal()) {
                unlockSystemPropertiesOnce();
            }
            setProperty(key, value);
        }
    }
    if (atLeastOnePropertyChanged) {
        // set primitive values directly for better performance
        getPropertyContainer().setProperties(container.getData());
    }
}
Also used : TypeProperty(org.structr.core.property.TypeProperty) AbstractRelationship(org.structr.core.entity.AbstractRelationship) CreationContainer(org.structr.core.graph.CreationContainer) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 97 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class StructrCsvModuleTest method createTestRelationships.

protected <T extends Relation> List<T> createTestRelationships(final Class<T> relType, final int number) throws FrameworkException {
    List<GenericNode> nodes = createTestNodes(GenericNode.class, 2);
    final NodeInterface startNode = nodes.get(0);
    final NodeInterface endNode = nodes.get(1);
    try (final Tx tx = app.tx()) {
        List<T> rels = new LinkedList<>();
        for (int i = 0; i < number; i++) {
            rels.add((T) app.create(startNode, endNode, relType));
        }
        tx.success();
        return rels;
    }
}
Also used : Tx(org.structr.core.graph.Tx) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 98 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class StructrDataFeedsModuleTest method cleanDatabase.

@Before
public void cleanDatabase() {
    try (final Tx tx = app.tx()) {
        final List<? extends NodeInterface> nodes = app.nodeQuery().getAsList();
        logger.info("Cleaning database: {} nodes", nodes.size());
        for (final NodeInterface node : nodes) {
            app.delete(node);
        }
        // delete remaining nodes without UUIDs etc.
        app.cypher("MATCH (n)-[r]-(m) DELETE n, r, m", Collections.emptyMap());
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Exception while trying to clean database: {}", fex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Before(org.junit.Before)

Example 99 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class SourcePattern method extract.

@Export
public void extract(final Map<String, Object> parameters) throws FrameworkException {
    final SourcePage page = getProperty(sourcePageProperty);
    if (page == null) {
        throw new FrameworkException(422, "Pattern has no source page, exiting.");
    }
    final String selector = getProperty(selectorProperty);
    if (selector == null) {
        throw new FrameworkException(422, "Pattern has no selector, exiting.");
    }
    final Long from = getProperty(fromProperty);
    final Long to = getProperty(toProperty);
    final List<SourcePattern> subPatterns = getProperty(subPatternsProperty);
    Document doc = null;
    NodeInterface parentObj = null;
    if (parameters.containsKey("object")) {
        parentObj = (NodeInterface) parameters.get("object");
    }
    if (parameters.containsKey("document")) {
        doc = (Document) parameters.get("document");
    } else {
        final String url = page.getProperty(SourcePage.url);
        if (url == null) {
            throw new FrameworkException(422, "This pattern's source page has no URL, exiting.");
        }
        // Get the content from the URL
        final String content = getContent(url);
        // Parse the document with Jsoup and extract the elements matched by the given selector
        doc = Jsoup.parse(content);
    }
    final String mappedType = getProperty(mappedTypeProperty);
    if (mappedType == null) {
        throw new FrameworkException(422, "No mapped type given, exiting.");
    }
    final Elements parts = doc.select(selector);
    // Loop through all elements found for this pattern; if a start index is given, start at this element
    for (int i = (from != null ? from.intValue() : 1); i <= (to != null ? to : parts.size()); i++) {
        // If no object was given (from a higher-level pattern), create a new object of the given type
        final NodeInterface obj = (parentObj == null ? create(mappedType) : parentObj);
        if (subPatterns.size() > 0) {
            // Loop through the sub patterns of this pattern
            for (final SourcePattern subPattern : subPatterns) {
                final String subSelector = selector + ":nth-child(" + i + ") > " + subPattern.getProperty(SourcePattern.selectorProperty);
                final String subPatternMappedAttribute = subPattern.getProperty(SourcePattern.mappedAttributeProperty);
                final String subPatternMappedAttributeFunction = subPattern.getProperty(SourcePattern.mappedAttributeFunctionProperty);
                final SourcePage subPatternSubPage = subPattern.getProperty(SourcePattern.subPageProperty);
                extractAndSetValue(obj, doc, subSelector, mappedType, subPatternMappedAttribute, subPatternMappedAttributeFunction, subPatternSubPage);
            }
        } else {
            final String mappedAttribute = getProperty(mappedAttributeProperty);
            final String mappedAttributeFunction = getProperty(mappedAttributeFunctionProperty);
            extractAndSetValue(obj, doc, selector, mappedType, mappedAttribute, mappedAttributeFunction, null);
        }
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) NodeInterface(org.structr.core.graph.NodeInterface) Export(org.structr.core.Export)

Example 100 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class SearchCommand method getAllSubtypesAsStringSet.

public static synchronized Set<String> getAllSubtypesAsStringSet(final String type) {
    Set<String> allSubtypes = subtypeMapForType.get(type);
    if (allSubtypes == null) {
        logger.debug("Subtype map cache miss.");
        allSubtypes = new LinkedHashSet<>();
        subtypeMapForType.put(type, allSubtypes);
        final ConfigurationProvider configuration = StructrApp.getConfiguration();
        final Map<String, Class<? extends NodeInterface>> nodeEntities = configuration.getNodeEntities();
        final Map<String, Class<? extends RelationshipInterface>> relEntities = configuration.getRelationshipEntities();
        // add type first (this is neccesary because two class objects of the same dynamic type node are not equal
        // to each other and not assignable, if the schema node was modified in the meantime)
        allSubtypes.add(type);
        // scan all node entities for subtypes
        for (final Map.Entry<String, Class<? extends NodeInterface>> entity : nodeEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
        // scan all relationship entities for subtypes
        for (final Map.Entry<String, Class<? extends RelationshipInterface>> entity : relEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
    } else {
        logger.debug("Subtype map cache hit.");
    }
    return Collections.unmodifiableSet(allSubtypes);
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) RelationshipInterface(org.structr.core.graph.RelationshipInterface) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12