View Javadoc

1   /*
2    * Copyright 2000 - 2010 Ivan Khalopik. All Rights Reserved.
3    */
4   
5   package org.greatage.domain;
6   
7   import org.greatage.util.DescriptionBuilder;
8   
9   import java.io.Serializable;
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.List;
13  
14  /**
15   * @author Ivan Khalopik
16   * @since 1.0
17   */
18  public class EntityQuery<PK extends Serializable, E extends Entity<PK>, Q extends EntityQuery<PK, E, Q>>
19  		implements EntityFilter<PK, E> {
20  
21  	private final Class<E> entityClass;
22  	private EntityRepository repository;
23  	private String queryString;
24  	private List<PK> includeKeys;
25  	private List<PK> excludeKeys;
26  
27  	public EntityQuery(final Class<E> entityClass) {
28  		this.entityClass = entityClass;
29  	}
30  
31  	public EntityQuery(final EntityRepository repository, final Class<E> entityClass) {
32  		this.repository = repository;
33  		this.entityClass = entityClass;
34  	}
35  
36  	/**
37  	 * Gets entity class.
38  	 *
39  	 * @return entity class
40  	 */
41  	public Class<E> getEntityClass() {
42  		return entityClass;
43  	}
44  
45  	/**
46  	 * Gets search query string for entity full-text search.
47  	 *
48  	 * @return search query string for entity full-text search
49  	 */
50  	public String getQueryString() {
51  		return queryString;
52  	}
53  
54  	/**
55  	 * Gets collection of entity primary keys that will be included into select result.
56  	 *
57  	 * @return collection of entity primary keys that will be included into select result
58  	 */
59  	public Collection<PK> getIncludeKeys() {
60  		return includeKeys;
61  	}
62  
63  	/**
64  	 * Gets collection of entity primary keys that will be excluded from select result.
65  	 *
66  	 * @return collection of entity primary keys that will be excluded from select result
67  	 */
68  	public Collection<PK> getExcludeKeys() {
69  		return excludeKeys;
70  	}
71  
72  	/**
73  	 * Sets search query string for entity full-text search.
74  	 *
75  	 * @param queryString search query string for entity full-text search
76  	 * @return the same query instance
77  	 */
78  	public Q setQueryString(final String queryString) {
79  		this.queryString = queryString;
80  		return query();
81  	}
82  
83  	/**
84  	 * Sets collection of entity primary keys that will be included into select result.
85  	 *
86  	 * @param keys collection of entity primary keys that will be included into select result
87  	 * @return the same query instance
88  	 */
89  	public Q includeKeys(final Collection<PK> keys) {
90  		if (includeKeys == null) {
91  			includeKeys = new ArrayList<PK>();
92  		}
93  		includeKeys.addAll(keys);
94  		return query();
95  	}
96  
97  	/**
98  	 * Sets collection of entity primary keys that will be excluded from select result.
99  	 *
100 	 * @param keys collection of entity primary keys that will be excluded from select result
101 	 * @return the same query instance
102 	 */
103 	public Q excludeKeys(final Collection<PK> keys) {
104 		if (excludeKeys == null) {
105 			excludeKeys = new ArrayList<PK>();
106 		}
107 		excludeKeys.addAll(keys);
108 		return query();
109 	}
110 
111 	public Q assign(final EntityRepository repository) {
112 		this.repository = repository;
113 		return query();
114 	}
115 
116 	/**
117 	 * Gets entities count selected by this query instance.
118 	 *
119 	 * @return entities count selected by this query instance
120 	 */
121 	public int count() {
122 		return repository().count(this);
123 	}
124 
125 	/**
126 	 * Gets paginated list of entities selected by this query instance.
127 	 *
128 	 * @param pagination selection pagination
129 	 * @return paginated list of entities selected by this query instance or empty list if not found
130 	 */
131 	public List<E> list(final Pagination pagination) {
132 		return repository().find(this, pagination);
133 	}
134 
135 	/**
136 	 * Gets list of entities selected by this query instance.
137 	 *
138 	 * @return list of entities selected by this query instance or empty list if not found
139 	 */
140 	public List<E> list() {
141 		return repository().find(this, Pagination.ALL);
142 	}
143 
144 	/**
145 	 * Gets unique entity selected by this query instance.
146 	 *
147 	 * @return unique entity selected by this query instance
148 	 */
149 	public E unique() {
150 		return repository().findUnique(this);
151 	}
152 
153 
154 	/**
155 	 * Gets EntityRepository instance assigned to this query.
156 	 *
157 	 * @return assigned EntityRepository instance
158 	 */
159 	protected EntityRepository repository() {
160 		if (repository == null) {
161 			throw new IllegalStateException("This query is not assigned to repository.");
162 		}
163 		return repository;
164 	}
165 
166 	/**
167 	 * Gets this query instance casted to Q generic parameter.
168 	 *
169 	 * @return the same query instance
170 	 */
171 	@SuppressWarnings({"unchecked"})
172 	protected Q query() {
173 		return (Q) this;
174 	}
175 
176 	@Override
177 	public String toString() {
178 		final DescriptionBuilder builder = new DescriptionBuilder(getClass());
179 		builder.append("class", entityClass);
180 		builder.append("queryString", queryString);
181 		return builder.toString();
182 	}
183 }