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.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 final Logger LOG = Logger.getLogger(SearchDispatcherImpl.class);
33
34 private List<SearchService> services;
35
36
37 private Map<String,SearchService> serviceMap = null;
38
39
40
41 public SearchDispatcherImpl() {
42 super();
43 }
44
45 public SearchDispatcherImpl(SearchService... services){
46 super();
47 this.services = new ArrayList<SearchService>();
48 if(services!=null){
49 for(SearchService service:services){
50 this.services.add(service);
51 }
52 }
53 }
54
55 public synchronized void init(){
56 if(serviceMap==null){
57 serviceMap = new HashMap<String,SearchService>();
58
59
60 for(SearchService service:services){
61 if(null==service){
62 LOG.warn("Null service passed to SearchDelegator");
63 }else{
64 try {
65 List<SearchTypeInfo> searchTypes = service.getSearchTypes();
66 if(searchTypes!=null){
67 for(SearchTypeInfo searchType:searchTypes){
68 serviceMap.put(searchType.getKey(),service);
69 }
70 }
71 } catch (OperationFailedException e) {
72 LOG.warn("Error getting searchTypes",e);
73 }
74 }
75 }
76 }
77 }
78
79
80
81
82
83
84
85 public SearchResult dispatchSearch(SearchRequest searchRequest) {
86
87
88 if(serviceMap==null){
89 init();
90 }
91
92 if(searchRequest != null){
93 String searchKey = searchRequest.getSearchKey();
94 SearchService searchService = serviceMap.get(searchKey);
95 if(searchService != null){
96 SearchResult searchResult;
97 try {
98 searchResult = searchService.search(searchRequest);
99 } catch (Exception e) {
100 LOG.warn("Error invoking search",e);
101 return null;
102 }
103 return searchResult;
104 }
105 LOG.error("Error Dispatching, Search Service not found for search key:"+searchRequest.getSearchKey());
106 }
107 return null;
108 }
109
110 public void setServices(List<SearchService> services) {
111 this.services = services;
112 }
113 }