2  * ================================================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ================================================================================
 
  22     class WidgetOnboardingDetailsModalCtrl {
 
  23         constructor($scope, $log, $interval, applicationsService, adminsService, microserviceService, widgetsCatalogService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies) {
 
  25             this.appUpdate = function(){
 
  26                 this.hasSelectedApp = false;
 
  28                 for(var i = 0; i < this.availableApps.length; i++){
 
  29                         if(this.availableApps[i].isSelected){
 
  31                                 if(!this.hasSelectedApp)
 
  32                                         this.hasSelectedApp = true;
 
  34                         if(this.availableApps[i].isSelected
 
  35                         && this.availableApps[i].roles.length == 0){
 
  37                                 this.availableRoles = [];    
 
  38                                 adminsService.getRolesByApp(this.availableApps[i].id).then(roles => {
 
  39                                            for(var i = 0; i < roles.length; i++){
 
  40                                                    this.availableRoles.push({
 
  46                                            this.availableApps[index].roles = this.availableRoles;
 
  50                 this.allRoleSelected = true;
 
  51                 this.checkRoleSelected();
 
  54             this.roleUpdate = function(app){
 
  55                         this.allRoleSelected = true;
 
  56                 for(var i = 0; i < app.roles.length; i++){
 
  57                         if(app.roles[i].isSelected){
 
  58                                 app.roleSelected = true;
 
  59                                 this.checkRoleSelected();
 
  63                         app.roleSelected = false;
 
  64                         this.checkRoleSelected();
 
  67             this.checkRoleSelected = function(){
 
  68                 for(var i = 0; i < this.availableApps.length; i++){
 
  69                         if(this.availableApps[i].isSelected
 
  70                         && !this.availableApps[i].roleSelected){
 
  71                                 this.allRoleSelected = false;
 
  77             this.getAppName = function(appId){
 
  78                  for(var i = 0; i < this.availableApps.length; i++){
 
  79                          if(this.availableApps[i].id == appId){
 
  80                                  return this.availableApps[i].name;
 
  85             let newWidgetModel = {
 
  92                 showAppOptions: false,
 
  93                 showRoleOptions: false,
 
  97             let getAvailableApps = () => {              
 
  99                 if(this.isEditMode == false){   
 
 100                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
 
 101                                 this.availableApps=[];
 
 102                                 for(var i=0;i<apps.length;i++) {
 
 103                                 if (!apps[i].restrictedApp) {
 
 104                             this.availableApps.push({
 
 117                 else if(this.isEditMode == true){
 
 118                         if(this.widget.allowAllUser == "Y")
 
 119                                 this.widget.allUser = true;
 
 120                         applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
 
 121                                 this.availableApps=[];
 
 122                                 let selectedApps = {};
 
 123                                 var availableApps = this.availableApps;  
 
 124                                 this.allRoleSelected = true;
 
 125                                 for(var i=0; i < this.widget.widgetRoles.length; i++){
 
 126                                         if(selectedApps[this.widget.widgetRoles[i].app.appId] != undefined)
 
 127                                                 selectedApps[this.widget.widgetRoles[i].app.appId] += this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";"; 
 
 129                                                 selectedApps[this.widget.widgetRoles[i].app.appId] = this.widget.widgetRoles[i].roleId + ";" + this.widget.widgetRoles[i].roleName + ";";                       
 
 133                                 apps.forEach(function(app, index){
 
 141                                                 if(selectedApps[app.id] != undefined){
 
 142                                                         adminsService.getRolesByApp(app.id).then(roles => {
 
 143                                                         var role = selectedApps[app.id].split(';');
 
 144                                                 var selectedRoles = [];
 
 146                                                 while((n+1) < role.length){
 
 153                                                         for(var m = 0; m < roles.length; m++){
 
 154                                                                 var hasSelected = true;
 
 155                                                                 for(var n = 0; n < selectedRoles.length; n++){
 
 156                                                                         if(selectedRoles[n].id == roles[m].id){
 
 169                                                         availableApps[index].roleSelected = true;
 
 170                                                         availableApps[index].isSelected = true;
 
 171                                                         availableApps[index].roles = selectedRoles;
 
 179             let getAvailableServices = () =>{
 
 180                 microserviceService.getServiceList().then(services => {
 
 181                         this.availableServices = [];
 
 182                         for(var i = 0; i < services.length; i++){
 
 183                                 this.availableServices.push({
 
 185                                         name: services[i].name,
 
 186                                         option: services[i].name + ": " + services[i].url
 
 189                                 if(this.widget.serviceId != null && this.widget.serviceId == services[i].id){
 
 190                                         this.widget.serviceURL = this.availableServices[i];
 
 200                 $log.info('WidgetOnboardingDetailsModalCtrl::init');
 
 201                 this.widgetsList = [];
 
 202                 this.duplicatedName = true;
 
 203                 this.allRoleSelected = false;
 
 205                 this.isSaving = false;
 
 206                 this.allUser = false;
 
 207                 this.emptyWidgetName = false;
 
 209                 if ($scope.ngDialogData && $scope.ngDialogData.widget) {
 
 210                     this.isEditMode = true;
 
 211                         this.allRoleSelected = true;
 
 212                     this.widget = _.clone($scope.ngDialogData.widget);
 
 214                     this.isEditMode = false;
 
 215                     this.widget = _.clone(newWidgetModel);
 
 218                 widgetsCatalogService.getManagedWidgets().then(res => {
 
 219                         for(var i = 0; i < res.length; i++){
 
 220                                 this.widgetsList.push(res[i].name);
 
 223                      $log.error('WidgetOnboardingDetailsModalCtrl::init error: ' + err);
 
 225                      this.isLoadingTable = false;
 
 228                  getAvailableServices();
 
 230             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
 
 231             this.conflictMessages = {};
 
 234             let resetConflict = fieldName => {
 
 235                 delete this.conflictMessages[fieldName];
 
 236                 if($scope.widgetForm[fieldName]){
 
 237                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
 
 241             this.updateSelectedRole = () => {
 
 242                 if (!this.selectedRole) {
 
 245                 this.widget.RoleId = this.selectedRole.id;
 
 246                 this.widget.RoleName = this.selectedRole.name;
 
 249             let emptyCookies = () => {
 
 250                 userProfileService.getUserProfile()
 
 252                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
 
 253                     $scope.orgUserId = profile.orgUserId;
 
 254                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
 
 255                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
 
 256                         $cookies.remove($scope.orgUserId + '_widget');
 
 261             this.updateWidgetName = () => {
 
 262                 for(var i = 0; i < this.widgetsList.length; i++){
 
 263                         if(this.widget.name.toUpperCase() == this.widgetsList[i].toUpperCase()){
 
 264                                 this.duplicatedName = false;
 
 268                 this.duplicatedName = true;
 
 271             this.saveChanges = () => {     
 
 274                         this.updateWidgetName();
 
 276                 if(this.duplicatedName == false 
 
 277                    || this.widget.name == ''
 
 278                    || this.widget.name == undefined){
 
 279                         this.emptyWidgetName = true;
 
 283                 if((this.widget.file == undefined && !this.isEditMode) ||
 
 284                 (!this.widget.allUser && this.appCounter == 0) ||
 
 285                 this.widget.name == null ||
 
 286                 (!this.widget.allUser && !this.allRoleSelected) ||
 
 287                 this.widget.saving == true)
 
 291                         this.widget.saving = true;
 
 292                 var selectedRoles = [];
 
 293                 if(!this.widget.allUser){
 
 294                         for(var i = 0; i < this.availableApps.length; i++){
 
 295                                 if(this.availableApps[i].isSelected){
 
 296                                                 for(var n = 0; n < this.availableApps[i].roles.length; n++) {
 
 297                                                         if(this.availableApps[i].roles[n].isSelected){
 
 300                                                                         appId: this.availableApps[i].id
 
 302                                                                 roleId: this.availableApps[i].roles[n].id,
 
 303                                                                 roleName: this.availableApps[i].roles[n].name,
 
 305                                                         selectedRoles.push(role);
 
 312                 var allowAllUser = 0;
 
 313                 if(this.widget.allUser)
 
 316                 var serviceId = null;
 
 317                 if(this.widget.serviceURL != null &&
 
 318                 this.widget.serviceURL != undefined){
 
 319                         serviceId = this.widget.serviceURL.id;
 
 323                 var file_loc = this.widget.name + ".zip";
 
 325                                 name: this.widget.name,
 
 326                                 desc: this.widget.desc,
 
 327                                 widgetRoles: selectedRoles,
 
 328                                 fileLocation: file_loc,
 
 329                                 allowAllUser: allowAllUser,
 
 335                         if(this.widget.file != undefined){
 
 336                                 widgetsCatalogService.updateWidgetWithFile(this.widget.file, this.widget.id, newWidget).then(res => {
 
 339                                                         confirmBoxService.showInformation("Could not save. Please retry.");
 
 340                                                         this.widget.saving = false;
 
 343                                                 confirmBoxService.showInformation(res.error);
 
 344                                                 this.widget.saving = false;
 
 347                                         $scope.closeThisDialog(true);
 
 348                                 this.widget.saving = false;
 
 352                                 widgetsCatalogService.updateWidget(this.widget.id, newWidget)
 
 354                                         $scope.closeThisDialog(true); 
 
 359                         widgetsCatalogService.createWidget(newWidget, this.widget.file).then(res => {
 
 362                                                         confirmBoxService.showInformation("Could not save. Please retry.");
 
 364                                                         confirmBoxService.showInformation(res.error);
 
 365                                                 this.widget.saving = false;
 
 368                                 $scope.closeThisDialog(true);
 
 369                                 this.widget.saving = false;
 
 374             $scope.$on('$stateChangeStart', e => {
 
 379     WidgetOnboardingDetailsModalCtrl.$inject = ['$scope', '$log', '$interval', 'applicationsService', 'adminsService', 'microserviceService', 'widgetsCatalogService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies'];
 
 380     angular.module('ecompApp').controller('WidgetOnboardingDetailsModalCtrl', WidgetOnboardingDetailsModalCtrl);