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 WIM Account Component.
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
23 import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, WIM_TYPES } from 'CommonModel';
28 import { environment } from 'environment';
29 import * as jsyaml from 'js-yaml';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
32 import { isNullOrUndefined } from 'util';
36 * @Component takes NewWIMAccountComponent.html as template url
39 templateUrl: './NewWIMAccountComponent.html',
40 styleUrls: ['./NewWIMAccountComponent.scss']
42 /** Exporting a class @exports NewWIMAccountComponent */
43 export class NewWIMAccountComponent implements OnInit {
44 /** To inject services @public */
45 public injector: Injector;
47 /** Setting wim types in array @public */
48 public wimType: TYPESECTION[];
50 /** Set WIM Type select to empty @public */
51 public wimTypeMod: string = null;
53 /** New WIM account form controls using formgroup @public */
54 public wimNewAccountForm: FormGroup;
56 /** Form submission Add */
57 public submitted: boolean = false;
59 /** Instance for active modal service @public */
60 public activeModal: NgbActiveModal;
62 /** Check the loading results for loader status @public */
63 public isLoadingResults: boolean = false;
65 /** Give the message for the loading @public */
66 public message: string = 'PLEASEWAIT';
68 /** Element ref for fileInputConfig @public */
69 @ViewChild('fileInputConfig', { static: true }) public fileInputConfig: ElementRef;
71 /** Element ref for fileInputConfig @public */
72 @ViewChild('fileInputConfigLabel', { static: true }) public fileInputConfigLabel: ElementRef;
74 /** Instance of the rest service @private */
75 private restService: RestService;
77 /** Controls the header form @private */
78 private headers: HttpHeaders;
80 /** FormBuilder instance added to the formBuilder @private */
81 private formBuilder: FormBuilder;
83 /** Notifier service to popup notification @private */
84 private notifierService: NotifierService;
86 /** Contains tranlsate instance @private */
87 private translateService: TranslateService;
89 /** Contains all methods related to shared @private */
90 private sharedService: SharedService;
92 constructor(injector: Injector) {
93 this.injector = injector;
94 this.restService = this.injector.get(RestService);
95 this.formBuilder = this.injector.get(FormBuilder);
96 this.notifierService = this.injector.get(NotifierService);
97 this.translateService = this.injector.get(TranslateService);
98 this.activeModal = this.injector.get(NgbActiveModal);
99 this.sharedService = this.injector.get(SharedService);
101 /** Initializing Form Action */
102 this.wimNewAccountForm = this.formBuilder.group({
103 name: ['', Validators.required],
104 wim_type: ['', Validators.required],
105 wim_url: ['', [Validators.required, Validators.pattern(this.sharedService.REGX_URL_PATTERN)]],
106 user: ['', Validators.required],
107 password: ['', Validators.required],
113 /** convenience getter for easy access to form fields */
114 get f(): FormGroup['controls'] { return this.wimNewAccountForm.controls; }
117 * Lifecyle Hooks the trigger before component is instantiate
119 public ngOnInit(): void {
120 this.wimType = WIM_TYPES;
121 this.headers = new HttpHeaders({
122 Accept: 'application/json',
123 'Content-Type': 'application/json',
124 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
128 /** On modal submit newWimAccountSubmit will called @public */
129 public newWimAccountSubmit(): void {
130 this.submitted = true;
131 const modalData: MODALCLOSERESPONSEDATA = {
134 this.sharedService.cleanForm(this.wimNewAccountForm);
135 if (this.wimNewAccountForm.value.description === '') {
136 this.wimNewAccountForm.value.description = null;
138 if (isNullOrUndefined(this.wimNewAccountForm.value.config) || this.wimNewAccountForm.value.config === '') {
139 delete this.wimNewAccountForm.value.config;
141 const validJSON: boolean = this.sharedService.checkJson(this.wimNewAccountForm.value.config);
143 this.wimNewAccountForm.value.config = JSON.parse(this.wimNewAccountForm.value.config);
145 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
149 if (!this.wimNewAccountForm.invalid) {
150 this.isLoadingResults = true;
151 const apiURLHeader: APIURLHEADER = {
152 url: environment.WIMACCOUNTS_URL,
153 httpOptions: { headers: this.headers }
155 this.restService.postResource(apiURLHeader, this.wimNewAccountForm.value)
156 .subscribe((result: {}) => {
157 this.activeModal.close(modalData);
158 this.isLoadingResults = false;
159 this.notifierService.notify('success', this.translateService.instant('PAGE.WIMACCOUNTS.CREATEDSUCCESSFULLY'));
160 }, (error: ERRORDATA) => {
161 this.restService.handleError(error, 'post');
162 this.isLoadingResults = false;
167 /** Config file process @private */
168 public configFile(files: FileList): void {
169 if (files && files.length === 1) {
170 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
171 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
172 // tslint:disable-next-line: no-backbone-get-set-outside-model
173 this.wimNewAccountForm.get('config').setValue(JSON.stringify(getConfigJson));
174 }).catch((err: string): void => {
175 if (err === 'typeError') {
176 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
178 this.notifierService.notify('error', this.translateService.instant('ERROR'));
180 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
181 this.fileInputConfig.nativeElement.value = null;
183 } else if (files && files.length > 1) {
184 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
186 this.fileInputConfigLabel.nativeElement.innerText = files[0].name;
187 this.fileInputConfig.nativeElement.value = null;