01: package com.technoetic.xplanner.db;
02:
03: import java.util.Iterator;
04: import java.util.LinkedList;
05: import java.util.List;
06:
07: import org.apache.commons.collections.CollectionUtils;
08: import org.apache.commons.collections.Predicate;
09:
10: import com.technoetic.xplanner.actions.SearchResultAuthorizationPredicate;
11: import com.technoetic.xplanner.domain.*;
12: import com.technoetic.xplanner.domain.repository.RepositoryException;
13:
14: /**
15: * Created by IntelliJ IDEA.
16: * User: tkmower
17: * Date: Dec 14, 2004
18: * Time: 2:58:40 PM
19: */
20: public class ContentSearchHelper {
21: protected String searchCriteria;
22: private List searchResults;
23: private Class[] searchableClasses = new Class[] { Project.class,
24: UserStory.class, Task.class, Note.class, Iteration.class };
25: private SearchResultFactory searchResultFactory;
26: private SearchContentQuery query;
27:
28: public ContentSearchHelper(SearchResultFactory searchResultFactory,
29: SearchContentQuery query) {
30: this .searchResultFactory = searchResultFactory;
31: this .query = query;
32: }
33:
34: public List getSearchResults() {
35: return searchResults;
36: }
37:
38: public void search(String searchCriteria, int userId,
39: int restrictedProjectId) throws RepositoryException {
40: this .searchCriteria = searchCriteria;
41: query.setRestrictedProjectId(restrictedProjectId);
42: List results = findMatchingResults(searchCriteria);
43: excludeResultsBasedOnUserPermissions(results, userId);
44: searchResults = results;
45: }
46:
47: private List findMatchingResults(String searchCriteria)
48: throws RepositoryException {
49: List results = new LinkedList();
50: for (int i = 0; i < searchableClasses.length; i++) {
51: convertMatchesToResults(findMatchingObjects(
52: searchableClasses[i], searchCriteria), results);
53: }
54: return results;
55: }
56:
57: protected List findObjectsMatching(Class[] searchableClasses,
58: String searchCriteria) throws RepositoryException {
59: List results = new LinkedList();
60: for (int i = 0; i < searchableClasses.length; i++) {
61: convertMatchesToResults(findMatchingObjects(
62: searchableClasses[i], searchCriteria), results);
63: }
64: return results;
65: }
66:
67: private List findMatchingObjects(Class objectClass,
68: String searchCriteria) throws RepositoryException {
69: return query.findWhereNameOrDescriptionContains(searchCriteria,
70: objectClass);
71: }
72:
73: private void excludeResultsBasedOnUserPermissions(List results,
74: int userId) {
75: Predicate predicate = getAuthorizationPredicate(userId);
76: CollectionUtils.filter(results, predicate);
77: }
78:
79: protected Predicate getAuthorizationPredicate(int userId) {
80: return new SearchResultAuthorizationPredicate(userId);
81: }
82:
83: private void convertMatchesToResults(List matches, List results) {
84: for (Iterator iterator = matches.iterator(); iterator.hasNext();) {
85: try {
86: results.add(searchResultFactory
87: .convertObjectToSearchResult(
88: (Nameable) iterator.next(),
89: searchCriteria));
90: } catch (Exception ex) {
91: }
92: }
93: }
94:
95: }
|