"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2015, 2018 * US Government Users Restricted Rights - * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'q', 'bi/commons/ui/properties/PropertyPageView', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/nls/StringResource'], function (_, Q, PropertyPageView, PropertyUIControl, StringResource) { 'use strict'; //NOSONAR: meant to be strict var SessionLoggingPane = PropertyPageView.extend({ sessionLoggingUrl: 'v1/glug/sessions/current', sessionConfigUrl: 'v1/glug/sessions', init: function init(options) { SessionLoggingPane.inherited('init', this, arguments); $.extend(this, options); }, render: function render() { SessionLoggingPane.inherited('render', this, arguments); //get current session logging setting return this._getCurrentSession().then(function (defaults) { //check if this session is expired var defaultSession = this._renderCurrentSession(defaults); this.currentSession = defaultSession; this.enableSessionButton = { type: 'SingleLineLinks', multiline: false, name: 'enableSessionComb', id: 'enableSessionComb', items: [{ align: 'left', items: [{ type: 'text', value: StringResource.get('sessionLoggingBtn') }] }, { align: 'right', items: [{ 'type': 'text', 'value': this.currentSession.enable === true ? StringResource.get('on') : StringResource.get('off'), 'class': 'enableSessionText' }, { 'type': 'ToggleButton', 'name': 'enableSession', 'id': 'enableSession', 'checked': this.currentSession.enable, 'onChange': function () { this.updateSessionLoggingConfiguration().then(function () { this.refresh(); }.bind(this)); }.bind(this) }] }] }; this.logIdentifiers = { 'type': 'SingleLineValue', 'label': StringResource.get("logIdentifier"), 'editable': false, 'value': this.currentSession.logIdentifier, 'multiline': true, 'name': 'logIdentifiers', 'ellipses': false }; var aControls = []; //if not enabled all turn grey if (!this.currentSession.enable) { this.logIdentifiers['disabled'] = true; this.logIdentifiers['multiline'] = false; this.enableSessionButton['checked'] = false; } aControls.push({ 'value': StringResource.get('sessionLoggingTitle'), 'type': 'Banner' }); aControls.push(this.enableSessionButton); aControls.push(this.logIdentifiers); aControls.push({ 'type': 'SectionLabel', 'label': '' }, { 'type': 'TextArea', 'label': StringResource.get("sessionLoggingDescription") }, { 'type': 'TextArea', 'label': StringResource.get("sessionLoggingNoification") }); this._oPropertyUIControl = this._getNewPropertyUIControl(aControls); return this._oPropertyUIControl.render(); }.bind(this)); }, _getNewPropertyUIControl: function _getNewPropertyUIControl(items) { return new PropertyUIControl({ 'el': this.$el, 'glassContext': this.glassContext, 'items': items }); }, _getCurrentSession: function _getCurrentSession() { var ajaxService = this.glassContext.services.ajax; var promise = ajaxService.ajax({ 'dataType': 'json', 'type': 'GET', 'data': {}, 'url': this.sessionLoggingUrl }); return Q.when(promise); }, _renderCurrentSession: function _renderCurrentSession(sessionJson) { var renderedObj; if (_.isUndefined(sessionJson.error)) { var renderedTime = this._doCalculation(sessionJson['expiry'], sessionJson['durationSec']); renderedObj = { 'enable': true, 'duration': renderedTime['duration'], 'startTime': renderedTime['startTime'], 'endTime': renderedTime['endTime'], 'logIdentifier': sessionJson['sessionID'] }; } else { renderedObj = { 'enable': false, 'duration': '1', 'startTime': '--', 'endTime': '--', 'logIdentifier': '--' }; } return renderedObj; }, _doCalculation: function _doCalculation(expiry, durationSec) { var renderedTime; var duration = durationSec / 3600; var endDate = new Date(expiry); var secToMilli = durationSec * 1000; var startDate = new Date(expiry - secToMilli); var startTime = this._formatTimeString(startDate); var endTime = this._formatTimeString(endDate); renderedTime = { 'duration': duration, 'startTime': startTime, 'endTime': endTime }; return renderedTime; }, _formatTimeString: function _formatTimeString(date) { var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'PM' : 'AM'; hours = hours % 12; // the hour '0' should be '12' hours = hours ? hours : 12; minutes = minutes < 10 ? '0' + minutes : minutes; var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var formatTimeStr = hours + ':' + minutes + ' ' + ampm + ' ' + month + '/' + day + '/' + year; return formatTimeStr; }, updateSessionLoggingConfiguration: function updateSessionLoggingConfiguration() { var uiControl = this.getPropertyUIControl(); var modifiedProps = uiControl.getModifiedProperties(); var duration = modifiedProps.sessionExpirationLevel; if (_.isUndefined(duration)) { duration = 1; } var active = modifiedProps.enableSession; var durationMin = duration * 60; if (active) { return this._enbaleSessionLogging(durationMin).then(function () { return this.glassContext.appController.showToast(StringResource.get('sessionSuccessMsg'), { 'type': 'success' }); }.bind(this)); } else { return this._disableSessionLogging().then(function () { return this.glassContext.appController.showToast(StringResource.get('sessionSuccessDisableMsg'), { 'type': 'success' }); }.bind(this)); } //be defaultly period n hours is 1 //check if enabling or disabling //update and update the value }, _enbaleSessionLogging: function _enbaleSessionLogging(durationMin) { var loggingSettingsDeferred; var loggingSettings = { "durationMin": durationMin }; var ajaxService = this.glassContext.services.ajax; loggingSettingsDeferred = ajaxService.ajax({ 'type': 'POST', 'url': this.sessionConfigUrl, 'contentType': 'application/json', 'data': JSON.stringify(loggingSettings) }); return Q.when(loggingSettingsDeferred); }, _disableSessionLogging: function _disableSessionLogging() { var loggingSettingsDeferred; var ajaxService = this.glassContext.services.ajax; loggingSettingsDeferred = ajaxService.ajax({ 'dataType': 'json', 'type': 'DELETE', 'data': {}, 'url': this.sessionLoggingUrl }); return Q.when(loggingSettingsDeferred); }, refresh: function refresh() { //remove all the items then render this._oPropertyUIControl.remove(); this.render(); } }); return SessionLoggingPane; });