/**
* This file is part of xss-jpa.
*
* Copyright 2012 Sebastian Schmelzer
* http://xss.nu/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with library. If not, see .
*
*/
package nu.xss.jpa.query.filter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
/**
* The Class EntityFilter.
*
* @param
* the element type
*/
public class EntityFilter extends AbstractFilter {
/** The entity. */
private Class entity;
/** The flags. */
private List flags = new ArrayList();
/**
* Instantiates a new entity filter.
*
* @param entity
* the entity
* @param flags
* the flags
*/
public EntityFilter(final Class entity, final EntityFilterFlags... flags) {
super();
this.setEntity(entity);
for (EntityFilterFlags f : flags) {
this.flags.add(f);
}
}
@Override
public void buildFilters(final CriteriaBuilder cb, final Root> from) {
List fields = getInheritedFields(entity);
List methods = new ArrayList();
System.out.println(fields.toString());
for (Method m: entity.getMethods()) {
if (m.getName().startsWith("get") && m.getName() != "getClass") {
methods.add(m);
}
}
System.out.println(methods.toString());
}
/**
* Gets the entity.
*
* @return the entity
*/
public Class getEntity() {
return entity;
}
/**
* Sets the entity.
*
* @param entity
* the new entity
*/
public void setEntity(final Class entity) {
this.entity = entity;
}
/**
* Gets the inherited fields.
*
* @param type
* the type
* @return the inherited fields
*/
private List getInheritedFields(final Class> type) {
List fields = new ArrayList();
for (Class> c = type; c != null; c = c.getSuperclass()) {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
}
return fields;
}
}