2  * ============LICENSE_START==========================================
 
   4  * ===================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ===================================================================
 
   8  * Unless otherwise specified, all software contained herein is licensed
 
   9  * under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this software except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *             http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * Unless otherwise specified, all documentation contained herein is licensed
 
  22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 
  23  * you may not use this documentation except in compliance with the License.
 
  24  * You may obtain a copy of the License at
 
  26  *             https://creativecommons.org/licenses/by/4.0/
 
  28  * Unless required by applicable law or agreed to in writing, documentation
 
  29  * distributed under the License is distributed on an "AS IS" BASIS,
 
  30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  31  * See the License for the specific language governing permissions and
 
  32  * limitations under the License.
 
  34  * ============LICENSE_END============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  40     class WidgetOnboardingDetailsModalCtrl {
 
  41         constructor($scope, $log, $interval, applicationsService, adminsService, microserviceService, widgetsCatalogService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies,items) {
 
  43             this.appUpdate = function(){
 
  44                 this.hasSelectedApp = false;
 
  46                 for(var i = 0; i < this.availableApps.length; i++){
 
  47                         if(this.availableApps[i].isSelected){
 
  49                                 if(!this.hasSelectedApp)
 
  50                                         this.hasSelectedApp = true;
 
  52                         if(this.availableApps[i].isSelected
 
  53                         && this.availableApps[i].roles.length == 0){
 
  55                                 this.availableRoles = [];    
 
  56                                 adminsService.getRolesByApp(this.availableApps[i].id).then(roles => {
 
  57                                            for(var i = 0; i < roles.length; i++){
 
  58                                                    this.availableRoles.push({
 
  64                                            this.availableApps[index].roles = this.availableRoles;
 
  68                 this.allRoleSelected = true;
 
  69                 this.checkRoleSelected();
 
  72             this.roleUpdate = function(app){
 
  73                         this.allRoleSelected = true;
 
  74                 for(var i = 0; i < app.roles.length; i++){
 
  75                         if(app.roles[i].isSelected){
 
  76                                 app.roleSelected = true;
 
  77                                 this.checkRoleSelected();
 
  81                         app.roleSelected = false;
 
  82                         this.checkRoleSelected();
 
  85             this.checkRoleSelected = function(){
 
  86                 for(var i = 0; i < this.availableApps.length; i++){
 
  87                         if(this.availableApps[i].isSelected
 
  88                         && !this.availableApps[i].roleSelected){
 
  89                                 this.allRoleSelected = false;
 
  95             this.getAppName = function(appId){
 
  96                  for(var i = 0; i < this.availableApps.length; i++){
 
  97                          if(this.availableApps[i].id == appId){
 
  98                                  return this.availableApps[i].name;
 
 103             let newWidgetModel = {
 
 110                 showAppOptions: false,
 
 111                 showRoleOptions: false,
 
 112                 hasSelectedApp: false
 
 115             let getAvailableApps = () => {              
 
 117                 if(this.isEditMode == false){   
 
 118                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
 
 119                                 this.availableApps=[];
 
 120                                 for(var i=0;i<apps.length;i++) {
 
 121                                 if (!apps[i].restrictedApp) {
 
 122                             this.availableApps.push({
 
 135                 else if(this.isEditMode == true){
 
 136                         if(this.widget.allowAllUser == "Y")
 
 137                                 this.widget.allUser = true;
 
 138                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
 
 139                                 this.availableApps=[];
 
 140                                 let selectedApps = {};
 
 141                                 var availableApps = this.availableApps;  
 
 142                                 this.allRoleSelected = true;
 
 143                                 for(var i=0; i < this.widget.widgetRoles.length; i++){
 
 144                                         if(selectedApps[this.widget.widgetRoles[i].app.appId] != undefined)
 
 145                                                 selectedApps[this.widget.widgetRoles[i].app.appId] += this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";"; 
 
 147                                                 selectedApps[this.widget.widgetRoles[i].app.appId] = this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";";                       
 
 151                                 apps.forEach(function(app, index){
 
 159                                                 if(selectedApps[app.id] != undefined){
 
 160                                                         adminsService.getRolesByApp(app.id).then(roles => {
 
 161                                                         var role = selectedApps[app.id].split(';');
 
 162                                                 var selectedRoles = [];
 
 164                                                 while((n+1) < role.length){
 
 171                                                         for(var m = 0; m < roles.length; m++){
 
 172                                                                 var hasSelected = true;
 
 173                                                                 for(var n = 0; n < selectedRoles.length; n++){
 
 174                                                                         if(selectedRoles[n].id == roles[m].id){
 
 187                                                         availableApps[index].roleSelected = true;
 
 188                                                         availableApps[index].isSelected = true;
 
 189                                                         availableApps[index].roles = selectedRoles;
 
 197             let getAvailableServices = () =>{
 
 198                 microserviceService.getServiceList().then(services => {
 
 199                         this.availableServices = [];
 
 200                         for(var i = 0; i < services.length; i++){
 
 201                                 this.availableServices.push({
 
 203                                         name: services[i].name,
 
 204                                         option: services[i].name + ": " + services[i].url
 
 207                                 if(this.widget.serviceId != null && this.widget.serviceId == services[i].id){
 
 208                                         this.widget.serviceURL = this.availableServices[i];
 
 218                 $log.info('WidgetOnboardingDetailsModalCtrl::init');
 
 219                 this.widgetsList = [];
 
 220                 this.duplicatedName = true;
 
 221                 this.allRoleSelected = false;
 
 223                 this.isSaving = false;
 
 224                 this.allUser = false;
 
 225                 this.emptyWidgetName = false;
 
 227                 if (items && items.widget) {
 
 228                     this.isEditMode = true;
 
 229                         this.allRoleSelected = true;
 
 230                     this.widget = _.clone(items.widget);
 
 232                     this.isEditMode = false;
 
 233                     this.widget = _.clone(newWidgetModel);
 
 236                 widgetsCatalogService.getManagedWidgets().then(res => {
 
 237                         for(var i = 0; i < res.length; i++){
 
 238                                 this.widgetsList.push(res[i].name);
 
 241                      $log.error('WidgetOnboardingDetailsModalCtrl::init error: ' + err);
 
 243                      this.isLoadingTable = false;
 
 246                  getAvailableServices();
 
 248             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
 
 249             this.conflictMessages = {};
 
 252             let resetConflict = fieldName => {
 
 253                 delete this.conflictMessages[fieldName];
 
 254                 if($scope.widgetForm[fieldName]){
 
 255                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
 
 259             this.updateSelectedRole = () => {
 
 260                 if (!this.selectedRole) {
 
 263                 this.widget.RoleId = this.selectedRole.id;
 
 264                 this.widget.RoleName = this.selectedRole.name;
 
 267             let emptyCookies = () => {
 
 268                 userProfileService.getUserProfile()
 
 270                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
 
 271                     $scope.orgUserId = profile.orgUserId;
 
 272                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
 
 273                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
 
 274                         $cookies.remove($scope.orgUserId + '_widget');
 
 279             this.updateWidgetName = () => {
 
 280                 for(var i = 0; i < this.widgetsList.length; i++){
 
 281                         if(this.widget.name.toUpperCase() == this.widgetsList[i].toUpperCase()){
 
 282                                 this.duplicatedName = false;
 
 286                 this.duplicatedName = true;
 
 289             this.saveChanges = () => {     
 
 292                         this.updateWidgetName();
 
 294                 if(this.duplicatedName == false 
 
 295                    || this.widget.name == ''
 
 296                    || this.widget.name == undefined){
 
 297                         this.emptyWidgetName = true;
 
 301                 if((this.widget.file == undefined && !this.isEditMode) ||
 
 302                 (!this.widget.allUser && this.appCounter == 0) ||
 
 303                 this.widget.name == null ||
 
 304                 (!this.widget.allUser && !this.allRoleSelected) ||
 
 305                 this.widget.saving == true)
 
 309                         this.widget.saving = true;
 
 310                 var selectedRoles = [];
 
 311                 if(!this.widget.allUser){
 
 312                         for(var i = 0; i < this.availableApps.length; i++){
 
 313                                 if(this.availableApps[i].isSelected){
 
 314                                                 for(var n = 0; n < this.availableApps[i].roles.length; n++) {
 
 315                                                         if(this.availableApps[i].roles[n].isSelected){
 
 318                                                                         appId: this.availableApps[i].id
 
 320                                                                 roleId: this.availableApps[i].roles[n].id,
 
 321                                                                 roleName: this.availableApps[i].roles[n].name,
 
 323                                                         selectedRoles.push(role);
 
 330                 var allowAllUser = 0;
 
 331                 if(this.widget.allUser)
 
 334                 var serviceId = null;
 
 335                 if(this.widget.serviceURL != null &&
 
 336                 this.widget.serviceURL != undefined){
 
 337                         serviceId = parseInt(this.widget.serviceURL);
 
 341                 var file_loc = this.widget.name + ".zip";
 
 343                                 name: this.widget.name,
 
 344                                 desc: this.widget.desc,
 
 345                                 widgetRoles: selectedRoles,
 
 346                                 fileLocation: file_loc,
 
 347                                 allowAllUser: allowAllUser,
 
 353                         if(this.widget.file != undefined){
 
 354                                 widgetsCatalogService.updateWidgetWithFile(this.widget.file, this.widget.id, newWidget).then(res => {
 
 357                                                         confirmBoxService.showInformation("Could not save. Please retry.");
 
 358                                                         this.widget.saving = false;
 
 361                                                 confirmBoxService.showInformation(res.error);
 
 362                                                 this.widget.saving = false;
 
 365                                         $scope.$dismiss('cancel');
 
 366                                 this.widget.saving = false;
 
 370                                 widgetsCatalogService.updateWidget(this.widget.id, newWidget)
 
 372                                         $scope.$dismiss('cancel');
 
 378                         widgetsCatalogService.createWidget(newWidget, this.widget.file).then(res => {
 
 381                                                         confirmBoxService.showInformation("Could not save. Please retry.");
 
 383                                                         confirmBoxService.showInformation(res.error);
 
 384                                                 this.widget.saving = false;
 
 387                                         $scope.$dismiss('cancel');
 
 389                                 this.widget.saving = false;
 
 394             $scope.$on('$stateChangeStart', e => {
 
 399     WidgetOnboardingDetailsModalCtrl.$inject = ['$scope', '$log', '$interval', 'applicationsService', 'adminsService', 'microserviceService', 'widgetsCatalogService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies','items'];
 
 400     angular.module('ecompApp').controller('WidgetOnboardingDetailsModalCtrl', WidgetOnboardingDetailsModalCtrl);