Search in sources :

Example 1 with DatabaseService

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

the class SyncCommand method execute.

@Override
public void execute(final Map<String, Object> attributes) throws FrameworkException {
    DatabaseService graphDb = Services.getInstance().getService(NodeService.class).getGraphDb();
    String mode = (String) attributes.get("mode");
    String fileName = (String) attributes.get("file");
    String validate = (String) attributes.get("validate");
    String query = (String) attributes.get("query");
    Long batchSize = (Long) attributes.get("batchSize");
    boolean doValidation = true;
    // should we validate imported nodes?
    if (validate != null) {
        try {
            doValidation = Boolean.valueOf(validate);
        } catch (Throwable t) {
            logger.warn("Unable to parse value for validation flag: {}", t.getMessage());
        }
    }
    if (fileName == null) {
        throw new FrameworkException(400, "Please specify sync file.");
    }
    if ("export".equals(mode)) {
        exportToFile(graphDb, fileName, query, true);
    } else if ("exportDb".equals(mode)) {
        exportToFile(graphDb, fileName, query, false);
    } else if ("import".equals(mode)) {
        importFromFile(graphDb, securityContext, fileName, doValidation, batchSize);
    } else {
        throw new FrameworkException(400, "Please specify sync mode (import|export).");
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) DatabaseService(org.structr.api.DatabaseService)

Example 2 with DatabaseService

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

the class TransactionCommand method beginTx.

public TransactionCommand beginTx() throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    TransactionReference tx = transactions.get();
    if (graphDb != null) {
        if (tx == null) {
            try {
                // start new transaction
                tx = new TransactionReference(graphDb.beginTx());
            } catch (NetworkException nex) {
                throw new DatabaseServiceNetworkException(503, nex.getMessage());
            }
            queues.set(new ModificationQueue());
            buffers.set(new ErrorBuffer());
            transactions.set(tx);
            currentCommand.set(this);
        }
        // increase depth
        tx.begin();
    } else {
        throw new DatabaseServiceNotAvailableException(503, "Database service is not available, ensure the database is running and that there is a working network connection to it");
    }
    return this;
}
Also used : ErrorBuffer(org.structr.common.error.ErrorBuffer) DatabaseServiceNotAvailableException(org.structr.common.error.DatabaseServiceNotAvailableException) DatabaseService(org.structr.api.DatabaseService) NetworkException(org.structr.api.NetworkException) DatabaseServiceNetworkException(org.structr.common.error.DatabaseServiceNetworkException) DatabaseServiceNetworkException(org.structr.common.error.DatabaseServiceNetworkException)

Example 3 with DatabaseService

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

the class DeleteRelationshipCommand method execute.

public Object execute(final RelationshipInterface rel, final boolean passiveDeletion) {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    if (graphDb != null && rel != null) {
        if (rel.getProperty(AbstractRelationship.id) == null) {
            logger.warn("Will not delete relationship which has no UUID: {} --[:{}]-->{}", new Object[] { rel.getSourceNode(), rel.getType(), rel.getTargetNode() });
            return null;
        }
        final Relationship relToDelete = rel.getRelationship();
        final RelationshipInterface finalRel = rel;
        TransactionCommand.relationshipDeleted(securityContext.getCachedUser(), finalRel, passiveDeletion);
        // callback
        finalRel.onRelationshipDeletion();
        // remove object from index
        finalRel.removeFromIndex();
        // delete node in database
        relToDelete.delete();
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) DatabaseService(org.structr.api.DatabaseService)

Example 4 with DatabaseService

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

the class FindRelationshipCommand method execute.

public T execute(String idString) throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    RelationshipFactory<T> relationshipFactory = new RelationshipFactory<>(securityContext);
    if (graphDb != null) {
        // single string value, try to parse to long
        try {
            return relationshipFactory.instantiate(graphDb.getRelationshipById(Long.parseLong(idString)));
        } catch (NumberFormatException ex) {
            // failed :(
            logger.debug("Could not parse {} to number", idString);
        }
    }
    return null;
}
Also used : DatabaseService(org.structr.api.DatabaseService)

Example 5 with DatabaseService

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

the class FindRelationshipCommand method execute.

public T execute(Long id) throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    RelationshipFactory<T> relationshipFactory = new RelationshipFactory<>(securityContext);
    if (graphDb != null) {
        return relationshipFactory.instantiate(graphDb.getRelationshipById(id));
    }
    return null;
}
Also used : DatabaseService(org.structr.api.DatabaseService)

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