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