nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / applications / application-details-dialog / application-details.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 'use strict';
21 (function () {
22     class AppDetailsModalCtrl {
23         constructor($scope, $log, applicationsService, errorMessageByCode,
24                     ECOMP_URL_REGEX,userProfileService, $cookies, confirmBoxService) {
25             let emptyImg = null;
26             this.emptyImgForPreview = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
27
28             let newAppModel = {
29                 'id': null,
30                 'name': null,
31                 'imageUrl': null,
32                 'description': null,
33                 'notes': null,
34                 'url': null,
35                 'alternateUrl': null,
36                 'restUrl': null,
37                 'isOpen': false,
38                 'username': null,
39                 'appPassword': null,
40                 'thumbnail': emptyImg,
41                 'isEnabled': true,
42                 'restrictedApp': false
43             };
44
45             let init = () => {
46                 $log.info('AppDetailsModalCtrl::init');
47                 this.isSaving = false;
48                 if($scope.ngDialogData && $scope.ngDialogData.app){
49                     $log.debug('AppDetailsModalCtrl:init:: Edit app mode for', $scope.ngDialogData.app);
50                     this.isEditMode = true;
51                     this.app = _.clone($scope.ngDialogData.app);
52                 }else{
53                     $log.debug('AppDetailsModalCtrl:init:: New app mode');
54                     this.isEditMode = false;
55                     this.app = _.clone(newAppModel);
56                 }
57                 this.originalImage = null
58             };
59
60             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
61
62             this.imageApi = {};
63             this.removeImage = () => {
64                 $log.debug('AppDetailsModalCtrl:removeImage:: entering removeImage');
65
66                 confirmBoxService.confirm("Are you sure you want to remove the image?").then(isConfirmed => {
67                     if(isConfirmed){
68                         this.imageApi.clearFile();
69                         this.app.thumbnail = emptyImg;
70                         this.originalImage = null;
71                         this.app.imageUrl = null;
72                     }
73                 }).catch(err => {
74                     $log.error('AppDetailsModalCtrl:removeImage error:: ',err);
75                 });
76             };
77
78             this.conflictMessages = {};
79             this.scrollApi = {};
80             let handleConflictErrors = err => {
81                 if(!err.data){
82                     return;
83                 }
84                 if(!err.data.length){
85                     err.data = [err.data]
86                 }
87                 _.forEach(err.data, item => {
88                     _.forEach(item.fields, field => {
89                         this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
90                         $scope.appForm[field.name].$setValidity('conflict', false);
91                         watchOnce[field.name]();
92                     });
93                 });
94                 this.scrollApi.scrollTop();
95             };
96
97             let resetConflict = fieldName => {
98                 delete this.conflictMessages[fieldName];
99                 if($scope.appForm[fieldName]){
100                     $scope.appForm[fieldName].$setValidity('conflict', true);
101                 }
102             };
103
104
105             let emptyCookies = () => {
106                 $log.debug('AppDetailsModalCtrl:emptyCookies:: entering emptyCookies');
107                 userProfileService.getUserProfile()
108                     .then(profile=> {
109                         $scope.userId = profile.orgUserId;
110                         $log.debug('AppDetailsModalCtrl:emptyCookies for the following userId: ' + profile.orgUserId);
111                         if ($cookies.getObject($scope.userId + '_apps') != undefined && $cookies.getObject($scope.userId + '_apps') != null) {
112                             $cookies.remove($scope.userId + '_apps');
113                             $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.userId + '_apps');
114                         }
115                         if ($cookies.getObject($scope.userId + '_widget') != undefined && $cookies.getObject($scope.userId + '_widget') != null) {
116                             $cookies.remove($scope.userId + '_widget');
117                             $log.debug('AppDetailsModalCtrl:emptyCookies removed: ' + $scope.userId + '_widget');
118                         }
119                     }).catch(err => {
120                         $log.error('AppDetailsModalCtrl:emptyCookies error:: '+ JSON.stringify(err));
121                     });
122             };
123
124
125             let watchOnce = {
126                 name: () => {
127                     let unregisterName = $scope.$watch('appDetails.app.name', (newVal, oldVal) => {
128                         if(newVal.toLowerCase() !== oldVal.toLowerCase()){
129                             resetConflict('name');
130                             unregisterName();
131                         }
132                     });
133                 },
134                 url: () => {
135                     let unregisterUrl = $scope.$watch('appDetails.app.url', (newVal, oldVal) => {
136                         if(newVal.toLowerCase() !== oldVal.toLowerCase()) {
137                             resetConflict('url');
138                             unregisterUrl();
139                         }
140                     });
141                 }
142             };
143
144             this.saveChanges = () => {
145                 if($scope.appForm.$invalid){
146                     return;
147                 }
148                 this.isSaving = true;
149
150                 if (this.app.restrictedApp) {
151                     this.app.restUrl = null;
152                     this.app.isOpen = true;
153                     this.app.username = null;
154                     this.app.appPassword = null;
155                     this.app.uebTopicName = null;
156                     this.app.uebKey = null;
157                     this.app.uebSecret = null;
158                 }
159                 if(this.isEditMode){
160                     applicationsService.updateOnboardingApp(this.app)
161                         .then(() => {
162                             $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: App update succeeded!');
163                             $scope.closeThisDialog(true);
164                             emptyCookies();
165                         }).catch(err => {
166                             if(err.status === 409){
167                                 handleConflictErrors(err);
168                             }
169                             if(err.status === 500){
170                                 confirmBoxService.showInformation('There was a problem updating the application changes. ' +
171                                     'Please try again later.').then(isConfirmed => {});
172                             }
173                             if(err.status === 403){
174                                 confirmBoxService.showInformation('There was a problem updating the application changes. ' +
175                                     'Please try again. If the problem persists, then try again later.').then(isConfirmed => {});
176                             }
177                             $log.error('applicationsService:updateOnboardingApp error status:: '+ err.status);
178                             $log.error('applicationsService:updateOnboardingApp error:: '+ JSON.stringify(err));
179                         }).finally(()=>{
180                             this.isSaving = false;
181                             var objOffsetVersion = objAgent.indexOf("MSIE");
182                             if (objOffsetVersion != -1) {
183                                 $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
184                                 $window.location.reload();
185                             }
186                         });
187                 }else{
188                     applicationsService.addOnboardingApp(this.app)
189                         .then(() => {
190                             $log.debug('App creation succeeded!');
191                             $scope.closeThisDialog(true);
192                             emptyCookies();
193                         }).catch(err => {
194                             if(err.status === 409){
195                                 handleConflictErrors(err);
196                             }
197                             if(err.status === 500){
198                                 confirmBoxService.showInformation('There was a problem adding the application information. ' +
199                                     'Please try again later.').then(isConfirmed => {});
200                             }
201                             $log.error('applicationsService:addOnboardingApp error status:: '+ err.status);
202                             $log.error('applicationsService:addOnboardingApp error:: '+ JSON.stringify(err));
203                         }).finally(()=>{
204                             this.isSaving = false;
205                             var objOffsetVersion = objAgent.indexOf("MSIE");
206                             if (objOffsetVersion != -1) {
207                                 $log.debug('applicationsService:addOnboardingApp:: Browser is IE, forcing Refresh');
208                                 $window.location.reload();
209                             }
210                         });
211                 }
212             };
213
214
215             init();
216
217             $scope.$watch('appDetails.originalImage', (newVal, oldVal) => {
218               if((!newVal || !newVal.resized) && !this.app.imageUrl){
219                   if (!newVal) {
220                       $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is null');
221                   } else {
222                       $log.debug('applicationsService:$scope.$watch:: originalImage: newVal is not resized and no imageUrl');
223                   }
224                   this.app.imageUrl = null;
225                   this.app.thumbnail = emptyImg;
226                   return;
227               }
228
229                 if(!(_.isEqual(newVal, oldVal))){
230                     $log.debug('applicationsService:$scope.$watch:: thumbnail updated!');
231                     this.app.imageUrl = null;
232                     this.app.thumbnail = newVal.resized.dataURL;
233                 }
234             });
235
236             $scope.$on('$stateChangeStart', e => {
237                 e.preventDefault();
238             });
239         }
240     }
241     AppDetailsModalCtrl.$inject = ['$scope', '$log', 'applicationsService', 'errorMessageByCode',
242         'ECOMP_URL_REGEX','userProfileService','$cookies', 'confirmBoxService'];
243     angular.module('ecompApp').controller('AppDetailsModalCtrl', AppDetailsModalCtrl);
244 })();