Package com.adaptrex.core.ext.data

Examples of com.adaptrex.core.ext.data.DataConfig


     *
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getEntityList(AdaptrexSession session, Store store) {
  DataConfig config = store.getConfig();
  List<Object> list = new ArrayList<Object>();

  try {
      EntityManager em = (EntityManager) session.getPersistence().getNativeSession();

      Map<String, Object> parameters = new HashMap<String, Object>(config.getParams());

     
      Class<?> clazz = config.getClazz();
      String className = clazz.getSimpleName();
      String alias = className.toLowerCase().substring(0, 1);

      /*
       * Core JPQL
       */
      String jpql = "SELECT " + alias + " FROM " + className + " " + alias;

      /*
       * Where
       */
      String where = "";
      if (config.getWhere() != null) {
    where += config.getWhere();
      }

      /*
       * Filter
       */
      for (Filter filter : config.getFilters()) {
      if (!where.isEmpty()) {
        where += " AND ";
      }

      String val = String.valueOf(filter.getValue());
      String prop = filter.getProperty();
      String param = prop.replace(".", "_");

      /*
       * Handle between inclusive
       */
      if (val.contains(">=<")) {
        String[] filterParts = val.split(">=<");

        where += "(" + alias + "." + prop + " BETWEEN :"
          + param + "_low AND :" + param + "_high)"
          + "";
        parameters.put(param + "_low", filterParts[0]);
        parameters.put(param + "_high", filterParts[1]);

      /*
       * Handle "OR"
       */
      } else if (val.contains("||")) {
        String whereOr = "";
        int i = 1;

        for (String orPart : val.split("\\|\\|")) {
        whereOr += whereOr.isEmpty() ? "(" : " OR ";
        whereOr += alias + "." + prop + " = :" + param + i;
        parameters.put(param + i, orPart);
        i++;
        }
        whereOr += ")";
        where += whereOr;

      } else {
        String eqType = val.contains("%") ? " like " : " = ";
        if (eqType.contains("like")) {
          where += "lower(" + alias + "." + prop + ")" + eqType + ":" + param;

        } else {
          where += alias + "." + prop + eqType + ":" + param;
        }

        parameters.put(param, val);
      }
      }


      /*
       * Add where clause
       */
      if (!where.isEmpty()) {
    jpql += " WHERE " + where;
      }


      if (config.getSorters().size() > 0) {
    String sortClause = "";
    for (Sorter sorter : config.getSorters()) {
        if (!sortClause.isEmpty()) {
      sortClause += ",";
        }
        sortClause += alias + "." + sorter.getProperty() + " " + sorter.getDirection();
    }
    jpql += " ORDER BY " + sortClause;
      }

      log.debug("JPQL: " + jpql);
      Query q = em.createQuery(jpql, clazz);

      /*
       * Add params
       * TODO: This is tedius... we want to automatically determine the
       * type for the field we're trying to set the parameter for
       */
      for (String key : parameters.keySet()) {
    Object paramObj = parameters.get(key);

    Class<?> paramType = q.getParameter(key).getParameterType();
    String typeName = paramType.getSimpleName().toLowerCase();

    try {
        if (typeName.equals("boolean")) {
      String stringVal = (String) paramObj;
      paramObj = stringVal.equals("1") || stringVal.equals("true") || stringVal.equals("Y");

        } else if (typeName.equals("date")) {
      paramObj = dateFormat.parse((String) paramObj);

        } else {
      Method m = paramType.getDeclaredMethod("valueOf", String.class);
      paramObj = m.invoke(null, paramObj);
        }
    } catch (Exception e) {
    }

    log.debug("  Param: " + key + " = " + paramObj);
    q.setParameter(key, paramObj);
      }

      store.setTotalCount(q.getResultList().size());

      if (config.getPageSize() != null) {
    q.setMaxResults(config.getPageSize());
      }
      if (config.getStart() != null) {
    q.setFirstResult(config.getStart());
      }
      list = q.getResultList();
  } catch (Exception e) {
      log.warn("Error", e);
  }
View Full Code Here


     * Get the config
     */
    AdaptrexPersistence persistence = Adaptrex.getAdaptrex()
        .getPersistenceManager().getPersistence(factoryName);
    Class<?> clazz = persistence.getEntityClass(className);
    DataConfig config = new DataConfig(clazz, persistence);

    /*
     * Create the model
     */
    AdaptrexSession session = new AdaptrexSession(persistence);
    Model model = new Model(session, config);

    /*
     * Set the model name
     */
    String modelName = namespace + ".model."
        + (name != null ? name : className);
    config.setModelName(name);

    if (include != null) {
      config.include(include);
    }
    if (exclude != null) {
      config.exclude(exclude);
    }

    /*
     * Add associations
     */
    if (associations != null) {
      config.associations(associations);
    }

    /*
     * The proxy needs additinoal information from the model. We shouldn't
     * create the proxy until the store has been fully configured
     */
    if (rest != null) {
      String restPath = getRequest().getContextPath()
          + "/rest/"
          + (rest.equals("true") ? clazz.getSimpleName()
              .toLowerCase() : rest);
      config.setProxy(new RestProxy(restPath, config));
    }

    /*
     * TODO: Make sure we're outputting our model definition in the same way
     * as in the store
View Full Code Here

       * Get the config
       */
      AdaptrexPersistence persistence =
          Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
      Class<?> clazz = persistence.getEntityClass(className);
      DataConfig config = new DataConfig(clazz, persistence);


      /*
       * Create the store
       */
      AdaptrexSession session = new AdaptrexSession(persistence);
      Store store = new Store(session, config);


      /*
       * Set the model name
       */
      String modelName = namespace + ".model." + (name != null ? name : className);
      config.setNamespace(namespace);
      config.setModelName(modelName);

      /*
       * If our store doesn't have a class drop out
       */
      if (clazz == null) {
        write("<!-- Error creating store: Could not find entity for " + className + " -->\n");
        return;
      }


      /*
       * Various store options
       */
      if (pageSize != null) {
        config.setPageSize(Integer.valueOf(pageSize));
      }
      if (include != null) {
        config.include(include);
      }
      if (exclude != null) {
        config.exclude(exclude);
      }

      if (clearOnPageLoad != null && clearOnPageLoad.equals("true")) {
        config.setClearOnPageLoad(true);
      }
      if (clearRemovedOnLoad != null && clearRemovedOnLoad.equals("true")) {
        config.setClearRemovedOnLoad(true);
      }
      if (autoSync != null && autoSync.equals("true")) {
        config.setAutoSync(true);
      }
      if (autoLoad != null && autoLoad.equals("true")) {
        config.setAutoLoad(true);
      }
      if (remoteGroup != null && remoteGroup.equals("true")) {
        config.setRemoteGroup(true);
      }
      if (remoteSort != null && remoteSort.equals("true")) {
        config.setRemoteSort(true);
      }
      if (remoteFilter != null && remoteFilter.equals("true")) {
        config.setRemoteFilter(true);
      }
      if (sortOnFilter != null && sortOnFilter.equals("true")) {
        config.setSortOnFilter(true);
      }


      /*
       * Filtering
       */
      if (filter != null) {
        for (String filterItem : filter.split(",")) {
          String[] parts = filterItem.split("=");
          String property = parts[0];
          String value = parts.length > 1 ? parts[1] : null;
          config.filter(property, value);
        }
      }


      /*
       * Grouping
       */
      if (group != null) {
        for (String groupItem : group.split(",")) {
          String[] parts = groupItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : null;
          String root = parts.length > 2 ? parts[2] : null;
          config.group(property, direction, root);
        }
      }

      /*
       * Sorting
       */
      if (sort != null) {
        for (String sortItem : sort.split(",")) {
          String[] parts = sortItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : "ASC";
          String root = parts.length > 2 ? parts[2] : null;

          config.sort(property, direction, root);
        }
      }

      /*
       * Buffering
       */
      if (buffered != null && buffered.contains("false")) {
        config.setBuffered(true);
        if (!buffered.equals("true")) {
          String[] parts = buffered.split(":");
          config.setTrailingBufferZone(Integer.valueOf(parts[0]));
          if (parts.length > 1) {
            config.setLeadingBufferZone(Integer.valueOf(parts[1]));
          }
          if (parts.length > 1) {
            config.setPurgePageCount(Integer.valueOf(parts[2]));
          }
        }
      }


      /*
       * Add associations
       */
      if (associations != null) {
        config.associations(associations);
      }


      /*
       * Load inline data?
       */
      if (inline != null && inline.equals("true")) {
        config.setInline(true);
      }


      if (where != null) {
        config.setWhere(where);
      }

      if (param != null) {
        for (String p : param.split(",")) {
          String[] parts = p.split(":");
          config.param(parts[0], parts[1]);
        }
      }

      if (start != null) {
        config.setStart(Integer.valueOf(start));
      }
     
     

      /*
       * The proxy needs additinoal information from the store.  We shouldn't create
       * the proxy until the store has been fully configured
       */
      if (rest != null) {
        String restPath = getRequest().getContextPath() + "/rest/"
            + (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
        config.setProxy(new RestProxy(restPath, config));
      }


      StoreDefinition storeDefinition = store.getStoreDefinition();
      ModelDefinition baseModelDefinition = store.getModelDefinition();

      /*
       * Write the javascript for this store/model.
       *
       * During development using Ext.Loader, we need to syncRequire Ext.data.Model.
       * If we asynchronously load all of our model's dependencies, they will not be
       * available at the time we define our store.  If they are not available, our
       * model definition will not yet be instantiated and Ext.Loader will attempt (and fail)
       * to load it dynamically.
       */
      String extBuild = param(a, AdaptrexConfig.EXT_BUILD);
      if (extBuild == null) extBuild = Adaptrex.getAdaptrex().getConfig().get(AdaptrexConfig.EXT_BUILD, "production")
     
      String storeName = namespace + ".store." + StringUtilities.pluralize(config.getSimpleModelName());
     
      /*
       * Build the output javascript
       */
      String output = "Ext.define(\"" + modelName + "\"," + StringUtilities.json(baseModelDefinition) + ");\n"
View Full Code Here

     *
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getEntityList(AdaptrexSession session, Store store) {
  DataConfig config = store.getConfig();
  List<Object> list = new ArrayList<Object>();

  try {
      EntityManager em = (EntityManager) session.getPersistence().getNativeSession();

      Map<String, Object> parameters = new HashMap<String, Object>(config.getParams());

     
      Class<?> clazz = config.getClazz();
      String className = clazz.getSimpleName();
      String alias = className.toLowerCase().substring(0, 1);

      /*
       * Core JPQL
       */
      String jpql = "SELECT " + alias + " FROM " + className + " " + alias;

      /*
       * Where
       */
      String where = "";
      if (config.getWhere() != null) {
    where += config.getWhere();
      }

      /*
       * Filter
       */
      for (Filter filter : config.getFilters()) {
      if (!where.isEmpty()) {
        where += " AND ";
      }

      String val = String.valueOf(filter.getValue());
      String prop = filter.getProperty();
      String param = prop.replace(".", "_");

      /*
       * Handle between inclusive
       */
      if (val.contains(">=<")) {
        String[] filterParts = val.split(">=<");

        where += "(" + alias + "." + prop + " BETWEEN :"
          + param + "_low AND :" + param + "_high)"
          + "";
        parameters.put(param + "_low", filterParts[0]);
        parameters.put(param + "_high", filterParts[1]);

      /*
       * Handle "OR"
       */
      } else if (val.contains("||")) {
        String whereOr = "";
        int i = 1;

        for (String orPart : val.split("\\|\\|")) {
        whereOr += whereOr.isEmpty() ? "(" : " OR ";
        whereOr += alias + "." + prop + " = :" + param + i;
        parameters.put(param + i, orPart);
        i++;
        }
        whereOr += ")";
        where += whereOr;

      } else {
        String eqType = val.contains("%") ? " like " : " = ";
        if (eqType.contains("like")) {
          where += "lower(" + alias + "." + prop + ")" + eqType + ":" + param;

        } else {
          where += alias + "." + prop + eqType + ":" + param;
        }

        parameters.put(param, val);
      }
      }


      /*
       * Add where clause
       */
      if (!where.isEmpty()) {
    jpql += " WHERE " + where;
      }


      if (config.getSorters().size() > 0) {
    String sortClause = "";
    for (Sorter sorter : config.getSorters()) {
        if (!sortClause.isEmpty()) {
      sortClause += ",";
        }
        sortClause += alias + "." + sorter.getProperty() + " " + sorter.getDirection();
    }
    jpql += " ORDER BY " + sortClause;
      }

      log.debug("JPQL: " + jpql);
      Query q = em.createQuery(jpql, clazz);

      /*
       * Add params
       * TODO: This is tedius... we want to automatically determine the
       * type for the field we're trying to set the parameter for
       */
      for (String key : parameters.keySet()) {
    Object paramObj = parameters.get(key);

    Class<?> paramType = q.getParameter(key).getParameterType();
    String typeName = paramType.getSimpleName().toLowerCase();

    try {
        if (typeName.equals("boolean")) {
      String stringVal = (String) paramObj;
      paramObj = stringVal.equals("1") || stringVal.equals("true") || stringVal.equals("Y");

        } else if (typeName.equals("date")) {
      paramObj = dateFormat.parse((String) paramObj);

        } else {
      Method m = paramType.getDeclaredMethod("valueOf", String.class);
      paramObj = m.invoke(null, paramObj);
        }
    } catch (Exception e) {
    }

    log.debug("  Param: " + key + " = " + paramObj);
    q.setParameter(key, paramObj);
      }

      store.setTotalCount(q.getResultList().size());

      if (config.getPageSize() != null) {
    q.setMaxResults(config.getPageSize());
      }
      if (config.getStart() != null) {
    q.setFirstResult(config.getStart());
      }
      list = q.getResultList();
  } catch (Exception e) {
      log.warn("Error", e);
  }
View Full Code Here

     *
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getEntityList(AdaptrexSession session, Store store) {
  DataConfig config = store.getConfig();
  List<Object> list = new ArrayList<Object>();

  try {
      EntityManager em = (EntityManager) session.getPersistence().getNativeSession();

      Map<String, Object> parameters = new HashMap<String, Object>(config.getParams());

     
      Class<?> clazz = config.getClazz();
      String className = clazz.getSimpleName();
      String alias = className.toLowerCase().substring(0, 1);

      /*
       * Core JPQL
       */
      String jpql = "SELECT " + alias + " FROM " + className + " " + alias;

      /*
       * Where
       */
      String where = "";
      if (config.getWhere() != null) {
    where += config.getWhere();
      }

      /*
       * Filter
       */
      for (Filter filter : config.getFilters()) {
      if (!where.isEmpty()) {
        where += " AND ";
      }

      String val = String.valueOf(filter.getValue());
      String prop = filter.getProperty();
      String param = prop.replace(".", "_");

      /*
       * Handle between inclusive
       */
      if (val.contains(">=<")) {
        String[] filterParts = val.split(">=<");

        where += "(" + alias + "." + prop + " BETWEEN :"
          + param + "_low AND :" + param + "_high)"
          + "";
        parameters.put(param + "_low", filterParts[0]);
        parameters.put(param + "_high", filterParts[1]);

      /*
       * Handle "OR"
       */
      } else if (val.contains("||")) {
        String whereOr = "";
        int i = 1;

        for (String orPart : val.split("\\|\\|")) {
        whereOr += whereOr.isEmpty() ? "(" : " OR ";
        whereOr += alias + "." + prop + " = :" + param + i;
        parameters.put(param + i, orPart);
        i++;
        }
        whereOr += ")";
        where += whereOr;

      } else {
        String eqType = val.contains("%") ? " like " : " = ";
        if (eqType.contains("like")) {
          where += "lower(" + alias + "." + prop + ")" + eqType + ":" + param;

        } else {
          where += alias + "." + prop + eqType + ":" + param;
        }

        parameters.put(param, val);
      }
      }


      /*
       * Add where clause
       */
      if (!where.isEmpty()) {
    jpql += " WHERE " + where;
      }


      if (config.getSorters().size() > 0) {
    String sortClause = "";
    for (Sorter sorter : config.getSorters()) {
        if (!sortClause.isEmpty()) {
      sortClause += ",";
        }
        sortClause += alias + "." + sorter.getProperty() + " " + sorter.getDirection();
    }
    jpql += " ORDER BY " + sortClause;
      }

      log.debug("JPQL: " + jpql);
      Query q = em.createQuery(jpql, clazz);

      /*
       * Add params
       * TODO: This is tedius... we want to automatically determine the
       * type for the field we're trying to set the parameter for
       */
      for (String key : parameters.keySet()) {
    Object paramObj = parameters.get(key);

    Class<?> paramType = q.getParameter(key).getParameterType();
    String typeName = paramType.getSimpleName().toLowerCase();

    try {
        if (typeName.equals("boolean")) {
      String stringVal = (String) paramObj;
      paramObj = stringVal.equals("1") || stringVal.equals("true") || stringVal.equals("Y");

        } else if (typeName.equals("date")) {
      paramObj = dateFormat.parse((String) paramObj);

        } else {
      Method m = paramType.getDeclaredMethod("valueOf", String.class);
      paramObj = m.invoke(null, paramObj);
        }
    } catch (Exception e) {
    }

    log.debug("  Param: " + key + " = " + paramObj);
    q.setParameter(key, paramObj);
      }

      store.setTotalCount(q.getResultList().size());

      if (config.getPageSize() != null) {
    q.setMaxResults(config.getPageSize());
      }
      if (config.getStart() != null) {
    q.setFirstResult(config.getStart());
      }
      list = q.getResultList();
  } catch (Exception e) {
      log.warn("Error", e);
  }
View Full Code Here

   * Get the config
   */
  AdaptrexPersistence persistence =
    Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
  Class<?> clazz = persistence.getEntityClass(className);
  DataConfig config = new DataConfig(clazz, persistence);


  /*
   * Create the model
   */
  AdaptrexSession session = new AdaptrexSession(persistence);
  Model model = new Model(session, config);

 
  /*
   * Set the model name
   */
  String modelName = ns + ".model." + (name != null ? name : className);
  config.setModelName(name);


  if (include != null) {
      config.include(include);
  }
  if (exclude != null) {
      config.exclude(exclude);
  }

  /*
   * Add associations
   */
  if (associations != null) {
      config.associations(associations);
  }

  /*
   * The proxy needs additinoal information from the model.  We shouldn't create
   * the proxy until the store has been fully configured
   */
  if (rest != null) {
      String restPath = getRequest().getContextPath() + "/rest/" +
      (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
      config.setProxy(new RestProxy(restPath, config));
  }


  ModelDefinition modelDef = model.getModelDefinition();
  String output = "Ext.define(\"" + modelName + "\"," + StringUtilities.json(modelDef) + ");\n";
View Full Code Here

       * Get the config
       */
      AdaptrexPersistence persistence =
          Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
      Class<?> clazz = persistence.getEntityClass(className);
      DataConfig config = new DataConfig(clazz, persistence);


      /*
       * Create the store
       */
      AdaptrexSession session = new AdaptrexSession(persistence);
      Store store = new Store(session, config);


      /*
       * Set the model name
       */
      String modelName = ns + ".model." + (name != null ? name : className);
      config.setModelName(modelName);

      /*
       * If our store doesn't have a class drop out
       */
      if (clazz == null) {
        write("<!-- Error creating store: Could not find entity for " + className + " -->\n");
        return;
      }


      /*
       * Various store options
       */
      if (pageSize != null) {
        config.setPageSize(Integer.valueOf(pageSize));
      }
      if (include != null) {
        config.include(include);
      }
      if (exclude != null) {
        config.exclude(exclude);
      }

      if (clearOnPageLoad != null && clearOnPageLoad.equals("true")) {
        config.setClearOnPageLoad(true);
      }
      if (clearRemovedOnLoad != null && clearRemovedOnLoad.equals("true")) {
        config.setClearRemovedOnLoad(true);
      }
      if (autoSync != null && autoSync.equals("true")) {
        config.setAutoSync(true);
      }
      if (autoLoad != null && autoLoad.equals("true")) {
        config.setAutoLoad(true);
      }
      if (remoteGroup != null && remoteGroup.equals("true")) {
        config.setRemoteGroup(true);
      }
      if (remoteSort != null && remoteSort.equals("true")) {
        config.setRemoteSort(true);
      }
      if (remoteFilter != null && remoteFilter.equals("true")) {
        config.setRemoteFilter(true);
      }
      if (sortOnFilter != null && sortOnFilter.equals("true")) {
        config.setSortOnFilter(true);
      }


      /*
       * Filtering
       */
      if (filter != null) {
        for (String filterItem : filter.split(",")) {
          String[] parts = filterItem.split("=");
          String property = parts[0];
          String value = parts.length > 1 ? parts[1] : null;
          config.filter(property, value);
        }
      }


      /*
       * Grouping
       */
      if (group != null) {
        for (String groupItem : group.split(",")) {
          String[] parts = groupItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : null;
          String root = parts.length > 2 ? parts[2] : null;
          config.group(property, direction, root);
        }
      }

      /*
       * Sorting
       */
      if (sort != null) {
        for (String sortItem : sort.split(",")) {
          String[] parts = sortItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : "ASC";
          String root = parts.length > 2 ? parts[2] : null;

          config.sort(property, direction, root);
        }
      }

      /*
       * Buffering
       */
      if (buffered != null && buffered.contains("false")) {
        config.setBuffered(true);
        if (!buffered.equals("true")) {
          String[] parts = buffered.split(":");
          config.setTrailingBufferZone(Integer.valueOf(parts[0]));
          if (parts.length > 1) {
            config.setLeadingBufferZone(Integer.valueOf(parts[1]));
          }
          if (parts.length > 1) {
            config.setPurgePageCount(Integer.valueOf(parts[2]));
          }
        }
      }


      /*
       * Add associations
       */
      if (associations != null) {
        config.associations(associations);
      }


      /*
       * Load inline data?
       */
      if (inline != null && inline.equals("true")) {
        config.setInline(true);
      }


      if (where != null) {
        config.setWhere(where);
      }

      if (param != null) {
        for (String p : param.split(",")) {
          String[] parts = p.split(":");
          config.param(parts[0], parts[1]);
        }
      }

      if (start != null) {
        config.setStart(Integer.valueOf(start));
      }
     
     

      /*
       * The proxy needs additinoal information from the store.  We shouldn't create
       * the proxy until the store has been fully configured
       */
      if (rest != null) {
        String restPath = getRequest().getContextPath() + "/rest/"
            + (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
        config.setProxy(new RestProxy(restPath, config));
      }


      StoreDefinition storeDefinition = store.getStoreDefinition();
      ModelDefinition baseModelDefinition = store.getModelDefinition();

      /*
       * Write the javascript for this store/model.
       *
       * During development using Ext.Loader, we need to syncRequire Ext.data.Model.
       * If we asynchronously load all of our model's dependencies, they will not be
       * available at the time we define our store.  If they are not available, our
       * model definition will not yet be instantiated and Ext.Loader will attempt (and fail)
       * to load it dynamically.
       */
      String storeName = ns + ".store." + StringUtilities.pluralize(config.getSimpleModelName());
      String output = ""
          + "Ext.syncRequire(['Ext.data.Model','Ext.data.Store','Ext.data.reader.Json','Ext.data.writer.Json'], function(){;\n"
          + "Ext.define(\"" + modelName + "\"," + StringUtilities.json(baseModelDefinition) + ");\n"
          + "Ext.define(\"" + storeName + "\"," + StringUtilities.json(storeDefinition) + ");\n"
          + "Ext.onReady(function() {Ext.create(\"" + storeName + "\", {storeId:\"" + storeName.split(".store.")[1] + "\"})})\n";
View Full Code Here

   * Get the config
   */
  AdaptrexPersistence persistence =
    Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
  Class<?> clazz = persistence.getEntityClass(className);
  DataConfig config = new DataConfig(clazz, persistence);


  /*
   * Create the model
   */
  AdaptrexSession session = new AdaptrexSession(persistence);
  Model model = new Model(session, config);

 
  /*
   * Set the model name
   */
  String modelName = namespace + ".model." + (name != null ? name : className);
  config.setModelName(name);


  if (include != null) {
      config.include(include);
  }
  if (exclude != null) {
      config.exclude(exclude);
  }

  /*
   * Add associations
   */
  if (associations != null) {
      config.associations(associations);
  }

  /*
   * The proxy needs additinoal information from the model.  We shouldn't create
   * the proxy until the store has been fully configured
   */
  if (rest != null) {
      String restPath = getRequest().getContextPath() + "/rest/" +
      (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
      config.setProxy(new RestProxy(restPath, config));
  }


  ModelDefinition modelDef = model.getModelDefinition();
  String output = "Ext.define(\"" + modelName + "\"," + StringUtilities.json(modelDef) + ");\n";
View Full Code Here

       * Get the config
       */
      AdaptrexPersistence persistence =
          Adaptrex.getAdaptrex().getPersistenceManager().getPersistence(factoryName);
      Class<?> clazz = persistence.getEntityClass(className);
      DataConfig config = new DataConfig(clazz, persistence);


      /*
       * Create the store
       */
      AdaptrexSession session = new AdaptrexSession(persistence);
      Store store = new Store(session, config);


      /*
       * Set the model name
       */
      String modelName = namespace + ".model." + (name != null ? name : className);
      config.setModelName(modelName);

      /*
       * If our store doesn't have a class drop out
       */
      if (clazz == null) {
        write("<!-- Error creating store: Could not find entity for " + className + " -->\n");
        return;
      }


      /*
       * Various store options
       */
      if (pageSize != null) {
        config.setPageSize(Integer.valueOf(pageSize));
      }
      if (include != null) {
        config.include(include);
      }
      if (exclude != null) {
        config.exclude(exclude);
      }

      if (clearOnPageLoad != null && clearOnPageLoad.equals("true")) {
        config.setClearOnPageLoad(true);
      }
      if (clearRemovedOnLoad != null && clearRemovedOnLoad.equals("true")) {
        config.setClearRemovedOnLoad(true);
      }
      if (autoSync != null && autoSync.equals("true")) {
        config.setAutoSync(true);
      }
      if (autoLoad != null && autoLoad.equals("true")) {
        config.setAutoLoad(true);
      }
      if (remoteGroup != null && remoteGroup.equals("true")) {
        config.setRemoteGroup(true);
      }
      if (remoteSort != null && remoteSort.equals("true")) {
        config.setRemoteSort(true);
      }
      if (remoteFilter != null && remoteFilter.equals("true")) {
        config.setRemoteFilter(true);
      }
      if (sortOnFilter != null && sortOnFilter.equals("true")) {
        config.setSortOnFilter(true);
      }


      /*
       * Filtering
       */
      if (filter != null) {
        for (String filterItem : filter.split(",")) {
          String[] parts = filterItem.split("=");
          String property = parts[0];
          String value = parts.length > 1 ? parts[1] : null;
          config.filter(property, value);
        }
      }


      /*
       * Grouping
       */
      if (group != null) {
        for (String groupItem : group.split(",")) {
          String[] parts = groupItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : null;
          String root = parts.length > 2 ? parts[2] : null;
          config.group(property, direction, root);
        }
      }

      /*
       * Sorting
       */
      if (sort != null) {
        for (String sortItem : sort.split(",")) {
          String[] parts = sortItem.split(":");
          String property = parts[0];
          String direction = parts.length > 1 ? parts[1] : "ASC";
          String root = parts.length > 2 ? parts[2] : null;

          config.sort(property, direction, root);
        }
      }

      /*
       * Buffering
       */
      if (buffered != null && buffered.contains("false")) {
        config.setBuffered(true);
        if (!buffered.equals("true")) {
          String[] parts = buffered.split(":");
          config.setTrailingBufferZone(Integer.valueOf(parts[0]));
          if (parts.length > 1) {
            config.setLeadingBufferZone(Integer.valueOf(parts[1]));
          }
          if (parts.length > 1) {
            config.setPurgePageCount(Integer.valueOf(parts[2]));
          }
        }
      }


      /*
       * Add associations
       */
      if (associations != null) {
        config.associations(associations);
      }


      /*
       * Load inline data?
       */
      if (inline != null && inline.equals("true")) {
        config.setInline(true);
      }


      if (where != null) {
        config.setWhere(where);
      }

      if (param != null) {
        for (String p : param.split(",")) {
          String[] parts = p.split(":");
          config.param(parts[0], parts[1]);
        }
      }

      if (start != null) {
        config.setStart(Integer.valueOf(start));
      }
     
     

      /*
       * The proxy needs additinoal information from the store.  We shouldn't create
       * the proxy until the store has been fully configured
       */
      if (rest != null) {
        String restPath = getRequest().getContextPath() + "/rest/"
            + (rest.equals("true") ? clazz.getSimpleName().toLowerCase() : rest);
        config.setProxy(new RestProxy(restPath, config));
      }


      StoreDefinition storeDefinition = store.getStoreDefinition();
      ModelDefinition baseModelDefinition = store.getModelDefinition();

      /*
       * Write the javascript for this store/model.
       *
       * During development using Ext.Loader, we need to syncRequire Ext.data.Model.
       * If we asynchronously load all of our model's dependencies, they will not be
       * available at the time we define our store.  If they are not available, our
       * model definition will not yet be instantiated and Ext.Loader will attempt (and fail)
       * to load it dynamically.
       */
      String storeName = namespace + ".store." + StringUtilities.pluralize(config.getSimpleModelName());
      String output = ""
          + "Ext.syncRequire(['Ext.data.Model','Ext.data.Store','Ext.data.reader.Json','Ext.data.writer.Json'], function(){;\n"
          + "Ext.define(\"" + modelName + "\"," + StringUtilities.json(baseModelDefinition) + ");\n"
          + "Ext.define(\"" + storeName + "\"," + StringUtilities.json(storeDefinition) + ");\n"
          + "Ext.onReady(function() {Ext.create(\"" + storeName + "\", {storeId:\"" + storeName.split(".store.")[1] + "\"})})\n";
View Full Code Here

     *
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getEntityList(AdaptrexSession session, Store store) {
  DataConfig config = store.getConfig();
  List<Object> list = new ArrayList<Object>();

  try {
      EntityManager em = (EntityManager) session.getPersistence().getNativeSession();

      Map<String, Object> parameters = new HashMap<String, Object>(config.getParams());

     
      Class<?> clazz = config.getClazz();
      String className = clazz.getSimpleName();
      String alias = className.toLowerCase().substring(0, 1);

      /*
       * Core JPQL
       */
      String jpql = "SELECT " + alias + " FROM " + className + " " + alias;

      /*
       * Where
       */
      String where = "";
      if (config.getWhere() != null) {
    where += config.getWhere();
      }

      /*
       * Filter
       */
      for (Filter filter : config.getFilters()) {
      if (!where.isEmpty()) {
        where += " AND ";
      }

      String val = String.valueOf(filter.getValue());
      String prop = filter.getProperty();
      String param = prop.replace(".", "_");

      /*
       * Handle between inclusive
       */
      if (val.contains(">=<")) {
        String[] filterParts = val.split(">=<");

        where += "(" + alias + "." + prop + " BETWEEN :"
          + param + "_low AND :" + param + "_high)"
          + "";
        parameters.put(param + "_low", filterParts[0]);
        parameters.put(param + "_high", filterParts[1]);

      /*
       * Handle "OR"
       */
      } else if (val.contains("||")) {
        String whereOr = "";
        int i = 1;

        for (String orPart : val.split("\\|\\|")) {
        whereOr += whereOr.isEmpty() ? "(" : " OR ";
        whereOr += alias + "." + prop + " = :" + param + i;
        parameters.put(param + i, orPart);
        i++;
        }
        whereOr += ")";
        where += whereOr;

      } else {
        String eqType = val.contains("%") ? " like " : " = ";
        if (eqType.contains("like")) {
          where += "lower(" + alias + "." + prop + ")" + eqType + ":" + param;

        } else {
          where += alias + "." + prop + eqType + ":" + param;
        }

        parameters.put(param, val);
      }
      }


      /*
       * Add where clause
       */
      if (!where.isEmpty()) {
    jpql += " WHERE " + where;
      }


      if (config.getSorters().size() > 0) {
    String sortClause = "";
    for (Sorter sorter : config.getSorters()) {
        if (!sortClause.isEmpty()) {
      sortClause += ",";
        }
        sortClause += alias + "." + sorter.getProperty() + " " + sorter.getDirection();
    }
    jpql += " ORDER BY " + sortClause;
      }

      log.debug("JPQL: " + jpql);
      Query q = em.createQuery(jpql, clazz);

      /*
       * Add params
       * TODO: This is tedius... we want to automatically determine the
       * type for the field we're trying to set the parameter for
       */
      for (String key : parameters.keySet()) {
    Object paramObj = parameters.get(key);

    Class<?> paramType = q.getParameter(key).getParameterType();
    String typeName = paramType.getSimpleName().toLowerCase();

    try {
        if (typeName.equals("boolean")) {
      String stringVal = (String) paramObj;
      paramObj = stringVal.equals("1") || stringVal.equals("true") || stringVal.equals("Y");

        } else if (typeName.equals("date")) {
      paramObj = dateFormat.parse((String) paramObj);

        } else {
      Method m = paramType.getDeclaredMethod("valueOf", String.class);
      paramObj = m.invoke(null, paramObj);
        }
    } catch (Exception e) {
    }

    log.debug("  Param: " + key + " = " + paramObj);
    q.setParameter(key, paramObj);
      }

      store.setTotalCount(q.getResultList().size());

      if (config.getPageSize() != null) {
    q.setMaxResults(config.getPageSize());
      }
      if (config.getStart() != null) {
    q.setFirstResult(config.getStart());
      }
      list = q.getResultList();
  } catch (Exception e) {
      log.warn("Error", e);
  }
View Full Code Here

TOP

Related Classes of com.adaptrex.core.ext.data.DataConfig

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.