Search in sources :

Example 11 with FeatureDiff

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

the class ApplyPatchOpTest method testAddFeatureAttributePatch.

@Test
public void testAddFeatureAttributePatch() throws Exception {
    insert(points1);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> newValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(null, newValue);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(modifiedPointsType));
    patch.addModifiedFeature(featureDiff);
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
// TODO
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Example 12 with FeatureDiff

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

the class CreatePatchOp method _call.

@Override
protected Patch _call() {
    Patch patch = new Patch();
    Map<ObjectId, RevFeatureType> featureTypes = Maps.newHashMap();
    while (diffs.hasNext()) {
        DiffEntry diffEntry = diffs.next();
        final NodeRef newObject = diffEntry.getNewObject();
        final NodeRef oldObject = diffEntry.getOldObject();
        if (diffEntry.changeType() == ChangeType.MODIFIED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                FeatureDiff diff = command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
                patch.addModifiedFeature(diff);
            } else if (revObject instanceof RevTree) {
                RevFeatureType oldFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
                RevFeatureType newFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
                patch.addFeatureType(oldFeatureType);
                patch.addFeatureType(newFeatureType);
                patch.addAlteredTree(diffEntry);
            }
        } else if (diffEntry.changeType() == ChangeType.ADDED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                RevFeatureType featureType;
                if (featureTypes.containsKey(newObject.getMetadataId())) {
                    featureType = featureTypes.get(newObject.getMetadataId());
                } else {
                    featureType = command(RevObjectParse.class).setObjectId(newObject.getMetadataId()).call(RevFeatureType.class).get();
                    featureTypes.put(newObject.getMetadataId(), featureType);
                }
                FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
                Feature feature = featureBuilder.build(diffEntry.newObjectId().toString(), (RevFeature) revObject);
                String name = diffEntry.newPath();
                patch.addAddedFeature(name, feature, featureType);
            } else if (revObject instanceof RevTree) {
                ObjectId metadataId = diffEntry.getNewObject().getMetadataId();
                if (!metadataId.isNull()) {
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
                    patch.addAlteredTree(diffEntry);
                    patch.addFeatureType(featureType);
                }
            }
        } else if (diffEntry.changeType() == ChangeType.REMOVED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.oldObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                RevFeatureType featureType;
                if (featureTypes.containsKey(oldObject.getMetadataId())) {
                    featureType = featureTypes.get(oldObject.getMetadataId());
                } else {
                    featureType = command(RevObjectParse.class).setObjectId(oldObject.getMetadataId()).call(RevFeatureType.class).get();
                    featureTypes.put(oldObject.getMetadataId(), featureType);
                }
                FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
                Feature feature = featureBuilder.build(diffEntry.oldObjectId().toString(), (RevFeature) revObject);
                String name = diffEntry.oldPath();
                patch.addRemovedFeature(name, feature, featureType);
            } else if (revObject instanceof RevTree) {
                ObjectId metadataId = diffEntry.getOldObject().getMetadataId();
                if (!metadataId.isNull()) {
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
                    patch.addAlteredTree(diffEntry);
                    patch.addFeatureType(featureType);
                }
            }
        }
    }
    return patch;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 13 with FeatureDiff

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

the class DiffFeatureTest method testDiffBetweenFeatureAndItself.

@Test
public void testDiffBetweenFeatureAndItself() {
    NodeRef oldRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(NodeRef.appendChild(pointsName, idP1)).call().orNull();
    NodeRef newRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(NodeRef.appendChild(pointsName, idP1)).call().orNull();
    FeatureDiff diff = geogig.command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(oldRef)).setNewVersion(Suppliers.ofInstance(newRef)).call();
    assertFalse(diff.hasDifferences());
    System.out.println(diff);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) FeatureNodeRefFromRefspec(org.locationtech.geogig.api.porcelain.FeatureNodeRefFromRefspec) Test(org.junit.Test)

Example 14 with FeatureDiff

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

the class DiffFeatureTest method testDiffBetweenEditedFeatures.

@Test
public void testDiffBetweenEditedFeatures() {
    NodeRef oldRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec("HEAD:" + NodeRef.appendChild(pointsName, idP1)).call().orNull();
    NodeRef newRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(NodeRef.appendChild(pointsName, idP1)).call().orNull();
    FeatureDiff diff = geogig.command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(oldRef)).setNewVersion(Suppliers.ofInstance(newRef)).call();
    assertTrue(diff.hasDifferences());
    System.out.println(diff);
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) FeatureNodeRefFromRefspec(org.locationtech.geogig.api.porcelain.FeatureNodeRefFromRefspec) Test(org.junit.Test)

Example 15 with FeatureDiff

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

the class ReportCommitConflictsOp method _call.

