2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file NS PackagesAction Component
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';
39 * @Component takes NsPackagesActionComponent.html as template url
42 selector: 'app-ns-packages-action',
43 templateUrl: './NsPackagesActionComponent.html',
44 styleUrls: ['./NsPackagesActionComponent.scss']
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 */
52 /** To inject services @public */
53 public injector: Injector;
55 /** Check the loading results for loader status @public */
56 public isLoadingDownloadResult: boolean = false;
58 /** Give the message for the loading @public */
59 public message: string = 'PLEASEWAIT';
61 /** Instance of the rest service @private */
62 private restService: RestService;
64 /** Holds teh instance of AuthService class of type AuthService @private */
65 private router: Router;
67 /** Instance of the modal service @private */
68 private modalService: NgbModal;
70 /** Variables holds NS ID @private */
71 private nsdID: string;
73 /** Variables holds NS name @private */
74 private nsdName: string;
76 /** Controls the header form @private */
77 private headers: HttpHeaders;
79 /** Contains all methods related to shared @private */
80 private sharedService: SharedService;
82 /** Notifier service to popup notification @private */
83 private notifierService: NotifierService;
85 /** Contains tranlsate instance @private */
86 private translateService: TranslateService;
88 /** Detect changes for the User Input */
89 private cd: ChangeDetectorRef;
91 /** Set timeout @private */
92 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
93 private timeOut: number = 1000;
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);
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'
112 this.nsdID = this.value.identifier;
113 this.nsdName = this.value.name;
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' });
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) => {
128 this.sharedService.callData();
130 }).catch((): void => {
131 // Catch Navigation Error
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
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' };
148 /** Download NS Package @public */
149 public downloadNSPackage(): void {
150 this.isLoadingDownloadResult = true;
151 const httpOptions: GETAPIURLHEADER = {
152 headers: this.headers,
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');
175 /** Compose NS Packages @public */
176 public composeNSPackages(): void {
177 this.router.navigate(['/packages/ns/compose/', this.nsdID]).catch(() => {
178 // Catch Navigation Error
182 /** Change the detaction @public */
183 public changeDetactionforDownload(): void {
185 this.cd.detectChanges();
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) => {
196 this.sharedService.callData();
198 }).catch((): void => {
199 // Catch Navigation Error