1 package org.kuali.student.common.ui.client.service;
2
3 import com.google.gwt.user.client.rpc.AsyncCallback;
4 import org.kuali.student.core.search.dto.SearchRequest;
5 import org.kuali.student.core.search.dto.SearchResult;
6
7 import java.util.HashMap;
8
9
10
11
12 public class CachingSearchService implements SearchRpcServiceAsync {
13
14 private static CachingSearchService INSTANCE = new CachingSearchService();
15
16 private SearchRpcServiceAsync searchService = SearchServiceFactory.getSearchService();
17
18 private HashMap<SearchRequest, SearchResult> cache = new HashMap<SearchRequest, SearchResult>();
19
20 public static CachingSearchService getSearchService() {
21 return INSTANCE;
22 }
23
24 @Override
25 public void search(final SearchRequest searchRequest, final AsyncCallback<SearchResult> callback) {
26 cachingSearch(searchRequest, callback);
27 }
28
29 @Override
30 public void cachingSearch(final SearchRequest searchRequest, final AsyncCallback<SearchResult> callback) {
31 if (cache.containsKey(searchRequest)) {
32 callback.onSuccess(cache.get(searchRequest));
33 } else {
34 searchService.cachingSearch(searchRequest, new AsyncCallback<SearchResult>() {
35 @Override
36 public void onFailure(Throwable caught) {
37 callback.onFailure(caught);
38 }
39
40 @Override
41 public void onSuccess(SearchResult searchResult) {
42 cache.put(searchRequest, searchResult);
43 callback.onSuccess(searchResult);
44 }
45 });
46 }
47 }
48 }