001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package jsx3.app;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Read-Write per-User settings for a particular GI application (server).
024:
025: This implementation stores settings in a browser cookie.
026: * @author Joe Walker [joe at getahead dot org]
027: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
028: */
029: public class UserSettings extends jsx3.lang.Object {
030: /**
031: * All reverse ajax proxies need context to work from
032: * @param scriptProxy The place we are writing scripts to
033: * @param context The script that got us to where we are now
034: */
035: public UserSettings(Context context, String extension,
036: ScriptProxy scriptProxy) {
037: super (context, extension, scriptProxy);
038: }
039:
040: /**
041: * The instance initializer.
042: * @param objServer the app server.
043: * @param intPersistence the persistence code, defaults to <code>PERSIST_INDEFINITE</code>.
044: */
045: public UserSettings(jsx3.app.Server objServer, int intPersistence) {
046: super ((Context) null, (String) null, (ScriptProxy) null);
047: ScriptBuffer script = new ScriptBuffer();
048: script
049: .appendCall("new UserSettings", objServer,
050: intPersistence);
051: setInitScript(script);
052: }
053:
054: /**
055: *
056: */
057: public static final int PERSIST_SESSION = 1;
058:
059: /**
060: *
061: */
062: public static final int PERSIST_INDEFINITE = 2;
063:
064: /**
065: * Returns a stored setting value.
066: * @param strKey the setting key.
067: * @param callback the stored value.
068: */
069: @SuppressWarnings("unchecked")
070: public void get(String strKey,
071: org.directwebremoting.proxy.Callback<String> callback) {
072: ScriptBuffer script = new ScriptBuffer();
073: String callbackPrefix = "";
074:
075: if (callback != null) {
076: callbackPrefix = "var reply = ";
077: }
078:
079: script.appendCall(callbackPrefix + getContextPath() + "get",
080: strKey);
081:
082: if (callback != null) {
083: String key = org.directwebremoting.extend.CallbackHelper
084: .saveCallback(callback, String.class);
085: script
086: .appendCall("__System.activateCallback", key,
087: "reply");
088: }
089:
090: getScriptProxy().addScript(script);
091: }
092:
093: /**
094: * Sets a stored setting value.
095: * @param strKey the setting key.
096: * @param value the value to store.
097: */
098: public void set(String strKey, Object[] value) {
099: ScriptBuffer script = new ScriptBuffer();
100: script.appendCall(getContextPath() + "set", strKey, value);
101: getScriptProxy().addScript(script);
102: }
103:
104: /**
105: * Sets a stored setting value.
106: * @param strKey the setting key.
107: * @param value the value to store.
108: */
109: public void set(String strKey, jsx3.lang.Object value) {
110: ScriptBuffer script = new ScriptBuffer();
111: script.appendCall(getContextPath() + "set", strKey, value);
112: getScriptProxy().addScript(script);
113: }
114:
115: /**
116: * Sets a stored setting value.
117: * @param strKey the setting key.
118: * @param value the value to store.
119: */
120: public void set(String strKey, Integer value) {
121: ScriptBuffer script = new ScriptBuffer();
122: script.appendCall(getContextPath() + "set", strKey, value);
123: getScriptProxy().addScript(script);
124: }
125:
126: /**
127: * Sets a stored setting value.
128: * @param strKey the setting key.
129: * @param value the value to store.
130: */
131: public void set(String strKey, String value) {
132: ScriptBuffer script = new ScriptBuffer();
133: script.appendCall(getContextPath() + "set", strKey, value);
134: getScriptProxy().addScript(script);
135: }
136:
137: /**
138: * Sets a stored setting value.
139: * @param strKey the setting key.
140: * @param value the value to store.
141: */
142: public void set(String strKey, boolean value) {
143: ScriptBuffer script = new ScriptBuffer();
144: script.appendCall(getContextPath() + "set", strKey, value);
145: getScriptProxy().addScript(script);
146: }
147:
148: /**
149: * Removes a stored setting value.
150: * @param strKey the key of the setting to remove.
151: */
152: public void remove(String strKey) {
153: ScriptBuffer script = new ScriptBuffer();
154: script.appendCall(getContextPath() + "remove", strKey);
155: getScriptProxy().addScript(script);
156: }
157:
158: /**
159: * Clears all settings of this user settings instance. This implementation deletes the cookie.
160: */
161: public void clear() {
162: ScriptBuffer script = new ScriptBuffer();
163: script.appendCall(getContextPath() + "clear");
164: getScriptProxy().addScript(script);
165: }
166:
167: /**
168: * Persists the user settings. Any modifications to this user settings instance will be lost if this method
169: is not called.
170: */
171: public void save() {
172: ScriptBuffer script = new ScriptBuffer();
173: script.appendCall(getContextPath() + "save");
174: getScriptProxy().addScript(script);
175: }
176:
177: }
|