"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2017 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['bi/admin/nls/StringResource', 'bi/admin/common/slideout/BasePane', 'bi/commons/ui/properties/PropertyUIControl', 'bi/admin/multitenancy/services/TenantsCustomizationService', 'bi/admin/common/utils/AJAXUtils'], function (StringResource, BasePane, PropertyUIControl, TenantsCustomizationService, AJAXUtils) { var TenantsDetailPane = BasePane.extend({ init: function init(options) { TenantsDetailPane.inherited('init', this, arguments); $.extend(this, options); this.tenantsController = this._getNewTenantsCustomizationService({ glassContext: this.glassContext }); if (this.tenant) { this.title = this.tenant.defaultName; } else { this.title = StringResource.get('addTenantTitle'); } }, _getNewTenantsCustomizationService: function _getNewTenantsCustomizationService(options) { return new TenantsCustomizationService(options); }, renderBody: function renderBody($body) { this.$el.addClass("adm-tenant-addtenant"); var tenantItems = [{ 'id': 'tenantName', 'name': 'tenantName', 'label': StringResource.get('tenantName'), 'value': this.tenant && this.tenant.defaultName ? this.tenant.defaultName : "", 'editable': true, 'type': 'SingleLineValue', 'staticValidationCallback': {}, 'validationAriaLabel': StringResource.get('tenantNameRequired'), 'isString': true }, { 'id': 'tenantId', 'name': 'tenantId', 'label': StringResource.get('tenantId'), 'value': this.tenant && this.tenant.tenantID ? this.tenant.tenantID.toString() : "", 'editable': !(this.tenant && this.tenant.tenantID), 'type': 'SingleLineValue', 'staticValidationCallback': {}, 'validationAriaLabel': StringResource.get('tenantIdRequired'), 'isString': true }, { 'id': 'tenantDescription', 'name': 'tenantDescription', 'label': StringResource.get('tenantDescription'), 'value': this.tenant && this.tenant.defaultDescription ? this.tenant.defaultDescription : "", 'editable': true, 'multiline': true, 'type': 'TextArea' }, { 'type': 'Footer', 'items': this._getFooterItems() }]; this.addTenantPropControl = this._getNewPropertyUIControl({ 'glassContext': this.glassContext, 'el': $body, 'staticValidationMode': true, 'items': tenantItems }); return this.addTenantPropControl.render().then(function () { $("div.property_tenantName div.validationIcon").attr('title', StringResource.get('tenantNameRequired')); $("div.property_tenantId div.validationIcon").attr('title', StringResource.get('tenantIdRequired')); return this.addTenantPropControl; }.bind(this)); }, _getNewPropertyUIControl: function _getNewPropertyUIControl(options) { return new PropertyUIControl(options); }, setFocus: function setFocus() { this.$el.find('#control_tenantName_tenantName').focus(); }, _getFooterItems: function _getFooterItems() { return [{ 'label': this.tenant ? StringResource.get('update') : StringResource.get('add'), 'onSelect': this.tenant ? this.updateTenant.bind(this) : this.addTenant.bind(this), 'primary': true, 'type': 'Button' }, { 'label': StringResource.get('close'), 'onSelect': this.closeSlideout.bind(this), 'primary': false, 'type': 'Button' }]; }, addTenant: function addTenant() { try { var tenantName = this.addTenantPropControl.getProperty('tenantName').getModifiedProperties().tenantName.toString(); var tenantID = this.addTenantPropControl.getProperty('tenantId').getModifiedProperties().tenantId.toString(); } catch (error) { if (error.name === "TypeError") { this.glassContext.appController.showErrorMessage(StringResource.get("enterRequiredFields")); return; } } return this.tenantsController.addTenant({ 'defaultName': tenantName, 'tenantID': tenantID, 'defaultDescription': this.addTenantPropControl.getProperty('tenantDescription').getModifiedProperties().tenantDescription, 'disabled': true }).then(function (data) { this.glassContext.appController.showToast(StringResource.get("multitenancySuccessAdd", { 'tenantName': tenantName })); this.slideout.hide(); if (this.onUpdate) { this.onUpdate(data); } this.slideout.$el.trigger('com.ibm.cognos.bi.admin.updatetenantlist'); }.bind(this), function (ajaxOptions, error) { this.glassContext.appController.showErrorMessage(StringResource.get("multitenacyFailedAdd", { 'tenantName': tenantName }) + ", " + AJAXUtils.buildErrorMessage(error.responseJSON.errors)); }.bind(this)); }, updateTenant: function updateTenant() { var tenantName = this.addTenantPropControl.getProperty('tenantName').getModifiedProperties().tenantName; tenantName = tenantName ? tenantName.toString() : this.tenant.defaultName; var tenantDescription = this.addTenantPropControl.getProperty('tenantDescription').getModifiedProperties().tenantDescription; return this.tenantsController.updateTenant(this.tenant.tenantID, { 'defaultName': tenantName, 'defaultDescription': tenantDescription ? tenantDescription : this.tenant.defaultDescription }).then(function (data) { this.glassContext.appController.showToast(StringResource.get("multitenancySuccessUpdate", { 'tenantName': tenantName })); this.slideout.hide(); this.slideout.$el.trigger('com.ibm.cognos.bi.admin.updatetenantlist'); }.bind(this), function (ajaxOptions, error) { this.glassContext.appController.showErrorMessage(StringResource.get("multitenacyFailedUpdate", { 'tenantName': tenantName }) + ", " + AJAXUtils.buildErrorMessage(error.responseJSON.errors)); }.bind(this)); }, closeSlideout: function closeSlideout() { this.slideout.hide(); } }); return TenantsDetailPane; });