01: package com.technoetic.xplanner.db;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import net.sf.hibernate.HibernateException;
07: import net.sf.hibernate.Query;
08:
09: import com.technoetic.xplanner.db.hibernate.ThreadSession;
10: import com.technoetic.xplanner.domain.repository.RepositoryException;
11:
12: public class SearchContentQuery {
13: private int restrictedProjectId = 0;
14:
15: public List findWhereNameOrDescriptionContains(
16: String searchCriteria, Class objectClass)
17: throws RepositoryException {
18: try {
19: return runSearchQuery(searchCriteria, objectClass);
20: } catch (HibernateException e) {
21: throw new RepositoryException(e);
22: }
23: }
24:
25: public void setRestrictedProjectId(int restrictedProjectId) {
26: this .restrictedProjectId = restrictedProjectId;
27: }
28:
29: private List runSearchQuery(String searchCriteria, Class objectClass)
30: throws HibernateException {
31: Query query = ThreadSession.get().getNamedQuery(
32: getQueryName(objectClass));
33: query.setString("contents", "%" + searchCriteria + "%");
34: if (restrictedProjectId > 0) {
35: query.setInteger("projectId", restrictedProjectId);
36: }
37: return copyResults(query.list());
38: }
39:
40: private String getQueryName(Class objectClass) {
41: return objectClass.getName()
42: + (restrictedProjectId == 0 ? "SearchQuery"
43: : "RestrictedSearchQuery");
44: }
45:
46: private List copyResults(List results) {
47: ArrayList returnValue = new ArrayList();
48: if (results != null) {
49: returnValue.addAll(results);
50: }
51: return returnValue;
52: }
53: }
|