Coverage Report - org.kuali.student.common.search.service.impl.SearchManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchManagerImpl
42%
21/50
20%
4/20
2.286
 
 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  1
         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  1
                 ApplicationContext ac = new FileSystemXmlApplicationContext(searchContextFile);
 57  1
                 searchInfoTypeMap = ac.getBeansOfType(SearchTypeInfo.class);
 58  1
                 searchCriteriaTypeMap = ac.getBeansOfType(SearchCriteriaTypeInfo.class);
 59  1
                 searchResultTypeInfoMap = ac.getBeansOfType(SearchResultTypeInfo.class);
 60  1
                 queryMap = (Map<String, String>) ac.getBean("queryMap");
 61  1
         }
 62  
 
 63  
         public SearchManagerImpl(String searchContextFile) {
 64  1
                 super();
 65  1
                 this.searchContextFile = searchContextFile;
 66  1
                 init();
 67  1
         }
 68  
 
 69  
         public SearchCriteriaTypeInfo getSearchCriteriaType(
 70  
                         String searchCriteriaTypeKey) throws DoesNotExistException,
 71  
                         InvalidParameterException, MissingParameterException,
 72  
                         OperationFailedException {
 73  0
                 return searchCriteriaTypeMap.get(searchCriteriaTypeKey);
 74  
         }
 75  
 
 76  
         public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes()
 77  
                         throws OperationFailedException {
 78  0
                 return new ArrayList<SearchCriteriaTypeInfo>(searchCriteriaTypeMap
 79  
                                 .values());
 80  
         }
 81  
 
 82  
         public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey)
 83  
                         throws DoesNotExistException, InvalidParameterException,
 84  
                         MissingParameterException, OperationFailedException {
 85  0
                 return searchResultTypeInfoMap.get(searchResultTypeKey);
 86  
         }
 87  
 
 88  
         public List<SearchResultTypeInfo> getSearchResultTypes()
 89  
                         throws OperationFailedException {
 90  0
                 return new ArrayList<SearchResultTypeInfo>(searchResultTypeInfoMap
 91  
                                 .values());
 92  
         }
 93  
 
 94  
         public SearchTypeInfo getSearchType(String searchTypeKey)
 95  
                         throws DoesNotExistException, InvalidParameterException,
 96  
                         MissingParameterException, OperationFailedException {
 97  0
                 return searchInfoTypeMap.get(searchTypeKey);
 98  
         }
 99  
 
 100  
         public List<SearchTypeInfo> getSearchTypes()
 101  
                         throws OperationFailedException {
 102  1
                 return new ArrayList<SearchTypeInfo>(searchInfoTypeMap.values());
 103  
         }
 104  
 
 105  
         public List<SearchTypeInfo> getSearchTypesByCriteria(
 106  
                         String searchCriteriaTypeKey) throws DoesNotExistException,
 107  
                         InvalidParameterException, MissingParameterException,
 108  
                         OperationFailedException {
 109  0
                 List<SearchTypeInfo> searchTypesByCriteria = new ArrayList<SearchTypeInfo>();
 110  0
                 for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
 111  0
                         if (searchCriteriaTypeKey.equals(searchTypeInfo
 112  
                                         .getSearchCriteriaTypeInfo().getKey())) {
 113  0
                                 searchTypesByCriteria.add(searchTypeInfo);
 114  
                         }
 115  
                 }
 116  0
                 return searchTypesByCriteria;
 117  
         }
 118  
 
 119  
         public List<SearchTypeInfo> getSearchTypesByResult(
 120  
                         String searchResultTypeKey) throws DoesNotExistException,
 121  
                         InvalidParameterException, MissingParameterException,
 122  
                         OperationFailedException {
 123  0
                 List<SearchTypeInfo> searchTypesByResult = new ArrayList<SearchTypeInfo>();
 124  0
                 for (SearchTypeInfo searchTypeInfo : searchInfoTypeMap.values()) {
 125  0
                         if (searchResultTypeKey.equals(searchTypeInfo
 126  
                                         .getSearchResultTypeInfo().getKey())) {
 127  0
                                 searchTypesByResult.add(searchTypeInfo);
 128  
                         }
 129  
                 }
 130  0
                 return searchTypesByResult;
 131  
         }
 132  
 
 133  
         public String getSearchContextFile() {
 134  0
                 return searchContextFile;
 135  
         }
 136  
 
 137  
         public void setSearchContext(String searchContextFile) {
 138  0
                 this.searchContextFile = searchContextFile;
 139  0
         }
 140  
 
 141  
         @Override
 142  
         public SearchResult search(SearchRequest searchRequest, SearchableDao dao) throws MissingParameterException {
 143  1
                 if(searchRequest == null){
 144  0
                         throw new MissingParameterException("Search Request can not be null.");
 145  
                 }
 146  
                 
 147  1
                 String searchKey = searchRequest.getSearchKey();
 148  
 
 149  1
                 SearchTypeInfo searchType = searchInfoTypeMap.get(searchKey);
 150  
                 
 151  
                 //If no type was found for the key, try to dispatch the search
 152  1
                 if(searchType == null){
 153  0
                         if(crossSearchManager!=null && crossSearchManager.getSearchDispatcher()!=null){
 154  0
                                 logger.info("Search type '"+searchKey+"' is not known to this service's search manager, attempting to dispatch search.");
 155  0
                                 return crossSearchManager.getSearchDispatcher().dispatchSearch(searchRequest);
 156  
                         }else{
 157  0
                                 logger.error("Search type '"+searchKey+"' is not known to this service's search manager.");
 158  0
                                 throw new RuntimeException("Search type '"+searchKey+"' is not known to this service's search manager.");
 159  
                         }
 160  
                 }
 161  
 
 162  
                 //Check if the search is a cross search
 163  1
                 if(searchType instanceof CrossSearchTypeInfo){
 164  1
                         if(crossSearchManager == null){
 165  
                                 //FIXME should we change these to Operation Failed Exceptions? also we need to handle invalid parameters.
 166  0
                                 throw new RuntimeException("Requested cross service search:"+searchKey+", but no cross service search manager was defined.");
 167  
                         }
 168  1
                         return crossSearchManager.doCrossSearch(searchRequest, (CrossSearchTypeInfo) searchType);
 169  
                 }
 170  
                 
 171  
 
 172  
                 try{
 173  0
                         return dao.search(searchRequest, queryMap, searchType);
 174  0
                 }catch (Exception e){
 175  0
                         logger.error("Search Failed for searchKey:"+searchKey,e);
 176  0
                         throw new RuntimeException("Search Failed for searchKey:"+searchKey, e);
 177  
                 }
 178  
         }
 179  
 
 180  
         public void setCrossSearchManager(CrossSearchManager crossSearchManager) {
 181  1
                 this.crossSearchManager = crossSearchManager;
 182  1
         }
 183  
 
 184  
 }