Search in sources :

Example 11 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class RevertFeatureWebOp 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, revert feature requires a transaction to preserve the stability of the repository.");
    }
    final Context geogig = this.getCommandLocator(context);
    Optional<RevTree> newTree = Optional.absent();
    Optional<RevTree> oldTree = Optional.absent();
    // get tree from new commit
    Optional<ObjectId> treeId = geogig.command(ResolveTreeish.class).setTreeish(newCommitId).call();
    Preconditions.checkState(treeId.isPresent(), "New commit id did not resolve to a valid tree.");
    newTree = geogig.command(RevObjectParse.class).setRefSpec(treeId.get().toString()).call(RevTree.class);
    Preconditions.checkState(newTree.isPresent(), "Unable to read the new commit tree.");
    // get tree from old commit
    treeId = geogig.command(ResolveTreeish.class).setTreeish(oldCommitId).call();
    Preconditions.checkState(treeId.isPresent(), "Old commit id did not resolve to a valid tree.");
    oldTree = geogig.command(RevObjectParse.class).setRefSpec(treeId.get().toString()).call(RevTree.class);
    Preconditions.checkState(newTree.isPresent(), "Unable to read the old commit tree.");
    // get feature from old tree
    Optional<NodeRef> node = geogig.command(FindTreeChild.class).setParent(oldTree.get()).setIndex(true).setChildPath(featurePath).call();
    boolean delete = false;
    if (!node.isPresent()) {
        delete = true;
        node = geogig.command(FindTreeChild.class).setParent(newTree.get()).setIndex(true).setChildPath(featurePath).call();
        Preconditions.checkState(node.isPresent(), "The feature was not found in either commit tree.");
    }
    // get the new parent tree
    ObjectId metadataId = ObjectId.NULL;
    Optional<NodeRef> parentNode = geogig.command(FindTreeChild.class).setParent(newTree.get()).setChildPath(node.get().getParentPath()).setIndex(true).call();
    RevTreeBuilder treeBuilder = null;
    if (parentNode.isPresent()) {
        metadataId = parentNode.get().getMetadataId();
        Optional<RevTree> parsed = geogig.command(RevObjectParse.class).setObjectId(parentNode.get().getNode().getObjectId()).call(RevTree.class);
        checkState(parsed.isPresent(), "Parent tree couldn't be found in the repository.");
        treeBuilder = new RevTreeBuilder(geogig.stagingDatabase(), parsed.get());
        treeBuilder.remove(node.get().getNode().getName());
    } else {
        treeBuilder = new RevTreeBuilder(geogig.stagingDatabase());
    }
    // put the old feature into the new tree
    if (!delete) {
        treeBuilder.put(node.get().getNode());
    }
    ObjectId newTreeId = geogig.command(WriteBack.class).setAncestor(newTree.get().builder(geogig.stagingDatabase())).setChildPath(node.get().getParentPath()).setToIndex(true).setTree(treeBuilder.build()).setMetadataId(metadataId).call();
    // build new commit with parent of new commit and the newly built tree
    CommitBuilder builder = new CommitBuilder();
    builder.setParentIds(Lists.newArrayList(newCommitId));
    builder.setTreeId(newTreeId);
    builder.setAuthor(authorName.orNull());
    builder.setAuthorEmail(authorEmail.orNull());
    builder.setMessage(commitMessage.or("Reverted changes made to " + featurePath + " at " + newCommitId.toString()));
    RevCommit mapped = builder.build();
    context.getGeoGIG().getRepository().objectDatabase().put(mapped);
    // merge commit into current branch
    final Optional<Ref> currHead = geogig.command(RefParse.class).setName(Ref.HEAD).call();
    if (!currHead.isPresent()) {
        throw new CommandSpecException("Repository has no HEAD, can't merge.");
    }
    MergeOp merge = geogig.command(MergeOp.class);
    merge.setAuthor(authorName.orNull(), authorEmail.orNull());
    merge.addCommit(Suppliers.ofInstance(mapped.getId()));
    merge.setMessage(mergeMessage.or("Merged revert of " + featurePath));
    try {
        final MergeReport report = merge.call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeMergeResponse(Optional.fromNullable(report.getMergeCommit()), report.getReport().get(), geogig, report.getOurs(), report.getPairs().get(0).getTheirs(), report.getPairs().get(0).getAncestor());
                out.finish();
            }
        });
    } catch (Exception e) {
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(currHead.get().getObjectId());
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(mapped.getId());
        final Optional<ObjectId> ancestor = geogig.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = geogig.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                Optional<RevCommit> mergeCommit = Optional.absent();
                out.writeMergeResponse(mergeCommit, report, geogig, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    }
}
Also used : CommitBuilder(org.locationtech.geogig.api.CommitBuilder) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) NodeRef(org.locationtech.geogig.api.NodeRef) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevCommit(org.locationtech.geogig.api.RevCommit) Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevTree(org.locationtech.geogig.api.RevTree)

