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 MicroserviceAddDetailsCtrl {
 
  23         constructor($scope, $log, $interval, widgetsCatalogService, applicationsService, adminsService, microserviceService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies,items) {
 
  25                 $scope.ngDialogData=items;
 
  26             let getAvailableApps = () => {       
 
  27                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
 
  28                         this.availableApps=[];
 
  33                         for(var i = 0; i < apps.length; i++) {
 
  34                         this.availableApps.push({
 
  38                         if(this.isEditMode == true && this.service.appId == apps[i].id){
 
  39                                 this.service.application = this.availableApps[i];
 
  47             let getAvailableWidgets = () => {       
 
  48                 microserviceService.getWidgetListByService(this.service.id).then(widgets =>{
 
  49                         this.availableWidgets = [];
 
  50                         for(var i = 0; i < widgets.length; i++){
 
  51                                 this.availableWidgets.push({
 
  61             let getAvailableSecurityTypes = () => {   
 
  62                 this.availableSecurityTypes = [];
 
  63                 this.availableSecurityTypes.push({
 
  65                         name: 'No Authentication'
 
  67                 this.availableSecurityTypes.push({
 
  69                         name: 'Basic Authentication'
 
  71                 this.availableSecurityTypes.push({
 
  73                         name: 'Cookie based Authentication'
 
  78                 $log.info('MicroserviceAddDetailsCtrl::init');
 
  80                 this.availableApps=[];
 
  81                 this.service.parameterList = [];
 
  82                 this.service.active = true;
 
  83                 this.emptyServiceName = false;
 
  84                 this.emptyServiceDesc = false;
 
  85                 this.emptyServiceURL = false;
 
  86                 this.emptyServiceApp = false;
 
  87                 this.dupliateName = false;
 
  88                 this.serviceList = $scope.ngDialogData.list;
 
  90                 if ($scope.ngDialogData && $scope.ngDialogData.service) {
 
  92                     this.isEditMode = true;
 
  93                     this.service = _.clone($scope.ngDialogData.service);
 
  95                     this.originalName  = this.service.name;
 
  96                     if(this.service.active == 'Y')
 
  97                         this.service.active = true;
 
  99                         this.service.active = false;
 
 101                     this.isEditMode = false;
 
 104                 getAvailableSecurityTypes();
 
 105                 //getAvailableWidgets();
 
 108                                  * 0: Basic Authentication
 
 110                                  * TODO: change the structure
 
 112                 if(this.service.securityType == "No Authentication"){
 
 113                         this.service.security = this.availableSecurityTypes[0];
 
 114                 }else if(this.service.securityType == "Basic Authentication"){
 
 115                         this.service.security = this.availableSecurityTypes[1];
 
 116                 }else if(this.service.securityType == "Cookie based Authentication"){
 
 117                         this.service.security = this.availableSecurityTypes[2];
 
 121             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
 
 122             this.conflictMessages = {};
 
 125             let resetConflict = fieldName => {
 
 126                 delete this.conflictMessages[fieldName];
 
 127                 if($scope.widgetForm[fieldName]){
 
 128                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
 
 132             this.addParameter = () => {
 
 133                 this.service.parameterList.push({}); 
 
 136             this.closeThisDialog = () => {
 
 137                 $scope.closeThisDialog(true);
 
 140             this.removeParamItem = (parameter) => {
 
 141                 microserviceService.getUserParameterById(parameter.id).then((res) => {
 
 143                                 var message = res.length + " users have their own widget parameters. Are you sure you want to delete?";
 
 144                                 confirmBoxService.editItem(message).then(isConfirmed => {
 
 146                                                 microserviceService.deleteUserParameterById(parameter.id).then((res) => {
 
 147                                                         for(var i = 0; i < this.service.parameterList.length; i++){
 
 148                                                 if(this.service.parameterList[i].para_key == parameter.para_key
 
 149                                                 && this.service.parameterList[i].para_value == parameter.para_value){
 
 150                                                         this.service.parameterList.splice(i, 1);
 
 159                                 for(var i = 0; i < this.service.parameterList.length; i++){
 
 160                                 if(this.service.parameterList[i].para_key == parameter.para_key
 
 161                                 && this.service.parameterList[i].para_value == parameter.para_value){
 
 162                                         this.service.parameterList.splice(i, 1);
 
 172             this.updateServiceName = () => {
 
 173                 this.dupliateName = false;
 
 174                 for(var i = 0; i < this.serviceList.length; i++){
 
 175                         if(this.serviceList[i].name == this.service.name){
 
 176                                 if(this.isEditMode && this.service.name == this.originalName)
 
 178                                 this.dupliateName = true;
 
 184             this.updateDesc = () => {
 
 185                 this.emptyServiceDesc = false;
 
 188             this.updateURL = () => {
 
 189                 this.emptyServiceURL  = false;
 
 192             this.updateApp = () => {
 
 193                 this.emptyServiceApp = false;
 
 196           //This is a fix for dropdown selection, due to b2b dropdown only update value field
 
 197                 $scope.$watch('microserviceAddDetails.service.application.name', (newVal, oldVal) => {
 
 198                         for(var i=0;i<$scope.microserviceAddDetails.availableApps.length;i++){                  
 
 199                                 if($scope.microserviceAddDetails.availableApps[i].name==newVal){
 
 200                                         $scope.microserviceAddDetails.service.application=angular.copy($scope.microserviceAddDetails.availableApps[i]);
 
 204                 $scope.$watch('microserviceAddDetails.service.security.name', (newVal, oldVal) => {
 
 205                         for(var i=0;i<$scope.microserviceAddDetails.availableSecurityTypes.length;i++){                         
 
 206                                 if($scope.microserviceAddDetails.availableSecurityTypes[i].name==newVal){
 
 207                                         $scope.microserviceAddDetails.service.security=angular.copy($scope.microserviceAddDetails.availableSecurityTypes[i]);
 
 212             let emptyCookies = () => {
 
 213                 userProfileService.getUserProfile()
 
 215                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
 
 216                     $scope.orgUserId = profile.orgUserId;
 
 217                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
 
 218                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
 
 219                         $cookies.remove($scope.orgUserId + '_widget');
 
 224             this.testServiceURL = () =>{
 
 225                 widgetsCatalogService.getServiceJSON(this.service.id).then(res => {
 
 226                         document.getElementById("microservice-details-input-json").innerHTML = (JSON.stringify(res));
 
 230             this.saveChanges = () => {     
 
 231                 /* TODO: add form validation */
 
 234                 this.updateServiceName();
 
 236                 if(this.service.name == ''
 
 237                 || this.service.name == undefined){
 
 238                         this.emptyServiceName = true;
 
 242                 if(this.dupliateName == true){
 
 247                 if(this.service.desc == ''
 
 248                 || this.service.desc == undefined){
 
 249                         this.emptyServiceDesc = true;
 
 253                 if(this.service.url == ''
 
 254                 || this.service.url == undefined){
 
 255                         this.emptyServiceURL = true;
 
 259                 if(this.service.application == undefined
 
 260                 || this.service.application == null){
 
 261                         this.emptyServiceApp = true;
 
 269                                  * Check the parameter list, delete those parameters that don't
 
 272                 for(var i = 0; i < this.service.parameterList.length; i++){
 
 273                         if(this.service.parameterList[i].para_key == undefined
 
 274                         || this.service.parameterList[i].para_key == null
 
 275                         || this.service.parameterList[i].para_key == ""){
 
 276                                 this.service.parameterList.splice(i, 1);
 
 284                 if(this.service.security == undefined ||
 
 285                 this.service.security == null)
 
 286                         securityType = "No Authentication";
 
 288                         securityType = this.service.security.name;
 
 289                         username = this.service.username;
 
 290                         password = this.service.password;
 
 294                 if(this.service.active == true)
 
 298                                 name: this.service.name,
 
 299                                 desc: this.service.desc,
 
 300                                 appId: this.service.application.id,
 
 301                                 url: this.service.url,
 
 302                                 securityType: securityType,
 
 306                                 parameterList: this.service.parameterList
 
 310                         // console.log(this.service.parameterList);
 
 311                         var message = "Are you sure you want to change '" + this.service.name + "'?"
 
 312                         confirmBoxService.editItem(message).then(isConfirmed => {
 
 314                                         microserviceService.updateService(this.service.id, newService).then(() => {
 
 315                                                 // TODO: result validation check
 
 316                                                 //this.closeThisDialog(); 
 
 317                                                 $scope.$dismiss('cancel');
 
 322                         microserviceService.createService(newService).then(() => {
 
 323                                 // TODO: result validation check
 
 324                                 //$scope.closeThisDialog(true);
 
 325                                 $scope.$dismiss('cancel');
 
 331             $scope.$on('$stateChangeStart', e => {
 
 336     MicroserviceAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'widgetsCatalogService', 'applicationsService', 'adminsService', 'microserviceService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies','items'];
 
 337     angular.module('ecompApp').controller('MicroserviceAddDetailsCtrl', MicroserviceAddDetailsCtrl);