Search in sources :

Example 16 with RevObject

use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.

the class GeoJsonExport method getFeatureType.

private SimpleFeatureType getFeatureType(String path, GeogigCLI cli) {
    checkParameter(path != null, "No path specified.");
    String refspec;
    if (path.contains(":")) {
        refspec = path;
    } else {
        refspec = "WORK_HEAD:" + path;
    }
    checkParameter(!refspec.endsWith(":"), "No path specified.");
    final GeoGIG geogig = cli.getGeogig();
    Optional<ObjectId> rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(refspec.split(":")[0]).call();
    checkParameter(rootTreeId.isPresent(), "Couldn't resolve '" + refspec + "' to a treeish object");
    RevTree rootTree = geogig.getRepository().getTree(rootTreeId.get());
    Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class).setChildPath(refspec.split(":")[1]).setParent(rootTree).setIndex(true).call();
    checkParameter(featureTypeTree.isPresent(), "pathspec '" + refspec.split(":")[1] + "' did not match any valid path");
    Optional<RevObject> revObject = cli.getGeogig().command(RevObjectParse.class).setObjectId(featureTypeTree.get().getMetadataId()).call();
    if (revObject.isPresent() && revObject.get() instanceof RevFeatureType) {
        RevFeatureType revFeatureType = (RevFeatureType) revObject.get();
        if (revFeatureType.type() instanceof SimpleFeatureType) {
            return (SimpleFeatureType) revFeatureType.type();
        } else {
            throw new InvalidParameterException("Cannot find feature type for the specified path");
        }
    } else {
        throw new InvalidParameterException("Cannot find feature type for the specified path");
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevTree(org.locationtech.geogig.api.RevTree)

Example 17 with RevObject

use of org.locationtech.geogig.api.RevObject 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 18 with RevObject

use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.

the class CatWebOp 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) {
    Preconditions.checkArgument(object != null && !object.equals(ObjectId.NULL));
    final Context geogig = this.getCommandLocator(context);
    Preconditions.checkState(geogig.stagingDatabase().exists(object));
    final RevObject revObject = geogig.stagingDatabase().get(object);
    switch(revObject.getType()) {
        case COMMIT:
            context.setResponseContent(new CommandResponse() {

                @Override
                public void write(ResponseWriter out) throws Exception {
                    out.start();
                    out.writeCommit((RevCommit) revObject, "commit", null, null, null);
                    out.finish();
                }
            });
            break;
        case TREE:
            context.setResponseContent(new CommandResponse() {

                @Override
                public void write(ResponseWriter out) throws Exception {
                    out.start();
                    out.writeTree((RevTree) revObject, "tree");
                    out.finish();
                }
            });
            break;
        case FEATURE:
            context.setResponseContent(new CommandResponse() {

                @Override
                public void write(ResponseWriter out) throws Exception {
                    out.start();
                    out.writeFeature((RevFeature) revObject, "feature");
                    out.finish();
                }
            });
            break;
        case FEATURETYPE:
            context.setResponseContent(new CommandResponse() {

                @Override
                public void write(ResponseWriter out) throws Exception {
                    out.start();
                    out.writeFeatureType((RevFeatureType) revObject, "featuretype");
                    out.finish();
                }
            });
            break;
        case TAG:
            context.setResponseContent(new CommandResponse() {

                @Override
                public void write(ResponseWriter out) throws Exception {
                    out.start();
                    out.writeTag((RevTag) revObject, "tag");
                    out.finish();
                }
            });
            break;
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) RevTag(org.locationtech.geogig.api.RevTag) RevObject(org.locationtech.geogig.api.RevObject) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevFeature(org.locationtech.geogig.api.RevFeature) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 19 with RevObject

use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.

the class Cat method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(paths.size() < 2, "Only one refspec allowed");
    checkParameter(!paths.isEmpty(), "A refspec must be specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    String path = paths.get(0);
    Optional<RevObject> obj = geogig.command(RevObjectParse.class).setRefSpec(path).call();
    checkParameter(obj.isPresent(), "refspec did not resolve to any object.");
    if (binary) {
        ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;
        ObjectWriter<RevObject> writer = factory.createObjectWriter(obj.get().getType());
        writer.write(obj.get(), System.out);
    } else {
        CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(obj.get())).call();
        console.println(s);
    }
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) ConsoleReader(jline.console.ConsoleReader) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 20 with RevObject

use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.

the class DiffTree method runInternal.

