settings.js

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