| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SearchDispatcherImpl |
|
| 3.8;3.8 |
| 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.HashMap; | |
| 20 | import java.util.List; | |
| 21 | import java.util.Map; | |
| 22 | ||
| 23 | import org.apache.log4j.Logger; | |
| 24 | import org.kuali.student.common.exceptions.OperationFailedException; | |
| 25 | import org.kuali.student.common.search.dto.SearchRequest; | |
| 26 | import org.kuali.student.common.search.dto.SearchResult; | |
| 27 | import org.kuali.student.common.search.dto.SearchTypeInfo; | |
| 28 | import org.kuali.student.common.search.service.SearchDispatcher; | |
| 29 | import org.kuali.student.common.search.service.SearchService; | |
| 30 | ||
| 31 | public class SearchDispatcherImpl implements SearchDispatcher{ | |
| 32 | 0 | final Logger LOG = Logger.getLogger(SearchDispatcherImpl.class); |
| 33 | ||
| 34 | private List<SearchService> services; | |
| 35 | ||
| 36 | //Map of search key->service | |
| 37 | 0 | private Map<String,SearchService> serviceMap = null; |
| 38 | ||
| 39 | ||
| 40 | ||
| 41 | public SearchDispatcherImpl() { | |
| 42 | 0 | super(); |
| 43 | 0 | } |
| 44 | ||
| 45 | public SearchDispatcherImpl(SearchService... services){ | |
| 46 | 0 | super(); |
| 47 | 0 | this.services = new ArrayList<SearchService>(); |
| 48 | 0 | if(services!=null){ |
| 49 | 0 | for(SearchService service:services){ |
| 50 | 0 | this.services.add(service); |
| 51 | } | |
| 52 | } | |
| 53 | 0 | } |
| 54 | ||
| 55 | public synchronized void init(){ | |
| 56 | 0 | if(serviceMap==null){ |
| 57 | 0 | serviceMap = new HashMap<String,SearchService>(); |
| 58 | ||
| 59 | //Look through each service, grab it's search keys and add them to the map | |
| 60 | 0 | for(SearchService service:services){ |
| 61 | 0 | if(null==service){ |
| 62 | 0 | LOG.warn("Null service passed to SearchDelegator"); |
| 63 | }else{ | |
| 64 | try { | |
| 65 | 0 | List<SearchTypeInfo> searchTypes = service.getSearchTypes(); |
| 66 | 0 | if(searchTypes!=null){ |
| 67 | 0 | for(SearchTypeInfo searchType:searchTypes){ |
| 68 | 0 | serviceMap.put(searchType.getKey(),service); |
| 69 | } | |
| 70 | } | |
| 71 | 0 | } catch (OperationFailedException e) { |
| 72 | 0 | LOG.warn("Error getting searchTypes",e); |
| 73 | 0 | } |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | 0 | } |
| 78 | ||
| 79 | ||
| 80 | /** | |
| 81 | * Delegates to the service responsible for the given search type key | |
| 82 | * @param searchRequest | |
| 83 | * @return The searchResult from the delegated search or null | |
| 84 | */ | |
| 85 | public SearchResult dispatchSearch(SearchRequest searchRequest) { | |
| 86 | //Lazy Load service map. THis might cause synchronization issues? | |
| 87 | //Needed because of circular bean dependencies... dispatch->serviceImpl->searchMgr->crossSvcMgr->dispatch | |
| 88 | 0 | if(serviceMap==null){ |
| 89 | 0 | init(); |
| 90 | } | |
| 91 | //Lookup which service to call for given search key and do the search | |
| 92 | 0 | if(searchRequest != null){ |
| 93 | 0 | String searchKey = searchRequest.getSearchKey(); |
| 94 | 0 | SearchService searchService = serviceMap.get(searchKey); |
| 95 | 0 | if(searchService != null){ |
| 96 | SearchResult searchResult; | |
| 97 | try { | |
| 98 | 0 | searchResult = searchService.search(searchRequest); |
| 99 | 0 | } catch (Exception e) { |
| 100 | 0 | LOG.warn("Error invoking search",e); |
| 101 | 0 | return null; |
| 102 | 0 | } |
| 103 | 0 | return searchResult; |
| 104 | } | |
| 105 | 0 | LOG.error("Error Dispatching, Search Service not found for search key:"+searchRequest.getSearchKey()); |
| 106 | } | |
| 107 | 0 | return null; |
| 108 | } | |
| 109 | ||
| 110 | public void setServices(List<SearchService> services) { | |
| 111 | 0 | this.services = services; |
| 112 | 0 | } |
| 113 | } |