Search in sources :

Example 16 with CommandContext

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

the class PushWebOp method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     */
@Override
public void run(CommandContext context) {
    final Context geogig = this.getCommandLocator(context);
    PushOp command = geogig.command(PushOp.class);
    if (refSpec != null) {
        command.addRefSpec(refSpec);
    }
    try {
        final TransferSummary dataPushed = command.setAll(pushAll).setRemote(remoteName).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeElement("Push", "Success");
                out.writeElement("dataPushed", String.valueOf(!dataPushed.isEmpty()));
                out.finish();
            }
        });
    } catch (SynchronizationException e) {
        switch(e.statusCode) {
            case REMOTE_HAS_CHANGES:
                context.setResponseContent(CommandResponse.error("Push failed: The remote repository has changes that would be lost in the event of a push."));
                break;
            case HISTORY_TOO_SHALLOW:
                context.setResponseContent(CommandResponse.error("Push failed: There is not enough local history to complete the push."));
            default:
                break;
        }
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PushOp(org.locationtech.geogig.api.porcelain.PushOp) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) TransferSummary(org.locationtech.geogig.api.porcelain.TransferSummary) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException)

Example 17 with CommandContext

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

the class FeatureDiffWeb 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 (path == null || path.trim().isEmpty()) {
        throw new CommandSpecException("No path for feature name specifed");
    }
    final Context geogig = this.getCommandLocator(context);
    ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get();
    ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get();
    RevFeature newFeature = null;
    RevFeatureType newFeatureType = null;
    RevFeature oldFeature = null;
    RevFeatureType oldFeatureType = null;
    final Map<PropertyDescriptor, AttributeDiff> diffs;
    Optional<NodeRef> ref = parseID(newId, geogig);
    Optional<RevObject> object;
    // need these to determine if the feature was added or removed so I can build the diffs
    // myself until the FeatureDiff supports null values
    boolean removed = false;
    boolean added = false;
    if (ref.isPresent()) {
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
        if (object.isPresent() && object.get() instanceof RevFeatureType) {
            newFeatureType = (RevFeatureType) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's featureType");
        }
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
        if (object.isPresent() && object.get() instanceof RevFeature) {
            newFeature = (RevFeature) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's feature");
        }
    } else {
        removed = true;
    }
    if (!oldId.equals(ObjectId.NULL)) {
        ref = parseID(oldId, geogig);
        if (ref.isPresent()) {
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
            if (object.isPresent() && object.get() instanceof RevFeatureType) {
                oldFeatureType = (RevFeatureType) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's featureType");
            }
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
            if (object.isPresent() && object.get() instanceof RevFeature) {
                oldFeature = (RevFeature) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's feature");
            }
        } else {
            added = true;
        }
    } else {
        added = true;
    }
    if (removed) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = oldFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent()));
                }
            }
        }
        diffs = tempDiffs;
    } else if (added) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = newFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull())));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value));
                }
            }
        }
        diffs = tempDiffs;
    } else {
        FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all);
        diffs = diff.getDiffs();
    }
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeFeatureDiffResponse(diffs);
            out.finish();
        }
    });
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) RevFeature(org.locationtech.geogig.api.RevFeature) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Geometry(com.vividsolutions.jts.geom.Geometry) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with CommandContext

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

