summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss/jpa/query/filter/EntityFilter.java
blob: 357f7b1eb75bfdb0ab767de53af3ec5c69552ac7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * 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 <http://www.gnu.org/licenses/>.
 *
 */
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 <E>
 *            the element type
 */
public class EntityFilter<E> extends AbstractFilter<E> {

	/** The entity. */
	private Class<E> entity;

	/** The flags. */
	private List<EntityFilterFlags> flags = new ArrayList<EntityFilterFlags>();

	/**
	 * Instantiates a new entity filter.
	 * 
	 * @param entity
	 *            the entity
	 * @param flags
	 *            the flags
	 */
	public EntityFilter(final Class<E> 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<Field> fields = getInheritedFields(entity);
		List<Method> methods = new ArrayList<Method>();
		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<E> getEntity() {
		return entity;
	}

	/**
	 * Sets the entity.
	 * 
	 * @param entity
	 *            the new entity
	 */
	public void setEntity(final Class<E> entity) {
		this.entity = entity;
	}

	/**
	 * Gets the inherited fields.
	 * 
	 * @param type
	 *            the type
	 * @return the inherited fields
	 */
	private List<Field> getInheritedFields(final Class<?> type) {
		List<Field> fields = new ArrayList<Field>();
		for (Class<?> c = type; c != null; c = c.getSuperclass()) {
			fields.addAll(Arrays.asList(c.getDeclaredFields()));
		}
		return fields;
	}

}