
1 // ${project.version} 2 var exec = require('cordova/exec'); 3 4 /** 5 * @class 6 * @classdesc Provides methods for interacting with the offline OData store. 7 * 8 */ 9 var OfflineStore = function(properties) { 10 /** 11 * The unique name of the store. 12 * @type {String} 13 * @readonly 14 */ 15 this.name = null; 16 17 /** 18 * Identifies the root of an OData service. 19 * @type {String} 20 * @readonly 21 */ 22 this.serviceRoot = null; 23 24 /** 25 * Object that contains the coverage of data as name value pairs. The names are arbitrary and used when performing 26 * refreshes with subsets. The values are OData URLs represent the coverage of data to be managed by the store. 27 * @type {Object} 28 * @readonly 29 */ 30 this.definingRequests = {}; 31 32 /** 33 * The host of the server. 34 * @type {String} 35 * @readonly 36 */ 37 this.host = null; 38 39 /** 40 * The port of the server. 41 * @type {Number} 42 * @readonly 43 */ 44 this.port = 80; 45 46 /** 47 * The URL suffix path to the server 48 * @type {String} 49 * @readonly 50 */ 51 this.urlSuffix = null; 52 53 /** 54 * Whether to use HTTP or HTTPS. Default is HTTP. 55 * @type {Boolean} 56 * @readonly 57 */ 58 this.https = false; 59 60 /** 61 * Any additional stream parameters. 62 * @type {String} 63 * @readonly 64 */ 65 this.streamParams = null; 66 67 /** 68 * Object that contains the headers to send as name value pairs. 69 * @type {Object} 70 * @readonly 71 */ 72 this.customHeaders = {}; 73 74 /** 75 * Object that contains the cookies to send as name value pairs. 76 * @type {Object} 77 * @readonly 78 */ 79 this.customCookies = {}; 80 81 /** 82 * If the OData Producer is able to support repeatable requests then enable use of this. 83 * @type {Boolean} 84 * @readonly 85 */ 86 this.enableRepeatableRequests = false; 87 88 /** 89 * Called when a modification request fails against the OData producer. 90 * This can be called multiple times during a single flush if there is a bunch of pending modifications. 91 * @type {OfflineStore~requesterror} 92 * @example 93 * store.onrequesterror = function(error) { 94 * console.log("Error occurred while sending modification to server. " + error); 95 * }; 96 */ 97 this.onrequesterror = null; 98 99 // Set the properties for the store. 100 for (var i in properties) { 101 if (typeof this[i] !== 'undefined' && properties.hasOwnProperty(i)) { 102 this[i] = properties[i]; 103 } 104 } 105 106 /** 107 * Return path in format "/path" 108 */ 109 function formatPath(path) 110 { 111 if (path && path.indexOf("/") !== 0) 112 { 113 path = "/" + path; 114 } 115 116 if (path && path.lastIndexOf("/") === path.length -1) 117 { 118 path = path.substring(0, path.length - 1); 119 } 120 121 return path ? path : ""; 122 } 123 124 if (this.serviceRoot.indexOf("http://") === 0 || this.serviceRoot.indexOf("https://") === 0) 125 { 126 this.serviceUri = this.serviceRoot; 127 } 128 else 129 { 130 this.serviceUri = (this.https ? "https" : "http") + "://" + this.host + ":" + this.port + formatPath(this.urlSuffix) + formatPath(this.serviceRoot); 131 } 132 }; 133 134 /** 135 * Offline store flush error callback. 136 * @callback OfflineStore~requesterror 137 * @param {String} error - The error message. 138 */ 139 140 /** 141 * Offline store success callback. 142 * @callback OfflineStore~success 143 */ 144 145 /** 146 * Offline store error callback. 147 * @callback OfflineStore~error 148 * @param {String} error - The error message. 149 */ 150 151 /** 152 * Creates a new OfflineStore object. 153 * The store will be available for offline access only after it is open successfully. 154 * @param {OfflineStore~success} success - Called when store successfully opens. 155 * @param {OfflineStore~error} error - Called when store fails to open. 156 * @param {Object} [options] - Options used when opening the store object 157 * @param {String} [options.encryptionKey] - Key to use to encrypt the local data store. 158 */ 159 OfflineStore.prototype.open = function(success, error, options) { 160 var self = this; 161 162 var win = function(result) { 163 // Store events share open success callback. 164 if (result && result.event) 165 { 166 var callback = "on" + result.event; 167 if (self[callback]) { 168 self[callback](result.message); 169 } 170 } 171 else { 172 sap.OData.stores.push(self); 173 success(); 174 } 175 }; 176 177 exec(win, error, 'OData', 'openOfflineStore', [this, options ? options : {}]); 178 }; 179 180 /** 181 * Closes the store and releases its resources. 182 * Closing the store will attempt to interrupt any operation already occurring against 183 * this store, and will block until the other operations have finished. 184 * @param {OfflineStore~success} success - Called when store successfully closes. 185 * @param {OfflineStore~error} error - Called when store fails to close. 186 */ 187 OfflineStore.prototype.close = function(success, error) { 188 var that = this; 189 190 var win = function() { 191 var index = sap.OData.stores.indexOf(that); 192 if (index > -1) { 193 sap.OData.stores.splice(index, 1); 194 } 195 success(); 196 }; 197 198 exec(win, error, 'OData', 'close', [this.serviceUri]); 199 }; 200 201 /** 202 * Refreshes the store with the OData service. 203 * The application must have network connectivity to perform this operation. 204 * @param {OfflineStore~success} success - Called when store is successfully refreshed. 205 * @param {OfflineStore~error} error - Called if store fails to refresh itself. 206 * @param {Array} [subset] - List of the names of the defining requests to refresh. 207 */ 208 OfflineStore.prototype.refresh = function(success, error, subset) { 209 exec(success, error, 'OData', 'refresh', [this.serviceUri, subset]); 210 }; 211 212 /** 213 * Removes the physical store from the filesystem. The store must be closed before clearing. 214 * @param {OfflineStore~success} success - Called when store is successfully cleared. 215 * @param {OfflineStore~error} error - Called if store cannot be cleared. 216 */ 217 OfflineStore.prototype.clear = function(success, error) { 218 exec(success, error, 'OData', 'clear', [this.name]); 219 }; 220 221 /** 222 * Starts sending pending modification requests to the server. 223 * The application must have network connectivity to perform this operation. 224 * @param {OfflineStore~success} success - Called when flush has finished. 225 * @param {OfflineStore~error} error - Called if error calling flush. 226 */ 227 OfflineStore.prototype.flush = function(success, error) { 228 exec(success, error, 'OData', 'flush', [this.serviceUri]); 229 }; 230 231 module.exports = OfflineStore;