Search in sources :

Example 1 with RemoveOp

use of org.locationtech.geogig.api.porcelain.RemoveOp in project GeoGig by boundlessgeo.

the class Remove method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    ConsoleReader console = cli.getConsole();
    // check that there is something to remove
    if (pathsToRemove.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    /*
         * Separate trees and features, and check that, if there are trees to remove, the -r
         * modifier is used
         */
    ArrayList<String> trees = new ArrayList<String>();
    Repository repository = cli.getGeogig().getRepository();
    for (String pathToRemove : pathsToRemove) {
        NodeRef.checkValidPath(pathToRemove);
        Optional<NodeRef> node = repository.command(FindTreeChild.class).setParent(repository.workingTree().getTree()).setIndex(true).setChildPath(pathToRemove).call();
        checkParameter(node.isPresent(), "pathspec '%s' did not match any feature or tree", pathToRemove);
        NodeRef nodeRef = node.get();
        if (nodeRef.getType() == TYPE.TREE) {
            checkParameter(recursive, "Cannot remove tree %s if -r is not specified", nodeRef.path());
            trees.add(pathToRemove);
        }
    }
    int featuresCount = pathsToRemove.size() - trees.size();
    /* Perform the remove operation */
    RemoveOp op = cli.getGeogig().command(RemoveOp.class);
    for (String pathToRemove : pathsToRemove) {
        op.addPathToRemove(pathToRemove);
    }
    op.setProgressListener(cli.getProgressListener()).call();
    /* And inform about it */
    if (featuresCount > 0) {
        console.print(String.format("Deleted %d feature(s)", featuresCount));
    }
    for (String tree : trees) {
        console.print(String.format("Deleted %s tree", tree));
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) Repository(org.locationtech.geogig.repository.Repository) ConsoleReader(jline.console.ConsoleReader) ArrayList(java.util.ArrayList) RemoveOp(org.locationtech.geogig.api.porcelain.RemoveOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 2 with RemoveOp

use of org.locationtech.geogig.api.porcelain.RemoveOp in project GeoGig by boundlessgeo.

the class RemoveWebOp method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() == null) {
        throw new CommandSpecException("No transaction was specified, remove requires a transaction to preserve the stability of the repository.");
    }
    if (this.path == null) {
        throw new CommandSpecException("No path was specified for removal.");
    }
    final Context geogig = this.getCommandLocator(context);
    RemoveOp command = geogig.command(RemoveOp.class);
    NodeRef.checkValidPath(path);
    Optional<NodeRef> node = geogig.command(FindTreeChild.class).setParent(geogig.workingTree().getTree()).setIndex(true).setChildPath(path).call();
    if (node.isPresent()) {
        NodeRef nodeRef = node.get();
        if (nodeRef.getType() == TYPE.TREE) {
            if (!recursive) {
                throw new CommandSpecException("Recursive option must be used to remove a tree.");
            }
        }
    }
    command.addPathToRemove(path).call();
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeElement("Deleted", path);
            out.finish();
        }
    });
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) NodeRef(org.locationtech.geogig.api.NodeRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) RemoveOp(org.locationtech.geogig.api.porcelain.RemoveOp) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Aggregations

NodeRef (org.locationtech.geogig.api.NodeRef)2 RemoveOp (org.locationtech.geogig.api.porcelain.RemoveOp)2 ArrayList (java.util.ArrayList)1 ConsoleReader (jline.console.ConsoleReader)1 Context (org.locationtech.geogig.api.Context)1 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)1 Repository (org.locationtech.geogig.repository.Repository)1 CommandContext (org.locationtech.geogig.web.api.CommandContext)1 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)1 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)1 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)1