c1559c75782538cec4af96f4663e8113aa312066
[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.showSpinner = true;
50         this.showSaveSQLDialog = false;
51         this.SQLPostResponse = true;
52         this.ValidatePostResponse = {};
53         this._sqlService.getSQLTabData(this.reportId1)
54             .subscribe((response) => {
55                 this.showSpinner = true;
56                 this.finalGetObj = response;
57                 this.sqlText = this.finalGetObj.query;
58                 this.showSpinner = false;
59             });
60     }
61
62
63     ngOnChanges() {
64         this.showSaveSQLDialog = false;
65         this.SQLPostResponse = true;
66         this.ValidatePostResponse = {};
67         this._sqlService.getSQLTabData(this.reportId1)
68             .subscribe((response) => {
69                 this.showSpinner = true;
70                 this.finalGetObj = response;
71                 this.sqlText = this.finalGetObj.query;
72
73                 this.showSpinner = false;
74             });
75     }
76
77
78     saveSQL() {
79         this.SQLPostResponse = true;
80         if (this.SQLPostResponse === true) {
81             this.SQLstatus = 'Success!';
82             this.SQLmessage = 'Your change has been saved! Definition is updated.';
83             this.showSaveSQLDialog = !this.showSaveSQLDialog;
84             this.SQLclosable = true;
85         } else {
86             this.SQLstatus = 'Failure!';
87             this.SQLmessage = 'Definition could not be updated.';
88             this.showSaveSQLDialog = !this.showSaveSQLDialog;
89             this.SQLclosable = true;
90         }
91     }
92
93
94     validate() {
95         this._sqlService.postSQLValidateAndSave(this.sqlText)
96             .subscribe((response) => {
97                 this.showSpinner = true;
98                 this.ValidateResponseString = response['data']['elements'];
99                 this.SetValidateResponseString(this.ValidateResponseString);
100                 this.ValidatePostResponse = JSON.parse(response['data']['elements']);
101                 if (this.ValidatePostResponse['query'] !== undefined) {
102                     this.showErrorSqlMessage = false;
103                     this.showModal = true;
104                     this.Validatestatus = 'SQL Test Run - Executed!';
105                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
106                     this.Validateclosable = true;
107                 } else {
108                     this.showErrorSqlMessage = false;
109                     this.showModal = false;
110                     this.Validatestatus = 'SQL Test Run - Failed!';
111                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
112                     this.Validateclosable = true;
113                 }
114                 this.showSpinner = false;
115             }, error => {
116                 this.errorMessageString = error.error;
117                 this.ValidatePostResponse = {};
118                 this.ValidateResponseString = '';
119                 this.showErrorSqlMessage = true;
120                 this.showModal = false;
121                 this.Validatestatus = 'SQL Test Run - Failed!';
122                 this.showValidateSQLDialog = !this.showValidateSQLDialog;
123                 this.Validateclosable = true;
124         });
125     }
126
127     closeSaveModal() {
128         this.showSaveSQLDialog = !this.showSaveSQLDialog;
129         this.SQLclosable = false;
130     }
131
132     closeValidateModal() {
133         if (this.reportMode === 'Create') {
134             if (this.Validatestatus == 'SQL Test Run - Failed!') {
135                 this.sqlText = this.sqlText;
136             } else {
137                 this._http.get(environment.baseUrl + 'report/wizard/retrieve_def_tab_wise_data/InSession')
138                     .subscribe((response) => {
139                         console.log(response);
140                         this._router.navigate(['v2/reports', 'Edit', response['reportId']]);
141                     });
142             }
143         }
144         this.showValidateSQLDialog = !this.showValidateSQLDialog;
145         this.Validateclosable = false;
146     }
147
148     SetValidateResponseString(ValidateResponseString1: string) {
149         this.ValidateResponseString = ValidateResponseString1;
150     }
151
152     GetValidateResponseString() {
153         return this.ValidateResponseString;
154     }
155
156 }