View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.kuali.student.r2.common.class1.search;
6   
7   import org.kuali.student.r2.common.dto.ContextInfo;
8   import org.kuali.student.r2.common.exceptions.*;
9   import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
10  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
11  import org.kuali.student.r2.core.search.service.SearchService;
12  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  /**
20   *
21   * @author nwright
22   */
23  public class SearchServiceDispatcherImpl
24          implements SearchService {
25  
26      private List<SearchService> searchServices;
27      private Map<String, SearchService> searchType2ServiceMap = new HashMap<String, SearchService>();
28  
29      public SearchServiceDispatcherImpl() {
30          super();
31      }
32  
33      public SearchServiceDispatcherImpl(SearchService... services){
34          super();
35          this.searchServices = new ArrayList<SearchService>();
36          if(services!=null){
37              for(SearchService service:services){
38                  this.searchServices.add(service);
39              }
40          }
41      }
42  
43      public List<SearchService> getSearchServices() {
44          if (this.searchServices == null) {
45              this.searchServices = new ArrayList<SearchService>();
46          }
47          return searchServices;
48      }
49  
50      public void setSearchServices(List<SearchService> searchServices) {
51          this.searchServices = searchServices;
52      }
53  
54  
55      @Override
56      public TypeInfo getSearchType(String searchTypeKey, ContextInfo contextInfo)
57              throws DoesNotExistException,
58              InvalidParameterException,
59              MissingParameterException,
60              OperationFailedException {
61          SearchService searchService = this.searchType2ServiceMap.get(searchTypeKey);
62          if (searchService != null) {
63              return searchService.getSearchType(searchTypeKey, contextInfo);
64          }
65          for (SearchService ss : searchServices) {
66              try {
67                  TypeInfo type = ss.getSearchType(searchTypeKey, contextInfo);
68                  if (type != null){
69                      this.searchType2ServiceMap.put(searchTypeKey, ss);
70                      return type;
71                  }
72              } catch (DoesNotExistException ex) {
73                  // continue
74              }
75          }
76          throw new DoesNotExistException(searchTypeKey);
77      }
78  
79      @Override
80      public List<TypeInfo> getSearchTypes(ContextInfo contextInfo)
81              throws InvalidParameterException,
82              MissingParameterException,
83              OperationFailedException {
84          List<TypeInfo> list = new ArrayList<TypeInfo>();
85          for (SearchService ss : this.searchServices) {
86              list.addAll(ss.getSearchTypes(contextInfo));
87          }
88          return list;
89      }
90  
91      @Override
92      public SearchResultInfo search(SearchRequestInfo searchRequestInfo, ContextInfo contextInfo)
93              throws MissingParameterException,
94              InvalidParameterException,
95              OperationFailedException,
96              PermissionDeniedException {
97          try {
98              // check type of search is supported
99              TypeInfo type = this.getSearchType(searchRequestInfo.getSearchKey(), contextInfo);
100         } catch (DoesNotExistException ex) {
101             throw new OperationFailedException("unknown/unsupported search type " + searchRequestInfo.getSearchKey());
102         } catch (InvalidParameterException ex) {
103             throw new OperationFailedException("unknown/unsupported search type " + searchRequestInfo.getSearchKey());
104         }
105         SearchService searchService = this.searchType2ServiceMap.get(searchRequestInfo.getSearchKey());
106         if (searchService == null) {
107             throw new OperationFailedException(searchRequestInfo.getSearchKey() + "was not in map as expected");
108         }
109         return searchService.search(searchRequestInfo, contextInfo);
110 
111     }
112 }