Search in sources :

Example 76 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class ResponseWriter method writeDiffEntries.

/**
     * Writes a set of {@link DiffEntry}s to the stream.
     * 
     * @param name the element name
     * @param start the change number to start writing from
     * @param length the number of changes to write
     * @param entries an iterator for the DiffEntries to write
     * @throws XMLStreamException
     */
public void writeDiffEntries(String name, int start, int length, Iterator<DiffEntry> entries) throws XMLStreamException {
    Iterators.advance(entries, start);
    if (length < 0) {
        length = Integer.MAX_VALUE;
    }
    int counter = 0;
    while (entries.hasNext() && counter < length) {
        DiffEntry entry = entries.next();
        out.writeStartElement(name);
        writeElement("changeType", entry.changeType().toString());
        NodeRef oldObject = entry.getOldObject();
        NodeRef newObject = entry.getNewObject();
        if (oldObject == null) {
            writeElement("newPath", newObject.path());
            writeElement("newObjectId", newObject.objectId().toString());
            writeElement("path", "");
            writeElement("oldObjectId", ObjectId.NULL.toString());
        } else if (newObject == null) {
            writeElement("newPath", "");
            writeElement("newObjectId", ObjectId.NULL.toString());
            writeElement("path", oldObject.path());
            writeElement("oldObjectId", oldObject.objectId().toString());
        } else {
            writeElement("newPath", newObject.path());
            writeElement("newObjectId", newObject.objectId().toString());
            writeElement("path", oldObject.path());
            writeElement("oldObjectId", oldObject.objectId().toString());
        }
        out.writeEndElement();
        counter++;
    }
    if (entries.hasNext()) {
        writeElement("nextPage", "true");
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 77 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class ResponseWriter method writeCommitResponse.

/**
     * Writes the response for the {@link Commit} command to the stream.
     * 
     * @param commit the commit
     * @param diff the changes returned from the command
     * @throws XMLStreamException
     */
public void writeCommitResponse(RevCommit commit, Iterator<DiffEntry> diff) throws XMLStreamException {
    int adds = 0, deletes = 0, changes = 0;
    DiffEntry diffEntry;
    while (diff.hasNext()) {
        diffEntry = diff.next();
        switch(diffEntry.changeType()) {
            case ADDED:
                ++adds;
                break;
            case REMOVED:
                ++deletes;
                break;
            case MODIFIED:
                ++changes;
                break;
        }
    }
    writeElement("commitId", commit.getId().toString());
    writeElement("added", Integer.toString(adds));
    writeElement("changed", Integer.toString(changes));
    writeElement("deleted", Integer.toString(deletes));
}
Also used : DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 78 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class ResponseWriter method writePullResponse.

public void writePullResponse(PullResult result, Iterator<DiffEntry> iter, Context geogig) throws XMLStreamException {
    out.writeStartElement("Pull");
    writeFetchResponse(result.getFetchResult());
    if (iter != null) {
        writeElement("Remote", result.getRemoteName());
        writeElement("Ref", result.getNewRef().localName());
        int added = 0;
        int removed = 0;
        int modified = 0;
        while (iter.hasNext()) {
            DiffEntry entry = iter.next();
            if (entry.changeType() == ChangeType.ADDED) {
                added++;
            } else if (entry.changeType() == ChangeType.MODIFIED) {
                modified++;
            } else if (entry.changeType() == ChangeType.REMOVED) {
                removed++;
            }
        }
        writeElement("Added", Integer.toString(added));
        writeElement("Modified", Integer.toString(modified));
        writeElement("Removed", Integer.toString(removed));
    }
    if (result.getMergeReport().isPresent() && result.getMergeReport().get().getReport().isPresent()) {
        MergeReport report = result.getMergeReport().get();
        writeMergeResponse(Optional.fromNullable(report.getMergeCommit()), report.getReport().get(), geogig, report.getOurs(), report.getPairs().get(0).getTheirs(), report.getPairs().get(0).getAncestor());
    }
    out.writeEndElement();
}
Also used : MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 79 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class Commit 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, commit requires a transaction to preserve the stability of the repository.");
    }
    final Context geogig = this.getCommandLocator(context);
    RevCommit commit;
    try {
        commit = geogig.command(CommitOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).setMessage(message).setAllowEmpty(true).setAll(all).call();
        assert commit != null;
    } catch (NothingToCommitException noChanges) {
        context.setResponseContent(CommandResponse.warning("Nothing to commit"));
        commit = null;
    } catch (IllegalStateException e) {
        context.setResponseContent(CommandResponse.warning(e.getMessage()));
        commit = null;
    }
    if (commit != null) {
        final RevCommit commitToWrite = commit;
        final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
        final Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId()).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommitResponse(commitToWrite, diff);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 80 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class ResponseWriter method writeGeometryChanges.

/**
     * Writes the response for a set of diffs while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param diff - a DiffEntry iterator to build the response from
     * @throws XMLStreamException
     */
public void writeGeometryChanges(final Context geogig, Iterator<DiffEntry> diff, int page, int elementsPerPage) throws XMLStreamException {
    Iterators.advance(diff, page * elementsPerPage);
    int counter = 0;
    Iterator<GeometryChange> changeIterator = Iterators.transform(diff, new Function<DiffEntry, GeometryChange>() {

        @Override
        public GeometryChange apply(DiffEntry input) {
            Optional<RevObject> feature = Optional.absent();
            Optional<RevObject> type = Optional.absent();
            String path = null;
            String crsCode = null;
            GeometryChange change = null;
            if (input.changeType() == ChangeType.ADDED || input.changeType() == ChangeType.MODIFIED) {
                feature = geogig.command(RevObjectParse.class).setObjectId(input.newObjectId()).call();
                type = geogig.command(RevObjectParse.class).setObjectId(input.getNewObject().getMetadataId()).call();
                path = input.getNewObject().path();
            } else if (input.changeType() == ChangeType.REMOVED) {
                feature = geogig.command(RevObjectParse.class).setObjectId(input.oldObjectId()).call();
                type = geogig.command(RevObjectParse.class).setObjectId(input.getOldObject().getMetadataId()).call();
                path = input.getOldObject().path();
            }
            if (feature.isPresent() && feature.get() instanceof RevFeature && type.isPresent() && type.get() instanceof RevFeatureType) {
                RevFeatureType featureType = (RevFeatureType) type.get();
                Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
                for (PropertyDescriptor attrib : attribs) {
                    PropertyType attrType = attrib.getType();
                    if (attrType instanceof GeometryType) {
                        GeometryType gt = (GeometryType) attrType;
                        CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
                        if (crs != null) {
                            try {
                                crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                            } catch (FactoryException e) {
                                crsCode = null;
                            }
                            if (crsCode != null) {
                                crsCode = "EPSG:" + crsCode;
                            }
                        }
                        break;
                    }
                }
                RevFeature revFeature = (RevFeature) feature.get();
                FeatureBuilder builder = new FeatureBuilder(featureType);
                GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
                change = new GeometryChange(simpleFeature, input.changeType(), path, crsCode);
            }
            return change;
        }
    });
    while (changeIterator.hasNext() && (elementsPerPage == 0 || counter < elementsPerPage)) {
        GeometryChange next = changeIterator.next();
        if (next != null) {
            GeogigSimpleFeature feature = next.getFeature();
            ChangeType change = next.getChangeType();
            out.writeStartElement("Feature");
            writeElement("change", change.toString());
            writeElement("id", next.getPath());
            List<Object> attributes = feature.getAttributes();
            for (Object attribute : attributes) {
                if (attribute instanceof Geometry) {
                    writeElement("geometry", ((Geometry) attribute).toText());
                    break;
                }
            }
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
            counter++;
        }
    }
    if (changeIterator.hasNext()) {
        writeElement("nextPage", "true");
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) PropertyType(org.opengis.feature.type.PropertyType) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryType(org.opengis.feature.type.GeometryType) ChangeType(org.locationtech.geogig.api.plumbing.diff.DiffEntry.ChangeType) RevFeature(org.locationtech.geogig.api.RevFeature) Collection(java.util.Collection) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)83 ObjectId (org.locationtech.geogig.api.ObjectId)38 Test (org.junit.Test)31 RevCommit (org.locationtech.geogig.api.RevCommit)31 NodeRef (org.locationtech.geogig.api.NodeRef)24 DiffOp (org.locationtech.geogig.api.porcelain.DiffOp)17 RevFeature (org.locationtech.geogig.api.RevFeature)15 RevTree (org.locationtech.geogig.api.RevTree)15 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)14 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)14 RevObject (org.locationtech.geogig.api.RevObject)11 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)11 Feature (org.opengis.feature.Feature)10 Optional (com.google.common.base.Optional)9 GeoGIG (org.locationtech.geogig.api.GeoGIG)8 Repository (org.locationtech.geogig.repository.Repository)8 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)8 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)8 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 IOException (java.io.IOException)5