Search in sources :

Example 1 with FeatureTypeDiff

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

the class ApplyPatchOpTest method testModifiedFeatureType.

@Test
public void testModifiedFeatureType() throws Exception {
    insert(points2, points3, points1B);
    Patch patch = new Patch();
    RevFeatureType oldFeatureType = RevFeatureTypeImpl.build(pointsType);
    RevFeatureType featureType = RevFeatureTypeImpl.build(modifiedPointsType);
    patch.addFeatureType(featureType);
    patch.addAlteredTree(new FeatureTypeDiff(pointsName, oldFeatureType.getId(), featureType.getId()));
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    RevTree root = repo.workingTree().getTree();
    assertNotNull(root);
    Optional<Node> typeTree = findTreeChild(root, pointsName);
    assertTrue(typeTree.isPresent());
    assertEquals(featureType.getId(), typeTree.get().getMetadataId().get());
    Optional<Node> featureNode = findTreeChild(root, NodeRef.appendChild(pointsName, idP2));
    assertTrue(featureNode.isPresent());
    assertEquals(oldFeatureType.getId(), featureNode.get().getMetadataId().get());
    featureNode = findTreeChild(root, NodeRef.appendChild(pointsName, idP1));
    assertTrue(featureNode.isPresent());
    assertFalse(featureNode.get().getMetadataId().isPresent());
}
Also used : FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) Node(org.locationtech.geogig.api.Node) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 2 with FeatureTypeDiff

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

the class ApplyPatchOpTest method testRemoveEmptyFeatureTypePatch.

@Test
public void testRemoveEmptyFeatureTypePatch() throws Exception {
    WorkingTree workingTree = geogig.getRepository().workingTree();
    workingTree.createTypeTree(pointsName, pointsType);
    geogig.command(AddOp.class).setUpdateOnly(false).call();
    Patch patch = new Patch();
    RevFeatureType featureType = RevFeatureTypeImpl.build(pointsType);
    patch.addFeatureType(featureType);
    patch.addAlteredTree(new FeatureTypeDiff(pointsName, featureType.getId(), null));
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    RevTree root = repo.workingTree().getTree();
    assertNotNull(root);
    Optional<Node> typeTree = findTreeChild(root, pointsName);
    assertFalse(typeTree.isPresent());
}
Also used : WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) Node(org.locationtech.geogig.api.Node) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 3 with FeatureTypeDiff

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

the class ApplyPatchOp method applyPatch.

private void applyPatch(Patch patch) {
    final WorkingTree workTree = workingTree();
    final StagingDatabase indexDb = stagingDatabase();
    if (reverse) {
        patch = patch.reversed();
    }
    List<FeatureInfo> removed = patch.getRemovedFeatures();
    for (FeatureInfo feature : removed) {
        workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
    }
    List<FeatureInfo> added = patch.getAddedFeatures();
    for (FeatureInfo feature : added) {
        workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
    }
    List<FeatureDiff> diffs = patch.getModifiedFeatures();
    for (FeatureDiff diff : diffs) {
        String path = diff.getPath();
        DepthSearch depthSearch = new DepthSearch(indexDb);
        Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
        RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
        String refSpec = Ref.WORK_HEAD + ":" + path;
        RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
        RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
        ImmutableList<Optional<Object>> values = feature.getValues();
        ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
        ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
        Map<Name, Optional<?>> attrs = Maps.newHashMap();
        for (int i = 0; i < oldDescriptors.size(); i++) {
            PropertyDescriptor descriptor = oldDescriptors.get(i);
            if (newDescriptors.contains(descriptor)) {
                Optional<Object> value = values.get(i);
                attrs.put(descriptor.getName(), value);
            }
        }
        Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
        for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
            Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
            if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
                Optional<?> oldValue = attrs.get(entry.getKey().getName());
                attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
            }
        }
        Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
        for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Entry<Name, Optional<?>> entry = iterator.next();
            featureBuilder.set(entry.getKey(), entry.getValue().orNull());
        }
        SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
        workTree.insert(NodeRef.parentPath(path), featureToInsert);
    }
    ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
    for (FeatureTypeDiff diff : alteredTrees) {
        Optional<RevFeatureType> featureType;
        if (diff.getOldFeatureType().isNull()) {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.createTypeTree(diff.getPath(), featureType.get().type());
        } else if (diff.getNewFeatureType().isNull()) {
            workTree.delete(diff.getPath());
        } else {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.updateTypeTree(diff.getPath(), featureType.get().type());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 4 with FeatureTypeDiff

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

the class ApplyPatchOpTest method testAddEmptyFeatureTypePatch.

@Test
public void testAddEmptyFeatureTypePatch() throws Exception {
    Patch patch = new Patch();
    RevFeatureType featureType = RevFeatureTypeImpl.build(pointsType);
    patch.addFeatureType(featureType);
    patch.addAlteredTree(new FeatureTypeDiff(pointsName, null, featureType.getId()));
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    RevTree root = repo.workingTree().getTree();
    assertNotNull(root);
    Optional<Node> typeTreeId = findTreeChild(root, pointsName);
    RevTree typeTree = repo.getTree(typeTreeId.get().getObjectId());
    assertNotNull(typeTree);
    assertEquals(featureType.getId(), typeTreeId.get().getMetadataId().get());
}
Also used : FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) Node(org.locationtech.geogig.api.Node) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Aggregations

RevFeatureType (org.locationtech.geogig.api.RevFeatureType)4 FeatureTypeDiff (org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff)4 Test (org.junit.Test)3 Node (org.locationtech.geogig.api.Node)3 RevTree (org.locationtech.geogig.api.RevTree)3 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)3 WorkingTree (org.locationtech.geogig.repository.WorkingTree)2 Optional (com.google.common.base.Optional)1 Entry (java.util.Map.Entry)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 FeatureInfo (org.locationtech.geogig.api.FeatureInfo)1 NodeRef (org.locationtech.geogig.api.NodeRef)1 RevFeature (org.locationtech.geogig.api.RevFeature)1 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)1 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)1 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)1 DepthSearch (org.locationtech.geogig.repository.DepthSearch)1 StagingDatabase (org.locationtech.geogig.storage.StagingDatabase)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 Name (org.opengis.feature.type.Name)1