}
Preconditions.checkState(merges != null);
Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
(SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type()
: theirFeatureType.type()));
ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType
: ourFeatureType).sortedDescriptors();
for (Entry<String, JsonElement> entry : merges.entrySet()) {
int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
JsonObject attributeObject = entry.getValue().getAsJsonObject();
if (attributeObject.has("ours")
&& attributeObject.get("ours").isJsonPrimitive()
&& attributeObject.get("ours").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), ourFeature == null ? null
: ourFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("theirs")
&& attributeObject.get("theirs").isJsonPrimitive()
&& attributeObject.get("theirs").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), theirFeature == null ? null
: theirFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("value")
&& attributeObject.get("value").isJsonPrimitive()) {
JsonPrimitive primitive = attributeObject.get("value")
.getAsJsonPrimitive();
if (primitive.isString()) {
try {
Object object = valueFromString(
FieldType.forBinding(descriptor.getType().getBinding()),
primitive.getAsString());
featureBuilder.set(descriptor.getName(), object);
} catch (Exception e) {
throw new Exception("Unable to convert attribute ("
+ entry.getKey() + ") to required type: "
+ descriptor.getType().getBinding().toString());
}
} else if (primitive.isNumber()) {
try {
Object value = valueFromNumber(
FieldType.forBinding(descriptor.getType().getBinding()),
primitive.getAsNumber());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute ("
+ entry.getKey() + ") to required type: "
+ descriptor.getType().getBinding().toString());
}
} else if (primitive.isBoolean()) {
try {
Object value = valueFromBoolean(
FieldType.forBinding(descriptor.getType().getBinding()),
primitive.getAsBoolean());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute ("
+ entry.getKey() + ") to required type: "
+ descriptor.getType().getBinding().toString());
}
} else if (primitive.isJsonNull()) {
featureBuilder.set(descriptor.getName(), null);
} else {
throw new Exception("Unsupported JSON type for attribute value ("
+ entry.getKey() + ")");
}
}
}
}
SimpleFeature feature = featureBuilder
.buildFeature(NodeRef.nodeFromPath(featureId));
RevFeature revFeature = RevFeatureBuilder.build(feature);
ggit.getRepository().stagingDatabase().put(revFeature);
getResponse().setEntity(