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 AppDetailsModalCtrl {
 
  41         constructor($scope, $log, applicationsService, errorMessageByCode,
 
  42                     ECOMP_URL_REGEX,userProfileService, $cookies, confirmBoxService,items) {
 
  43 //            let emptyImg = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
 
  44             // empty image should really be empty, or it causes problems for the back end
 
  46             this.emptyImgForPreview = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
 
  47             $scope.ngDialogData=items;
 
  60                 'thumbnail': emptyImg,
 
  62                 'restrictedApp': false,
 
  64                 'isCentralAuth': false                
 
  68                 $log.info('AppDetailsModalCtrl::init');
 
  69                 this.isSaving = false;
 
  70                 if($scope.ngDialogData && $scope.ngDialogData.app){
 
  71                     $log.debug('AppDetailsModalCtrl:init:: Edit app mode for', $scope.ngDialogData.app);
 
  72                     this.isEditMode = true;
 
  73                     this.app = _.clone($scope.ngDialogData.app);
 
  75                     $log.debug('AppDetailsModalCtrl:init:: New app mode');
 
  76                     this.isEditMode = false;
 
  77                     this.app = _.clone(newAppModel);
 
  79                 this.originalImage = null
 
  82             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
 
  85             this.removeImage = () => {
 
  86                 $log.debug('AppDetailsModalCtrl:removeImage:: entering removeImage');
 
  88                 confirmBoxService.confirm("Are you sure you want to remove the image?").then(isConfirmed => {
 
  90                         this.imageApi.clearFile();
 
  91                         this.app.thumbnail = emptyImg;
 
  92                         this.originalImage = null;
 
  93                         this.app.imageUrl = null;
 
  94                         this.app.imageLink = null;
 
  97                     $log.error('AppDetailsModalCtrl:removeImage error:: ',err);
 
 101             //This part handles conflict errors (409)
 
 102             this.conflictMessages = {};
 
 104             let handleConflictErrors = err => {
 
 108                 if(!err.data.length){ //support objects
 
 109                     err.data = [err.data]
 
 111                 _.forEach(err.data, item => {
 
 112                     _.forEach(item.fields, field => {
 
 113                         //set conflict message
 
 114                         this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
 
 115                         //set field as invalid
 
 116                         $scope.appForm[field.name].$setValidity('conflict', false);
 
 117                         //set watch once to clear error after user correction
 
 118                         watchOnce[field.name]();
 
 121                 this.scrollApi.scrollTop();
 
 124             let resetConflict = fieldName => {
 
 125                 delete this.conflictMessages[fieldName];
 
 126                 if($scope.appForm[fieldName]){
 
 127                     $scope.appForm[fieldName].$setValidity('conflict', true);
 
 132             let emptyCookies = () => {
 
 133                 $log.debug('AppDetailsModalCtrl:emptyCookies:: entering emptyCookies');
 
 134                 userProfileService.getUserProfile()
 
 136                         // $log.info(profile);
 
 137                         $scope.orgUserId = profile.orgUserId;
 
 138                         $log.debug('AppDetailsModalCtrl:emptyCookies for the following orgUserId: ' + profile.orgUserId);
 
 139                         if ($cookies.getObject($scope.orgUserId + '_apps') != undefined && $cookies.getObject($scope.orgUserId + '_apps') != null) {
 
 140                             $cookies.remove($scope.orgUserId + '_apps');
 
 141                             $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.orgUserId + '_apps');
 
 143                         if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
 
 144                             $cookies.remove($scope.orgUserId + '_widget');
 
 145                             $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.orgUserId + '_widget');
 
 148                         $log.error('AppDetailsModalCtrl:emptyCookies error:: '+ JSON.stringify(err));
 
 155                     let unregisterName = $scope.$watch('appDetails.app.name', (newVal, oldVal) => {
 
 156                         // $log.debug('newVal, oldVal = ' + newVal.toLowerCase() + " | " + oldVal.toLowerCase())
 
 157                         if(newVal.toLowerCase() !== oldVal.toLowerCase()){
 
 158                             resetConflict('name');
 
 164                     let unregisterUrl = $scope.$watch('appDetails.app.url', (newVal, oldVal) => {
 
 165                         if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
 
 166                             resetConflict('url');
 
 172             //***************************
 
 174             this.saveChanges = () => {
 
 176                  if(((angular.isUndefined(this.app.name) || !this.app.name)&&(angular.isUndefined(this.app.url) || !this.app.url)
 
 177                                  &&(angular.isUndefined(this.app.username) || !this.app.username)&&(angular.isUndefined(this.app.appPassword) || !this.app.appPassword))) {
 
 178                          confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
 
 180                  }else if(!((angular.isUndefined(this.app.name) || !!this.app.name)&&(angular.isUndefined(this.app.url) || !!this.app.url))){
 
 181                      confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
 
 184                 this.isSaving = true;
 
 185                 // For a restricted app, null out all irrelevant fields
 
 186                 if (this.app.restrictedApp) {
 
 187                     this.app.restUrl = null;
 
 188                     this.app.isOpen = true;
 
 189                     this.app.username = null;
 
 190                     this.app.appPassword = null;
 
 191                     this.app.uebTopicName = null;
 
 192                     this.app.uebKey = null;
 
 193                     this.app.uebSecret = null;
 
 196                     if (this.app.nameSpace=="") {this.app.nameSpace = null;}
 
 197                     applicationsService.updateOnboardingApp(this.app)
 
 199                             $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: App update succeeded!');
 
 200                           //  $scope.closeThisDialog(true);
 
 201                             $scope.$dismiss('cancel');
 
 204                             switch (err.status) {
 
 205                                 case '409':         // Conflict
 
 206                                     handleConflictErrors(err);
 
 208                                 case '500':         // Internal Server Error
 
 209                                     confirmBoxService.showInformation('There was a problem updating the application changes. ' +
 
 210                                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
 
 212                                 case '403':         // Forbidden... possible webjunction error to try again
 
 213                                     confirmBoxService.showInformation('There was a problem updating the application changes. ' +
 
 214                                         'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
 
 217                                     confirmBoxService.showInformation('There was a problem updating the application changes. ' +
 
 218                                         'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
 
 220                             $log.error('applicationsService:updateOnboardingApp error:: '+ JSON.stringify(err));
 
 222                             this.isSaving = false;
 
 224                             var objOffsetVersion = objAgent.indexOf("MSIE");
 
 225                             if (objOffsetVersion != -1) {
 
 226                                 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
 
 227                                 $window.location.reload();            // for bug in IE 11
 
 232                     applicationsService.addOnboardingApp(this.app)
 
 234                             $log.debug('App creation succeeded!');
 
 235                             //$scope.closeThisDialog(true);
 
 236                             $scope.$dismiss('cancel');
 
 239                             switch (err.status) {
 
 240                                 case '409':         // Conflict
 
 241                                     handleConflictErrors(err);
 
 243                                 case '500':         // Internal Server Error
 
 244                                     confirmBoxService.showInformation('There was a problem adding the application information. ' +
 
 245                                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
 
 248                                     confirmBoxService.showInformation('There was a problem updating the application changes. ' +
 
 249                                         'Please try again. If the problem persists, then try again later. Error: ' +
 
 250                                         err.status).then(isConfirmed => {});
 
 252                             $log.error('applicationsService:addOnboardingApp error:: '+ JSON.stringify(err));
 
 254                             this.isSaving = false;
 
 256                             var objOffsetVersion = objAgent.indexOf("MSIE");
 
 257                             if (objOffsetVersion != -1) {
 
 258                                 $log.debug('applicationsService:addOnboardingApp:: Browser is IE, forcing Refresh');
 
 259                                 $window.location.reload();            // for bug in IE 11
 
 270             $scope.$watch('appDetails.originalImage', (newVal, oldVal) => {
 
 271               if((!newVal || !newVal.resized) && !this.app.imageUrl){
 
 273                       $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is null');
 
 275                       $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is not resized and no imageUrl');
 
 277                   this.app.imageUrl = null;
 
 278                   this.app.thumbnail = emptyImg;
 
 282                 if(!(_.isEqual(newVal, oldVal))){
 
 283                     $log.debug('applicationsService:$scope.$watch:: thumbnail updated!');
 
 284                     this.app.imageUrl = null;
 
 285                     this.app.imageLink = null;
 
 286                     this.app.thumbnail = newVal.resized.dataURL;
 
 290             $scope.$on('$stateChangeStart', e => {
 
 291                 //Disable navigation when modal is opened
 
 296     AppDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'errorMessageByCode',
 
 297         'ECOMP_URL_REGEX','userProfileService','$cookies', 'confirmBoxService','items'];
 
 298     angular.module('ecompApp').controller('AppDetailsModalCtrl', AppDetailsModalCtrl);