[jboss-user] [JBoss Seam] - Re: Ma generic do even more version of EntityQuery : MyEntit

koenhandekyn do-not-reply at jboss.com
Thu Nov 22 08:31:41 EST 2007


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();
  | 
  |   }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4107109#4107109

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4107109



More information about the jboss-user mailing list