001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.domain;
018:
019: import static info.jtrac.Constants.*;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.LinkedHashMap;
025: import java.util.List;
026: import java.util.Map;
027: import org.apache.wicket.Component;
028: import org.hibernate.criterion.DetachedCriteria;
029: import org.hibernate.criterion.Order;
030: import org.hibernate.criterion.Restrictions;
031:
032: /**
033: * Object that holds filter criteria when searching for Items
034: * and also creates a Hibernate Criteria query to pass to the DAO
035: */
036: public class ItemSearch implements Serializable {
037:
038: private Space space; // if null, means aggregate across all spaces
039: private User user; // this will be set in the case space is null
040:
041: private int pageSize = 25;
042: private int currentPage;
043: private long resultCount;
044: private String sortFieldName = "id";
045: private boolean sortDescending = true;
046: private boolean showHistory;
047: private boolean showDetail;
048:
049: private long selectedItemId;
050: private String relatingItemRefId;
051: private Collection<Long> itemIds;
052:
053: private List<ColumnHeading> columnHeadings;
054: private Map<String, FilterCriteria> filterCriteriaMap = new LinkedHashMap<String, FilterCriteria>();
055:
056: public ItemSearch(User user, Component c) {
057: this .user = user;
058: this .columnHeadings = ColumnHeading.getColumnHeadings(user);
059: }
060:
061: public ItemSearch(Space space, Component c) {
062: this .space = space;
063: this .columnHeadings = ColumnHeading.getColumnHeadings(space);
064: }
065:
066: private DetachedCriteria parent; // temp working variable hack
067:
068: // have to do this two step process as "order by" clause conflicts with "count (*)" clause
069: // so the DAO has to use getCriteriaForCount() separately
070: public DetachedCriteria getCriteria() {
071: DetachedCriteria criteria = getCriteriaForCount();
072: if (sortFieldName == null) { // can happen only for multi-space search
073: sortFieldName = "id"; // effectively is a sort on created date
074: }
075: if (sortFieldName.equals("id") || sortFieldName.equals("space")) {
076: if (showHistory) {
077: // if showHistory: sort by item.id and then history.id
078: if (sortDescending) {
079: if (space == null) {
080: DetachedCriteria parentSpace = parent
081: .createCriteria("space");
082: parentSpace.addOrder(Order.desc("name"));
083: }
084: criteria.addOrder(Order.desc("parent.id"));
085: criteria.addOrder(Order.desc("id"));
086: } else {
087: if (space == null) {
088: DetachedCriteria parentSpace = parent
089: .createCriteria("space");
090: parentSpace.addOrder(Order.asc("name"));
091: }
092: criteria.addOrder(Order.asc("parent.id"));
093: criteria.addOrder(Order.asc("id"));
094: }
095: } else {
096: if (sortDescending) {
097: if (space == null) {
098: DetachedCriteria parentSpace = criteria
099: .createCriteria("space");
100: parentSpace.addOrder(Order.desc("name"));
101: }
102: criteria.addOrder(Order.desc("id"));
103: } else {
104: if (space == null) {
105: DetachedCriteria parentSpace = criteria
106: .createCriteria("space");
107: parentSpace.addOrder(Order.asc("name"));
108: }
109: criteria.addOrder(Order.asc("id"));
110: }
111: }
112: } else {
113: if (sortDescending) {
114: criteria.addOrder(Order.desc(sortFieldName));
115: } else {
116: criteria.addOrder(Order.asc(sortFieldName));
117: }
118: }
119: return criteria;
120: }
121:
122: public DetachedCriteria getCriteriaForCount() {
123: DetachedCriteria criteria = null;
124: if (showHistory) {
125: criteria = DetachedCriteria.forClass(History.class);
126: // apply restrictions to parent, this is an inner join =============
127: parent = criteria.createCriteria("parent");
128: if (space == null) {
129: parent.add(Restrictions
130: .in("space", getSelectedSpaces()));
131: } else {
132: parent.add(Restrictions.eq("space", space));
133: }
134: if (itemIds != null) {
135: parent.add(Restrictions.in("id", itemIds));
136: }
137: } else {
138: criteria = DetachedCriteria.forClass(Item.class);
139: if (space == null) {
140: criteria.add(Restrictions.in("space",
141: getSelectedSpaces()));
142: } else {
143: criteria.add(Restrictions.eq("space", space));
144: }
145: if (itemIds != null) {
146: criteria.add(Restrictions.in("id", itemIds));
147: }
148: }
149: for (ColumnHeading ch : columnHeadings) {
150: ch.addRestrictions(criteria, this );
151: }
152: return criteria;
153: }
154:
155: public List<Field> getFields() {
156: if (space == null) {
157: List<Field> list = new ArrayList<Field>(2);
158: Field severity = new Field(Field.Name.SEVERITY);
159: severity.initOptions();
160: list.add(severity);
161: Field priority = new Field(Field.Name.PRIORITY);
162: priority.initOptions();
163: list.add(priority);
164: return list;
165: } else {
166: return space.getMetadata().getFieldList();
167: }
168: }
169:
170: private ColumnHeading getColumnHeading(String name) {
171: for (ColumnHeading ch : columnHeadings) {
172: if (ch.getName().equals(name)) {
173: return ch;
174: }
175: }
176: return null;
177: }
178:
179: private String getStringValue(ColumnHeading ch) {
180: String s = (String) ch.getFilterCriteria().getValue();
181: if (s == null || s.trim().length() == 0) {
182: ch.getFilterCriteria().setExpression(null);
183: return null;
184: }
185: return s;
186: }
187:
188: public String getRefId() {
189: ColumnHeading ch = getColumnHeading(ColumnHeading.ID);
190: return getStringValue(ch);
191: }
192:
193: public String getSearchText() {
194: ColumnHeading ch = getColumnHeading(ColumnHeading.DETAIL);
195: return getStringValue(ch);
196: }
197:
198: public Collection<Space> getSelectedSpaces() {
199: ColumnHeading ch = getColumnHeading(ColumnHeading.SPACE);
200: List values = ch.getFilterCriteria().getValues();
201: if (values == null || values.size() == 0) {
202: ch.getFilterCriteria().setExpression(null);
203: return user.getSpaces();
204: }
205: return values;
206: }
207:
208: public void toggleSortDirection() {
209: sortDescending = !sortDescending;
210: }
211:
212: private List getSingletonList(Object o) {
213: List list = new ArrayList(1);
214: list.add(o);
215: return list;
216: }
217:
218: public void setLoggedBy(User loggedBy) {
219: ColumnHeading ch = getColumnHeading(ColumnHeading.LOGGED_BY);
220: ch.getFilterCriteria().setExpression(
221: FilterCriteria.Expression.IN);
222: ch.getFilterCriteria().setValues(getSingletonList(loggedBy));
223: }
224:
225: public void setAssignedTo(User assignedTo) {
226: ColumnHeading ch = getColumnHeading(ColumnHeading.ASSIGNED_TO);
227: ch.getFilterCriteria().setExpression(
228: FilterCriteria.Expression.IN);
229: ch.getFilterCriteria().setValues(getSingletonList(assignedTo));
230: }
231:
232: public void setStatus(int i) {
233: ColumnHeading ch = getColumnHeading(ColumnHeading.STATUS);
234: ch.getFilterCriteria().setExpression(
235: FilterCriteria.Expression.IN);
236: ch.getFilterCriteria().setValues(getSingletonList(i));
237: }
238:
239: public List<ColumnHeading> getColumnHeadingsToRender() {
240: List<ColumnHeading> list = new ArrayList<ColumnHeading>(
241: columnHeadings.size());
242: for (ColumnHeading ch : columnHeadings) {
243: if (ch.isVisible()) {
244: list.add(ch);
245: }
246: }
247: return list;
248: }
249:
250: //==========================================================================
251:
252: public Space getSpace() {
253: return space;
254: }
255:
256: public void setSpace(Space space) {
257: this .space = space;
258: }
259:
260: public User getUser() {
261: return user;
262: }
263:
264: public void setUser(User user) {
265: this .user = user;
266: }
267:
268: public int getPageSize() {
269: return pageSize;
270: }
271:
272: public void setPageSize(int pageSize) {
273: this .pageSize = pageSize;
274: }
275:
276: public int getCurrentPage() {
277: return currentPage;
278: }
279:
280: public void setCurrentPage(int currentPage) {
281: this .currentPage = currentPage;
282: }
283:
284: public long getResultCount() {
285: return resultCount;
286: }
287:
288: public void setResultCount(long resultCount) {
289: this .resultCount = resultCount;
290: }
291:
292: public String getSortFieldName() {
293: return sortFieldName;
294: }
295:
296: public void setSortFieldName(String sortFieldName) {
297: this .sortFieldName = sortFieldName;
298: }
299:
300: public boolean isSortDescending() {
301: return sortDescending;
302: }
303:
304: public void setSortDescending(boolean sortDescending) {
305: this .sortDescending = sortDescending;
306: }
307:
308: public boolean isShowHistory() {
309: return showHistory;
310: }
311:
312: public void setShowHistory(boolean showHistory) {
313: this .showHistory = showHistory;
314: }
315:
316: public boolean isShowDetail() {
317: return showDetail;
318: }
319:
320: public void setShowDetail(boolean showDetail) {
321: this .showDetail = showDetail;
322: }
323:
324: public long getSelectedItemId() {
325: return selectedItemId;
326: }
327:
328: public void setSelectedItemId(long selectedItemId) {
329: this .selectedItemId = selectedItemId;
330: }
331:
332: public String getRelatingItemRefId() {
333: return relatingItemRefId;
334: }
335:
336: public void setRelatingItemRefId(String relatingItemRefId) {
337: this .relatingItemRefId = relatingItemRefId;
338: }
339:
340: public Collection<Long> getItemIds() {
341: return itemIds;
342: }
343:
344: public void setItemIds(Collection<Long> itemIds) {
345: this .itemIds = itemIds;
346: }
347:
348: public List<ColumnHeading> getColumnHeadings() {
349: return columnHeadings;
350: }
351:
352: public void setColumnHeadings(List<ColumnHeading> columnHeadings) {
353: this .columnHeadings = columnHeadings;
354: }
355:
356: public Map<String, FilterCriteria> getFilterCriteriaMap() {
357: return filterCriteriaMap;
358: }
359:
360: public void setFilterCriteriaMap(
361: Map<String, FilterCriteria> filterCriteriaMap) {
362: this.filterCriteriaMap = filterCriteriaMap;
363: }
364:
365: }
|