use of com.google.common.base.Optional 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();
}
use of com.google.common.base.Optional 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());
}
}
}
use of com.google.common.base.Optional in project GeoGig by boundlessgeo.
the class OSMUnmapOp method _call.
@Override
protected RevTree _call() {
Optional<OSMMappingLogEntry> entry = command(ReadOSMMappingLogEntry.class).setPath(path).call();
if (entry.isPresent()) {
Optional<Mapping> opt = command(ReadOSMMapping.class).setEntry(entry.get()).call();
if (opt.isPresent()) {
mapping = opt.get();
}
}
Iterator<NodeRef> iter = command(LsTreeOp.class).setReference(path).setStrategy(Strategy.FEATURES_ONLY).call();
FeatureMapFlusher flusher = new FeatureMapFlusher(workingTree());
while (iter.hasNext()) {
NodeRef node = iter.next();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
String id = null;
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
if (descriptor.getName().getLocalPart().equals("id")) {
id = values.get(i).get().toString();
}
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
Preconditions.checkNotNull(id, "No 'id' attribute found");
SimpleFeature feature = featureBuilder.buildFeature(id);
unmapFeature(feature, flusher);
}
flusher.flushAll();
if (entry.isPresent()) {
Iterator<DiffEntry> diffs = command(DiffTree.class).setPathFilter(path).setNewTree(workingTree().getTree().getId()).setOldTree(entry.get().getPostMappingId()).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (diff.changeType().equals(DiffEntry.ChangeType.REMOVED)) {
ObjectId featureId = diff.getOldObject().getNode().getObjectId();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(featureId).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(diff.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
String id = null;
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
if (descriptor.getName().getLocalPart().equals("id")) {
id = values.get(i).get().toString();
}
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
Preconditions.checkNotNull(id, "No 'id' attribute found");
SimpleFeature feature = featureBuilder.buildFeature(id);
Class<?> clazz = feature.getDefaultGeometryProperty().getType().getBinding();
String deletePath = clazz.equals(Point.class) ? OSMUtils.NODE_TYPE_NAME : OSMUtils.WAY_TYPE_NAME;
workingTree().delete(deletePath, id);
}
}
}
return workingTree().getTree();
}
use of com.google.common.base.Optional in project GeoGig by boundlessgeo.
the class OSMUnmapOp method unmapNode.
private void unmapNode(SimpleFeature feature, FeatureMapFlusher mapFlusher) {
boolean modified = false;
String id = feature.getID();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.nodeType());
Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + id).call(RevFeature.class);
Map<String, String> tagsMap = Maps.newHashMap();
long timestamp = System.currentTimeMillis();
int version = 1;
long changeset = -1;
String user = UNKNOWN_USER;
Collection<Tag> tags = Lists.newArrayList();
if (rawFeature.isPresent()) {
ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
tags = OSMUtils.buildTagsCollectionFromString(values.get(NODE_TAGS_FIELD_INDEX).get().toString());
for (Tag tag : tags) {
tagsMap.put(tag.getKey(), tag.getValue());
}
Optional<Object> timestampOpt = values.get(NODE_TIMESTAMP_FIELD_INDEX);
if (timestampOpt.isPresent()) {
timestamp = ((Long) timestampOpt.get()).longValue();
}
Optional<Object> versionOpt = values.get(NODE_VERSION_FIELD_INDEX);
if (versionOpt.isPresent()) {
version = ((Integer) versionOpt.get()).intValue();
}
Optional<Object> changesetOpt = values.get(NODE_CHANGESET_FIELD_INDEX);
if (changesetOpt.isPresent()) {
changeset = ((Long) changesetOpt.get()).longValue();
}
Optional<Object> userOpt = values.get(NODE_USER_FIELD_INDEX);
if (userOpt.isPresent()) {
user = (String) userOpt.get();
}
}
Map<String, String> unaliased = Maps.newHashMap();
Collection<Property> properties = feature.getProperties();
for (Property property : properties) {
String name = property.getName().getLocalPart();
if (name.equals("id") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
continue;
}
Object value = property.getValue();
if (value != null) {
String tagName = name;
if (mapping != null) {
if (unaliased.containsKey(name)) {
tagName = unaliased.get(name);
} else {
tagName = mapping.getTagNameFromAlias(path, tagName);
unaliased.put(name, tagName);
}
}
if (!DefaultField.isDefaultField(tagName)) {
if (tagsMap.containsKey(tagName)) {
if (!modified) {
String oldValue = tagsMap.get(tagName);
modified = !value.equals(oldValue);
}
} else {
modified = true;
}
tagsMap.put(tagName, value.toString());
}
}
}
if (!modified && rawFeature.isPresent()) {
// no changes after unmapping tags, so there's nothing else to do
return;
}
Collection<Tag> newTags = Lists.newArrayList();
Set<Entry<String, String>> entries = tagsMap.entrySet();
for (Entry<String, String> entry : entries) {
newTags.add(new Tag(entry.getKey(), entry.getValue()));
}
featureBuilder.set("tags", OSMUtils.buildTagsString(newTags));
featureBuilder.set("location", feature.getDefaultGeometry());
featureBuilder.set("changeset", changeset);
featureBuilder.set("timestamp", timestamp);
featureBuilder.set("version", version);
featureBuilder.set("user", user);
featureBuilder.set("visible", true);
if (rawFeature.isPresent()) {
// the feature has changed, so we cannot reuse some attributes.
// We reconstruct the feature and insert it
featureBuilder.set("timestamp", System.currentTimeMillis());
featureBuilder.set("changeset", null);
featureBuilder.set("version", null);
featureBuilder.set("visible", true);
mapFlusher.put("node", featureBuilder.buildFeature(id));
} else {
// The feature didn't exist, so we have to add it
mapFlusher.put("node", featureBuilder.buildFeature(id));
}
}
use of com.google.common.base.Optional in project GeoGig by boundlessgeo.
the class ResolveTreeish method call.
/**
* @param resolved an {@link Optional} with an ObjectId to resolve
* @return an {@link Optional} of the {@link ObjectId} that was resolved, or
* {@link Optional#absent()} if it did not resolve.
*/
private Optional<ObjectId> call(Optional<ObjectId> resolved) {
if (!resolved.isPresent()) {
return Optional.absent();
}
ObjectId objectId = resolved.get();
if (objectId.isNull()) {
// might be an empty commit ref
return Optional.of(RevTree.EMPTY_TREE_ID);
}
final TYPE objectType = command(ResolveObjectType.class).setObjectId(objectId).call();
switch(objectType) {
case TREE:
// ok
break;
case COMMIT:
{
Optional<RevCommit> commit = command(RevObjectParse.class).setObjectId(objectId).call(RevCommit.class);
if (commit.isPresent()) {
objectId = commit.get().getTreeId();
} else {
objectId = null;
}
break;
}
case TAG:
{
Optional<RevTag> tag = command(RevObjectParse.class).setObjectId(objectId).call(RevTag.class);
if (tag.isPresent()) {
ObjectId commitId = tag.get().getCommitId();
return call(Optional.of(commitId));
}
}
default:
throw new IllegalArgumentException(String.format("Provided ref spec ('%s') doesn't resolve to a tree-ish object: %s", treeishRefSpec, String.valueOf(objectType)));
}
return Optional.fromNullable(objectId);
}
Aggregations