Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / utilities / ns-packages-action / NsPackagesActionComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file NS PackagesAction Component
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { ChangeDetectorRef, Component, Injector } from '@angular/core';
23 import { Router } from '@angular/router';
24 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { ClonePackageComponent } from 'ClonePackage';
28 import { ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA } from 'CommonModel';
29 import { DeleteComponent } from 'DeleteComponent';
30 import { environment } from 'environment';
31 import { InstantiateNsComponent } from 'InstantiateNs';
32 import { NSData } from 'NSDModel';
33 import { RestService } from 'RestService';
34 import { SharedService } from 'SharedService';
35 import { ShowContentComponent } from 'ShowContent';
36
37 /**
38  * Creating component
39  * @Component takes NsPackagesActionComponent.html as template url
40  */
41 @Component({
42   selector: 'app-ns-packages-action',
43   templateUrl: './NsPackagesActionComponent.html',
44   styleUrls: ['./NsPackagesActionComponent.scss']
45 })
46
47 /** Exporting a class @exports NsPackagesActionComponent */
48 export class NsPackagesActionComponent {
49   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
50   public value: NSData;
51
52   /** To inject services @public */
53   public injector: Injector;
54
55   /** Check the loading results for loader status @public */
56   public isLoadingDownloadResult: boolean = false;
57
58   /** Give the message for the loading @public */
59   public message: string = 'PLEASEWAIT';
60
61   /** Instance of the rest service @private */
62   private restService: RestService;
63
64   /** Holds teh instance of AuthService class of type AuthService @private */
65   private router: Router;
66
67   /** Instance of the modal service @private */
68   private modalService: NgbModal;
69
70   /** Variables holds NS ID @private */
71   private nsdID: string;
72
73   /** Variables holds NS name @private */
74   private nsdName: string;
75
76   /** Controls the header form @private */
77   private headers: HttpHeaders;
78
79   /** Contains all methods related to shared @private */
80   private sharedService: SharedService;
81
82   /** Notifier service to popup notification @private */
83   private notifierService: NotifierService;
84
85   /** Contains tranlsate instance @private */
86   private translateService: TranslateService;
87
88   /** Detect changes for the User Input */
89   private cd: ChangeDetectorRef;
90
91   /** Set timeout @private */
92   // eslint-disable-next-line @typescript-eslint/no-magic-numbers
93   private timeOut: number = 1000;
94
95   constructor(injector: Injector) {
96     this.injector = injector;
97     this.restService = this.injector.get(RestService);
98     this.sharedService = this.injector.get(SharedService);
99     this.modalService = this.injector.get(NgbModal);
100     this.router = this.injector.get(Router);
101     this.notifierService = this.injector.get(NotifierService);
102     this.translateService = this.injector.get(TranslateService);
103     this.cd = this.injector.get(ChangeDetectorRef);
104   }
105
106   /** Lifecyle Hooks the trigger before component is instantiate @public */
107   public ngOnInit(): void {
108     this.headers = new HttpHeaders({
109       Accept: 'application/zip, application/json',
110       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
111     });
112     this.nsdID = this.value.identifier;
113     this.nsdName = this.value.name;
114   }
115
116   /** Instantiate NS using modalservice @public */
117   public instantiateNS(): void {
118     // eslint-disable-next-line security/detect-non-literal-fs-filename
119     this.modalService.open(InstantiateNsComponent, { backdrop: 'static' });
120   }
121
122   /** Delete NS Package @public */
123   public deleteNSPackage(): void {
124     // eslint-disable-next-line security/detect-non-literal-fs-filename
125     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
126     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
127       if (result) {
128         this.sharedService.callData();
129       }
130     }).catch((): void => {
131       // Catch Navigation Error
132   });
133   }
134
135   /** Set instance for NSD Edit @public */
136   public nsdEdit(): void {
137     this.router.navigate(['/packages/ns/edit/', this.nsdID]).catch(() => {
138       // Catch Navigation Error
139     });
140   }
141
142   /** list out all the file content of a descriptors @public */
143   public showContent(): void {
144     // eslint-disable-next-line security/detect-non-literal-fs-filename
145     this.modalService.open(ShowContentComponent, { backdrop: 'static' }).componentInstance.params = { id: this.nsdID, page: 'nsd' };
146   }
147
148   /** Download NS Package @public */
149   public downloadNSPackage(): void {
150     this.isLoadingDownloadResult = true;
151     const httpOptions: GETAPIURLHEADER = {
152       headers: this.headers,
153       responseType: 'blob'
154     };
155     this.restService.getResource(environment.NSDESCRIPTORS_URL + '/' + this.nsdID + '/nsd_content', httpOptions)
156       .subscribe((response: Blob) => {
157         const binaryData: Blob[] = [];
158         binaryData.push(response);
159         this.sharedService.downloadFiles(this.nsdName, binaryData, response.type);
160         this.isLoadingDownloadResult = false;
161         this.changeDetactionforDownload();
162       }, (error: ERRORDATA) => {
163         this.isLoadingDownloadResult = false;
164         this.notifierService.notify('error', this.translateService.instant('ERROR'));
165         this.changeDetactionforDownload();
166         if (typeof error.error === 'object') {
167           error.error.text().then((data: string): void => {
168             error.error = JSON.parse(data);
169             this.restService.handleError(error, 'getBlob');
170           });
171         }
172       });
173   }
174
175   /** Compose NS Packages @public */
176   public composeNSPackages(): void {
177     this.router.navigate(['/packages/ns/compose/', this.nsdID]).catch(() => {
178       // Catch Navigation Error
179     });
180   }
181
182   /** Change the detaction @public */
183    public changeDetactionforDownload(): void {
184     setTimeout(() => {
185       this.cd.detectChanges();
186     }, this.timeOut);
187   }
188
189   /** Clone NS Packages @public */
190   public cloneNSPackage(): void {
191     // eslint-disable-next-line security/detect-non-literal-fs-filename
192     const cloneModal: NgbModalRef = this.modalService.open(ClonePackageComponent, { backdrop: 'static' });
193     cloneModal.componentInstance.params = { id: this.nsdID, page: 'nsd', name: this.nsdName };
194     cloneModal.result.then((result: MODALCLOSERESPONSEDATA) => {
195       if (result) {
196         this.sharedService.callData();
197       }
198     }).catch((): void => {
199       // Catch Navigation Error
200   });
201   }
202 }