Search in sources :

Example 1 with ObjectReader

use of org.locationtech.geogig.storage.ObjectReader in project GeoGig by boundlessgeo.

the class PatchSerializer method addElement.

private static void addElement(List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes) {
    String[] headerTokens = lines.get(0).split("\t");
    if (headerTokens.length == 4 || headerTokens.length == 3) {
        // modified // modification
        if (lines.size() == 1) {
            // feature type
            FeatureTypeDiff diff = new FeatureTypeDiff(headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2]));
            patch.addAlteredTree(diff);
        } else {
            // feature
            String element = Joiner.on("\n").join(lines.subList(1, lines.size()));
            ByteArrayInputStream stream;
            stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
            String operation = headerTokens[0].trim();
            if (operation.equals("M")) {
                String fullPath = headerTokens[1].trim();
                String oldMetadataId = headerTokens[2].trim();
                String newMetadataId = headerTokens[3].trim();
                RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId);
                RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId);
                Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
                for (int i = 1; i < lines.size(); i++) {
                    addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType);
                }
                FeatureDiff featureDiff = new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType);
                patch.addModifiedFeature(featureDiff);
            } else if (operation.equals("A") || operation.equals("R")) {
                String fullPath = headerTokens[1].trim();
                String featureTypeId = headerTokens[2].trim();
                RevFeatureType revFeatureType;
                revFeatureType = featureTypes.get(featureTypeId);
                FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType);
                ObjectReader<RevFeature> reader = factory.createFeatureReader();
                RevFeature revFeature = reader.read(null, stream);
                Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature);
                if (operation.equals("R")) {
                    patch.addRemovedFeature(fullPath, feature, revFeatureType);
                } else {
                    patch.addAddedFeature(fullPath, feature, revFeatureType);
                }
            } else {
                throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
            }
        }
    } else if (headerTokens.length == 1) {
        // feature type definition
        String element = Joiner.on("\n").join(lines);
        ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
        String[] tokens = lines.get(1).split("\t");
        ObjectReader<RevFeatureType> reader = factory.createFeatureTypeReader();
        RevFeatureType featureType = reader.read(null, stream);
        featureTypes.put(featureType.getId().toString(), featureType);
    } else {
        throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) ByteArrayInputStream(java.io.ByteArrayInputStream) RevFeature(org.locationtech.geogig.api.RevFeature) ObjectReader(org.locationtech.geogig.storage.ObjectReader) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)1 RevFeature (org.locationtech.geogig.api.RevFeature)1 RevFeatureBuilder (org.locationtech.geogig.api.RevFeatureBuilder)1 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)1 ObjectReader (org.locationtech.geogig.storage.ObjectReader)1 Feature (org.opengis.feature.Feature)1 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)1