View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.r1.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.r1.common.dao.SearchableDao;
24  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
25  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
26  import org.kuali.student.r2.common.exceptions.MissingParameterException;
27  import org.kuali.student.r2.common.exceptions.OperationFailedException;
28  import org.kuali.student.r1.common.search.dto.CrossSearchTypeInfo;
29  import org.kuali.student.r1.common.search.dto.SearchCriteriaTypeInfo;
30  import org.kuali.student.r1.common.search.dto.SearchRequest;
31  import org.kuali.student.r1.common.search.dto.SearchResult;
32  import org.kuali.student.r1.common.search.dto.SearchResultTypeInfo;
33  import org.kuali.student.r1.common.search.dto.SearchTypeInfo;
34  import org.kuali.student.r1.common.search.service.SearchManager;
35  import org.springframework.context.ApplicationContext;
36  import org.springframework.context.support.FileSystemXmlApplicationContext;
37  
38  /**
39   * Loads all search info for a service into memory
40   *
41   */
42  @Deprecated
43  public class SearchManagerImpl implements SearchManager{
44  
45  	final Logger logger = Logger.getLogger(SearchManagerImpl.class);
46  	
47  	private String searchContextFile;
48  	private Map<String, SearchTypeInfo> searchInfoTypeMap;
49  	private Map<String, SearchCriteriaTypeInfo> searchCriteriaTypeMap;
50  	private Map<String, SearchResultTypeInfo> searchResultTypeInfoMap;
51  	private Map<String, String> queryMap;
52  	
53  	private CrossSearchManager crossSearchManager;
54  	
55  	@SuppressWarnings("unchecked")
56  	private void init() {
57  		ApplicationContext ac = new FileSystemXmlApplicationContext(searchContextFile);
58  		searchInfoTypeMap = ac.getBeansOfType(SearchTypeInfo.class);
59  		searchCriteriaTypeMap = ac.getBeansOfType(SearchCriteriaTypeInfo.class);
60  		searchResultTypeInfoMap = ac.getBeansOfType(SearchResultTypeInfo.class);
61  		queryMap = (Map<String, String>) ac.getBean("queryMap");
62  	}
63  
64  	public SearchManagerImpl(String searchContextFile) {
65  		super();
66  		this.searchContextFile = searchContextFile;
67  		init();
68  	}
69  
70  	public SearchCriteriaTypeInfo getSearchCriteriaType(
71  			String searchCriteriaTypeKey) throws DoesNotExistException,
72  			InvalidParameterException, MissingParameterException,
73  			OperationFailedException {
74  		return searchCriteriaTypeMap.get(searchCriteriaTypeKey);
75  	}
76  
77  	public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes()
78  			throws OperationFailedException {
79  		return new ArrayList<SearchCriteriaTypeInfo>(searchCriteriaTypeMap
80  				.values());
81  	}
82  
83  	public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey)
84  			throws DoesNotExistException, InvalidParameterException,
85  			MissingParameterException, OperationFailedException {
86  		return searchResultTypeInfoMap.get(searchResultTypeKey);
87  	}
88  
89  	public List<SearchResultTypeInfo> getSearchResultTypes()
90  			throws OperationFailedException {
91  		return new ArrayList<SearchResultTypeInfo>(searchResultTypeInfoMap
92  				.values());
93  	}
94  
95  	public SearchTypeInfo getSearchType(String searchTypeKey)
96  			throws DoesNotExistException, InvalidParameterException,
97  			MissingParameterException, OperationFailedException {
98  		return searchInfoTypeMap.get(searchTypeKey);
99  	}
100 
101 	public List<SearchTypeInfo> getSearchTypes()
102 			throws OperationFailedException {
103 		return new ArrayList<SearchTypeInfo>(searchInfoTypeMap.values());
104 	}
105 
106 	public List<SearchTypeInfo> getSearchTypesByCriteria(
107 			String searchCriteriaTypeKey) throws DoesNotExistException,
108 			InvalidParameterException, MissingParameterException,
109 			OperationFailedException {
110 		List<SearchTypeInfo> searchTypesByCriteria = new ArrayList<SearchTypeInfo>();
111 		for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
112 			if (searchCriteriaTypeKey.equals(searchTypeInfo
113 					.getSearchCriteriaTypeInfo().getKey())) {
114 				searchTypesByCriteria.add(searchTypeInfo);
115 			}
116 		}
117 		return searchTypesByCriteria;
118 	}
119 
120 	public List<SearchTypeInfo> getSearchTypesByResult(
121 			String searchResultTypeKey) throws DoesNotExistException,
122 			InvalidParameterException, MissingParameterException,
123 			OperationFailedException {
124 		List<SearchTypeInfo> searchTypesByResult = new ArrayList<SearchTypeInfo>();
125 		for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
126 			if (searchResultTypeKey.equals(searchTypeInfo
127 					.getSearchResultTypeInfo().getKey())) {
128 				searchTypesByResult.add(searchTypeInfo);
129 			}
130 		}
131 		return searchTypesByResult;
132 	}
133 
134 	public String getSearchContextFile() {
135 		return searchContextFile;
136 	}
137 
138 	public void setSearchContext(String searchContextFile) {
139 		this.searchContextFile = searchContextFile;
140 	}
141 
142 	@Override
143 	public SearchResult search(SearchRequest searchRequest, SearchableDao dao) throws MissingParameterException {
144 		if(searchRequest == null){
145 			throw new MissingParameterException("Search Request can not be null.");
146 		}
147 		
148 		String searchKey = searchRequest.getSearchKey();
149 
150 		SearchTypeInfo searchType = searchInfoTypeMap.get(searchKey);
151 		
152 		//If no type was found for the key, try to dispatch the search
153 		if(searchType == null){
154 			if(crossSearchManager!=null && crossSearchManager.getSearchDispatcher()!=null){
155 				logger.info("Search type '"+searchKey+"' is not known to this service's search manager, attempting to dispatch search.");
156 				return crossSearchManager.getSearchDispatcher().dispatchSearch(searchRequest);
157 			}else{
158 				logger.error("Search type '"+searchKey+"' is not known to this service's search manager.");
159 				throw new RuntimeException("Search type '"+searchKey+"' is not known to this service's search manager.");
160 			}
161 		}
162 
163 		//Check if the search is a cross search
164 		if(searchType instanceof CrossSearchTypeInfo){
165 			if(crossSearchManager == null){
166 				//FIXME should we change these to Operation Failed Exceptions? also we need to handle invalid parameters.
167 				throw new RuntimeException("Requested cross service search:"+searchKey+", but no cross service search manager was defined.");
168 			}
169 			return crossSearchManager.doCrossSearch(searchRequest, (CrossSearchTypeInfo) searchType);
170 		}
171 		
172 
173 		try{
174 			return dao.search(searchRequest, queryMap, searchType);
175 		}catch (Exception e){
176 			logger.error("Search Failed for searchKey:"+searchKey,e);
177 			throw new RuntimeException("Search Failed for searchKey:"+searchKey, e);
178 		}
179 	}
180 
181 	public void setCrossSearchManager(CrossSearchManager crossSearchManager) {
182 		this.crossSearchManager = crossSearchManager;
183 	}
184 
185 }