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.ui.server.gwt;
17  
18  import com.google.gwt.user.server.rpc.RemoteServiceServlet;
19  
20  import org.kuali.student.common.assembly.transform.IdTranslatorFilter;
21  import org.kuali.student.common.exceptions.MissingParameterException;
22  import org.kuali.student.common.search.dto.*;
23  import org.kuali.student.common.search.service.SearchDispatcher;
24  import org.kuali.student.common.ui.client.service.SearchRpcService;
25  
26  import java.util.List;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  public class SearchDispatchRpcGwtServlet extends RemoteServiceServlet implements SearchRpcService {
30  
31      private static final long serialVersionUID = 1L;
32  
33      private IdTranslatorFilter idTranslatorFilter;
34  
35      private SearchDispatcher searchDispatcher;
36  
37      private ConcurrentHashMap<SearchRequest, SearchResult> cache = new ConcurrentHashMap<SearchRequest, SearchResult>();
38  
39      public SearchDispatchRpcGwtServlet() {
40          super();
41      }
42  
43      /**
44       * Delegates to the service responsible for the given search type key
45       *
46       * @param searchRequest
47       * @return The searchResult from the delegated search or null
48       * @throws MissingParameterException
49       */
50      @Override
51      public SearchResult search(SearchRequest searchRequest) {
52          SearchResult searchResult = searchDispatcher.dispatchSearch(searchRequest);
53          List<SearchParam> params  = searchRequest.getParams();
54          if(params != null && params.size() > 0){
55              SearchParam firstParam = params.get(0);
56              if(firstParam.getKey().equals("lu.queryParam.cluVersionIndId")){
57                  doIdTranslation(searchResult);
58  
59              }
60          }
61          return searchResult;
62      }
63  
64      @Override
65      public SearchResult cachingSearch(SearchRequest searchRequest) {
66          if(cache.containsKey(searchRequest)){
67              return cache.get(searchRequest);
68          }
69          SearchResult searchResult = search(searchRequest);
70          cache.putIfAbsent(searchRequest, searchResult);
71          return searchResult;
72      }
73  
74      private void doIdTranslation(SearchResult searchResult) {
75          for (SearchResultRow searchResultRow : searchResult.getRows()) {
76              for (SearchResultCell searchResultCell : searchResultRow.getCells()) {
77                  String value = searchResultCell.getValue();
78                  if (value != null && value.startsWith("kuali.atp")) {
79                      String newValue = idTranslatorFilter.getTranslationForAtp(value);
80                      if (newValue != null) {
81                          searchResultCell.setValue(newValue);
82                      }
83                  }
84              }
85          }
86      }
87  
88      public void setSearchDispatcher(SearchDispatcher searchDispatcher) {
89          this.searchDispatcher = searchDispatcher;
90      }
91  
92      public void setIdTranslatorFilter(IdTranslatorFilter idTranslatorFilter) {
93          this.idTranslatorFilter = idTranslatorFilter;
94      }
95  }