@Override
  public Object mergeResults(final Map<String, Object> results) throws Exception {

    if (results.isEmpty()) return null;

    Object aggregatedResult = null;

    for (Map.Entry<String, Object> entry : results.entrySet()) {
      final String nodeName = entry.getKey();
      final Object nodeResult = entry.getValue();

      if (nodeResult instanceof Collection) {
        if (aggregatedResult == null) aggregatedResult = new ArrayList();

        ((List) aggregatedResult).addAll((Collection<?>) nodeResult);

      } else if (nodeResult instanceof Exception)

        // RECEIVED EXCEPTION
        throw (Exception) nodeResult;
      else if (nodeResult instanceof OIdentifiable) {
        if (aggregatedResult == null) aggregatedResult = new ArrayList();

        ((List) aggregatedResult).add(nodeResult);

      } else if (nodeResult instanceof Number) {
        if (aggregatedResult == null) aggregatedResult = nodeResult;
        else OMultiValue.add(aggregatedResult, nodeResult);
      }
    }

    return aggregatedResult;
  }
Ejemplo n.º 2
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));
        }
      }
    }
  }
Ejemplo n.º 3
0
 @Override
 public Object execute(
     Object iThis,
     OIdentifiable iCurrentRecord,
     Object iCurrentResult,
     Object[] iParams,
     OCommandContext iContext) {
   if (iParams[0] instanceof Number) {
     addValue((Number) iParams[0]);
   } else if (OMultiValue.isMultiValue(iParams[0])) {
     for (Object n : OMultiValue.getMultiValueIterable(iParams[0])) {
       addValue((Number) n);
     }
   }
   return null;
 }
Ejemplo n.º 4
0
  @Override
  public String toString() {
    if (!deserialized) return "[size=" + size + "]";

    if (size < 10) return OMultiValue.toString(this);
    else return "[size=" + size + "]";
  }
Ejemplo n.º 5
0
  public static String toString(final ORecordLazyMultiValue iMultivalue) {
    final boolean previousAutoConvertSetting = iMultivalue.isAutoConvertToRecord();
    iMultivalue.setAutoConvertToRecord(false);

    final String result = OMultiValue.toString(iMultivalue);

    iMultivalue.setAutoConvertToRecord(previousAutoConvertSetting);
    return result;
  }
Ejemplo n.º 6
0
  @SuppressWarnings("unchecked")
  public void writeResult(Object iResult, final String iFormat)
      throws InterruptedException, IOException {
    if (iResult == null)
      send(
          OHttpUtils.STATUS_OK_NOCONTENT_CODE, "", OHttpUtils.CONTENT_TEXT_PLAIN, null, null, true);
    else {
      if (iResult instanceof Map<?, ?>) {
        iResult = ((Map<?, ?>) iResult).entrySet().iterator();
      } else if (OMultiValue.isMultiValue(iResult)
          && (OMultiValue.getSize(iResult) > 0
              && !(OMultiValue.getFirstValue(iResult) instanceof OIdentifiable))) {
        final List<OIdentifiable> resultSet = new ArrayList<OIdentifiable>();
        resultSet.add(new ODocument().field("value", iResult));
        iResult = resultSet.iterator();

      } else if (iResult instanceof OIdentifiable) {
        // CONVERT SIGLE VALUE IN A COLLECTION
        final List<OIdentifiable> resultSet = new ArrayList<OIdentifiable>();
        resultSet.add((OIdentifiable) iResult);
        iResult = resultSet.iterator();
      } else if (iResult instanceof Iterable<?>)
        iResult = ((Iterable<OIdentifiable>) iResult).iterator();
      else if (OMultiValue.isMultiValue(iResult))
        iResult = OMultiValue.getMultiValueIterator(iResult);
      else {
        final List<OIdentifiable> resultSet = new ArrayList<OIdentifiable>();
        resultSet.add(new ODocument().field("value", iResult));
        iResult = resultSet.iterator();
      }

      if (iResult == null)
        send(
            OHttpUtils.STATUS_OK_NOCONTENT_CODE,
            "",
            OHttpUtils.CONTENT_TEXT_PLAIN,
            null,
            null,
            true);
      else if (iResult instanceof Iterator<?>)
        writeRecords((Iterator<OIdentifiable>) iResult, null, iFormat);
    }
  }
