Ma generic do even more version of EntityQuery : MyEntityQue
koenhandekyn Nov 22, 2007 4:00 AMi 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;
}
}