Пример #1
0
  public void formatMultiValue(
      final Iterator<?> iIterator, final StringWriter buffer, final String format)
      throws IOException {
    if (iIterator != null) {
      int counter = 0;
      String objectJson;

      while (iIterator.hasNext()) {
        final Object entry = iIterator.next();
        if (entry != null) {
          if (counter++ > 0) buffer.append(", ");

          if (entry instanceof OIdentifiable) {
            ORecord<?> rec = ((OIdentifiable) entry).getRecord();
            try {
              objectJson = rec.getRecord().toJSON(format);

              buffer.append(objectJson);
            } catch (Exception e) {
              OLogManager.instance()
                  .error(this, "Error transforming record " + rec.getIdentity() + " to JSON", e);
            }
          } else if (OMultiValue.isMultiValue(entry))
            formatMultiValue(OMultiValue.getMultiValueIterator(entry), buffer, format);
          else buffer.append(OJSONWriter.writeValue(entry, format));
        }
      }
    }
  }
Пример #2
0
  public void writeRecords(
      final Iterator<OIdentifiable> iRecords, final String iFetchPlan, String iFormat)
      throws IOException {
    if (iRecords == null) return;

    if (iFormat == null) iFormat = JSON_FORMAT;

    final StringWriter buffer = new StringWriter();
    final OJSONWriter json = new OJSONWriter(buffer, iFormat);
    json.beginObject();

    final String format = iFetchPlan != null ? iFormat + ",fetchPlan:" + iFetchPlan : iFormat;

    // WRITE RECORDS
    json.beginCollection(1, true, "result");
    formatMultiValue(iRecords, buffer, format);
    json.endCollection(1, true);

    json.endObject();

    send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
  }