Search in sources :

Example 1 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship in project structr by structr.

the class SimpleTest method test01DOMChildren.

@Test
public void test01DOMChildren() {
    final String pageName = "page-01";
    final String pageTitle = "Page Title";
    try (final Tx tx = app.tx()) {
        Page page = Page.createNewPage(securityContext, pageName);
        if (page != null) {
            DOMElement html = (DOMElement) page.createElement("html");
            DOMElement head = (DOMElement) page.createElement("head");
            DOMElement title = (DOMElement) page.createElement("title");
            Text titleText = page.createTextNode(pageTitle);
            for (AbstractRelationship r : page.getIncomingRelationships()) {
                System.out.println("============ Relationship: " + r.toString());
                assertEquals("PAGE", r.getRelType().name());
            }
            html.appendChild(head);
            for (AbstractRelationship r : head.getIncomingRelationships()) {
                System.out.println("============ Relationship: " + r.toString());
                assertEquals("CONTAINS", r.getRelType().name());
            }
            head.appendChild(title);
            title.appendChild(titleText);
            for (AbstractRelationship r : ((DOMNode) titleText).getIncomingRelationships()) {
                System.out.println("============ Relationship: " + r.toString());
                assertEquals("CONTAINS", r.getRelType().name());
            }
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Page(org.structr.web.entity.dom.Page) Text(org.w3c.dom.Text) DOMElement(org.structr.web.entity.dom.DOMElement) DOMNode(org.structr.web.entity.dom.DOMNode) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship in project structr by structr.

the class SyncCommand method exportToFile.

// ----- static methods -----
/**
 * Exports the whole structr database to a file with the given name.
 *
 * @param graphDb
 * @param fileName
 * @param includeFiles
 * @throws FrameworkException
 */
public static void exportToFile(final DatabaseService graphDb, final String fileName, final String query, final boolean includeFiles) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
        final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
        final Set<AbstractNode> nodes = new HashSet<>();
        final Set<AbstractRelationship> rels = new HashSet<>();
        boolean conditionalIncludeFiles = includeFiles;
        if (query != null) {
            logger.info("Using Cypher query {} to determine export set, disabling export of files", query);
            conditionalIncludeFiles = false;
            final List<GraphObject> result = StructrApp.getInstance().cypher(query, null);
            for (final GraphObject obj : result) {
                if (obj.isNode()) {
                    nodes.add((AbstractNode) obj.getSyncNode());
                } else {
                    rels.add((AbstractRelationship) obj.getSyncRelationship());
                }
            }
            logger.info("Query returned {} nodes and {} relationships.", new Object[] { nodes.size(), rels.size() });
        } else {
            nodes.addAll(nodeFactory.bulkInstantiate(graphDb.getAllNodes()));
            rels.addAll(relFactory.bulkInstantiate(graphDb.getAllRelationships()));
        }
        try (final FileOutputStream fos = new FileOutputStream(fileName)) {
            exportToStream(fos, nodes, rels, null, conditionalIncludeFiles);
        }
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
        throw new FrameworkException(500, t.getMessage());
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject) FileOutputStream(java.io.FileOutputStream) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship in project structr by structr.

the class BulkCopyRelationshipPropertyCommand method execute.

@Override
public void execute(final Map<String, Object> map) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    final String sourceKey = (String) map.get("sourceKey");
    final String destKey = (String) map.get("destKey");
    if (sourceKey == null || destKey == null) {
        throw new IllegalArgumentException("This command requires one argument of type Map. Map must contain values for 'sourceKey' and 'destKey'.");
    }
    if (graphDb != null) {
        final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "CopyRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" rels
                if (rel.getProperty(GraphObject.id) != null) {
                    Class type = rel.getClass();
                    PropertyKey destPropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, destKey);
                    PropertyKey sourcePropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sourceKey);
                    try {
                        // copy properties
                        rel.setProperty(destPropertyKey, rel.getProperty(sourcePropertyKey));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to copy relationship property {} of relationship {} to {}: {}", new Object[] { sourcePropertyKey, rel.getUuid(), destPropertyKey, fex.getMessage() });
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to copy relationship properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to copy relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} nodes", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 4 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship in project structr by structr.

the class BulkSetRelationshipPropertiesCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relationshipFactory = new RelationshipFactory(securityContext);
    if (graphDb != null) {
        Iterator<AbstractRelationship> relIterator = null;
        final String typeName = "type";
        if (properties.containsKey(typeName)) {
            relIterator = StructrApp.getInstance(securityContext).relationshipQuery(SchemaHelper.getEntityClassForRawType(typeName)).getAsList().iterator();
            properties.remove(typeName);
        } else {
            relIterator = Iterables.map(relationshipFactory, graphDb.getAllRelationships()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" nodes
                if (rel.getProperty(AbstractRelationship.id) != null) {
                    for (Entry entry : properties.entrySet()) {
                        String key = (String) entry.getKey();
                        Object val = entry.getValue();
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(rel.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                rel.setProperty(propertyKey, val);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set relationship property {} of relationship {} to {}: {}", new Object[] { propertyKey, rel.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to set properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} relationships", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 5 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship in project structr by structr.

the class BulkSetUuidCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final String nodeType = (String) attributes.get("type");
    final String relType = (String) attributes.get("relType");
    final Boolean allNodes = (Boolean) attributes.get("allNodes");
    final Boolean allRels = (Boolean) attributes.get("allRels");
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    final RelationshipFactory relFactory = new RelationshipFactory(superUserContext);
    if (nodeType != null || Boolean.TRUE.equals(allNodes)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (Boolean.TRUE.equals(allNodes)) {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
            info("Start setting UUID on all nodes");
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByTypeProperty(nodeType)).iterator();
            info("Start setting UUID on nodes of type {}", new Object[] { nodeType });
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "SetNodeUuid", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(final SecurityContext securityContext, final AbstractNode node) {
                try {
                    if (node.getProperty(GraphObject.id) == null) {
                        node.unlockSystemPropertiesOnce();
                        node.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of node {}", node, fex);
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set UUID of node {}", node, t);
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on node", t);
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} nodes", count);
        return;
    }
    if (relType != null || Boolean.TRUE.equals(allRels)) {
        Iterator<AbstractRelationship> relIterator = null;
        if (Boolean.TRUE.equals(allRels)) {
            relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
            info("Start setting UUID on all rels", new Object[] { relType });
        } else {
            relIterator = Iterables.map(relFactory, graphDb.getRelationshipsByType(relType)).iterator();
            info("Start setting UUID on rels of type {}", new Object[] { relType });
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipUuid", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                try {
                    if (rel.getProperty(GraphObject.id) == null) {
                        rel.unlockSystemPropertiesOnce();
                        rel.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of relationship {}: {}", new Object[] { rel, fex.getMessage() });
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to set UUID of relationship {}: {}", rel, t.getMessage());
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on relationships {}", t.toString());
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} relationships", count);
        return;
    }
    info("Unable to determine entity type to set UUID.");
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext)

Aggregations

AbstractRelationship (org.structr.core.entity.AbstractRelationship)31 NodeInterface (org.structr.core.graph.NodeInterface)15 FrameworkException (org.structr.common.error.FrameworkException)11 AbstractNode (org.structr.core.entity.AbstractNode)9 SecurityContext (org.structr.common.SecurityContext)7 GraphObject (org.structr.core.GraphObject)6 Tx (org.structr.core.graph.Tx)6 Test (org.junit.Test)4 DatabaseService (org.structr.api.DatabaseService)4 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 ArrayList (java.util.ArrayList)3 TestOne (org.structr.core.entity.TestOne)3 PropertyKey (org.structr.core.property.PropertyKey)3 Href (org.structr.api.util.html.attr.Href)2 TestTwo (org.structr.core.entity.TestTwo)2 CreationContainer (org.structr.core.graph.CreationContainer)2 DOMNode (org.structr.web.entity.dom.DOMNode)2 FileOutputStream (java.io.FileOutputStream)1 HashSet (java.util.HashSet)1