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 | 0 | public class CachingSearchService implements SearchRpcServiceAsync { |
13 | |
|
14 | 0 | private static CachingSearchService INSTANCE = new CachingSearchService(); |
15 | |
|
16 | 0 | private SearchRpcServiceAsync searchService = SearchServiceFactory.getSearchService(); |
17 | |
|
18 | 0 | private HashMap<SearchRequest, SearchResult> cache = new HashMap<SearchRequest, SearchResult>(); |
19 | |
|
20 | |
public static CachingSearchService getSearchService() { |
21 | 0 | return INSTANCE; |
22 | |
} |
23 | |
|
24 | |
@Override |
25 | |
public void search(final SearchRequest searchRequest, final AsyncCallback<SearchResult> callback) { |
26 | 0 | cachingSearch(searchRequest, callback); |
27 | 0 | } |
28 | |
|
29 | |
@Override |
30 | |
public void cachingSearch(final SearchRequest searchRequest, final AsyncCallback<SearchResult> callback) { |
31 | 0 | if (cache.containsKey(searchRequest)) { |
32 | 0 | callback.onSuccess(cache.get(searchRequest)); |
33 | |
} else { |
34 | 0 | searchService.cachingSearch(searchRequest, new AsyncCallback<SearchResult>() { |
35 | |
@Override |
36 | |
public void onFailure(Throwable caught) { |
37 | 0 | callback.onFailure(caught); |
38 | 0 | } |
39 | |
|
40 | |
@Override |
41 | |
public void onSuccess(SearchResult searchResult) { |
42 | 0 | cache.put(searchRequest, searchResult); |
43 | 0 | callback.onSuccess(searchResult); |
44 | 0 | } |
45 | |
}); |
46 | |
} |
47 | 0 | } |
48 | |
} |