1 /*
2 * Sybase Hybrid App version 2.3.4
3 *
4 * SUPStorage.js
5 * This file will not be regenerated, so it is possible to modify it, but it
6 * is not recommended.
7 *
8 * Copyright (c) 2012 Sybase Inc. All rights reserved.
9 */
10
11 /**
12 * The namespace for the Hybrid Web Container javascript
13 * @namespace
14 */
15 hwc = (typeof hwc === "undefined" || !hwc) ? {} : hwc; // SUP 'namespace'
16
17
18 /**
19 * Access the storage functions, which allow you to specify a cache that stores results from online requests.
20 *
21 * These functions give you the ability to:
22 * Name the cached result sets
23 * Enumerate the cached result sets
24 * Read, delete, and modify cached contents individually for each cached result set
25 * Cached result sets must be stored as strings (before deserialization to an xmlWorkflowMessage structure).
26 */
27 (function(hwc, window, undefined) {
28
29 /**
30 * Creates a SUPStorage with the specified storeName. Provides encrypted storage of name value pairs. Results from online requests are one example.
31 * Strings stored in SUPStorage are encrypted and persisted to survive multiple invocations of the mobile workflow application.
32 * @desc Storage
33 * @memberOf hwc
34 * @constructor
35 * @param {string} store the store name
36 *
37 * @example
38 * var store1 = new hwc.SUPStorage("one");
39 */
40 hwc.SUPStorage = function(store) {
41 this.bForSharedStorage = false;
42 this.store = store ? store : "";
43 };
44
45 /**
46 * Gets the number of available keys in this object. The keys themselves may be
47 * retrieved using key().
48 * @desc Storage
49 * @memberOf hwc.SUPStorage
50 * @public
51 * @example
52 * // Create the SUP Storage
53 * var store = new hwc.SUPStorage ("one");
54 * store.setItem ("foo", "bar"); // add an item.
55 * store.setItem ("foo1", "bar"); // add an item.
56 * store.setItem ("foo2", "bar"); // add an item.
57 * var result = store.length; // result = 3
58 */
59 hwc.SUPStorage.prototype.length = function() {
60 var response;
61 hwc.traceEnteringMethod("hwc.SUPStorage.length");
62 try {
63 if (hwc.isWindowsMobile() || hwc.isIOS()) {
64 response = hwc.getDataFromContainer("workflowstorage", "&command=length&shared=" + this.bForSharedStorage +
65 "&store=" + encodeURIComponent(this.store));
66 return parseInt(response, 10);
67 }
68 else {
69 if (this.bForSharedStorage) {
70 return _SharedStorage.length(hwc.versionURLParam);
71 }
72 else {
73 return SUPStorage.length(this.store);
74 }
75 }
76 } finally {
77 hwc.traceLeavingMethod("hwc.SUPStorage.length");
78 }
79 };
80
81 /**
82 * Returns the key at the supplied index. Keys are guaranteed to remain
83 * at the same index until a modification is made.
84 *
85 * @desc Storage
86 * @public
87 * @memberOf hwc.SUPStorage
88 * @param {Integer} index 0-based index to the key. Must be less than the value retrieved
89 * by .length.
90 * @returns {string} The key, or null if the index is invalid.
91 * @example
92 * // Create the SUP Storage
93 * var store = new hwc.SUPStorage ("one");
94 * store.setItem ("foo", "bar"); // add an item.
95 * var result = store.key (0); // will returns "foo".
96 */
97 hwc.SUPStorage.prototype.key = function(index) {
98 var key, isExist;
99 hwc.traceEnteringMethod("hwc.SUPStorage.key");
100 try {
101 if (null === index) {
102 return null;
103 }
104
105 if (hwc.isWindowsMobile() || hwc.isIOS()) {
106 key = hwc.getDataFromContainer("workflowstorage", "&command=key&shared=" + this.bForSharedStorage +
107 "&store=" + encodeURIComponent(this.store) + "&index=" + encodeURIComponent(index));
108
109 if (key === null || typeof key === 'undefined' || key === "") {
110 isExist = hwc.getDataFromContainer("workflowstorage", "&command=exist&shared=" + this.bForSharedStorage +
111 "&store=" + encodeURIComponent(this.store) + "&index=" + encodeURIComponent(index));
112
113 //WM returns empty string if an item does not exist or if the value is empty string
114 //call exist to distinguish this
115 if (isExist == "true") {
116 key = "";
117 }
118 else {
119 key = null;
120 }
121 }
122 }
123 else {
124 if (this.bForSharedStorage) {
125 key = _SharedStorage.key(index, hwc.versionURLParam);
126 }
127 else {
128 key = SUPStorage.key(this.store, index);
129 }
130 }
131
132 if (key === null || typeof key === 'undefined') {
133 return null;
134 } else {
135 return key + "";
136 }
137 } finally {
138 hwc.traceLeavingMethod("hwc.SUPStorage.key");
139 }
140 };
141
142 /**
143 * Helper method for parameter validation
144 * @private
145 * @param {string} input: input value .
146 * @returns {string} if input is null, return empty string
147 */
148 function checkNull(input) {
149 if (null === input) {
150 input = "";
151 }
152 return input;
153 }
154
155 /**
156 * Retrieves the value associated with a specified key.
157 *
158 * @desc Storage
159 * @memberOf hwc.SUPStorage
160 * @param {string} key String key corresponding to the requested value.
161 * @returns {string} A String value corresponding to the key, or null if either the key
162 * is not known, or if the key exists but its value was set to null.
163 * @example
164 * // Create the SUP Storage
165 * var store = new hwc.SUPStorage ("one");
166 * store.setItem ("foo", "bar"); // add an item.
167 * result = store.getItem ("foo"); // will returns "bar".
168 * result = store.getItem ("foo1"); // foo1 does not exists; will return null.
169 */
170 hwc.SUPStorage.prototype.getItem = function(key) {
171 var value, isExist;
172 key = key ? key : "";
173
174 hwc.traceEnteringMethod("hwc.SUPStorage.getItem");
175 try {
176 if (hwc.isWindowsMobile() || hwc.isIOS()) {
177 value = hwc.getDataFromContainer("workflowstorage", "&command=getItem&shared=" + this.bForSharedStorage +
178 "&store=" + encodeURIComponent(this.store) + "&key=" + encodeURIComponent(key));
179
180 if (value === null || typeof value === 'undefined' || value === "") {
181 isExist = hwc.getDataFromContainer("workflowstorage", "&command=exist&shared=" + this.bForSharedStorage +
182 "&store=" + encodeURIComponent(this.store) + "&key=" + encodeURIComponent(key));
183
184 //WM returns empty string if an item does not exist or if the value is empty string
185 //call exist to distinguish this
186 if (isExist == "true") {
187 value = "";
188 }
189 else {
190 value = null;
191 }
192 }
193 }
194 else {
195 if (this.bForSharedStorage) {
196 value = _SharedStorage.getItem(key, hwc.versionURLParam);
197 }
198 else {
199 value = SUPStorage.getItem(this.store, key);
200 }
201 }
202
203 if (value === null || typeof value === 'undefined') {
204 return null;
205 } else {
206 return value + "";
207 }
208 } finally {
209 hwc.traceLeavingMethod("hwc.SUPStorage.getItem");
210 }
211 };
212
213 /**
214 * A constant for the maximum length for a string being stored on BB7
215 * BB7 cannot handle strings with length longer than 524000
216 * This restriction applies to real devices as well as simulators.
217 */
218 hwc.SUPStorage.BB7_MAX_STRING_STORAGE_LENGTH = 524000;
219
220 /**
221 * Sets the value associated with a specified key. This replaces the key's
222 * previous value, if any.
223 *
224 * @desc Storage
225 * @memberOf hwc.SUPStorage
226 * @param {string} key String key corresponding to the value.
227 * @param {string} value String value to store.
228 * @example
229 * // Create the SUP Storage
230 * var store = new hwc.SUPStorage ("one");
231 * store.setItem ("foo", "bar"); // add an item.
232 */
233 hwc.SUPStorage.prototype.setItem = function(key, value) {
234 var result;
235 hwc.traceEnteringMethod("hwc.SUPStorage.setItem");
236 key = key ? key : "";
237 value = value ? value : "";
238 try {
239 if (hwc.isWindowsMobile() || hwc.isIOS()) {
240 hwc.postDataToContainer("workflowstorage", "command=setItem&store=" + encodeURIComponent(this.store) + "&shared=" + this.bForSharedStorage + "&key=" +
241 encodeURIComponent(key) + "&value=" + encodeURIComponent(value));
242 }
243 else {
244 if (hwc.isBlackBerry7() && value.length > hwc.SUPStorage.BB7_MAX_STRING_STORAGE_LENGTH) {
245 throw new hwc.SUPStorageException(hwc.SUPStorageException.MAX_SIZE_REACHED, "SUP storage maximum size reached - maximum length of string to store on BB7 is 524000 but attempted to store string of length " + value.length);
246 }
247 if (this.bForSharedStorage) {
248 result = _SharedStorage.setItem(key, value, hwc.versionURLParam);
249 }
250 else {
251 result = SUPStorage.setItem(this.store, key, value);
252 }
253 if (result !== 0) {
254 throw new hwc.SUPStorageException(result, "SUP storage maximum size reached");
255 }
256 }
257 } finally {
258 hwc.traceLeavingMethod("hwc.SUPStorage.setItem");
259 }
260 };
261
262 /**
263 * Removes the key and its associated value from this object. If the
264 * key does not exist, has no effect.
265 *
266 * @desc Storage
267 * @memberOf hwc.SUPStorage
268 * @param {string} key String key to remove.
269 * @example
270 * // Create the SUP Storage
271 * var store = new hwc.SUPStorage ("one");
272 * store.setItem ("foo", "bar"); // add an item.
273 * store.removeItem ("foo");
274 * result = store.getItem ("food"); // will be null.
275 */
276 hwc.SUPStorage.prototype.removeItem = function(key) {
277 hwc.traceEnteringMethod("hwc.SUPStorage.removeItem");
278 try {
279 key = key ? key : "";
280 if (hwc.isWindowsMobile() || hwc.isIOS()) {
281 hwc.getDataFromContainer("workflowstorage", "&command=removeItem&shared=" + this.bForSharedStorage +
282 "&store=" + encodeURIComponent(this.store) + "&key=" + encodeURIComponent(key));
283 }
284 else {
285 if (this.bForSharedStorage) {
286 _SharedStorage.removeItem(key, hwc.versionURLParam);
287 }
288 else {
289 SUPStorage.removeItem(this.store, key);
290 }
291 }
292 } finally {
293 hwc.traceLeavingMethod("hwc.SUPStorage.removeItem");
294 }
295 };
296
297 /**
298 * Removes all key/value pairs from this object.
299 * @desc Storage
300 * @memberOf hwc.SUPStorage
301 */
302 hwc.SUPStorage.prototype.clear = function() {
303 hwc.traceEnteringMethod("hwc.SUPStorage.clear");
304 try {
305 if (hwc.isWindowsMobile() || hwc.isIOS()) {
306 hwc.getDataFromContainer("workflowstorage", "&command=clear&shared=" + this.bForSharedStorage +
307 "&store=" + encodeURIComponent(this.store));
308 }
309 else {
310 if (this.bForSharedStorage) {
311 _SharedStorage.clear(hwc.versionURLParam);
312 }
313 else {
314 SUPStorage.clear(this.store);
315 }
316 }
317 } finally {
318 hwc.traceLeavingMethod("hwc.SUPStorage.clear");
319 }
320 };
321
322 /**
323 * Exception thrown when Storage space is exceeded.
324 * @desc Storage
325 * @constructor
326 * @memberOf hwc
327 * @param {Integer} code the error code
328 * @param {string} message the error message.
329 */
330 hwc.SUPStorageException = function(code, message) {
331 this.code = code;
332 this.message = message;
333 };
334
335 hwc.SUPStorageException.UNKNOWN_ERROR = 1;
336 hwc.SUPStorageException.MAX_SIZE_REACHED = 2;
337 hwc.SUPStorageException.SHARED_STORAGE_DISABLED = 3;
338
339 // shared storage key.
340 hwc.sharedStorageKey = undefined;
341
342 /**
343 * Method to return the shared storage key defined for the hybrid app by designer. An empty string is returned if the shared storage function is disabled.
344 * @desc Storage
345 * @memberOf hwc
346 * @returns {string} the shared storage key.
347 */
348 hwc.getSharedStorageKey = function() {
349 if (hwc.sharedStorageKey === undefined ) {
350 var key = hwc.getQueryVariable("sharedStorageKey");
351 hwc.sharedStorageKey = (key === undefined) ? "":key;
352 }
353 return hwc.sharedStorageKey;
354 };
355
356 /**
357 * Indicates whether the shared storage is enabled for the hybrid app.
358 * @desc Storage
359 * @memberOf hwc
360 * @returns {boolean} true if the shared storage is enabled; false otherwise.
361 */
362 hwc.isSharedStorageEnabled = function() {
363 var key = hwc.getSharedStorageKey();
364 if (key === undefined || key === "") {
365 return false;
366 }
367 else {
368 return true;
369 }
370 };
371
372 /**
373 * Constructs a new SUP shared storage. You can use the returned value to access the shared storage data with the exising SUPStorage interface,
374 * however, the operation only affects the items belonging to the specified shared storage key.
375 * @classdesc
376 * @memberOf hwc
377 * @desc Storage
378 */
379 hwc.SharedStorage = function() {
380 if (hwc.isSharedStorageEnabled() === false ) {
381 throw new hwc.SUPStorageException(hwc.SUPStorageException.SHARED_STORAGE_DISABLED, "Shared storage is disabled");
382 }
383 this.bForSharedStorage = true;
384 this.store = "";
385 };
386
387 hwc.SharedStorage.prototype = new hwc.SUPStorage();
388 hwc.SharedStorage.constructor = hwc.SharedStorage;
389 })(hwc, window);
390
391
392
393