Angular upgrade - Dynamic widget,widget catalog
[portal.git] / portal-FE-common / src / app / pages / web-analytics / web-analytics-details-dialog / web-analytics-details-dialog.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 import { Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
40 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
41 import { WebAnalyticsService } from 'src/app/shared/services';
42
43 @Component({
44   selector: 'app-web-analytics-details-dialog',
45   templateUrl: './web-analytics-details-dialog.component.html',
46   styleUrls: ['./web-analytics-details-dialog.component.scss']
47 })
48 export class WebAnalyticsDetailsDialogComponent implements OnInit {
49
50   result: any;
51   isEditMode: boolean = false;
52   isAllApplications: boolean = true;
53   emptyImg = null;
54   allApplications: any = [];
55   allApps: any = [];
56             
57   newAppModel = {
58     'appId': null,
59     'appName':null,
60     'reportName': null,
61     'reportSrc': null,
62     'resourceId': null
63   };
64
65   newApp={
66     'appId': '',
67     'appName':''                        
68   }
69
70   constructor(public activeModal: NgbActiveModal, public ngbModal: NgbModal, 
71     public webAnalyticsService : WebAnalyticsService) { }
72
73   @Input() userTableAppReport: any;
74   @Output() passEntry: EventEmitter<any> = new EventEmitter();
75
76   ngOnInit() {
77     this.isAllApplications = true;
78     if(this.userTableAppReport.appName){
79       this.isEditMode = true;
80     }else{
81       this.isEditMode = false;
82     }
83     //console.log("IsEditMode in Web analytycs Dialog :: ",this.isEditMode)
84     this.getAllApplications();
85   }
86
87   getAllApplications(){
88     this.isAllApplications = true;
89     this.webAnalyticsService.getAllApplications()
90     .subscribe(_data => {
91         this.result = _data;
92         if (this.result == null || this.result == 'undefined') {
93              //console.log('WebAnalyticsService::getAllApplications Failed: Result or result.data is null');
94         }else {
95           for (let i = 0; i < this.result.length; i++) {
96             var application = {
97                 appId : this.result[i].id,
98                 appName: this.result[i].name,
99                 enabled : this.result[i].enabled,
100                 restrictedApp :this.result[i].restrictedApp,
101             };
102             this.allApps.push(application);  
103          }
104          for (let i = 0; i < this.allApps.length; i++) {
105           if((this.allApps[i].enabled == true && this.allApps[i].restrictedApp == false) || (this.allApps[i].appId == 1) ) {
106             var validApplication = {
107               appId : this.allApps[i].appId,
108               appName: this.allApps[i].appName,
109             };
110             this.allApplications.push(validApplication);
111           }
112         }
113       }
114     }, error =>{
115       this.isAllApplications = false;
116       console.log(error);
117     });
118   }
119
120   saveChanges(){
121     //console.log("Save Changes Called.");
122     let selectedApplication = this.userTableAppReport.appName;
123     this.newAppModel.appId = selectedApplication.appId;
124     this.newAppModel.appName = selectedApplication.appName;
125     this.newAppModel.reportName = this.userTableAppReport.reportName;
126     this.newAppModel.reportSrc =  this.userTableAppReport.reportSrc;
127     this.newAppModel.resourceId =  this.userTableAppReport.resourceId;
128
129     if (this.isEditMode) {
130       this.newAppModel.appId = this.userTableAppReport.appId;
131       //console.log("Update Analytics..newAppModel :: ",this.userTableAppReport);
132       this.webAnalyticsService.updateWebAnalyticsReport(this.newAppModel)
133       .subscribe(_data => {
134           this.result = _data;
135           //console.log("Update Analytics Response:: ",this.result);
136           this.passEntry.emit(this.result);
137           this.ngbModal.dismissAll();
138       }, error =>{
139         this.isAllApplications = false;
140         console.log(error);
141       });
142     }else{
143       //console.log("Save Analytics.newAppModel :: ",this.userTableAppReport);
144       this.webAnalyticsService.save(this.newAppModel)
145       .subscribe(_data => {
146           this.result = _data;
147           //console.log("Save Analytics Response:: ",this.result);
148           this.passEntry.emit(this.result);
149           this.ngbModal.dismissAll();
150       }, error =>{
151         this.isAllApplications = false;
152         console.log(error);
153       });
154     }
155   }
156 }