Search in sources :

Example 11 with DatabaseService

use of org.structr.api.DatabaseService 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 12 with DatabaseService

use of org.structr.api.DatabaseService 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)

Example 13 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class GetAllRelationships method execute.

public List<AbstractRelationship> execute() throws FrameworkException {
    RelationshipFactory relationshipFactory = new RelationshipFactory(securityContext);
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    if (graphDb != null) {
        return relationshipFactory.bulkInstantiate(graphDb.getAllRelationships());
    }
    return Collections.emptyList();
}
Also used : DatabaseService(org.structr.api.DatabaseService)

Example 14 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class UiSyncCommand method doImport.

private void doImport(final String fileName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    final DatabaseService graphDb = app.getDatabaseService();
    SyncCommand.importFromFile(graphDb, securityContext, fileName, true);
    // import done, now the ShadowDocument needs some special care. :(
    try (final Tx tx = app.tx()) {
        final List<ShadowDocument> shadowDocuments = app.nodeQuery(ShadowDocument.class).includeDeletedAndHidden().getAsList();
        if (shadowDocuments.size() > 1) {
            final PropertyKey<List<DOMNode>> elementsKey = StructrApp.key(Page.class, "elements");
            final List<DOMNode> collectiveChildren = new LinkedList<>();
            // sort by node id (higher node ID is newer entity)
            Collections.sort(shadowDocuments, new Comparator<ShadowDocument>() {

                @Override
                public int compare(final ShadowDocument t1, final ShadowDocument t2) {
                    return Long.valueOf(t2.getId()).compareTo(t1.getId());
                }
            });
            final ShadowDocument previousShadowDoc = shadowDocuments.get(0);
            final ShadowDocument newShadowDoc = shadowDocuments.get(1);
            // collect children of both shadow documents
            collectiveChildren.addAll(previousShadowDoc.getProperty(elementsKey));
            collectiveChildren.addAll(newShadowDoc.getProperty(elementsKey));
            // delete old shadow document
            app.delete(previousShadowDoc);
            // add children to new shadow document
            newShadowDoc.setProperties(securityContext, new PropertyMap(elementsKey, collectiveChildren));
        }
        tx.success();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) List(java.util.List) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 15 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class CypherTest method test01DeleteAfterLookupWithCypherInTransaction.

@Test
public void test01DeleteAfterLookupWithCypherInTransaction() {
    try {
        final TestSix testSix = this.createTestNode(TestSix.class);
        final TestOne testOne = this.createTestNode(TestOne.class);
        SixOneOneToOne rel = null;
        assertNotNull(testSix);
        assertNotNull(testOne);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            tx.success();
        }
        assertNotNull(rel);
        DatabaseService graphDb = app.command(GraphDatabaseCommand.class).execute();
        try (final Tx tx = app.tx()) {
            NativeResult<Relationship> result = graphDb.execute("MATCH (n)<-[r:ONE_TO_ONE]-() RETURN r");
            final Iterator<Relationship> rels = result.columnAs("r");
            assertTrue(rels.hasNext());
            rels.next().delete();
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            rel.getUuid();
            fail("Accessing a deleted relationship should thow an exception.");
            tx.success();
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) NotFoundException(org.structr.api.NotFoundException) TestOne(org.structr.core.entity.TestOne) DatabaseService(org.structr.api.DatabaseService) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) GraphDatabaseCommand(org.structr.core.graph.GraphDatabaseCommand) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Aggregations

DatabaseService (org.structr.api.DatabaseService)31 SecurityContext (org.structr.common.SecurityContext)12 FrameworkException (org.structr.common.error.FrameworkException)12 AbstractNode (org.structr.core.entity.AbstractNode)8 Node (org.structr.api.graph.Node)7 GraphObject (org.structr.core.GraphObject)7 Tx (org.structr.core.graph.Tx)7 Relationship (org.structr.api.graph.Relationship)6 PropertyKey (org.structr.core.property.PropertyKey)6 StructrAndSpatialPredicate (org.structr.common.StructrAndSpatialPredicate)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 AbstractRelationship (org.structr.core.entity.AbstractRelationship)5 LinkedHashSet (java.util.LinkedHashSet)4 LinkedList (java.util.LinkedList)4 Entry (java.util.Map.Entry)4 Test (org.junit.Test)4 List (java.util.List)3 Label (org.structr.api.graph.Label)3 StructrTest (org.structr.common.StructrTest)3