Ejemplo n.º 7
0
  /**
   * Returns a Property value.
   *
   * @param key Property name
   * @return Property value if any, otherwise NULL.
   */
  @Override
  public <T> T getProperty(final String key) {
    if (key == null) return null;

    final OrientBaseGraph graph = getGraph();
    if (key.equals("_class"))
      return (T) ODocumentInternal.getImmutableSchemaClass(getRecord()).getName();
    else if (key.equals("_version")) return (T) new Integer(getRecord().getVersion());
    else if (key.equals("_rid")) return (T) rawElement.getIdentity().toString();

    final Object fieldValue = getRecord().field(key);
    if (graph != null
        && fieldValue instanceof OIdentifiable
        && !(((OIdentifiable) fieldValue).getRecord() instanceof ORecordBytes))
      // CONVERT IT TO VERTEX/EDGE
      return (T) graph.getElement(fieldValue);
    else if (OMultiValue.isMultiValue(fieldValue)
        && OMultiValue.getFirstValue(fieldValue) instanceof OIdentifiable) {
      final OIdentifiable firstValue = (OIdentifiable) OMultiValue.getFirstValue(fieldValue);

      if (firstValue instanceof ODocument) {
        final ODocument document = (ODocument) firstValue;

        if (document.isEmbedded() || ODocumentInternal.getImmutableSchemaClass(document) == null)
          return (T) fieldValue;
      }

      if (graph != null)
        // CONVERT IT TO ITERABLE<VERTEX/EDGE>
        return (T)
            new OrientElementIterable<OrientElement>(
                graph, OMultiValue.getMultiValueIterable(fieldValue));
    }

    return (T) fieldValue;
  }
Ejemplo n.º 8
0
  protected static boolean comparesValues(
      final Object iValue, final ORecord<?> iRecord, final boolean iConsiderIn) {
    // ORID && RECORD
    final ORID other = ((ORecord<?>) iRecord).getIdentity();

    if (!other.isPersistent() && iRecord instanceof ODocument) {
      // ODOCUMENT AS RESULT OF SUB-QUERY: GET THE FIRST FIELD IF ANY
      final String[] firstFieldName = ((ODocument) iRecord).fieldNames();
      if (firstFieldName.length > 0) {
        Object fieldValue = ((ODocument) iRecord).field(firstFieldName[0]);
        if (fieldValue != null) {
          if (iConsiderIn && OMultiValue.isMultiValue(fieldValue)) {
            for (Object o : OMultiValue.getMultiValueIterable(fieldValue)) {
              if (o != null && o.equals(iValue)) return true;
            }
          }

          return fieldValue.equals(iValue);
        }
      }
      return false;
    }
    return other.equals(iValue);
  }
  public Object execute(
      Object iThis,
      final OIdentifiable iCurrentRecord,
      Object iCurrentResult,
      final Object[] iParams,
      OCommandContext iContext) {
    if (iParams.length > 1)
      // IN LINE MODE
      context = new HashSet<Object>();

    for (Object value : iParams) {
      if (value != null) {
        if (iParams.length == 1 && context == null)
          // AGGREGATION MODE (STATEFULL)
          context = new HashSet<Object>();

        if (value instanceof ODocument) context.add(value);
        else OMultiValue.add(context, value);
      }
    }

    return prepareResult(context);
  }
