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");
}
}
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.");
}
}
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;
}
}
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);
}
}
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());
}
}
}
Aggregations