1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.search.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.log4j.Logger;
23 import org.kuali.student.common.dao.SearchableDao;
24 import org.kuali.student.common.exceptions.DoesNotExistException;
25 import org.kuali.student.common.exceptions.InvalidParameterException;
26 import org.kuali.student.common.exceptions.MissingParameterException;
27 import org.kuali.student.common.exceptions.OperationFailedException;
28 import org.kuali.student.common.search.dto.CrossSearchTypeInfo;
29 import org.kuali.student.common.search.dto.SearchCriteriaTypeInfo;
30 import org.kuali.student.common.search.dto.SearchRequest;
31 import org.kuali.student.common.search.dto.SearchResult;
32 import org.kuali.student.common.search.dto.SearchResultTypeInfo;
33 import org.kuali.student.common.search.dto.SearchTypeInfo;
34 import org.kuali.student.common.search.service.SearchManager;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.context.support.FileSystemXmlApplicationContext;
37
38
39
40
41
42 public class SearchManagerImpl implements SearchManager{
43
44 final Logger logger = Logger.getLogger(SearchManagerImpl.class);
45
46 private String searchContextFile;
47 private Map<String, SearchTypeInfo> searchInfoTypeMap;
48 private Map<String, SearchCriteriaTypeInfo> searchCriteriaTypeMap;
49 private Map<String, SearchResultTypeInfo> searchResultTypeInfoMap;
50 private Map<String, String> queryMap;
51
52 private CrossSearchManager crossSearchManager;
53
54 @SuppressWarnings("unchecked")
55 private void init() {
56 ApplicationContext ac = new FileSystemXmlApplicationContext(searchContextFile);
57 searchInfoTypeMap = ac.getBeansOfType(SearchTypeInfo.class);
58 searchCriteriaTypeMap = ac.getBeansOfType(SearchCriteriaTypeInfo.class);
59 searchResultTypeInfoMap = ac.getBeansOfType(SearchResultTypeInfo.class);
60 queryMap = (Map<String, String>) ac.getBean("queryMap");
61 }
62
63 public SearchManagerImpl(String searchContextFile) {
64 super();
65 this.searchContextFile = searchContextFile;
66 init();
67 }
68
69 public SearchCriteriaTypeInfo getSearchCriteriaType(
70 String searchCriteriaTypeKey) throws DoesNotExistException,
71 InvalidParameterException, MissingParameterException,
72 OperationFailedException {
73 return searchCriteriaTypeMap.get(searchCriteriaTypeKey);
74 }
75
76 public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes()
77 throws OperationFailedException {
78 return new ArrayList<SearchCriteriaTypeInfo>(searchCriteriaTypeMap
79 .values());
80 }
81
82 public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey)
83 throws DoesNotExistException, InvalidParameterException,
84 MissingParameterException, OperationFailedException {
85 return searchResultTypeInfoMap.get(searchResultTypeKey);
86 }
87
88 public List<SearchResultTypeInfo> getSearchResultTypes()
89 throws OperationFailedException {
90 return new ArrayList<SearchResultTypeInfo>(searchResultTypeInfoMap
91 .values());
92 }
93
94 public SearchTypeInfo getSearchType(String searchTypeKey)
95 throws DoesNotExistException, InvalidParameterException,
96 MissingParameterException, OperationFailedException {
97 return searchInfoTypeMap.get(searchTypeKey);
98 }
99
100 public List<SearchTypeInfo> getSearchTypes()
101 throws OperationFailedException {
102 return new ArrayList<SearchTypeInfo>(searchInfoTypeMap.values());
103 }
104
105 public List<SearchTypeInfo> getSearchTypesByCriteria(
106 String searchCriteriaTypeKey) throws DoesNotExistException,
107 InvalidParameterException, MissingParameterException,
108 OperationFailedException {
109 List<SearchTypeInfo> searchTypesByCriteria = new ArrayList<SearchTypeInfo>();
110 for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
111 if (searchCriteriaTypeKey.equals(searchTypeInfo
112 .getSearchCriteriaTypeInfo().getKey())) {
113 searchTypesByCriteria.add(searchTypeInfo);
114 }
115 }
116 return searchTypesByCriteria;
117 }
118
119 public List<SearchTypeInfo> getSearchTypesByResult(
120 String searchResultTypeKey) throws DoesNotExistException,
121 InvalidParameterException, MissingParameterException,
122 OperationFailedException {
123 List<SearchTypeInfo> searchTypesByResult = new ArrayList<SearchTypeInfo>();
124 for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
125 if (searchResultTypeKey.equals(searchTypeInfo
126 .getSearchResultTypeInfo().getKey())) {
127 searchTypesByResult.add(searchTypeInfo);
128 }
129 }
130 return searchTypesByResult;
131 }
132
133 public String getSearchContextFile() {
134 return searchContextFile;
135 }
136
137 public void setSearchContext(String searchContextFile) {
138 this.searchContextFile = searchContextFile;
139 }
140
141 @Override
142 public SearchResult search(SearchRequest searchRequest, SearchableDao dao) throws MissingParameterException {
143 if(searchRequest == null){
144 throw new MissingParameterException("Search Request can not be null.");
145 }
146
147 String searchKey = searchRequest.getSearchKey();
148
149 SearchTypeInfo searchType = searchInfoTypeMap.get(searchKey);
150
151
152 if(searchType == null){
153 if(crossSearchManager!=null && crossSearchManager.getSearchDispatcher()!=null){
154 logger.info("Search type '"+searchKey+"' is not known to this service's search manager, attempting to dispatch search.");
155 return crossSearchManager.getSearchDispatcher().dispatchSearch(searchRequest);
156 }else{
157 logger.error("Search type '"+searchKey+"' is not known to this service's search manager.");
158 throw new RuntimeException("Search type '"+searchKey+"' is not known to this service's search manager.");
159 }
160 }
161
162
163 if(searchType instanceof CrossSearchTypeInfo){
164 if(crossSearchManager == null){
165
166 throw new RuntimeException("Requested cross service search:"+searchKey+", but no cross service search manager was defined.");
167 }
168 return crossSearchManager.doCrossSearch(searchRequest, (CrossSearchTypeInfo) searchType);
169 }
170
171
172 try{
173 return dao.search(searchRequest, queryMap, searchType);
174 }catch (Exception e){
175 logger.error("Search Failed for searchKey:"+searchKey,e);
176 throw new RuntimeException("Search Failed for searchKey:"+searchKey, e);
177 }
178 }
179
180 public void setCrossSearchManager(CrossSearchManager crossSearchManager) {
181 this.crossSearchManager = crossSearchManager;
182 }
183
184 }