use of org.structr.api.DatabaseService in project structr by structr.
the class FindRelationshipCommand method execute.
// ~--- methods --------------------------------------------------------
public T execute(Relationship relationship) throws FrameworkException {
DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
RelationshipFactory<T> relationshipFactory = new RelationshipFactory<>(securityContext);
if (graphDb != null) {
return relationshipFactory.instantiate(relationship);
}
return (null);
}
use of org.structr.api.DatabaseService in project structr by structr.
the class GetAllNodes method execute.
public List<AbstractNode> execute() throws FrameworkException {
DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
NodeFactory nodeFactory = new NodeFactory(securityContext);
if (graphDb != null) {
return nodeFactory.bulkInstantiate(graphDb.getAllNodes());
}
return Collections.emptyList();
}
use of org.structr.api.DatabaseService in project structr by structr.
the class BulkChangeNodePropertyKeyCommand method execute.
// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
final NodeFactory nodeFactory = new NodeFactory(superUserContext);
String type = null;
final String oldKey = (String) properties.get("oldKey");
final String newKey = (String) properties.get("newKey");
if (graphDb != null && StringUtils.isNotBlank(oldKey) && StringUtils.isNotBlank(newKey)) {
Iterator<AbstractNode> nodeIterator = null;
if (properties.containsKey(AbstractNode.type.dbName())) {
type = (String) properties.get(AbstractNode.type.dbName());
nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByLabel(type)).iterator();
properties.remove(AbstractNode.type.dbName());
} else {
nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
}
final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "ChangeNodePropertyKey", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
for (Entry entry : properties.entrySet()) {
String key = (String) entry.getKey();
PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
if (propertyKey != null) {
Node dbNode = node.getNode();
if (dbNode.hasProperty(newKey)) {
logger.error("Node {} has already a property with key {}", new Object[] { node, newKey });
throw new IllegalStateException("Node has already a property of the new key");
}
if (dbNode.hasProperty(oldKey)) {
dbNode.setProperty(newKey, dbNode.getProperty(oldKey));
dbNode.removeProperty(oldKey);
}
node.updateInIndex();
}
}
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
logger.warn("Unable to set properties of node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to set node properties: {}", t.getMessage());
}
});
logger.info("Fixed {} nodes ...", count);
} else {
logger.info("No values for oldKey and/or newKey found, aborting.");
}
logger.info("Done");
}
use of org.structr.api.DatabaseService 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);
}
}
use of org.structr.api.DatabaseService in project structr by structr.
the class BulkCreateLabelsCommand method execute.
@Override
public void execute(Map<String, Object> attributes) {
final String entityType = (String) attributes.get("type");
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
final NodeFactory nodeFactory = new NodeFactory(superUserContext);
final boolean removeUnused = !attributes.containsKey("removeUnused");
final Iterator<AbstractNode> nodeIterator = Iterables.map(nodeFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getNodesByTypeProperty(entityType))).iterator();
if (entityType == null) {
info("Node type not set or no entity class found. Starting creation of labels for all nodes.");
} else {
info("Starting creation of labels for all nodes of type {}", entityType);
}
final long count = bulkGraphOperation(securityContext, nodeIterator, 10000, "CreateLabels", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
TypeProperty.updateLabels(graphDb, node, node.getClass(), removeUnused);
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
warn("Unable to create labels for node {}: {}", node, t.getMessage());
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
warn("Unable to create labels for node: {}", t.getMessage());
}
});
info("Done with creating labels on {} nodes", count);
}
Aggregations