use of org.structr.api.DatabaseService in project structr by structr.
the class BulkRebuildIndexCommand method rebuildNodeIndex.
// ----- private methods -----
private void rebuildNodeIndex(final String entityType) {
final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
Iterator<AbstractNode> nodeIterator = null;
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 (re-)indexing all nodes");
} else {
info("Starting (re-)indexing all nodes of type {}", entityType);
}
long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "RebuildNodeIndex", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
node.updateInIndex();
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
logger.warn("Unable to index node {}: {}", new Object[] { node, t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to index node: {}", t.getMessage());
}
});
info("Done with (re-)indexing {} nodes", count);
}
use of org.structr.api.DatabaseService in project structr by structr.
the class BulkRebuildIndexCommand method rebuildRelationshipIndex.
private void rebuildRelationshipIndex(final String relType) {
final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getRelationshipsByType(relType))).iterator();
if (relType == null) {
info("Relationship type not set, starting (re-)indexing all relationships");
} else {
info("Starting (re-)indexing all relationships of type {}", new Object[] { relType });
}
long count = bulkGraphOperation(securityContext, relIterator, 1000, "RebuildRelIndex", new BulkGraphOperation<AbstractRelationship>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
rel.updateInIndex();
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
logger.warn("Unable to index relationship {}: {}", new Object[] { rel, t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to index relationship: {}", t.getMessage());
}
});
info("Done with (re-)indexing {} relationships", count);
}
use of org.structr.api.DatabaseService in project structr by structr.
the class BulkSetNodePropertiesCommand 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);
final String type = (String) properties.get("type");
if (StringUtils.isBlank(type)) {
throw new FrameworkException(422, "Type must not be empty");
}
final Class cls = SchemaHelper.getEntityClassForRawType(type);
if (cls == null) {
throw new FrameworkException(422, "Invalid type " + type);
}
if (graphDb != null) {
final App app = StructrApp.getInstance(securityContext);
Iterator<AbstractNode> nodeIterator = null;
if (properties.containsKey(AbstractNode.type.dbName())) {
try (final Tx tx = app.tx()) {
nodeIterator = app.nodeQuery(cls).getResult().getResults().iterator();
properties.remove(AbstractNode.type.dbName());
tx.success();
}
} else {
nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
}
// remove "type" so it won't be set later
properties.remove("type");
final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "SetNodeProperties", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
// Treat only "our" nodes
if (node.getProperty(GraphObject.id) != null) {
for (Entry entry : properties.entrySet()) {
String key = (String) entry.getKey();
Object val = null;
// allow to set new type
if (key.equals("newType")) {
key = "type";
}
PropertyConverter inputConverter = StructrApp.key(cls, key).inputConverter(securityContext);
if (inputConverter != null) {
try {
val = inputConverter.convert(entry.getValue());
} catch (FrameworkException ex) {
logger.error("", ex);
}
} else {
val = entry.getValue();
}
PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
if (propertyKey != null) {
try {
node.unlockSystemPropertiesOnce();
node.setProperty(propertyKey, val);
} catch (FrameworkException fex) {
logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, node.getUuid(), val, fex.getMessage() });
}
}
}
}
}
@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);
}
logger.info("Done");
}
use of org.structr.api.DatabaseService in project structr by structr.
the class ClearDatabase method execute.
// ~--- methods --------------------------------------------------------
public void execute() throws FrameworkException {
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final NodeFactory nodeFactory = new NodeFactory(securityContext);
if (graphDb != null) {
Iterator<NodeInterface> nodeIterator = null;
final App app = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
nodeIterator = app.nodeQuery(NodeInterface.class).getAsList().iterator();
tx.success();
} catch (FrameworkException fex) {
logger.warn("Exception while creating all nodes iterator.", fex);
}
final long deletedNodes = bulkGraphOperation(securityContext, nodeIterator, 1000, "ClearDatabase", new BulkGraphOperation<NodeInterface>() {
@Override
public void handleGraphObject(SecurityContext securityContext, NodeInterface node) {
// Delete only "our" nodes
if (node.getProperty(GraphObject.id) != null) {
try {
app.delete(node);
} catch (FrameworkException fex) {
logger.warn("Unable to delete node {}: {}", new Object[] { node.getUuid(), fex.getMessage() });
}
}
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, NodeInterface node) {
logger.warn("Unable to delete node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to clear database: {}", t.getMessage());
}
});
logger.info("Finished deleting {} nodes", deletedNodes);
}
}
use of org.structr.api.DatabaseService in project structr by structr.
the class CreateNodeCommand method execute.
public T execute(final PropertyMap attributes) throws FrameworkException {
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final Principal user = securityContext.getUser(false);
T node = null;
if (graphDb != null) {
final NodeFactory<T> nodeFactory = new NodeFactory<>(securityContext);
final PropertyMap properties = new PropertyMap(attributes);
final PropertyMap toNotify = new PropertyMap();
final Object typeObject = properties.get(AbstractNode.type);
final Class nodeType = getTypeOrGeneric(typeObject);
final Set<String> labels = TypeProperty.getLabelsForType(nodeType);
final CreationContainer tmp = new CreationContainer();
final Date now = new Date();
final boolean isCreation = true;
// use user-supplied UUID?
String uuid = properties.get(GraphObject.id);
if (uuid == null) {
// no, create new one
uuid = getNextUuid();
properties.put(GraphObject.id, uuid);
} else {
// enable UUID validation
securityContext.uuidWasSetManually(true);
}
// use property keys to set property values on creation dummy
// set default values for common properties in creation query
GraphObject.id.setProperty(securityContext, tmp, uuid);
GraphObject.type.setProperty(securityContext, tmp, nodeType.getSimpleName());
AbstractNode.createdDate.setProperty(securityContext, tmp, now);
AbstractNode.lastModifiedDate.setProperty(securityContext, tmp, now);
// default property values
AbstractNode.visibleToPublicUsers.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.visibleToPublicUsers, false));
AbstractNode.visibleToAuthenticatedUsers.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.visibleToAuthenticatedUsers, false));
AbstractNode.hidden.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.hidden, false));
AbstractNode.deleted.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.deleted, false));
if (user != null) {
final String userId = user.getProperty(GraphObject.id);
AbstractNode.createdBy.setProperty(securityContext, tmp, userId);
AbstractNode.lastModifiedBy.setProperty(securityContext, tmp, userId);
}
// prevent double setting of properties
properties.remove(AbstractNode.id);
properties.remove(AbstractNode.type);
properties.remove(AbstractNode.visibleToPublicUsers);
properties.remove(AbstractNode.visibleToAuthenticatedUsers);
properties.remove(AbstractNode.hidden);
properties.remove(AbstractNode.deleted);
properties.remove(AbstractNode.lastModifiedDate);
properties.remove(AbstractNode.lastModifiedBy);
properties.remove(AbstractNode.createdDate);
properties.remove(AbstractNode.createdBy);
// move properties to creation container that can be set directly on creation
tmp.filterIndexableForCreation(securityContext, properties, tmp, toNotify);
// collect default values and try to set them on creation
for (final PropertyKey key : StructrApp.getConfiguration().getPropertySet(nodeType, PropertyView.All)) {
if (key instanceof AbstractPrimitiveProperty && !tmp.hasProperty(key.jsonName())) {
final Object defaultValue = key.defaultValue();
if (defaultValue != null) {
key.setProperty(securityContext, tmp, defaultValue);
}
}
}
node = (T) nodeFactory.instantiateWithType(createNode(graphDb, user, labels, tmp.getData()), nodeType, null, isCreation);
if (node != null) {
TransactionCommand.nodeCreated(user, node);
securityContext.disableModificationOfAccessTime();
node.setProperties(securityContext, properties);
securityContext.enableModificationOfAccessTime();
// ensure modification callbacks are called (necessary for validation)
for (final Entry<PropertyKey, Object> entry : toNotify.entrySet()) {
final PropertyKey key = entry.getKey();
final Object value = entry.getValue();
if (!key.isUnvalidated()) {
TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) node, key, null, value);
}
}
properties.clear();
// ensure indexing of newly created node
node.addToIndex();
// invalidate UUID cache
StructrApp.invalidate(uuid);
}
}
if (node != null) {
// notify node of its creation
node.onNodeCreation();
// iterate post creation transformations
final Set<Transformation<GraphObject>> transformations = StructrApp.getConfiguration().getEntityCreationTransformations(node.getClass());
for (Transformation<GraphObject> transformation : transformations) {
transformation.apply(securityContext, node);
}
}
return node;
}
Aggregations