Ejemplo n.º 10
0
  @SuppressWarnings("unchecked")
  private static void fetchCollection(
      final ORecordSchemaAware<?> iRootRecord,
      final Object iUserObject,
      final Map<String, Integer> iFetchPlan,
      final Object fieldValue,
      final String fieldName,
      final int iCurrentLevel,
      final int iLevelFromRoot,
      final int iFieldDepthLevel,
      final Map<ORID, Integer> parsedRecords,
      final String iFieldPathFromRoot,
      final OFetchListener iListener,
      final OFetchContext iContext)
      throws IOException {
    final Collection<?> linked;
    if (fieldValue instanceof ODocument)
      linked = new OMVRBTreeRIDSet().fromDocument((ODocument) fieldValue);
    else if (fieldValue instanceof Collection<?>) {
      linked = (Collection<OIdentifiable>) fieldValue;
      iContext.onBeforeCollection(iRootRecord, fieldName, iUserObject, linked);
    } else if (fieldValue instanceof Map<?, ?>) {
      linked = (Collection<?>) ((Map<?, ?>) fieldValue).values();
      iContext.onBeforeMap(iRootRecord, fieldName, iUserObject);
    } else throw new IllegalStateException("Unrecognized type: " + fieldValue.getClass());

    final Iterator<?> iter;
    if (linked instanceof ORecordLazyMultiValue)
      iter = ((ORecordLazyMultiValue) linked).rawIterator();
    else iter = linked.iterator();

    while (iter.hasNext()) {
      final Object o = iter.next();
      if (o == null) continue;

      if (o instanceof OIdentifiable) {
        OIdentifiable d = (OIdentifiable) o;

        // GO RECURSIVELY
        final Integer fieldDepthLevel = parsedRecords.get(d.getIdentity());
        if (!d.getIdentity().isValid()
            || (fieldDepthLevel != null && fieldDepthLevel.intValue() == iLevelFromRoot)) {
          removeParsedFromMap(parsedRecords, d);
          d = d.getRecord();

          if (!(d instanceof ODocument)) {
            iListener.processStandardField(null, d, fieldName, iContext, iUserObject, "");
          } else {
            iContext.onBeforeDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
            final Object userObject =
                iListener.fetchLinkedCollectionValue(
                    iRootRecord, iUserObject, fieldName, (ODocument) d, iContext);
            processRecord(
                (ODocument) d,
                userObject,
                iFetchPlan,
                iCurrentLevel,
                iLevelFromRoot,
                iFieldDepthLevel,
                parsedRecords,
                iFieldPathFromRoot,
                iListener,
                iContext,
                "");
            iContext.onAfterDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
          }
        } else {
          iListener.parseLinkedCollectionValue(iRootRecord, d, iUserObject, fieldName, iContext);
        }
      } else if (o instanceof Map<?, ?>) {
        fetchMap(
            iRootRecord,
            iUserObject,
            iFetchPlan,
            o,
            null,
            iCurrentLevel + 1,
            iLevelFromRoot,
            iFieldDepthLevel,
            parsedRecords,
            iFieldPathFromRoot,
            iListener,
            iContext);
      } else if (OMultiValue.isMultiValue(o)) {
        fetchCollection(
            iRootRecord,
            iUserObject,
            iFetchPlan,
            o,
            null,
            iCurrentLevel + 1,
            iLevelFromRoot,
            iFieldDepthLevel,
            parsedRecords,
            iFieldPathFromRoot,
            iListener,
            iContext);
      }
    }

    if (fieldValue instanceof Collection<?>)
      iContext.onAfterCollection(iRootRecord, fieldName, iUserObject);
    else if (fieldValue instanceof Map<?, ?>)
      iContext.onAfterMap(iRootRecord, fieldName, iUserObject);
  }
Ejemplo n.º 11
0
  @SuppressWarnings("unchecked")
  private static void fetchMap(
      final ORecordSchemaAware<?> iRootRecord,
      final Object iUserObject,
      final Map<String, Integer> iFetchPlan,
      Object fieldValue,
      String fieldName,
      final int iCurrentLevel,
      final int iLevelFromRoot,
      final int iFieldDepthLevel,
      final Map<ORID, Integer> parsedRecords,
      final String iFieldPathFromRoot,
      final OFetchListener iListener,
      final OFetchContext iContext)
      throws IOException {
    final Map<String, ODocument> linked = (Map<String, ODocument>) fieldValue;
    iContext.onBeforeMap(iRootRecord, fieldName, iUserObject);

    for (Object key : linked.keySet()) {
      final Object o = linked.get(key);

      if (o instanceof OIdentifiable) {
        final ODocument d = ((OIdentifiable) o).getRecord();
        if (d != null) {
          // GO RECURSIVELY
          final Integer fieldDepthLevel = parsedRecords.get(d.getIdentity());
          if (!d.getIdentity().isValid()
              || (fieldDepthLevel != null && fieldDepthLevel.intValue() == iLevelFromRoot)) {
            removeParsedFromMap(parsedRecords, d);
            iContext.onBeforeDocument(iRootRecord, d, key.toString(), iUserObject);
            final Object userObject =
                iListener.fetchLinkedMapEntry(
                    iRootRecord, iUserObject, fieldName, key.toString(), d, iContext);
            processRecord(
                d,
                userObject,
                iFetchPlan,
                iCurrentLevel,
                iLevelFromRoot,
                iFieldDepthLevel,
                parsedRecords,
                iFieldPathFromRoot,
                iListener,
                iContext,
                "");
            iContext.onAfterDocument(iRootRecord, d, key.toString(), iUserObject);
          } else {
            iListener.parseLinked(iRootRecord, d, iUserObject, key.toString(), iContext);
          }
        }
      } else if (o instanceof Map) {
        fetchMap(
            iRootRecord,
            iUserObject,
            iFetchPlan,
            o,
            key.toString(),
            iCurrentLevel + 1,
            iLevelFromRoot,
            iFieldDepthLevel,
            parsedRecords,
            iFieldPathFromRoot,
            iListener,
            iContext);
      } else if (OMultiValue.isMultiValue(o)) {
        fetchCollection(
            iRootRecord,
            iUserObject,
            iFetchPlan,
            o,
            key.toString(),
            iCurrentLevel + 1,
            iLevelFromRoot,
            iFieldDepthLevel,
            parsedRecords,
            iFieldPathFromRoot,
            iListener,
            iContext);
      } else
        iListener.processStandardField(iRootRecord, o, key.toString(), iContext, iUserObject, "");
    }
    iContext.onAfterMap(iRootRecord, fieldName, iUserObject);
  }
