Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / comp-spec-validation / comp-spec-validation.component.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
20 import { SpecValidationService } from '../services/spec-validation.service';
21 import { DownloadService } from '../services/download.service';
22 import { Ng4LoadingSpinnerService } from 'ng4-loading-spinner';
23
24 @Component({
25   selector: 'app-comp-spec-validation',
26   templateUrl: './comp-spec-validation.component.html',
27   styleUrls: ['./comp-spec-validation.component.css']
28 })
29 export class CompSpecValidationComponent implements OnInit {
30
31   @ViewChild('myFile', {static: false})
32   myInputVariable: ElementRef;
33
34   spec_validator_action;
35   release = '';
36   type = '';
37   shouldValidate = false;
38   shouldDownload = false;
39   validCompSpec = false;
40   specValidated = false;
41   specValidationOutputSummary: any;
42   
43   constructor(private specValidator: SpecValidationService, private downloadService: DownloadService, private spinnerService: Ng4LoadingSpinnerService ) { }
44
45   ngOnInit() {
46
47   }
48
49   compSpecSelected: any;
50   onCompSpecUpload(event){
51     this.compSpecSelected = event.target.files[0];
52     this.readCsFileContent(this.compSpecSelected);
53   }
54
55   compSpecContent: any = null;
56   readCsFileContent(file) {
57     if (file) {
58       let fileReader = new FileReader();
59       fileReader.onload = (e) => { this.compSpecContent = fileReader.result; };
60       fileReader.readAsText(file);
61     }
62   }
63
64   validateRadioButton(){
65     this.shouldValidate = true;
66     this.shouldDownload = false;
67   }
68
69   downloadRadioButton(){
70     this.shouldValidate = false;
71     this.shouldDownload = true;
72     this.compSpecContent = null;
73     this.specValidated = false
74   }
75
76   resetFile(){
77     this.myInputVariable.nativeElement.value = "";
78     this.compSpecContent = null
79     this.compSpecSelected = null
80     this.specValidated = false
81   }
82
83   specValidationOutputHeader = ''
84   specValidationOutputMessage = '';
85   validateSpec(){
86     this.specValidationOutputHeader = ""
87     this.specValidationOutputMessage = ""
88     this.specValidationOutputSummary = ""
89
90     this.spinnerService.show()
91     this.validateJsonStructure()
92     this.specValidator.sendSpecFile(this.compSpecContent, this.type, this.release).subscribe(
93       res => {}, err => {
94         this.setSpecValidationMessage(err)
95       }
96     )
97   }
98
99   setSpecValidationMessage(res){
100     if(res.status === 200){
101       this.specValidationOutputHeader = "Success: Valid Component Spec"
102       this.specValidationOutputMessage = ""
103       this.validCompSpec = true
104     } else {
105       this.specValidationOutputHeader = `${res.status} Error: Invalid Component Spec`
106       this.specValidationOutputSummary = res.error.summary
107       
108       for(let item of res.error.errors){
109         this.specValidationOutputMessage += `- ${item}\n\n`
110       }
111
112       this.validCompSpec = false
113     }
114
115     this.specValidated = true;
116     this.spinnerService.hide()
117   }
118
119   validateJsonStructure() {
120     try {
121       JSON.parse(this.compSpecContent);
122     } catch (error) {
123       this.specValidationOutputHeader = "Error: Invalid Component Spec"
124       this.specValidationOutputSummary = "JSON Structure Error"
125       this.specValidationOutputMessage = error
126       this.validCompSpec = false
127       this.specValidated = true
128       this.spinnerService.hide()
129       throw new Error('JSON Structure error, quit!');
130     }
131   }
132
133   downloadSchema(){
134     this.spinnerService.show()
135     this.specValidator.getSchema(this.type).subscribe(
136       res => {
137         this.downloadService.downloadJSON(res, `${this.release}+_${this.type}_Schema`)
138         this.spinnerService.hide()
139       }, err => {
140         console.log(err)
141         this.spinnerService.hide()
142       }
143     )
144   }
145 }