7a547e3d1949e7ec19cfda918645c4993c52c446
[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     keyWordAssistance: any[];
40
41
42     constructor(private _http: HttpClient, private _router: Router, private _sqlService: SqlService) {
43         this.showSaveSQLDialog = false;
44         this.SQLPostResponse = true;
45         this.ValidatePostResponse = {};
46         this.showErrorSqlMessage = false;
47         this.keyWordAssistance = [];
48
49     }
50
51     ngOnInit() {
52         this.showSaveSQLDialog = false;
53         this.SQLPostResponse = true;
54         this.ValidatePostResponse = {};
55         this.keyWordAssistance.push( 'SELECT',  'DISTINCT', 'FROM', 'WHERE',
56             'GROUP BY', 'HAVING',  'ORDER BY',  'ASC',   'DESC', 'AND',  'OR',  'NOT',  'EXISTS',
57             'IS',  'NULL',  'IN',  'BETWEEN', 'COUNT(', 'SUM(', 'AVG(', 'MAX(', 'MIN(', 'NVL(',
58             'DECODE(', 'SYSDATE', 'TO_CHAR(', 'TO_NUMBER(', 'TO_DATE(',
59             'TRUNC(', 'ROUND(', 'ABS(', 'SUBSTR(', 'REPLACE(', 'LOWER(', 'UPPER(',
60             'LTRM(', 'RTRIM(', 'LPAD(', 'RPAD(', 'linkToReport');
61         this._sqlService.getSQLTabData(this.reportId1)
62             .subscribe((response) => {
63                 this.showSpinner = true;
64                 this.finalGetObj = response;
65                 this.sqlText = this.finalGetObj.query;
66                 this.showSpinner = false;
67             });
68     }
69
70
71     ngOnChanges() {
72         this.showSaveSQLDialog = false;
73         this.SQLPostResponse = true;
74         this.ValidatePostResponse = {};
75         this._sqlService.getSQLTabData(this.reportId1)
76             .subscribe((response) => {
77                 this.showSpinner = true;
78                 this.finalGetObj = response;
79                 this.sqlText = this.finalGetObj.query;
80
81                 this.showSpinner = false;
82             });
83     }
84
85
86     saveSQL() {
87         this.SQLPostResponse = true;
88         if (this.SQLPostResponse === true) {
89             this.SQLstatus = 'Success!';
90             this.SQLmessage = 'Your change has been saved! Definition is updated.';
91             this.showSaveSQLDialog = !this.showSaveSQLDialog;
92             this.SQLclosable = true;
93         } else {
94             this.SQLstatus = 'Failure!';
95             this.SQLmessage = 'Definition could not be updated.';
96             this.showSaveSQLDialog = !this.showSaveSQLDialog;
97             this.SQLclosable = true;
98         }
99     }
100
101
102     validate() {
103         this._sqlService.postSQLValidateAndSave(this.sqlText)
104             .subscribe((response) => {
105                 this.showSpinner = true;
106                 this.ValidateResponseString = response['data']['elements'];
107                 this.SetValidateResponseString(this.ValidateResponseString);
108                 this.ValidatePostResponse = JSON.parse(response['data']['elements']);
109                 if (this.ValidatePostResponse['query'] !== undefined) {
110                     this.showErrorSqlMessage = false;
111                     this.showModal = true;
112                     this.Validatestatus = 'SQL Test Run - Executed!';
113                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
114                     this.Validateclosable = true;
115                 } else {
116                     this.showErrorSqlMessage = false;
117                     this.showModal = false;
118                     this.Validatestatus = 'SQL Test Run - Failed!';
119                     this.showValidateSQLDialog = !this.showValidateSQLDialog;
120                     this.Validateclosable = true;
121                 }
122                 this.showSpinner = false;
123             }, error => {
124                 this.errorMessageString = error.error;
125                 this.ValidatePostResponse = {};
126                 this.ValidateResponseString = '';
127                 this.showErrorSqlMessage = true;
128                 this.showModal = false;
129                 this.Validatestatus = 'SQL Test Run - Failed!';
130                 this.showValidateSQLDialog = !this.showValidateSQLDialog;
131                 this.Validateclosable = true;
132         });
133     }
134
135     closeSaveModal() {
136         this.showSaveSQLDialog = !this.showSaveSQLDialog;
137         this.SQLclosable = false;
138     }
139
140     closeValidateModal() {
141         if (this.reportMode === 'Create') {
142             if (this.Validatestatus == 'SQL Test Run - Failed!') {
143                 this.sqlText = this.sqlText;
144             } else {
145                 this._http.get(environment.baseUrl + 'report/wizard/retrieve_def_tab_wise_data/InSession')
146                     .subscribe((response) => {
147                         console.log(response);
148                         this._router.navigate(['v2/app/reports', 'Edit', response['reportId']]);
149                     });
150             }
151         }
152         this.showValidateSQLDialog = !this.showValidateSQLDialog;
153         this.Validateclosable = false;
154     }
155
156     SetValidateResponseString(ValidateResponseString1: string) {
157         this.ValidateResponseString = ValidateResponseString1;
158     }
159
160     GetValidateResponseString() {
161         return this.ValidateResponseString;
162     }
163
164     addText(word: string) {
165         this.sqlText =  this.sqlText + '  '  + word + '  ';
166     }
167 }