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.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   * Loads all search info for a service into memory
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 		//Check if the search is a cross search
150 		SearchTypeInfo searchType = searchInfoTypeMap.get(searchKey);
151 		if(searchType instanceof CrossSearchTypeInfo){
152 			if(crossSearchManager == null){
153 				//FIXME should we change these to Operation Failed Exceptions? also we need to handle invalid parameters.
154 				throw new RuntimeException("Requested cross service search:"+searchKey+", but no cross service search manager was defined.");
155 			}
156 			return crossSearchManager.doCrossSearch(searchRequest, (CrossSearchTypeInfo) searchType);
157 		}
158 		
159 
160 		try{
161 			return dao.search(searchRequest, queryMap, searchType);
162 		}catch (Exception e){
163 			logger.error("Search Failed for searchKey:"+searchKey,e);
164 			throw new RuntimeException("Search Failed for searchKey:"+searchKey, e);
165 		}
166 	}
167 
168 	public void setCrossSearchManager(CrossSearchManager crossSearchManager) {
169 		this.crossSearchManager = crossSearchManager;
170 	}
171 
172 }