private NodeRef findOrCreateTree(final String treePath, final FeatureType type) {

    RevTree tree =
        context
            .command(FindOrCreateSubtree.class)
            .setChildPath(treePath)
            .setIndex(true)
            .setParent(workHead)
            .setParentPath(NodeRef.ROOT)
            .call();

    ObjectId metadataId = ObjectId.NULL;
    if (type != null) {
      RevFeatureType revFeatureType = RevFeatureTypeImpl.build(type);
      if (tree.isEmpty()) {
        indexDatabase.put(revFeatureType);
      }
      metadataId = revFeatureType.getId();
    }
    Envelope bounds = SpatialOps.boundsOf(tree);
    Node node =
        Node.create(NodeRef.nodeFromPath(treePath), tree.getId(), metadataId, TYPE.TREE, bounds);

    String parentPath = NodeRef.parentPath(treePath);
    return new NodeRef(node, parentPath, ObjectId.NULL);
  }
示例#2
0
  private static void addElement(
      List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes)
      throws IOException {
    String[] headerTokens = lines.get(0).split("\t");
    if (headerTokens.length == 4 || headerTokens.length == 3) { // feature or feature type
      // modified // modification
      if (lines.size() == 1) { // feature type
        FeatureTypeDiff diff =
            new FeatureTypeDiff(
                headerTokens[0],
                ObjectId.valueOf(headerTokens[1]),
                ObjectId.valueOf(headerTokens[2]));
        patch.addAlteredTree(diff);
      } else { // feature
        String element = Joiner.on("\n").join(lines.subList(1, lines.size()));
        ByteArrayInputStream stream;
        stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
        String operation = headerTokens[0].trim();
        if (operation.equals("M")) {
          String fullPath = headerTokens[1].trim();
          String oldMetadataId = headerTokens[2].trim();
          String newMetadataId = headerTokens[3].trim();
          RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId);
          RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId);

          Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
          for (int i = 1; i < lines.size(); i++) {
            addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType);
          }
          FeatureDiff featureDiff =
              new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType);
          patch.addModifiedFeature(featureDiff);
        } else if (operation.equals("A") || operation.equals("R")) {
          String fullPath = headerTokens[1].trim();
          String featureTypeId = headerTokens[2].trim();
          RevFeatureType revFeatureType;
          revFeatureType = featureTypes.get(featureTypeId);
          FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType);
          RevFeature revFeature = (RevFeature) serializer.read(null, stream);
          Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature);
          if (operation.equals("R")) {
            patch.addRemovedFeature(fullPath, feature, revFeatureType);
          } else {
            patch.addAddedFeature(fullPath, feature, revFeatureType);
          }
        } else {
          throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
        }
      }

    } else if (headerTokens.length == 1) { // feature type definition
      String element = Joiner.on("\n").join(lines);
      ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
      String[] tokens = lines.get(1).split("\t");
      RevFeatureType featureType = (RevFeatureType) serializer.read(null, stream);
      featureTypes.put(featureType.getId().toString(), featureType);
    } else {
      throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
    }
  }
示例#3
0
  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)
            .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");
    }
  }
 private ReferencedEnvelope createProjectedFilter(ObjectId metadataId) {
   final ReferencedEnvelope boundsFilter = this.boundsFilter;
   RevFeatureType featureType = ftypeSource.getFeatureType(metadataId);
   CoordinateReferenceSystem nativeCrs = featureType.type().getCoordinateReferenceSystem();
   if (null == nativeCrs || nativeCrs instanceof DefaultEngineeringCRS) {
     return boundsFilter;
   }
   ReferencedEnvelope transformedFilter;
   try {
     transformedFilter = boundsFilter.transform(nativeCrs, true);
   } catch (TransformException | FactoryException e) {
     throw Throwables.propagate(e);
   }
   return transformedFilter;
 }
示例#5
0
 private static void addDifference(
     String s,
     Map<PropertyDescriptor, AttributeDiff> map,
     RevFeatureType oldRevFeatureType,
     RevFeatureType newRevFeatureType) {
   String[] tokens = s.split("\t");
   PropertyDescriptor descriptor = oldRevFeatureType.type().getDescriptor(tokens[0]);
   if (descriptor == null) {
     descriptor = newRevFeatureType.type().getDescriptor(tokens[0]);
   }
   AttributeDiff ad =
       AttributeDiffFactory.attributeDiffFromText(
           descriptor.getType().getBinding(), s.substring(s.indexOf("\t") + 1));
   map.put(descriptor, ad);
 }
  public void post(Representation entity) {
    InputStream input = null;

    try {
      input = getRequest().getEntity().getStream();
      final GeoGIG ggit = getGeogig(getRequest()).get();
      final Reader body = new InputStreamReader(input);
      final JsonParser parser = new JsonParser();
      final JsonElement conflictJson = parser.parse(body);

      if (conflictJson.isJsonObject()) {
        final JsonObject conflict = conflictJson.getAsJsonObject();
        String featureId = null;
        RevFeature ourFeature = null;
        RevFeatureType ourFeatureType = null;
        RevFeature theirFeature = null;
        RevFeatureType theirFeatureType = null;
        JsonObject merges = null;
        if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) {
          featureId = conflict.get("path").getAsJsonPrimitive().getAsString();
        }
        Preconditions.checkState(featureId != null);

        if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) {
          String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString();
          Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit);
          if (ourNode.isPresent()) {
            Optional<RevObject> object =
                ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call();
            Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);

            ourFeature = (RevFeature) object.get();

            object =
                ggit.command(RevObjectParse.class)
                    .setObjectId(ourNode.get().getMetadataId())
                    .call();
            Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);

            ourFeatureType = (RevFeatureType) object.get();
          }
        }

        if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) {
          String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString();
          Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit);
          if (theirNode.isPresent()) {
            Optional<RevObject> object =
                ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call();
            Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);

            theirFeature = (RevFeature) object.get();

            object =
                ggit.command(RevObjectParse.class)
                    .setObjectId(theirNode.get().getMetadataId())
                    .call();
            Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);

            theirFeatureType = (RevFeatureType) object.get();
          }
        }

        if (conflict.has("merges") && conflict.get("merges").isJsonObject()) {
          merges = conflict.get("merges").getAsJsonObject();
        }
        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(
                new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN));
      }

    } catch (Exception e) {
      throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
    } finally {
      if (input != null) Closeables.closeQuietly(input);
    }
  }