@Override
protected MergeScenarioReport _call() {
    MergeScenarioReport report = new MergeScenarioReport();
    ObjectId parentCommitId = ObjectId.NULL;
    if (commit.getParentIds().size() > 0) {
        parentCommitId = commit.getParentIds().get(0);
    }
    ObjectId parentTreeId = ObjectId.NULL;
    Repository repository = repository();
    if (repository.commitExists(parentCommitId)) {
        parentTreeId = repository.getCommit(parentCommitId).getTreeId();
    }
    // get changes
    Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(parentTreeId).setNewTree(commit.getTreeId()).setReportTrees(true).call();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
        Optional<RevObject> obj = command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call();
        switch(diff.changeType()) {
            case ADDED:
                if (obj.isPresent()) {
                    TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                    if (TYPE.TREE.equals(type)) {
                        NodeRef headVersion = command(FindTreeChild.class).setChildPath(path).setParent(repository.getOrCreateHeadTree()).call().get();
                        if (!headVersion.getMetadataId().equals(diff.getNewObject().getMetadataId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.getNewObject().getMetadataId(), headVersion.getMetadataId()));
                        }
                    } else {
                        if (!obj.get().getId().equals(diff.newObjectId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.newObjectId(), obj.get().getId()));
                        }
                    }
                } else {
                    report.addUnconflicted(diff);
                }
                break;
            case REMOVED:
                if (obj.isPresent()) {
                    if (obj.get().getId().equals(diff.oldObjectId())) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), ObjectId.NULL, obj.get().getId()));
                    }
                }
                break;
            case MODIFIED:
                TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                if (TYPE.TREE.equals(type)) {
                    // one
                    if (!diff.isChange()) {
                        report.addUnconflicted(diff);
                    }
                } else {
                    String refSpec = Ref.HEAD + ":" + path;
                    obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
                    if (!obj.isPresent()) {
                        // git reports this as a conflict but does not mark as conflicted, just adds
                        // the missing file.
                        // We add it and consider it unconflicted
                        report.addUnconflicted(diff);
                        break;
                    }
                    RevFeature feature = (RevFeature) obj.get();
                    DepthSearch depthSearch = new DepthSearch(repository.objectDatabase());
                    Optional<NodeRef> noderef = depthSearch.find(this.workingTree().getTree(), path);
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
                    ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                    FeatureDiff featureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).setNewVersion(Suppliers.ofInstance(diff.getNewObject())).call();
                    Set<Entry<PropertyDescriptor, AttributeDiff>> attrDiffs = featureDiff.getDiffs().entrySet();
                    RevFeature newFeature = command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
                    boolean ok = true;
                    for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = attrDiffs.iterator(); iterator.hasNext() && ok; ) {
                        Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
                        AttributeDiff attrDiff = entry.getValue();
                        PropertyDescriptor descriptor = entry.getKey();
                        switch(attrDiff.getType()) {
                            case ADDED:
                                if (descriptors.contains(descriptor)) {
                                    ok = false;
                                }
                                break;
                            case REMOVED:
                            case MODIFIED:
                                if (!descriptors.contains(descriptor)) {
                                    ok = false;
                                    break;
                                }
                                for (int i = 0; i < descriptors.size(); i++) {
                                    if (descriptors.get(i).equals(descriptor)) {
                                        Optional<Object> value = feature.getValues().get(i);
                                        Optional<Object> newValue = newFeature.getValues().get(i);
                                        if (!newValue.equals(value)) {
                                            // check
                                            if (!attrDiff.canBeAppliedOn(value)) {
                                                ok = false;
                                            }
                                            break;
                                        }
                                    }
                                }
                        }
                    }
                    if (ok) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), diff.newObjectId(), obj.get().getId()));
                    }
                }
                break;
        }
    }
    return report;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Repository(org.locationtech.geogig.repository.Repository) DepthSearch(org.locationtech.geogig.repository.DepthSearch) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject)

Aggregations

FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)19 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)15 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)15 Test (org.junit.Test)11 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)11 RevFeature (org.locationtech.geogig.api.RevFeature)10 NodeRef (org.locationtech.geogig.api.NodeRef)8 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)7 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)7 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)6 Optional (com.google.common.base.Optional)5 ObjectId (org.locationtech.geogig.api.ObjectId)5 RevTree (org.locationtech.geogig.api.RevTree)5 DiffFeature (org.locationtech.geogig.api.plumbing.DiffFeature)5 RevObject (org.locationtech.geogig.api.RevObject)4 CannotApplyPatchException (org.locationtech.geogig.api.porcelain.CannotApplyPatchException)4 Entry (java.util.Map.Entry)3 Node (org.locationtech.geogig.api.Node)3 TYPE (org.locationtech.geogig.api.RevObject.TYPE)3