Search in sources :

Example 1 with FieldType

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

the class Log method writeCSV.

private void writeCSV(GeoGIG geogig, Writer out, Iterator<RevCommit> log) throws Exception {
    String response = "ChangeType,FeatureId,CommitId,Parent CommitIds,Author Name,Author Email,Author Commit Time,Committer Name,Committer Email,Committer Commit Time,Commit Message";
    out.write(response);
    response = "";
    String path = paths.get(0);
    // This is the feature type object
    Optional<NodeRef> ref = geogig.command(FindTreeChild.class).setChildPath(path).setParent(geogig.getRepository().workingTree().getTree()).call();
    Optional<RevObject> type = Optional.absent();
    if (ref.isPresent()) {
        type = geogig.command(RevObjectParse.class).setRefSpec(ref.get().getMetadataId().toString()).call();
    } else {
        throw new CommandSpecException("Couldn't resolve the given path.");
    }
    if (type.isPresent() && type.get() instanceof RevFeatureType) {
        RevFeatureType featureType = (RevFeatureType) type.get();
        Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
        int attributeLength = attribs.size();
        for (PropertyDescriptor attrib : attribs) {
            response += "," + escapeCsv(attrib.getName().toString());
        }
        response += '\n';
        out.write(response);
        response = "";
        RevCommit commit = null;
        while (log.hasNext()) {
            commit = log.next();
            String parentId = commit.getParentIds().size() >= 1 ? commit.getParentIds().get(0).toString() : ObjectId.NULL.toString();
            Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId().toString()).setFilter(path).call();
            while (diff.hasNext()) {
                DiffEntry entry = diff.next();
                response += entry.changeType().toString() + ",";
                String fid = "";
                if (entry.newPath() != null) {
                    if (entry.oldPath() != null) {
                        fid = entry.oldPath() + " -> " + entry.newPath();
                    } else {
                        fid = entry.newPath();
                    }
                } else if (entry.oldPath() != null) {
                    fid = entry.oldPath();
                }
                response += fid + ",";
                response += commit.getId().toString() + ",";
                response += parentId;
                if (commit.getParentIds().size() > 1) {
                    for (int index = 1; index < commit.getParentIds().size(); index++) {
                        response += " " + commit.getParentIds().get(index).toString();
                    }
                }
                response += ",";
                if (commit.getAuthor().getName().isPresent()) {
                    response += escapeCsv(commit.getAuthor().getName().get());
                }
                response += ",";
                if (commit.getAuthor().getEmail().isPresent()) {
                    response += escapeCsv(commit.getAuthor().getEmail().get());
                }
                response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format(new Date(commit.getAuthor().getTimestamp())) + ",";
                if (commit.getCommitter().getName().isPresent()) {
                    response += escapeCsv(commit.getCommitter().getName().get());
                }
                response += ",";
                if (commit.getCommitter().getEmail().isPresent()) {
                    response += escapeCsv(commit.getCommitter().getEmail().get());
                }
                response += "," + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format(new Date(commit.getCommitter().getTimestamp())) + ",";
                String message = escapeCsv(commit.getMessage());
                response += message;
                if (entry.newObjectId() == ObjectId.NULL) {
                    // Feature was removed so we need to fill out blank attribute values
                    for (int index = 0; index < attributeLength; index++) {
                        response += ",";
                    }
                } else {
                    // Feature was added or modified so we need to write out the
                    // attribute
                    // values from the feature
                    Optional<RevObject> feature = geogig.command(RevObjectParse.class).setObjectId(entry.newObjectId()).call();
                    RevFeature revFeature = (RevFeature) feature.get();
                    List<Optional<Object>> values = revFeature.getValues();
                    for (int index = 0; index < values.size(); index++) {
                        Optional<Object> value = values.get(index);
                        PropertyDescriptor attrib = (PropertyDescriptor) attribs.toArray()[index];
                        String stringValue = "";
                        if (value.isPresent()) {
                            FieldType attributeType = FieldType.forBinding(attrib.getType().getBinding());
                            switch(attributeType) {
                                case DATE:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy z").format((java.sql.Date) value.get());
                                    break;
                                case DATETIME:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Date) value.get());
                                    break;
                                case TIME:
                                    stringValue = new SimpleDateFormat("HH:mm:ss z").format((Time) value.get());
                                    break;
                                case TIMESTAMP:
                                    stringValue = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z").format((Timestamp) value.get());
                                    break;
                                default:
                                    stringValue = escapeCsv(value.get().toString());
                            }
                            response += "," + stringValue;
                        } else {
                            response += ",";
                        }
                    }
                }
                response += '\n';
                out.write(response);
                response = "";
            }
        }
    } else {
        // Couldn't resolve FeatureType
        throw new CommandSpecException("Couldn't resolve the given path to a feature type.");
    }
}
Also used : Time(java.sql.Time) Timestamp(java.sql.Timestamp) ParseTimestamp(org.locationtech.geogig.api.plumbing.ParseTimestamp) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Date(java.util.Date) FieldType(org.locationtech.geogig.storage.FieldType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with FieldType

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

the class ResponseWriter method writeFeature.

public void writeFeature(RevFeature feature, String tag) throws XMLStreamException {
    out.writeStartElement(tag);
    writeElement("id", feature.getId().toString());
    ImmutableList<Optional<Object>> values = feature.getValues();
    for (Optional<Object> opt : values) {
        final FieldType type = FieldType.forValue(opt);
        String valueString = TextValueSerializer.asString(opt);
        out.writeStartElement("attribute");
        writeElement("type", type.toString());
        writeElement("value", valueString);
        out.writeEndElement();
    }
    out.writeEndElement();
}
Also used : Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) FieldType(org.locationtech.geogig.storage.FieldType)