Ejemplo n.º 12
0
  private static void processRecord(
      final ORecordSchemaAware<?> record,
      final Object iUserObject,
      final Map<String, Integer> iFetchPlan,
      final int iCurrentLevel,
      final int iLevelFromRoot,
      final int iFieldDepthLevel,
      final Map<ORID, Integer> parsedRecords,
      final String iFieldPathFromRoot,
      final OFetchListener iListener,
      final OFetchContext iContext,
      final String iFormat)
      throws IOException {

    Object fieldValue;

    iContext.onBeforeFetch(record);
    if (debug) {
      System.out.println("processRecord start");
      System.out.println("iFieldDepthLevel: " + iFieldDepthLevel);
      System.out.println("record: " + record.toString());
      System.out.println("iFetchPlan: " + iFetchPlan);
      System.out.println("iCurrentLevel: " + iCurrentLevel);
      System.out.println("iLevelFromRoot: " + iLevelFromRoot);
      System.out.println("iCurrentLevel: " + iCurrentLevel);
      System.out.println("parsedRecords: " + parsedRecords);
      System.out.println("iFieldPathFromRoot: " + iFieldPathFromRoot);
    }

    for (String fieldName : record.fieldNames()) {
      String fieldPath =
          !iFieldPathFromRoot.isEmpty() ? iFieldPathFromRoot + "." + fieldName : fieldName;
      if (debug) {
        System.out.println("     fieldName: " + fieldName);
        System.out.println("     fieldPath: " + fieldPath);
      }
      int depthLevel;
      depthLevel = getDepthLevel(iFetchPlan, fieldPath);
      if (depthLevel == -2) continue;
      if (iFieldDepthLevel > -1) depthLevel = iFieldDepthLevel;

      if (debug) System.out.println("     depthLevel: " + depthLevel);

      fieldValue = record.field(fieldName);

      boolean fetch =
          !iFormat.contains("shallow")
              && (!(fieldValue instanceof ODocument)
                  || depthLevel == -1
                  || iCurrentLevel <= depthLevel
                  || iFetchPlan.containsKey(fieldPath));

      final boolean isEmbedded =
          fieldValue instanceof ODocument
              && (((ODocument) fieldValue).isEmbedded()
                  || !((ODocument) fieldValue).getIdentity().isValid());

      if (!fetch && isEmbedded && iContext.fetchEmbeddedDocuments())
        // EMBEDDED, GO DEEPER
        fetch = true;

      if (iFormat.contains("shallow")
          || fieldValue == null
          || (!fetch && fieldValue instanceof OIdentifiable)
          || !(fieldValue instanceof OIdentifiable)
              && (!(fieldValue instanceof ORecordLazyMultiValue)
                  || !((ORecordLazyMultiValue) fieldValue).rawIterator().hasNext()
                  || !(((ORecordLazyMultiValue) fieldValue).rawIterator().next()
                      instanceof OIdentifiable))
              && (!(OMultiValue.getFirstValue(fieldValue) instanceof OIdentifiable
                  || OMultiValue.getFirstValue(OMultiValue.getFirstValue(fieldValue))
                      instanceof OIdentifiable
                  || OMultiValue.getFirstValue(
                          OMultiValue.getFirstValue(OMultiValue.getFirstValue(fieldValue)))
                      instanceof OIdentifiable))) {
        iContext.onBeforeStandardField(fieldValue, fieldName, iUserObject);
        iListener.processStandardField(
            record, fieldValue, fieldName, iContext, iUserObject, iFormat);
        iContext.onAfterStandardField(fieldValue, fieldName, iUserObject);
      } else {
        try {
          if (fetch) {
            final int nextLevel = isEmbedded ? iLevelFromRoot : iLevelFromRoot + 1;

            fetch(
                record,
                iUserObject,
                iFetchPlan,
                fieldValue,
                fieldName,
                iCurrentLevel,
                nextLevel,
                iFieldDepthLevel,
                parsedRecords,
                depthLevel,
                fieldPath,
                iListener,
                iContext);
          }

        } catch (Exception e) {
          e.printStackTrace();
          OLogManager.instance()
              .error(null, "Fetching error on record %s", e, record.getIdentity());
        }
      }
    }

    iContext.onAfterFetch(record);
  }