Package com.dooapp.gaedo.blueprints

Source Code of com.dooapp.gaedo.blueprints.GraphUtils

package com.dooapp.gaedo.blueprints;

import com.dooapp.gaedo.blueprints.transformers.LiteralTransformer;
import com.dooapp.gaedo.blueprints.transformers.Literals;
import com.dooapp.gaedo.properties.Property;
import com.tinkerpop.blueprints.pgm.CloseableSequence;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
import com.tinkerpop.blueprints.pgm.Vertex;

public class GraphUtils {

  /**
   * Helper function locating first matching vertex with the given property value
   * @param graph
   * @param property
   * @param object
   * @return
   */
  public static Vertex locateVertex(IndexableGraph graph, String property, Object value) {
    CloseableSequence<Vertex> matching = graph.getIndex(Index.VERTICES, Vertex.class).get(property, value);
    if(matching.hasNext()) {
      return matching.next();
    } else {
      return null;
    }
  }

  public static Vertex locateVertex(IndexableGraph graph, Properties p, Object value) {
    return locateVertex(graph, p.name(), value);
  }

  /**
   * Generate edge name from property infos
   * @param p source property
   * @return an edge name (by default property container class name + "." + property name
   */
  public static String getEdgeNameFor(Property p) {
    return p.getDeclaringClass().getSimpleName()+"."+p.getName();
  }

  /**
   * Create a vertex out of a basic object. if object is of a simple type, we'll use value as id. Elsewhere, we will generate an id for object
   * @param database database in which vertex will be stored
   * @param value
   * @return
   */
  public static Vertex getVertexForBasicObject(IndexableGraph database, Object value) {
    Vertex returned = null;
    // Now distinct behaviour between known objects and unknown ones
    Class<? extends Object> valueClass = value.getClass();
    if(Literals.containsKey(valueClass)) {
      LiteralTransformer transformer = Literals.get(valueClass);
      returned = transformer.getVertexFor(database, valueClass.cast(value));
    } else {
      throw new ObjectIsNotARealLiteralException(value, valueClass);
      // TODO do not forget to set id property
    }
    returned.setProperty(Properties.type.name(), valueClass.getName());
    return returned;
  }

  /**
   * Create an object instance from a managed vertice compatible with this service contained class
   * @param key vertex containing object id
   * @return a fresh instance, with only id set
   */
  public static Object createInstance(Vertex key) {
    String effectiveType = key.getProperty(Properties.type.name()).toString();
    try {
      Class effectiveClass = Class.forName(effectiveType);
      if(Literals.containsKey(effectiveClass)) {
        LiteralTransformer transformer = Literals.get(effectiveClass);
        return transformer.loadObject(effectiveClass, key);
      } else {
        return  effectiveClass.newInstance();
      }
    } catch(Exception e) {
      throw new UnableToCreateException(effectiveType, e);
    }
  }

  /**
   * Get an id for any object, provided one property can be used to define an id on it
   * @param database source database
   * @param object used object
   * @param idProperty id property
   * @return a composite id made of id container class, and the result of {@link LiteralTransformer#getVertexId(com.tinkerpop.blueprints.pgm.Graph, Object)}
   */
  public static Object getIdVertexId(IndexableGraph database, Object object, Property idProperty) {
    Object objectId = idProperty.get(object);
    return idProperty.getDeclaringClass().getCanonicalName()+":"+Literals.get(objectId.getClass()).getVertexId(database, objectId);
  }

}
TOP

Related Classes of com.dooapp.gaedo.blueprints.GraphUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.