/**
     * Executes the diff-tree command with the specified options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (refSpec.size() > 2) {
        throw new CommandFailedException("Tree refspecs list is too long :" + refSpec);
    }
    if (treeStats && describe) {
        throw new CommandFailedException("Cannot use --describe and --tree-stats simultaneously");
    }
    GeoGIG geogig = cli.getGeogig();
    org.locationtech.geogig.api.plumbing.DiffTree diff = geogig.command(org.locationtech.geogig.api.plumbing.DiffTree.class);
    String oldVersion = resolveOldVersion();
    String newVersion = resolveNewVersion();
    diff.setOldVersion(oldVersion).setNewVersion(newVersion);
    Iterator<DiffEntry> diffEntries;
    if (paths.isEmpty()) {
        diffEntries = diff.setProgressListener(cli.getProgressListener()).call();
    } else {
        diffEntries = Iterators.emptyIterator();
        for (String path : paths) {
            Iterator<DiffEntry> moreEntries = diff.setPathFilter(path).setProgressListener(cli.getProgressListener()).call();
            diffEntries = Iterators.concat(diffEntries, moreEntries);
        }
    }
    DiffEntry diffEntry;
    HashMap<String, Long[]> stats = Maps.newHashMap();
    while (diffEntries.hasNext()) {
        diffEntry = diffEntries.next();
        StringBuilder sb = new StringBuilder();
        String path = diffEntry.newPath() != null ? diffEntry.newPath() : diffEntry.oldPath();
        if (describe) {
            sb.append(diffEntry.changeType().toString().charAt(0)).append(' ').append(path).append(LINE_BREAK);
            if (diffEntry.changeType() == ChangeType.MODIFIED) {
                FeatureDiff featureDiff = geogig.command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> diffs = featureDiff.getDiffs();
                HashSet<PropertyDescriptor> diffDescriptors = Sets.newHashSet(diffs.keySet());
                NodeRef noderef = diffEntry.changeType() != ChangeType.REMOVED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                int idx = 0;
                for (PropertyDescriptor descriptor : descriptors) {
                    if (diffs.containsKey(descriptor)) {
                        AttributeDiff ad = diffs.get(descriptor);
                        sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                        if (!ad.getType().equals(TYPE.ADDED)) {
                            Object value = ad.getOldValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        if (!ad.getType().equals(TYPE.REMOVED)) {
                            Object value = ad.getNewValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        diffDescriptors.remove(descriptor);
                    } else {
                        sb.append("U ").append(descriptor.getName().toString()).append(LINE_BREAK);
                        sb.append(TextValueSerializer.asString(values.get(idx))).append(LINE_BREAK);
                    }
                    idx++;
                }
                for (PropertyDescriptor descriptor : diffDescriptors) {
                    AttributeDiff ad = diffs.get(descriptor);
                    sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                    if (!ad.getType().equals(TYPE.ADDED)) {
                        Object value = ad.getOldValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                    if (!ad.getType().equals(TYPE.REMOVED)) {
                        Object value = ad.getNewValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                }
            } else {
                NodeRef noderef = diffEntry.changeType() == ChangeType.ADDED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                int i = 0;
                for (Optional<Object> value : values) {
                    sb.append(diffEntry.changeType().toString().charAt(0));
                    sb.append(' ');
                    sb.append(featureType.sortedDescriptors().get(i).getName().toString());
                    sb.append(LINE_BREAK);
                    sb.append(TextValueSerializer.asString(value));
                    sb.append(LINE_BREAK);
                    i++;
                }
                sb.append(LINE_BREAK);
            }
            sb.append(LINE_BREAK);
            cli.getConsole().println(sb.toString());
        } else if (treeStats) {
            String parent = NodeRef.parentPath(path);
            if (!stats.containsKey(parent)) {
                stats.put(parent, new Long[] { 0l, 0l, 0l });
            }
            Long[] counts = stats.get(parent);
            if (diffEntry.changeType() == ChangeType.ADDED) {
                counts[0]++;
            } else if (diffEntry.changeType() == ChangeType.REMOVED) {
                counts[1]++;
            } else if (diffEntry.changeType() == ChangeType.MODIFIED) {
                counts[2]++;
            }
        } else {
            sb.append(path).append(' ');
            sb.append(diffEntry.oldObjectId().toString());
            sb.append(' ');
            sb.append(diffEntry.newObjectId().toString());
            cli.getConsole().println(sb.toString());
        }
    }
    if (treeStats) {
        for (String path : stats.keySet()) {
            StringBuffer sb = new StringBuffer();
            sb.append(path);
            Long[] counts = stats.get(path);
            for (int i = 0; i < counts.length; i++) {
                sb.append(" " + counts[i].toString());
            }
            cli.getConsole().println(sb.toString());
        }
    }
}
Also used : CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) 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) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Aggregations

RevObject (org.locationtech.geogig.api.RevObject)84 ObjectId (org.locationtech.geogig.api.ObjectId)51 RevCommit (org.locationtech.geogig.api.RevCommit)30 NodeRef (org.locationtech.geogig.api.NodeRef)22 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)22 RevTree (org.locationtech.geogig.api.RevTree)21 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 RevFeature (org.locationtech.geogig.api.RevFeature)17 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)16 Feature (org.opengis.feature.Feature)16 IOException (java.io.IOException)15 LinkedList (java.util.LinkedList)15 LogOp (org.locationtech.geogig.api.porcelain.LogOp)14 GeoGIG (org.locationtech.geogig.api.GeoGIG)13 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)11 HashMap (java.util.HashMap)10 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)10 Optional (com.google.common.base.Optional)8 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)8