Example 12 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class CommandResource method runCommand.

private Representation runCommand(Variant variant, Request request) {
    final Optional<GeoGIG> geogig = getGeogig(request);
    Preconditions.checkState(geogig.isPresent());
    Representation rep = null;
    WebAPICommand command = null;
    Form options = getRequest().getResourceRef().getQueryAsForm();
    String commandName = (String) getRequest().getAttributes().get("command");
    MediaType format = resolveFormat(options, variant);
    try {
        ParameterSet params = new FormParams(options);
        command = CommandBuilder.build(commandName, params);
        assert command != null;
    } catch (CommandSpecException ex) {
        rep = formatException(ex, format);
    }
    try {
        if (command != null) {
            RestletContext ctx = new RestletContext(geogig.get());
            command.run(ctx);
            rep = ctx.getRepresentation(format, getJSONPCallback());
        }
    } catch (IllegalArgumentException ex) {
        rep = formatException(ex, format);
    } catch (Exception ex) {
        rep = formatUnexpectedException(ex, format);
    }
    return rep;
}
Also used : ParameterSet(org.locationtech.geogig.web.api.ParameterSet) WebAPICommand(org.locationtech.geogig.web.api.WebAPICommand) Form(org.restlet.data.Form) Representation(org.restlet.resource.Representation) WriterRepresentation(org.locationtech.geogig.rest.WriterRepresentation) RestletException(org.locationtech.geogig.rest.RestletException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MediaType(org.restlet.data.MediaType) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 13 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class MergeFeatureResource method parseID.

private Optional<NodeRef> parseID(ObjectId commitId, String path, GeoGIG geogig) {
    Optional<RevObject> object = geogig.command(RevObjectParse.class).setObjectId(commitId).call();
    RevCommit commit = null;
    if (object.isPresent() && object.get() instanceof RevCommit) {
        commit = (RevCommit) object.get();
    } else {
        throw new CommandSpecException("Couldn't resolve id: " + commitId.toString() + " to a commit");
    }
    object = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call();
    if (object.isPresent()) {
        RevTree tree = (RevTree) object.get();
        return geogig.command(FindTreeChild.class).setParent(tree).setChildPath(path).call();
    } else {
        throw new CommandSpecException("Couldn't resolve commit's treeId");
    }
}
Also used : RevObject(org.locationtech.geogig.api.RevObject) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 14 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class OsmImportWebOp method getRepresentation.

@Override
public Representation getRepresentation(final Variant variant) {
    final Request request = getRequest();
    final Context context = super.getContext(request);
    Form options = getRequest().getResourceRef().getQueryAsForm();
    final String urlOrFilepath = options.getFirstValue("uri");
    final boolean add = Boolean.valueOf(options.getFirstValue("add"));
    final String mappingFile = options.getFirstValue("mapping");
    Mapping mapping = null;
    if (mappingFile != null) {
        mapping = Mapping.fromFile(mappingFile);
    }
    final boolean noRaw = Boolean.valueOf(options.getFirstValue("noRaw"));
    final String message = options.getFirstValue("message");
    if (urlOrFilepath == null) {
        String msg = "Missing parameter: uri\n" + "Usage: GET <repo context>/osm/import?uri=<osm file URI>[&<arg>=<value>]+\n" + "Arguments:\n" + " * uri: Mandatory. URL or path to OSM data file in the server filesystem\n" + " * add: Optional. true|false. Default: false. If true, do not remove previous data before importing.\n" + " * mapping: Optional. Location of mapping file in the server filesystem\n" + " * noRaw: Optional. true|false. Default: false. If true, do not import raw data when using a mapping\n" + " * message: Optional. Message for the commit to create.";
        throw new CommandSpecException(msg);
    }
    OSMImportOp command = context.command(OSMImportOp.class);
    command.setAdd(add);
    command.setDataSource(urlOrFilepath);
    command.setMapping(mapping);
    command.setMessage(message);
    command.setNoRaw(noRaw);
    AsyncCommand<Optional<OSMReport>> asyncCommand;
    URL repo = context.repository().getLocation();
    String description = String.format("osm import %s, repository: %s", urlOrFilepath, repo);
    asyncCommand = AsyncContext.get().run(command, description);
    final String rootPath = request.getRootRef().toString();
    MediaType mediaType = variant.getMediaType();
    Representation rep = new OSMReportRepresentation(mediaType, asyncCommand, rootPath);
    return rep;
}
Also used : Context(org.locationtech.geogig.api.Context) AsyncContext(org.locationtech.geogig.rest.AsyncContext) Optional(com.google.common.base.Optional) Form(org.restlet.data.Form) Request(org.restlet.data.Request) Mapping(org.locationtech.geogig.osm.internal.Mapping) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) Representation(org.restlet.resource.Representation) URL(java.net.URL) MediaType(org.restlet.data.MediaType) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 15 with CommandSpecException

