From 3d9ae8d1f685aa9be55e6169f558a34eb7351f1f Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 16 May 2016 17:50:58 +0530 Subject: [PATCH] Fix for https://wso2.org/jira/browse/IOTS-152 --- .../configuration.hbs | 7 +- .../public/js/platform-configuration.js | 120 ++++++++++++++++++ 2 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs index 7074e69854..5960212352 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/configuration.hbs @@ -15,8 +15,7 @@
+{{#zone "bottomJs"}} + {{js "js/platform-configuration.js"}} +{{/zone}} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js new file mode 100644 index 0000000000..f2704873a4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.platform.configuration/public/js/platform-configuration.js @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +$(document).ready(function () { + + var configParams = { + "NOTIFIER_TYPE": "notifierType", + "NOTIFIER_FREQUENCY": "notifierFrequency" + }; + + var responseCodes = { + "CREATED": "Created", + "SUCCESS": "201", + "INTERNAL_SERVER_ERROR": "Internal Server Error" + }; + + /** + * Checks if provided input is valid against RegEx input. + * + * @param regExp Regular expression + * @param inputString Input string to check + * @returns {boolean} Returns true if input matches RegEx + */ + function isPositiveInteger(str) { + return /^\+?(0|[1-9]\d*)$/.test(str); + } + + invokerUtil.get( + "/devicemgt_admin/configuration", + function (data) { + data = JSON.parse(data); + if (data && data.configuration) { + for (var i = 0; i < data.configuration.length; i++) { + var config = data.configuration[i]; + if (config.name == configParams["NOTIFIER_FREQUENCY"]) { + $("input#monitoring-config-frequency").val(config.value / 1000); + } + } + } + }, function (data) { + console.log(data); + }); + + /** + * Following click function would execute + * when a user clicks on "Save" button + * on General platform configuration page in WSO2 EMM Console. + */ + $("button#save-general-btn").click(function () { + var notifierFrequency = $("input#monitoring-config-frequency").val(); + var errorMsgWrapper = "#email-config-error-msg"; + var errorMsg = "#email-config-error-msg span"; + + if (!notifierFrequency) { + $(errorMsg).text("Monitoring frequency is a required field. It cannot be empty."); + $(errorMsgWrapper).removeClass("hidden"); + } else if (!isPositiveInteger(notifierFrequency)) { + $(errorMsg).text("Provided monitoring frequency is invalid. "); + $(errorMsgWrapper).removeClass("hidden"); + } else { + var addConfigFormData = {}; + var configList = new Array(); + + var monitorFrequency = { + "name": configParams["NOTIFIER_FREQUENCY"], + "value": String((notifierFrequency * 1000)), + "contentType": "text" + }; + + configList.push(monitorFrequency); + addConfigFormData.configuration = configList; + + var addConfigAPI = "/devicemgt_admin/configuration"; + invokerUtil.post( + addConfigAPI, + addConfigFormData, + function (data) { + data = JSON.parse(data); + if (data.statusCode == responseCodes["SUCCESS"]) { + $("#config-save-form").addClass("hidden"); + $("#record-created-msg").removeClass("hidden"); + } else if (data == 500) { + $(errorMsg).text("Exception occurred at backend."); + } else if (data == 403) { + $(errorMsg).text("Action was not permitted."); + } else { + $(errorMsg).text("An unexpected error occurred."); + } + + $(errorMsgWrapper).removeClass("hidden"); + }, function (data) { + data = data.status; + if (data == 500) { + $(errorMsg).text("Exception occurred at backend."); + } else if (data == 403) { + $(errorMsg).text("Action was not permitted."); + } else { + $(errorMsg).text("An unexpected error occurred."); + } + $(errorMsgWrapper).removeClass("hidden"); + } + ); + } + }); +});