settings.js

1       // ${project.version}
2       var exec = require("cordova/exec");
3       
4       
5       /**
6        * The Kapsel Settings plugin provides settings exchange functionality.
7        *
8        * @namespace
9        * @alias Settings
10       * @memberof sap
11       */
12      var SettingsExchange = function () {};
13      
14      SettingsExchange.prototype.connectionData   = null;
15      SettingsExchange.prototype.store            = null;
16      SettingsExchange.prototype.settingsSuccess  = null;
17      SettingsExchange.prototype.SettingsError    = null;
18      SettingsExchange.prototype.isInitialized    = false;
19      
20      
21      /**
22       * Starts the settings exchange process upon onSapLogonSuccess event.
23       * @private
24       */
25      var doSettingExchange = function () {
26          
27          
28          sap.Settings.isInitialized = true;
29          var pd ="";
30          sap.Logon.unlock(function (connectionInfo) {
31                           var userName = connectionInfo["registrationContext"]["user"];
32                           var password  = connectionInfo["registrationContext"]["password"];
33                           var applicationConnectionId = connectionInfo["applicationConnectionId"];
34                           var securityConfig = connectionInfo["registrationContext"]["securityConfig"];
35                           var endpoint = connectionInfo["applicationEndpointURL"];
36                           var keySSLEnabled = "false";
37                           var splitendpoint = endpoint.split("/");
38                           if (splitendpoint[0] == "https:")
39                           {
40                               keySSLEnabled="true";
41                           }
42                           if (securityConfig == null) {
43                               securityConfig = "";
44                           }
45                           var burl = splitendpoint[2];
46                           var appId = splitendpoint[3];
47                           pd = appId+userName+password;
48                           sap.Settings.store = new sap.EncryptedStorage("SettingsStore", pd);
49                           connectionData = {
50                               "keyMAFLogonOperationContextConnectionData": {
51                                   "keyMAFLogonConnectionDataApplicationSettings":
52                                   {
53                                       "DeviceType":device.platform,
54                                       "DeviceModel":device.model,
55                                       "ApplicationConnectionId":applicationConnectionId
56                                   },
57                                   "keyMAFLogonConnectionDataBaseURL":burl
58                               },
59                               "keyMAFLogonOperationContextApplicationId":appId,
60                               "keyMAFLogonOperationContextBackendUserName":userName,
61                               "keyMAFLogonOperationContextBackendPassword":password,
62                               "keyMAFLogonOperationContextSecurityConfig":securityConfig,
63                               "keySSLEnabled":keySSLEnabled
64                           };
65                           sap.Settings.start(connectionData,
66                                              function(mesg) {
67                                                  sap.Settings.isInitialized = true;
68                                                  sap.Logger.debug("Setting Exchange is successful "+mesg,"SMP_SETTINGS_JS",function(m){},function(m){});
69                                              },
70                                              function(mesg){
71                                                  sap.Logger.debug("Setting Exchange failed" + mesg,"SMP_SETTINGS_JS",function(m){},function(m){});
72                                                  sap.Settings.isInitialized = false;
73                                              });
74                           }
75                           , function () {
76                           sap.Logger.debug("unlock failed ","SMP_SETTINGS_JS",function(m){},function(m){});
77                           }
78                           );
79          
80          
81      };
82      
83      document.addEventListener("onSapLogonSuccess", doSettingExchange, false);
84      document.addEventListener("onSapResumeSuccess", doSettingExchange, false);
85      
86      SettingsExchange.prototype.reset = function(key, sucessCB, errorCB)
87      {
88          if ((typeof(sap.Settings.store) != undefined) && (sap.Settings.store != null)) {
89              sap.Settings.store.removeItem(key, sucessCB, errorCB);
90          } else {
91              errorCB("Cannot access setting store");
92          }
93      }
94      
95      
96      /**
97       * Starts the settings exchange.
98       * @public
99       * @memberof sap.Settings
100      * @method start
101      * @param {String} connectionData The example below shows the structure of the connection data.
102      * @param {function} successCallback Function to invoke if the exchange is successful.
103      * @param {function} errorCallback Function to invoke if the exchange failed.
104      * @example
105      * connectionData = {
106      *      "keyMAFLogonOperationContextConnectionData": {
107      *      "keyMAFLogonConnectionDataApplicationSettings":
108      *      {
109      *      "DeviceType":device.platform,
110      *      "DeviceModel":device.model,
111      *      "ApplicationConnectionId":"yourappconnectionid"
112      *      },
113      *      "keyMAFLogonConnectionDataBaseURL":"servername:port"
114      *  },
115      *  "keyMAFLogonOperationContextApplicationId":"yourapplicationid",
116      *  "keyMAFLogonOperationContextBackendUserName":"yourusername",
117      *  "keyMAFLogonOperationContextBackendPassword":"password",
118      *  "keyMAFLogonOperationContextSecurityConfig":"securityConfigName",
119      *  "keySSLEnabled":keySSLEnabled
120      *  };
121      * sap.Settings.start(connectionData, function(mesg) {
122      *                                    
123      *                                         sap.Logger.debug("Setting Exchange is successful "+mesg,"SMP_SETTINGS_JS",function(m){},function(m){});
124      *                                     },
125      *                                    function(mesg){
126      *                                        sap.Logger.debug("Setting Exchange failed" + mesg,"SMP_SETTINGS_JS",function(m){},function(m){});
127      *                                    });
128      */
129     SettingsExchange.prototype.start = function (connectionData, successCallback, errorCallback) {
130         sap.Settings.settingsSuccess = successCallback;
131         sap.Settings.SettingsError = errorCallback;
132         sap.Settings.connectionData = connectionData;
133         sap.Logger.debug("Accessing the data from vault","SMP_SETTINGS_JS",function(m){},function(m){});
134         sap.Settings.store.getItem("settingsdata", sap.Settings.getStoreDataSuccess, sap.Settings.getStoreDataError);
135         
136         
137     };
138     
139     
140     
141     
142     
143     
144     /**
145      *  This is a private function. End user will not use this plugin directly.
146      *  This function gets called after the start function is able to read the current settings from the secured storage.
147      *  @private
148      *  @param {String} value This is the value of the current setting exchange stored in the secured store.
149      **/
150     
151     SettingsExchange.prototype.getStoreDataSuccess  = function(value){
152         storedSettings = value;
153         sap.Logger.debug("Exchanging the data","SMP_SETTINGS_JS",function(m){},function(m){});
154         exec(sap.Settings.SettingsExchangeDone,
155              sap.Settings.SettingsExchangeError,
156              "SMPSettingsExchangePlugin",
157              "start", [JSON.stringify(connectionData),storedSettings]);
158     }
159     
160     /**
161      *  This is a private function. End user will not use this plugin directly.
162      *  This function is called after the start function is unable to read the current settings from the secured storage.
163      *  @private
164      *  @param {String} message This is the error message produced by the encrypted storage.
165      **/
166     SettingsExchange.prototype.getStoreDataError  = function(mesage){
167         sap.Logger.debug("Setting exchange failed to read data store: Proceeding without data",function(m){},function(m){});
168     }
169     
170     
171     /**
172      *  This is a private function. End user will not use this plugin directly.
173      *  This function is called after the settings exchange completes succefully.
174      *  @private
175      *  @param {String} message This is the  message produced when the settings plugin completes successfully.
176      **/
177     
178     SettingsExchange.prototype.SettingsExchangeDone = function(message) {
179         sap.Logger.debug("Setting Exchange Success","SMP_SETTINGS_JS",function(m){},function(m){});
180         var jsondata =  JSON.parse(message);
181         settingsString = JSON.stringify(jsondata["data"]);
182         sap.Settings.store.setItem("settingsdata",settingsString,sap.Settings.SettingsWriteDone,sap.Settings.SettingsWriteError);
183         if (sap.Settings.settingsSuccess != null) {
184             sap.Logger.debug("Setting exchange successful","SMP_SETTINGS_JS",function(m){},function(m){});
185             sap.Settings.settingsSuccess(jsondata["msg"]);
186         }
187     }
188     
189     /**
190      *  This is a private function. End user will not use this plugin directly.
191      *  This function is called after the settings exchange completes succefully
192      *  @private
193      *  @param {String} message This is the error message produced when the settings plugin has an error.
194      **/
195     SettingsExchange.prototype.SettingsExchangeError = function(message) {
196         sap.Logger.error("Setting Exchange failed calling the error callback funciton","SMP_SETTINGS_JS",function(m){},function(m){});
197         if (sap.Settings.SettingsError != null) {
198             sap.Settings.SettingsError(message);
199         }
200     }
201     
202     /**
203      *  This is a private function. End user will not use this plugin directly.
204      *  This function is called after the setting data is stored successfully.
205      *  @private
206      *  @param {String} message This is the message produced upon successful storing of settings to the encrypted store.
207      **/
208     SettingsExchange.prototype.SettingsWriteDone  = function(message) {
209         sap.Logger.debug("Setting stored","SMP_SETTINGS_JS",function(m){},function(m){});
210         
211     }
212     
213     /**
214      *  This is a private function. End user will not use this plugin directly.
215      *  This function is called after the storing of the setting data fails.
216      *  @private
217      *  @param {String} message This is the message produced upon failure to store the settings to the encrypted store.
218      **/
219     SettingsExchange.prototype.SettingsWriteError  = function(message) {
220         sap.Logger.error("Setting store failed","SMP_SETTINGS_JS",function(m){},function(m){});
221     }
222     
223     /**
224      *  This is a private function. End user will not use this plugin directly.
225      *  This function is called after the deviceready. This uploads the logs to the server.
226      *  @private
227      *  @param {boolean} uploadLog This indicates whether the upload log is currently enabled or disbled.
228      **/
229     SettingsExchange.prototype.logLevelUpdated  = function(logLevel)
230     {
231         sap.Logger.setLogLevel(logLevel, sap.Settings.LogLevelSetSuccess, sap.Settings.LogLevelSetFailed);
232         sap.Logger.upload(sap.Settings.logUploadedSuccess, sap.Settings.logUploadFailed);
233     }
234     
235     /**
236      *  This is a private function. End user will not use this plugin directly.
237      *  This function is called when the log upload succeeds.
238      *  @private
239      *  @param {mesg} logupload message
240      **/
241     SettingsExchange.prototype.LogLevelSetSuccess = function(mesg){
242         sap.Logger.debug("Log level set successful","SMP_SETTINGS_JS",function(m){},function(m){});
243     }
244     /**
245      *  This is a private function. End user will not use this plugin directly.
246      *  This function is called when the log upload succeeds.
247      *  @private
248      *  @param {mesg} logupload message
249      **/
250     SettingsExchange.prototype.LogLevelSetFailed = function(mesg){
251         sap.Logger.error("Log level set failed","SMP_SETTINGS_JS",function(m){},function(m){});
252     }
253     
254     /**
255      *  This is a private function. End user will not use this plugin directly.
256      *  This function is called when the log upload succeeds.
257      *  @private
258      *  @param {mesg} logupload message
259      **/
260     SettingsExchange.prototype.logUploadedSuccess = function(mesg){
261         sap.Logger.debug("Log upload successful","SMP_SETTINGS_JS",function(m){},function(m){});
262     }
263     /**
264      *  This is a private function. End user will not use this plugin directly.
265      *  This function is called when the log upload fails.
266      *  @private
267      *  @param {mesg} logupload failure message
268      **/
269     SettingsExchange.prototype.logUploadFailed = function(mesg) {
270         sap.Logger.error("upload log failed","SMP_SETTINGS_JS",function(m){},function(m){});
271         
272     }
273     
274     
275     
276     module.exports = new SettingsExchange();
277     
278     
279     
280     
281