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);
}
}
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.");
}
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();
}
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();
}
}
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");
}
}
Aggregations