1 Reply Latest reply on Nov 22, 2007 8:31 AM by koenhandekyn

    Ma generic do even more version of EntityQuery : MyEntityQue

    koenhandekyn

      i want to share with you this generic do-even-more version of EntityQuery

      with the below class it's even easier to write EntityQuery objects.


      the EJBQL is generated automatically (example FROM Account account).
      there is a getExample() method that returns an example instance (for search paramaters as in the seam-gen generated code)
      each time setOrder is hit, if the field was the same as the current, the order switches (for asc/desc switching).
      the current order can be queried with getReverse


      example using MyEntityQuery

      @Name("managedRoles")
      @Scope(ScopeType.CONVERSATION)
      public class ManagedRolesQuery extends MyEntityQuery<Role> {
      
       private static final String[] RESTRICTIONS = {
       "role.creationPath LIKE concat(#{currentSubscription.targetPath},'%')",
       "role.name LIKE concat(#{managedRoles.example.name},'%')" };
      
      
       @Override
       public List<String> getRestrictions() {
       return Arrays.asList(RESTRICTIONS);
       }
      
      }
      



      feedback welcom !

      package up.seam;
      
      import java.lang.reflect.ParameterizedType;
      import java.lang.reflect.Type;
      
      import org.jboss.seam.framework.EntityQuery;
      
      import up.util.LangUtil;
      
      public class MyEntityQuery<T> extends EntityQuery {
      
       // you cannot initialize (results in bug for seam 2.0.0.GA)
       protected T example = null;
      
       public T getExample() {
       if (example == null) {
       example = createInstance();
       }
       return example;
       }
      
       public void setExample(final T example) {
       this.example = example;
       }
      
       protected Boolean reverse = false;
      
       public Boolean getReverse() {
       return reverse;
       }
      
       public void setReverse(Boolean reverse) {
       this.reverse = reverse;
       }
      
       @Override
       public Integer getMaxResults() {
       return 25;
       }
      
       @Override
       public void setOrder(final String order) {
      
       System.out.println("REQUEST TO SORT: " + order + ", was " + super.getOrder());
       if (LangUtil.isNotEmpty(order)) {
       if (order.equals(this.getOrder())) {
       this.reverse = !this.reverse;
       } else {
       this.reverse = false;
       }
       super.setOrder(order);
       }
       }
      
       @Override
       public String getOrder() {
       if (reverse)
       return super.getOrder() + " desc";
       else
       return super.getOrder();
       }
      
       private static final long serialVersionUID = 1L;
      
      
       @Override
       public String getEjbql() {
      
       final String entityClassName = getEntityClass().getSimpleName();
       // System.out.println(entityClassName);
      
       final String ejbql = "from "+entityClassName+" "+entityClassName.toLowerCase();
       // System.out.println(ejbql);
       return ejbql;
       }
      
       public T createInstance() {
      
       if (getEntityClass() != null) {
       try {
       return getEntityClass().newInstance();
       } catch (Exception e) {
       throw new RuntimeException(e);
       }
       } else {
       return null;
       }
       }
      
       protected Class<T> entityClass;
      
       public Class<T> getEntityClass() {
       if (entityClass == null) {
       Type type = getClass().getGenericSuperclass();
       System.out.println("found class " + getClass());
       System.out.println("found type " + type);
       if (type instanceof ParameterizedType) {
       ParameterizedType paramType = (ParameterizedType) type;
       entityClass = (Class<T>) paramType.getActualTypeArguments()[0];
       } else {
       throw new IllegalArgumentException("Could not guess entity class by reflection");
       }
       }
       return entityClass;
       }
      
      }



        • 1. Re: Ma generic do even more version of EntityQuery : MyEntit
          koenhandekyn

          this new version below adds methods that generate the default search constraints given a list of fields.

          just override getFields and get the constraints for free using getSearchFieldRestrictions

          kind regards

          koen

          package up.seam;
          
          import java.lang.annotation.Annotation;
          import java.lang.reflect.ParameterizedType;
          import java.lang.reflect.Type;
          import java.util.Arrays;
          import java.util.LinkedList;
          import java.util.List;
          
          import org.jboss.seam.annotations.Name;
          import org.jboss.seam.framework.EntityQuery;
          
          import up.util.LangUtil;
          
          public class MyEntityQuery<T> extends EntityQuery {
          
           // you cannot initialize (results in bug for seam 2.0.0.GA)
           protected T example = null;
          
           public T getExample() {
           if (example == null) {
           example = createInstance();
           }
           return example;
           }
          
           public void setExample(final T example) {
           this.example = example;
           }
          
           protected Boolean reverse = false;
          
           public Boolean getReverse() {
           return reverse;
           }
          
           public void setReverse(Boolean reverse) {
           this.reverse = reverse;
           }
          
           @Override
           public Integer getMaxResults() {
           return 25;
           }
          
           @Override
           public void setOrder(final String order) {
          
           System.out.println("REQUEST TO SORT: " + order + ", was " + super.getOrder());
           if (LangUtil.isNotEmpty(order)) {
           if (order.equals(this.getOrder())) {
           this.reverse = !this.reverse;
           } else {
           this.reverse = false;
           }
           super.setOrder(order);
           }
           }
          
           @Override
           public String getOrder() {
           if (reverse)
           return super.getOrder() + " desc";
           else
           return super.getOrder();
           }
          
           private static final long serialVersionUID = 1L;
          
           @Override
           public String getEjbql() {
          
           final String entityClassName = getEntityClass().getSimpleName();
           // System.out.println(entityClassName);
          
           final String ejbql = "from " + entityClassName + " " + entityClassName.toLowerCase();
           // System.out.println(ejbql);
           return ejbql;
           }
          
           public T createInstance() {
          
           if (getEntityClass() != null) {
           try {
           return getEntityClass().newInstance();
           } catch (Exception e) {
           throw new RuntimeException(e);
           }
           } else {
           return null;
           }
           }
          
           protected Class<T> entityClass;
          
           public Class<T> getEntityClass() {
           if (entityClass == null) {
           Type type = getClass().getGenericSuperclass();
           System.out.println("found class " + getClass());
           System.out.println("found type " + type);
           if (type instanceof ParameterizedType) {
           ParameterizedType paramType = (ParameterizedType) type;
           entityClass = (Class<T>) paramType.getActualTypeArguments()[0];
           } else {
           throw new IllegalArgumentException("Could not guess entity class by reflection");
           }
           }
           return entityClass;
           }
          
           public List<String> getSearchFields() {
           return null;
           }
          
           @Override
           public List<String> getRestrictions() {
          
           if (getSearchFields() == null)
           return null;
          
           final List<String> searchFieldRestrictions;
           searchFieldRestrictions = new LinkedList<String>();
          
           final String componentName;
           componentName = getComponentName();
          
           for (final String field : getSearchFields()) {
           searchFieldRestrictions.add("account." + field + " LIKE concat(#{" + componentName + ".example." + field + "},'%')");
           }
          
           return searchFieldRestrictions;
           }
          
           public String getComponentName() {
          
           final Annotation nameAnnotation = this.getClass().getAnnotation(Name.class);
           Name name = (Name) nameAnnotation;
           return name.value();
          
           }
          }