01: package org.drools.brms.client.packages;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: import org.drools.brms.client.common.ErrorPopup;
23: import org.drools.brms.client.common.GenericCallback;
24: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
25: import org.drools.brms.client.rpc.RepositoryServiceFactory;
26:
27: import com.google.gwt.user.client.Command;
28:
29: /**
30: * This utility cache will maintain a cache of suggestion completion engines,
31: * as they are somewhat heavy to load.
32: * If it needs to be loaded, then it will load, and then call the appropriate action,
33: * and keep it in the cache.
34: *
35: * @author Michael Neale
36: *
37: */
38: public class SuggestionCompletionCache {
39:
40: private static SuggestionCompletionCache INSTANCE = new SuggestionCompletionCache();
41:
42: Map cache = new HashMap();
43:
44: public static SuggestionCompletionCache getInstance() {
45: return INSTANCE;
46: }
47:
48: /**
49: * This will do the action, after refreshing the cache if necessary.
50: */
51: public void doAction(String packageName, Command command) {
52:
53: if (!this .cache.containsKey(packageName)) {
54: loadPackage(packageName, command);
55: } else {
56: command.execute();
57: }
58:
59: }
60:
61: public SuggestionCompletionEngine getEngineFromCache(
62: String packageName) {
63: SuggestionCompletionEngine eng = (SuggestionCompletionEngine) cache
64: .get(packageName);
65: if (eng == null) {
66: ErrorPopup
67: .showMessage("Unable to get content assistance for this rule.");
68: return null;
69: }
70: return eng;
71: }
72:
73: void loadPackage(final String packageName, final Command command) {
74: System.out.println("Loading package Suggestions...");
75: RepositoryServiceFactory.getService()
76: .loadSuggestionCompletionEngine(packageName,
77: new GenericCallback() {
78: public void onSuccess(Object data) {
79: SuggestionCompletionEngine engine = (SuggestionCompletionEngine) data;
80: cache.put(packageName, engine);
81: command.execute();
82: }
83: });
84: }
85:
86: /**
87: * Removed the package from the cache, causing it to be loaded the next time.
88: */
89: public void refreshPackage(String packageName, Command done) {
90: if (cache.containsKey(packageName)) {
91: cache.remove(packageName);
92: loadPackage(packageName, done);
93: } else {
94: done.execute();
95: }
96:
97: }
98:
99: }
|