use of org.locationtech.geogig.web.api.CommandSpecException in project GeoGig by boundlessgeo.

the class MergeWebOp 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, merge requires a transaction to preserve the stability of the repository.");
    } else if (this.commit == null) {
        throw new CommandSpecException("No commits were specified for merging.");
    }
    final GeogigTransaction transaction = (GeogigTransaction) this.getCommandLocator(context);
    final Optional<Ref> currHead = transaction.command(RefParse.class).setName(Ref.HEAD).call();
    if (!currHead.isPresent()) {
        throw new CommandSpecException("Repository has no HEAD, can't merge.");
    }
    MergeOp merge = transaction.command(MergeOp.class);
    merge.setAuthor(authorName.orNull(), authorEmail.orNull());
    final Optional<ObjectId> oid = transaction.command(RevParse.class).setRefSpec(commit).call();
    if (oid.isPresent()) {
        merge.addCommit(Suppliers.ofInstance(oid.get()));
    } else {
        throw new CommandSpecException("Couldn't resolve '" + commit + "' to a commit.");
    }
    try {
        final MergeReport report = merge.setNoCommit(noCommit).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeMergeResponse(Optional.fromNullable(report.getMergeCommit()), report.getReport().get(), transaction, report.getOurs(), report.getPairs().get(0).getTheirs(), report.getPairs().get(0).getAncestor());
                out.finish();
            }
        });
    } catch (Exception e) {
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(currHead.get().getObjectId());
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(oid.get());
        final Optional<ObjectId> ancestor = transaction.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = transaction.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                Optional<RevCommit> mergeCommit = Optional.absent();
                out.writeMergeResponse(mergeCommit, report, transaction, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    }
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevCommit(org.locationtech.geogig.api.RevCommit)

Aggregations

CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)25 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)21 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)21 Context (org.locationtech.geogig.api.Context)16 CommandContext (org.locationtech.geogig.web.api.CommandContext)15 ObjectId (org.locationtech.geogig.api.ObjectId)9 RevCommit (org.locationtech.geogig.api.RevCommit)9 Optional (com.google.common.base.Optional)6 NodeRef (org.locationtech.geogig.api.NodeRef)5 Ref (org.locationtech.geogig.api.Ref)5 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)5 IOException (java.io.IOException)4 RevFeature (org.locationtech.geogig.api.RevFeature)4 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)4 RevObject (org.locationtech.geogig.api.RevObject)4 RevTree (org.locationtech.geogig.api.RevTree)4 GeoGIG (org.locationtech.geogig.api.GeoGIG)3 GeogigTransaction (org.locationtech.geogig.api.GeogigTransaction)3 Remote (org.locationtech.geogig.api.Remote)3 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)3