62c7d7a4a266c2c0f3413b6407a5864c993b18e5
[portal/sdk.git] /
1 import {Component, OnInit, Input, AfterViewInit, ViewEncapsulation} from '@angular/core';
2 import {HttpClient, HttpHeaders} from '@angular/common/http';
3 import {environment} from '../../../../../../environments/environment';
4 import {Router} from '@angular/router';
5 import {SqlService} from './sql.service';
6 import {error} from 'util';
7
8 @Component({
9     selector: 'app-sqlcomponent',
10     templateUrl: './sql.component.html',
11     styleUrls: ['./sql.component.css'],
12     encapsulation: ViewEncapsulation.None,
13 })
14 export class SQLComponent implements OnInit {
15
16     @Input('reportId') reportId1: string;
17     @Input('reportMode') reportMode: string;
18
19
20     showSaveSQLDialog: boolean;
21     SQLPostResponse: any;
22     ValidatePostResponse: any;
23     showValidateSQLDialog: boolean;
24     SQLstatus: string;
25     Validatestatus: string;
26     SQLmessage: string;
27     Validatemessage: string;
28     sqlText: string;
29     showModal: boolean;
30     ValidateResponseString: string;
31
32     finalGetObj: any;
33     showSpinner: boolean;
34     showErrorSqlMessage: boolean;
35     errorMessageString = '';
36
37     @Input() SQLclosable = true;
38     @Input() Validateclosable = true;
39
40
41     constructor(private _http: HttpClient, private _router: Router, private _sqlService: SqlService) {
42         this.showSaveSQLDialog = false;
43         this.SQLPostResponse = true;
44         this.ValidatePostResponse = {};
45         this.showErrorSqlMessage = false;
46     }
47
48     ngOnInit() {
49         this.showSaveSQLDialog = false;
50         this.SQLPostResponse = true;
51         this.ValidatePostResponse = {};
52         this._sqlService.getSQLTabData(this.reportId1)
53             .subscribe((response) => {
54                 this.showSpinner = true;
55                 this.finalGetObj = response;
56                 this.sqlText = this.finalGetObj.query;
57                 this.showSpinner = false;
58             });
59     }
60
61
62     ngOnChanges() {
63         this.showSaveSQLDialog = false;
64         this.SQLPostResponse = true;
65         this.ValidatePostResponse = {};
66         this._sqlService.getSQLTabData(this.reportId1)
67             .subscribe((response) => {
68                 this.showSpinner = true;
69                 this.finalGetObj = response;
70                 this.sqlText = this.finalGetObj.query;
71
72                 this.showSpinner = false;
73             });
74     }
75
76
77     saveSQL() {
78         this.SQLPostResponse = true;
79         if (this.SQLPostResponse === true) {
80             this.SQLstatus = 'Success!';
81             this.SQLmessage = 'Your change has been saved! Definition is updated.';
82             this.showSaveSQLDialog = !this.showSaveSQLDialog;
83             this.SQLclosable = true;
84         } else {
85             this.SQLstatus = 'Failure!';
86             this.SQLmessage = 'Definition could not be updated.';
87             this.showSaveSQLDialog = !this.showSaveSQLDialog;
88             this.SQLclosable = true;
89         }
90     }
91
92
93     validate() {
94         this._sqlService.postSQLValidateAndSave(this.sqlText)
95             .subscribe((response) => {
96                 this.showSpinner = true;
97                 this.ValidateResponseString = response['data']['elements'];
98                 this.SetValidateResponseString(this.ValidateResponseString);
99                 this.ValidatePostResponse = JSON.parse(response['data']['elements']);
100                 if (this.ValidatePostResponse['query'] !== undefined) {
101                     this.showErrorSqlMessage = false;
102                     this.showModal = true;
103                     this.Validatestatus = 'SQL Test Run - Executed!';
104                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
105                     this.Validateclosable = true;
106                 } else {
107                     this.showErrorSqlMessage = false;
108                     this.showModal = false;
109                     this.Validatestatus = 'SQL Test Run - Failed!';
110                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
111                     this.Validateclosable = true;
112                 }
113                 this.showSpinner = false;
114             }, error => {
115                 this.errorMessageString = error.error;
116                 this.ValidatePostResponse = {};
117                 this.ValidateResponseString = '';
118                 this.showErrorSqlMessage = true;
119                 this.showModal = false;
120                 this.Validatestatus = 'SQL Test Run - Failed!';
121                 this.showValidateSQLDialog = !this.showValidateSQLDialog;
122                 this.Validateclosable = true;
123         });
124     }
125
126     closeSaveModal() {
127         this.showSaveSQLDialog = !this.showSaveSQLDialog;
128         this.SQLclosable = false;
129     }
130
131     closeValidateModal() {
132         if (this.reportMode === 'Create') {
133             if (this.Validatestatus == 'SQL Test Run - Failed!') {
134                 this.sqlText = this.sqlText;
135             } else {
136                 this._http.get(environment.baseUrl + 'report/wizard/retrieve_def_tab_wise_data/InSession')
137                     .subscribe((response) => {
138                        // console.log(response);
139                         this._router.navigate(['v2/reports', 'Edit', response['reportId']]);
140                     });
141             }
142         }
143         this.showValidateSQLDialog = !this.showValidateSQLDialog;
144         this.Validateclosable = false;
145     }
146
147     SetValidateResponseString(ValidateResponseString1: string) {
148         this.ValidateResponseString = ValidateResponseString1;
149     }
150
151     GetValidateResponseString() {
152         return this.ValidateResponseString;
153     }
154
155 }