Example 3 with FieldType

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

the class Insert method createFeature.

private Feature createFeature(List<String> featureChanges, Map<String, SimpleFeatureBuilder> featureTypes) {
    String path = featureChanges.get(0);
    String tree = NodeRef.parentPath(path);
    String featureId = NodeRef.nodeFromPath(path);
    if (!featureTypes.containsKey(tree)) {
        Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:" + tree).call();
        checkParameter(opt.isPresent(), "The parent tree does not exist: " + tree);
        SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) opt.get().type());
        featureTypes.put(tree, builder);
    }
    SimpleFeatureBuilder ftb = featureTypes.get(tree);
    SimpleFeatureType ft = ftb.getFeatureType();
    for (int i = 1; i < featureChanges.size(); i++) {
        String[] tokens = featureChanges.get(i).split("\t");
        Preconditions.checkArgument(tokens.length == 2, "Wrong attribute definition: " + featureChanges.get(i));
        String fieldName = tokens[0];
        AttributeDescriptor desc = ft.getDescriptor(fieldName);
        Preconditions.checkNotNull(desc, "Wrong attribute in feature description");
        FieldType type = FieldType.forBinding(desc.getType().getBinding());
        Object value = TextValueSerializer.fromString(type, tokens[1]);
        ftb.set(tokens[0], value);
    }
    return ftb.buildFeature(featureId);
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) FieldType(org.locationtech.geogig.storage.FieldType)

Example 4 with FieldType

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

the class FormatCommonV1 method readAttributeType.

private static AttributeType readAttributeType(DataInput in, FeatureTypeFactory typeFactory) throws IOException {
    final Name name = readName(in);
    final byte typeTag = in.readByte();
    final FieldType type = FieldType.valueOf(typeTag);
    if (Geometry.class.isAssignableFrom(type.getBinding())) {
        // as opposed to a raw
        final boolean isCRSCode = in.readBoolean();
        // WKT string
        final String crsText = in.readUTF();
        final CoordinateReferenceSystem crs;
        try {
            if (isCRSCode) {
                if ("urn:ogc:def:crs:EPSG::0".equals(crsText)) {
                    crs = null;
                } else {
                    boolean forceLongitudeFirst = crsText.startsWith("EPSG:");
                    crs = CRS.decode(crsText, forceLongitudeFirst);
                }
            } else {
                crs = CRS.parseWKT(crsText);
            }
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        }
        return typeFactory.createGeometryType(name, type.getBinding(), crs, false, false, Collections.<Filter>emptyList(), null, null);
    } else {
        return typeFactory.createAttributeType(name, type.getBinding(), false, false, Collections.<Filter>emptyList(), null, null);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Name(org.opengis.feature.type.Name) FieldType(org.locationtech.geogig.storage.FieldType)

Example 5 with FieldType

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

the class FormatCommonV1 method readFeature.

public static RevFeature readFeature(ObjectId id, DataInput in) throws IOException {
    final int count = in.readInt();
    final ImmutableList.Builder<Optional<Object>> builder = ImmutableList.builder();
    for (int i = 0; i < count; i++) {
        final byte fieldTag = in.readByte();
        final FieldType fieldType = FieldType.valueOf(fieldTag);
        Object value = DataStreamValueSerializerV1.read(fieldType, in);
        builder.add(Optional.fromNullable(value));
    }
    return new RevFeatureImpl(id, builder.build());
}
Also used : Optional(com.google.common.base.Optional) ImmutableList(com.google.common.collect.ImmutableList) RevFeatureImpl(org.locationtech.geogig.api.RevFeatureImpl) RevObject(org.locationtech.geogig.api.RevObject) FieldType(org.locationtech.geogig.storage.FieldType)

Aggregations

FieldType (org.locationtech.geogig.storage.FieldType)9 Optional (com.google.common.base.Optional)5 RevObject (org.locationtech.geogig.api.RevObject)5 ImmutableList (com.google.common.collect.ImmutableList)2 RevFeatureImpl (org.locationtech.geogig.api.RevFeatureImpl)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)2 Name (org.opengis.feature.type.Name)2 FactoryException (org.opengis.referencing.FactoryException)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 Point (com.vividsolutions.jts.geom.Point)1 Integer.toBinaryString (java.lang.Integer.toBinaryString)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 NodeRef (org.locationtech.geogig.api.NodeRef)1 RevCommit (org.locationtech.geogig.api.RevCommit)1