use of org.structr.common.SecurityContext in project structr by structr.
the class BulkDeleteSoftDeletedNodesCommand 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, true, false);
if (graphDb != null) {
final Iterator<AbstractNode> nodeIterator = Iterables.map(nodeFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getAllNodes())).iterator();
final boolean erase;
if (properties.containsKey("erase") && properties.get("erase").equals("true")) {
erase = true;
} else {
erase = false;
}
bulkGraphOperation(securityContext, nodeIterator, 1000, "DeleteSoftDeletedNodes", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
if (node.isDeleted()) {
logger.info("Found deleted node: {}", node);
if (erase) {
try {
StructrApp.getInstance(securityContext).delete(node);
} catch (FrameworkException ex) {
logger.warn("Could not delete node " + node, ex);
}
}
}
}
@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("Done");
}
use of org.structr.common.SecurityContext in project structr by structr.
the class BulkFixNodePropertiesCommand method execute.
@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
final String propertyName = (String) attributes.get("name");
final String entityTypeName = (String) attributes.get("type");
if (entityTypeName != null) {
final Class type = SchemaHelper.getEntityClassForRawType(entityTypeName);
if (type != null) {
final DatabaseService db = StructrApp.getInstance(securityContext).getDatabaseService();
final NodeFactory factory = new NodeFactory(securityContext);
final Iterator<AbstractNode> nodeIterator = Iterables.map(factory, db.getNodesByLabel(entityTypeName)).iterator();
logger.info("Trying to fix properties of all {} nodes", type.getSimpleName());
long nodeCount = bulkGraphOperation(securityContext, nodeIterator, 100, "FixNodeProperties", new BulkGraphOperation<AbstractNode>() {
private void fixProperty(AbstractNode node, Property propertyToFix) {
Node databaseNode = node.getNode();
if (databaseNode.hasProperty(propertyToFix.dbName())) {
// check value with property converter
PropertyConverter converter = propertyToFix.databaseConverter(securityContext, node);
if (converter != null) {
try {
Object value = databaseNode.getProperty(propertyToFix.dbName());
converter.revert(value);
} catch (ClassCastException cce) {
// exception, needs fix
String databaseName = propertyToFix.dbName();
Object databaseValue = databaseNode.getProperty(databaseName);
Object correctedValue = propertyToFix.fixDatabaseProperty(databaseValue);
if (databaseValue != null && correctedValue != null) {
try {
// try to set database value to corrected value
databaseNode.setProperty(databaseName, correctedValue);
} catch (Throwable t) {
logger.warn("Unable to fix property {} of {} with UUID {} which is of type {}", new Object[] { propertyToFix.dbName(), type.getSimpleName(), node.getUuid(), databaseValue != null ? databaseValue.getClass() : "null" });
}
}
} catch (Throwable t) {
// log exceptions of other types
logger.warn("", t);
}
}
}
}
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
if (propertyName != null) {
PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, propertyName);
if (key != null) {
// needs type cast to Property to use fixDatabaseProperty method
if (key instanceof Property) {
fixProperty(node, (Property) key);
}
}
} else {
for (PropertyKey key : node.getPropertyKeys(PropertyView.All)) {
// needs type cast to Property to use fixDatabaseProperty method
if (key instanceof Property) {
fixProperty(node, (Property) key);
}
}
}
}
});
logger.info("Fixed {} nodes", nodeCount);
return;
}
}
logger.info("Unable to determine property and/or entity type to fix.");
}
use of org.structr.common.SecurityContext 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.common.SecurityContext 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.common.SecurityContext 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");
}
Aggregations