the class ResolveConflict 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, add requires a transaction to preserve the stability of the repository.");
    }
    final Context geogig = this.getCommandLocator(context);
    RevTree revTree = geogig.workingTree().getTree();
    Optional<NodeRef> nodeRef = geogig.command(FindTreeChild.class).setParent(revTree).setChildPath(NodeRef.parentPath(path)).setIndex(true).call();
    Preconditions.checkArgument(nodeRef.isPresent(), "Invalid reference: %s", NodeRef.parentPath(path));
    RevFeatureType revFeatureType = geogig.command(RevObjectParse.class).setObjectId(nodeRef.get().getMetadataId()).call(RevFeatureType.class).get();
    RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(objectId).call(RevFeature.class).get();
    CoordinateReferenceSystem crs = revFeatureType.type().getCoordinateReferenceSystem();
    Envelope bounds = ReferencedEnvelope.create(crs);
    Optional<Object> o;
    for (int i = 0; i < revFeature.getValues().size(); i++) {
        o = revFeature.getValues().get(i);
        if (o.isPresent() && o.get() instanceof Geometry) {
            Geometry g = (Geometry) o.get();
            if (bounds.isNull()) {
                bounds.init(JTS.bounds(g, crs));
            } else {
                bounds.expandToInclude(JTS.bounds(g, crs));
            }
        }
    }
    NodeRef node = new NodeRef(Node.create(NodeRef.nodeFromPath(path), objectId, ObjectId.NULL, TYPE.FEATURE, bounds), NodeRef.parentPath(path), ObjectId.NULL);
    Optional<NodeRef> parentNode = geogig.command(FindTreeChild.class).setParent(geogig.workingTree().getTree()).setChildPath(node.getParentPath()).setIndex(true).call();
    RevTreeBuilder treeBuilder = null;
    ObjectId metadataId = ObjectId.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.objectDatabase(), parsed.get());
        treeBuilder.remove(node.getNode().getName());
    } else {
        treeBuilder = new RevTreeBuilder(geogig.stagingDatabase());
    }
    treeBuilder.put(node.getNode());
    ObjectId newTreeId = geogig.command(WriteBack.class).setAncestor(geogig.workingTree().getTree().builder(geogig.stagingDatabase())).setChildPath(node.getParentPath()).setToIndex(true).setTree(treeBuilder.build()).setMetadataId(metadataId).call();
    geogig.workingTree().updateWorkHead(newTreeId);
    AddOp command = geogig.command(AddOp.class);
    command.addPattern(path);
    command.call();
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeElement("Add", "Success");
            out.finish();
        }
    });
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) AddOp(org.locationtech.geogig.api.porcelain.AddOp) ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(com.vividsolutions.jts.geom.Envelope) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Geometry(com.vividsolutions.jts.geom.Geometry) NodeRef(org.locationtech.geogig.api.NodeRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree)

Example 19 with CommandContext

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

the class TagWebOp method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     */
@Override
public void run(CommandContext context) {
    if (list) {
        final Context geogig = this.getCommandLocator(context);
        final List<RevTag> tags = geogig.command(TagListOp.class).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeTagListResponse(tags);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) RevTag(org.locationtech.geogig.api.RevTag) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) TagListOp(org.locationtech.geogig.api.porcelain.TagListOp)

Example 20 with CommandContext

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

the class UpdateRefWeb 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 (name == null) {
        throw new CommandSpecException("No name was given.");
    } else if (!(delete) && newValue == null) {
        throw new CommandSpecException("Nothing specified to update with, must specify either deletion or new value to update to.");
    }
    final Context geogig = this.getCommandLocator(context);
    Optional<Ref> ref;
    try {
        ref = geogig.command(RefParse.class).setName(name).call();
        if (!ref.isPresent()) {
            throw new CommandSpecException("Invalid name: " + name);
        }
        if (ref.get() instanceof SymRef) {
            Optional<Ref> target = geogig.command(RefParse.class).setName(newValue).call();
            if (target.isPresent() && !(target.get() instanceof SymRef)) {
                ref = geogig.command(UpdateSymRef.class).setDelete(delete).setName(name).setNewValue(target.get().getName()).call();
            } else {
                throw new CommandSpecException("Invalid new target: " + newValue);
            }
        } else {
            Optional<ObjectId> target = geogig.command(RevParse.class).setRefSpec(newValue).call();
            if (target.isPresent()) {
                ref = geogig.command(UpdateRef.class).setDelete(delete).setName(ref.get().getName()).setNewValue(target.get()).call();
            } else {
                throw new CommandSpecException("Invalid new value: " + newValue);
            }
        }
    } catch (Exception e) {
        context.setResponseContent(CommandResponse.error("Aborting UpdateRef: " + e.getMessage()));
        return;
    }
    if (ref.isPresent()) {
        final Ref newRef = ref.get();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeUpdateRefResponse(newRef);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RefParse(org.locationtech.geogig.api.plumbing.RefParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Aggregations

Context (org.locationtech.geogig.api.Context)24 CommandContext (org.locationtech.geogig.web.api.CommandContext)24 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)24 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)24 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)15 ObjectId (org.locationtech.geogig.api.ObjectId)9 RevCommit (org.locationtech.geogig.api.RevCommit)7 NodeRef (org.locationtech.geogig.api.NodeRef)6 Ref (org.locationtech.geogig.api.Ref)6 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)5 Optional (com.google.common.base.Optional)4 SymRef (org.locationtech.geogig.api.SymRef)4 RevFeature (org.locationtech.geogig.api.RevFeature)3 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)3 RevTree (org.locationtech.geogig.api.RevTree)3 RefParse (org.locationtech.geogig.api.plumbing.RefParse)3 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)3 RevParse (org.locationtech.geogig.api.plumbing.RevParse)3 MergeScenarioReport (org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport)3 Geometry (com.vividsolutions.jts.geom.Geometry)2