@Override
 protected void print(RevFeature feature, Writer w) throws IOException {
   ImmutableList<Optional<Object>> values = feature.getValues();
   for (Optional<Object> opt : values) {
     final FieldType type = FieldType.forValue(opt);
     String valueString = TextValueSerializer.asString(opt);
     println(w, type.toString() + "\t" + valueString);
   }
   w.flush();
 }
        private AttributeDescriptor parseAttributeDescriptor(String line) {
          ArrayList<String> tokens = Lists.newArrayList(Splitter.on('\t').split(line));
          Preconditions.checkArgument(
              tokens.size() == 5 || tokens.size() == 6, "Wrong attribute definition: %s", line);
          NameImpl name = new NameImpl(tokens.get(0));
          Class<?> type;
          try {
            type = FieldType.valueOf(tokens.get(1)).getBinding();
          } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Wrong type: " + tokens.get(1));
          }
          int min = Integer.parseInt(tokens.get(2));
          int max = Integer.parseInt(tokens.get(3));
          boolean nillable = Boolean.parseBoolean(tokens.get(4));

          /*
           * Default values that are currently not encoded.
           */
          boolean isIdentifiable = false;
          boolean isAbstract = false;
          List<Filter> restrictions = null;
          AttributeType superType = null;
          InternationalString description = null;
          Object defaultValue = null;

          AttributeType attributeType;
          AttributeDescriptor attributeDescriptor;
          if (Geometry.class.isAssignableFrom(type)) {
            String crsText = tokens.get(5);
            CoordinateReferenceSystem crs = CrsTextSerializer.deserialize(crsText);

            attributeType =
                typeFactory.createGeometryType(
                    name,
                    type,
                    crs,
                    isIdentifiable,
                    isAbstract,
                    restrictions,
                    superType,
                    description);
            attributeDescriptor =
                typeFactory.createGeometryDescriptor(
                    (GeometryType) attributeType, name, min, max, nillable, defaultValue);
          } else {
            attributeType =
                typeFactory.createAttributeType(
                    name, type, isIdentifiable, isAbstract, restrictions, superType, description);
            attributeDescriptor =
                typeFactory.createAttributeDescriptor(
                    attributeType, name, min, max, nillable, defaultValue);
          }
          return attributeDescriptor;
        }
 private Object parseAttribute(String line) {
   List<String> tokens = Lists.newArrayList(Splitter.on('\t').split(line));
   Preconditions.checkArgument(tokens.size() == 2, "Wrong attribute definition: %s", line);
   String typeName = tokens.get(0);
   String value = tokens.get(1);
   FieldType type;
   try {
     type = FieldType.valueOf(typeName);
   } catch (IllegalArgumentException e) {
     throw new IllegalArgumentException("Wrong type name: " + typeName);
   }
   return TextValueSerializer.fromString(type, value);
 }
 private void printAttributeDescriptor(Writer w, PropertyDescriptor attrib)
     throws IOException {
   print(w, attrib.getName().toString());
   print(w, "\t");
   print(w, FieldType.forBinding(attrib.getType().getBinding()).name());
   print(w, "\t");
   print(w, Integer.toString(attrib.getMinOccurs()));
   print(w, "\t");
   print(w, Integer.toString(attrib.getMaxOccurs()));
   print(w, "\t");
   print(w, Boolean.toString(attrib.isNillable()));
   PropertyType attrType = attrib.getType();
   if (attrType instanceof GeometryType) {
     GeometryType gt = (GeometryType) attrType;
     CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
     String crsText = CrsTextSerializer.serialize(crs);
     print(w, "\t");
     println(w, crsText);
   } else {
     println(w, "");
   }
 }
Пример #5
0
  private void writeCSV(GeoGIT geogit, 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 =
        geogit
            .command(FindTreeChild.class)
            .setChildPath(path)
            .setParent(geogit.getRepository().workingTree().getTree())
            .call();
    Optional<RevObject> type = Optional.absent();
    if (ref.isPresent()) {
      type =
          geogit
              .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 =
            geogit
                .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 =
                geogit.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.");
    }
  }