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-Packages component.
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
23 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, ERRORDATA } from 'CommonModel';
27 import { ComposePackages } from 'ComposePackages';
28 import { DataService } from 'DataService';
29 import { environment } from 'environment';
30 import { LocalDataSource } from 'ng2-smart-table';
31 import { NSData, NSDDetails } from 'NSDModel';
32 import { NsPackagesActionComponent } from 'NsPackagesAction';
33 import { RestService } from 'RestService';
34 import { Subscription } from 'rxjs';
35 import { SharedService } from 'SharedService';
39 * @Component takes NSPackagesComponent.html as template url
42 selector: 'app-ns-packages',
43 templateUrl: './NSPackagesComponent.html',
44 styleUrls: ['./NSPackagesComponent.scss']
47 /** Exporting a class @exports NSPackagesComponent */
48 export class NSPackagesComponent implements OnInit {
49 /** To inject services @public */
50 public injector: Injector;
52 /** Formation of appropriate Data for LocalDatasource @public */
53 public dataSource: LocalDataSource = new LocalDataSource();
55 /** handle translate @public */
56 public translateService: TranslateService;
58 /** Columns list of the smart table @public */
59 public columnLists: object = {};
61 /** Settings for smarttable to populate the table with columns @public */
62 public settings: object = {};
64 /** Check the loading results @public */
65 public isLoadingResults: boolean = true;
67 /** Give the message for the loading @public */
68 public message: string = 'PLEASEWAIT';
70 /** Class for empty and present data @public */
71 public checkDataClass: string;
73 /** Element ref for fileInput @public */
74 @ViewChild('fileInput', { static: true }) public fileInput: ElementRef;
76 /** Instance of the rest service @private */
77 private restService: RestService;
79 /** dataService to pass the data from one component to another @private */
80 private dataService: DataService;
82 /** Formation of appropriate Data for LocalDatasource @private */
83 private nsData: NSData[] = [];
85 /** Contains all methods related to shared @private */
86 private sharedService: SharedService;
88 /** variables holds file information @private */
89 private fileData: string | ArrayBuffer;
91 /** Controls the header form @private */
92 private headers: HttpHeaders;
94 /** Notifier service to popup notification @private */
95 private notifierService: NotifierService;
97 /** Instance of the modal service @private */
98 private modalService: NgbModal;
100 /** Instance of subscriptions @private */
101 private generateDataSub: Subscription;
103 constructor(injector: Injector) {
104 this.injector = injector;
105 this.restService = this.injector.get(RestService);
106 this.dataService = this.injector.get(DataService);
107 this.sharedService = this.injector.get(SharedService);
108 this.translateService = this.injector.get(TranslateService);
109 this.notifierService = this.injector.get(NotifierService);
110 this.modalService = this.injector.get(NgbModal);
113 /** Lifecyle Hooks the trigger before component is instantiate @public */
114 public ngOnInit(): void {
115 this.generateColumns();
116 this.generateSettings();
118 this.headers = new HttpHeaders({
119 'Content-Type': 'application/gzip',
120 Accept: 'application/json',
121 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
123 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
126 /** smart table Header Colums @public */
127 public generateColumns(): void {
129 name: { title: this.translateService.instant('NAME'), width: '15%', sortDirection: 'asc' },
130 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '15%' },
131 version: { title: this.translateService.instant('VERSION'), width: '10%' },
132 designer: { title: this.translateService.instant('DESIGNER'), width: '15%' },
133 description: { title: this.translateService.instant('DESCRIPTION'), width: '25%' },
135 name: 'Actions', width: '20%', filter: false, sort: false, type: 'custom',
136 title: this.translateService.instant('ACTIONS'),
137 valuePrepareFunction: (cell: NSData, row: NSData): NSData => row, renderComponent: NsPackagesActionComponent
142 /** smart table Data Settings @public */
143 public generateSettings(): void {
146 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
150 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
153 columns: this.columnLists,
160 attr: this.sharedService.tableClassConfig(),
161 pager: this.sharedService.paginationPagerConfig(),
162 noDataMessage: this.translateService.instant('NODATAMSG')
166 /** smart table listing manipulation @public */
167 public onChange(perPageValue: number): void {
168 this.dataSource.setPaging(1, perPageValue, true);
171 /** smart table listing manipulation @public */
172 public onUserRowSelect(event: MessageEvent): void {
173 Object.assign(event.data, { page: 'ns-package' });
174 this.dataService.changeMessage(event.data);
177 /** Drag and drop feature and fetchind the details of files @public */
178 public filesDropped(files: FileList): void {
179 if (files && files.length === 1) {
180 this.isLoadingResults = true;
181 this.sharedService.getFileString(files, 'gz').then((fileContent: ArrayBuffer): void => {
182 const apiURLHeader: APIURLHEADER = {
183 url: environment.NSDESCRIPTORSCONTENT_URL,
184 httpOptions: { headers: this.headers }
186 this.saveFileData(apiURLHeader, fileContent);
187 }).catch((err: string): void => {
188 this.isLoadingResults = false;
189 if (err === 'typeError') {
190 this.notifierService.notify('error', this.translateService.instant('GZFILETYPEERRROR'));
192 this.notifierService.notify('error', this.translateService.instant('ERROR'));
195 } else if (files && files.length > 1) {
196 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
200 /** Post the droped files and reload the page @public */
201 public saveFileData(urlHeader: APIURLHEADER, fileData: {}): void {
202 this.fileInput.nativeElement.value = null;
203 this.restService.postResource(urlHeader, fileData).subscribe((result: {}) => {
204 this.notifierService.notify('success', this.translateService.instant('PAGE.NSPACKAGE.CREATEDSUCCESSFULLY'));
206 }, (error: ERRORDATA) => {
207 this.restService.handleError(error, 'post');
208 this.isLoadingResults = false;
212 /** Generate nsData object from loop and return for the datasource @public */
213 public generateNSData(nsdpackagedata: NSDDetails): NSData {
215 name: nsdpackagedata.name,
216 identifier: nsdpackagedata._id,
217 description: nsdpackagedata.description,
218 version: nsdpackagedata.version,
219 designer: nsdpackagedata.designer !== undefined ? nsdpackagedata.designer : '-'
223 /** Fetching the data from server to Load in the smarttable @public */
224 public generateData(): void {
225 this.isLoadingResults = true;
226 this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL).subscribe((nsdPackageData: NSDDetails[]) => {
228 nsdPackageData.forEach((nsdpackagedata: NSDDetails) => {
229 const nsDataObj: NSData = this.generateNSData(nsdpackagedata);
230 this.nsData.push(nsDataObj);
232 if (this.nsData.length > 0) {
233 this.checkDataClass = 'dataTables_present';
235 this.checkDataClass = 'dataTables_empty';
237 this.dataSource.load(this.nsData).then((data: boolean) => {
238 this.isLoadingResults = false;
240 this.isLoadingResults = false;
242 }, (error: ERRORDATA) => {
243 this.restService.handleError(error, 'get');
244 this.isLoadingResults = false;
247 /** Handle compose new ns package method @public */
248 public composeNSPackage(): void {
249 // eslint-disable-next-line security/detect-non-literal-fs-filename
250 this.modalService.open(ComposePackages, { backdrop: 'static' }).componentInstance.params = { page: 'ns-package' };
254 * Lifecyle hook which get trigger on component destruction
256 public ngOnDestroy(): void {
257 this.generateDataSub.unsubscribe();