Package com.adaptrex.core.persistence.api

Examples of com.adaptrex.core.persistence.api.ORMPersistenceManager


 
  private static String[]  dateFormatStrings = {"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"};
  @Override
  public ORMModelInstance update(Map<String, Object> data) {
    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(this.extConfig.getFactoryName());
    EntityManager em = (EntityManager) orm.getSession();
    EntityTransaction tx = em.getTransaction();
   
    try {
      tx.begin();
      for (String key : data.keySet()) {
        if (key.equals("id")) {
          continue;
        }
        Object val = data.get(key);

        /*
         * Get our class
         */
        Class<?> clazz = this.entity.getClass();
       
        /*
         * Get info about the field we're updating
         */
        Field field = null;
        try {
          field = clazz.getDeclaredField(key);
        } catch (Exception e) {
        }
       
       
        /*
         * If we don't have a field with the current name, check for a
         * foreign entity
         */
        if (key.endsWith("Id")
            && orm.isManyToOne(clazz,
                key.substring(0, key.length() - 2))) {
         
          key = key.substring(0, key.length() - 2);
          try {
            field = entity.getClass().getDeclaredField(key);
            if (field != null && val != null) {
              val = orm.getEntity(field.getType(), val);
            }
          } catch (Exception e) {
            log.debug("Couldn't set " + field.getName());
            continue;
          }
        }

        /*
         * Check for 1:m or n:m
         */
        if (key.endsWith("Ids")) {
          key = StringUtilities.pluralize(key.substring(0, key.length() - 3));
          if (orm.isManyToOne(clazz, key) || orm.isManyToMany(clazz, key)) {
            Class<?> fieldType = null;
           
            try {
              evictAssociated(em, entity);
            } catch (Exception e) {
              log.info("Couldn't evict associated entity for " + entity.getClass().getName());
            }
           
            try {
              field = entity.getClass().getDeclaredField(key);
              fieldType = field.getType();
             
              List<Object> assocObjList = new ArrayList<Object>();

              Class<?> itemType = null;
              Type type = field.getGenericType();
              if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                itemType = (Class<?>) pt.getActualTypeArguments()[0];
              }
             
              @SuppressWarnings("unchecked")
              List<Object> listItemIds = (List<Object>) StringUtilities.fromJson((String) val, List.class);
              for (Object listItemId : listItemIds) {
                assocObjList.add(orm.getEntity(itemType, listItemId));
              }

              if (fieldType.getSimpleName().equals("Set")) {
                val = new HashSet<Object>(assocObjList);
              } else {
View Full Code Here


  }

  @Override
  public ORMModelInstance delete() {

    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(this.extConfig.getFactoryName());
    EntityManager em = (EntityManager) orm.getSession();
    EntityTransaction tx = em.getTransaction();
    try {
      tx.begin();
     
      try {
View Full Code Here

 
  public static ORMPersistenceManager getPersistenceManager(String factoryName) {
    if (factoryName == null) factoryName = getConfig().get(AdaptrexConfig.PERSISTENCE_ORM_DEFAULT);
    String key = factoryName != null ? factoryName : "_default";
   
    ORMPersistenceManager persistenceManager = persistenceManagers.get(key);
    if (persistenceManager == null) {
      synchronized(AdaptrexRegistry.class) {
        if (persistenceManagers.get(key) != null) {
          return persistenceManagers.get(key);
        }
View Full Code Here

 
  private static String[]  dateFormatStrings = {"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"};
  @Override
  public ORMModelInstance update(Map<String, Object> data) {
    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(this.extConfig.getFactoryName());
    EntityManager em = (EntityManager) orm.getSession();
    EntityTransaction tx = em.getTransaction();
   
    try {
      tx.begin();
      for (String key : data.keySet()) {
        if (key.equals("id")) {
          continue;
        }
        Object val = data.get(key);

        /*
         * Get our class
         */
        Class<?> clazz = this.entity.getClass();
       
        /*
         * Get info about the field we're updating
         */
        Field field = null;
        try {
          field = clazz.getDeclaredField(key);
        } catch (Exception e) {
        }
       
       
        /*
         * If we don't have a field with the current name, check for a
         * foreign entity
         */
        if (key.endsWith("Id")
            && orm.isManyToOne(clazz,
                key.substring(0, key.length() - 2))) {
         
          key = key.substring(0, key.length() - 2);
          try {
            field = entity.getClass().getDeclaredField(key);
            if (field != null && val != null) {
              val = orm.getEntity(field.getType(), val);
            }
          } catch (Exception e) {
            log.debug("Couldn't set " + field.getName());
            continue;
          }
        }

        /*
         * Check for 1:m or n:m
         */
        if (key.endsWith("Ids")) {
          key = StringUtilities.pluralize(key.substring(0, key.length() - 3));
          if (orm.isManyToOne(clazz, key) || orm.isManyToMany(clazz, key)) {
            Class<?> fieldType = null;
           
            try {
              evictAssociated(em, entity);
            } catch (Exception e) {
              log.info("Couldn't evict associated entity for " + entity.getClass().getName());
            }
           
            try {
              field = entity.getClass().getDeclaredField(key);
              fieldType = field.getType();
             
              List<Object> assocObjList = new ArrayList<Object>();

              Class<?> itemType = null;
              Type type = field.getGenericType();
              if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                itemType = (Class<?>) pt.getActualTypeArguments()[0];
              }
             
              @SuppressWarnings("unchecked")
              List<Object> listItemIds = (List<Object>) StringUtilities.fromJson((String) val, List.class);
              for (Object listItemId : listItemIds) {
                assocObjList.add(orm.getEntity(itemType, listItemId));
              }

              if (fieldType.getSimpleName().equals("Set")) {
                val = new HashSet<Object>(assocObjList);
              } else {
View Full Code Here

  }

  @Override
  public ORMModelInstance delete() {

    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(this.extConfig.getFactoryName());
    EntityManager em = (EntityManager) orm.getSession();
    EntityTransaction tx = em.getTransaction();
    try {
      tx.begin();
     
      try {
View Full Code Here

 
  public static ORMPersistenceManager getPersistenceManager(String factoryName) {
    if (factoryName == null) factoryName = getConfig().get(AdaptrexConfig.PERSISTENCE_ORM_DEFAULT);
    String key = factoryName != null ? factoryName : "_default";
   
    ORMPersistenceManager persistenceManager = persistenceManagers.get(key);
    if (persistenceManager == null) {
      synchronized(AdaptrexRegistry.class) {
        if (persistenceManagers.get(key) != null) {
          return persistenceManagers.get(key);
        }
View Full Code Here

      if (params.containsKey("associations")) {
        extConfig.setAssociations(params.get("associations").get(0));
      }     
    }
   
    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(factoryName);
    this.modelInstance = orm.createModelinstance(extConfig, data);
  }
View Full Code Here

    this.modelInstance = orm.createModelinstance(extConfig, data);
  }
 
  public RestModel(String className, Object id, UriInfo uriInfo, String factoryName) {
    ExtConfig extConfig = new ExtConfig(className, factoryName);
    ORMPersistenceManager orm = AdaptrexRegistry.getPersistenceManager(factoryName);
    entity = orm.getEntity(extConfig.getEntityClass(), id);
   
    /*
     * Process parameters
     */
    if (uriInfo != null) {
      MultivaluedMap<String,String> params = uriInfo.getQueryParameters();
      /*
       * Add include/exclude/associations to the config
       */
      if (params.containsKey("include")) {
        extConfig.setIncludes(params.get("include").get(0));
      }
      if (params.containsKey("exclude")) {
        extConfig.setExcludes(params.get("exclude").get(0));
      }
      if (params.containsKey("associations")) {
        extConfig.setAssociations(params.get("associations").get(0));
      }     
    }
   
    this.modelInstance = orm.getModelInstance(extConfig, entity);
  }
View Full Code Here

   
   
    /*
     * First, make sure an association exists
     */
    ORMPersistenceManager orm = parentConfig.getORMPersistenceManager();
   
    String associationSimpleName = associationName;
    if (associationName.contains(".")) {
      associationSimpleName = associationName.split("\\.")[1];
    }
    boolean isManyToOne = orm.isManyToOne(parentClass, associationSimpleName);
    boolean isOneToMany = orm.isOneToMany(parentClass, associationSimpleName) || orm.isManyToMany(parentClass, associationSimpleName);
    if (!isManyToOne && !isOneToMany) {
      throw new RuntimeException("Could not find association from "
          + parentConfig.getModelName() + " to "
          + associationName);
    }

    /*
     * If we've got a :n relationship, get the type of the collection elements
     */
    String entityClassName = associationName;
    if (isOneToMany || isManyToOne) {
      entityClassName = orm.getFieldEntityClass(parentClass, StringUtilities.uncapitalize(associationSimpleName)).getSimpleName();
    }
   
   
    /*
     * Set the association type
View Full Code Here

  public ModelInstance(ExtConfig extConfig, Object id) {
    this(extConfig, id, null, null);
  }

  private ModelInstance(ExtConfig extConfig, Object id, String key, Object value) {
    ORMPersistenceManager orm = extConfig.getORMPersistenceManager();
   
    Object entity;
    if (id != null) {
      entity = orm.getEntity(extConfig.getEntityClass(), id);
    } else {
      entity = orm.getEntity(extConfig.getEntityClass(), key, value);
    }
   
    ORMModelInstance ormModelInstance = orm.getModelInstance(extConfig, entity);
    this.data = ormModelInstance.getData();
  }
View Full Code Here

TOP

Related Classes of com.adaptrex.core.persistence.api.ORMPersistenceManager

Copyright © 2018 www.massapicom. 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.