import {Component, OnInit, Input, AfterViewInit, ViewEncapsulation} from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {environment} from '../../../../../../environments/environment'; import {Router} from '@angular/router'; import {SqlService} from './sql.service'; import {error} from 'util'; @Component({ selector: 'app-sqlcomponent', templateUrl: './sql.component.html', styleUrls: ['./sql.component.css'], encapsulation: ViewEncapsulation.None, }) export class SQLComponent implements OnInit { @Input('reportId') reportId1: string; @Input('reportMode') reportMode: string; showSaveSQLDialog: boolean; SQLPostResponse: any; ValidatePostResponse: any; showValidateSQLDialog: boolean; SQLstatus: string; Validatestatus: string; SQLmessage: string; Validatemessage: string; sqlText: string; showModal: boolean; ValidateResponseString: string; finalGetObj: any; showSpinner: boolean; showErrorSqlMessage: boolean; errorMessageString = ''; @Input() SQLclosable = true; @Input() Validateclosable = true; keyWordAssistance: any[]; constructor(private _http: HttpClient, private _router: Router, private _sqlService: SqlService) { this.showSaveSQLDialog = false; this.SQLPostResponse = true; this.ValidatePostResponse = {}; this.showErrorSqlMessage = false; this.keyWordAssistance = []; } ngOnInit() { this.showSaveSQLDialog = false; this.SQLPostResponse = true; this.ValidatePostResponse = {}; this.keyWordAssistance.push( 'SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY', 'HAVING', 'ORDER BY', 'ASC', 'DESC', 'AND', 'OR', 'NOT', 'EXISTS', 'IS', 'NULL', 'IN', 'BETWEEN', 'COUNT(', 'SUM(', 'AVG(', 'MAX(', 'MIN(', 'NVL(', 'DECODE(', 'SYSDATE', 'TO_CHAR(', 'TO_NUMBER(', 'TO_DATE(', 'TRUNC(', 'ROUND(', 'ABS(', 'SUBSTR(', 'REPLACE(', 'LOWER(', 'UPPER(', 'LTRM(', 'RTRIM(', 'LPAD(', 'RPAD(', 'linkToReport'); this._sqlService.getSQLTabData(this.reportId1) .subscribe((response) => { this.showSpinner = true; this.finalGetObj = response; this.sqlText = this.finalGetObj.query; this.showSpinner = false; }); } ngOnChanges() { this.showSaveSQLDialog = false; this.SQLPostResponse = true; this.ValidatePostResponse = {}; this._sqlService.getSQLTabData(this.reportId1) .subscribe((response) => { this.showSpinner = true; this.finalGetObj = response; this.sqlText = this.finalGetObj.query; this.showSpinner = false; }); } saveSQL() { this.SQLPostResponse = true; if (this.SQLPostResponse === true) { this.SQLstatus = 'Success!'; this.SQLmessage = 'Your change has been saved! Definition is updated.'; this.showSaveSQLDialog = !this.showSaveSQLDialog; this.SQLclosable = true; } else { this.SQLstatus = 'Failure!'; this.SQLmessage = 'Definition could not be updated.'; this.showSaveSQLDialog = !this.showSaveSQLDialog; this.SQLclosable = true; } } validate() { this._sqlService.postSQLValidateAndSave(this.sqlText) .subscribe((response) => { this.showSpinner = true; this.ValidateResponseString = response['data']['elements']; this.SetValidateResponseString(this.ValidateResponseString); this.ValidatePostResponse = JSON.parse(response['data']['elements']); if (this.ValidatePostResponse['query'] !== undefined) { this.showErrorSqlMessage = false; this.showModal = true; this.Validatestatus = 'SQL Test Run - Executed!'; this.showValidateSQLDialog = !this.showValidateSQLDialog; this.Validateclosable = true; } else { this.showErrorSqlMessage = false; this.showModal = false; this.Validatestatus = 'SQL Test Run - Failed!'; this.showValidateSQLDialog = !this.showValidateSQLDialog; this.Validateclosable = true; } this.showSpinner = false; }, error => { this.errorMessageString = error.error; this.ValidatePostResponse = {}; this.ValidateResponseString = ''; this.showErrorSqlMessage = true; this.showModal = false; this.Validatestatus = 'SQL Test Run - Failed!'; this.showValidateSQLDialog = !this.showValidateSQLDialog; this.Validateclosable = true; }); } closeSaveModal() { this.showSaveSQLDialog = !this.showSaveSQLDialog; this.SQLclosable = false; } closeValidateModal() { if (this.reportMode === 'Create') { this._http.get(environment.baseUrl + 'report/wizard/retrieve_def_tab_wise_data/InSession') .subscribe((response) => { console.log(response); this._router.navigate(['v2/app/reports', 'Edit', response['reportId']]); }); } this.showValidateSQLDialog = !this.showValidateSQLDialog; this.Validateclosable = false; } SetValidateResponseString(ValidateResponseString1: string) { this.ValidateResponseString = ValidateResponseString1; } GetValidateResponseString() { return this.ValidateResponseString; } addText(word: string) { this.sqlText = this.sqlText + ' ' + word + ' '; } }