60a74dff57fb25fc355e221750269760cff50247
[portal/sdk.git] /
1 import { Component, OnInit, Input, AfterViewInit } 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
7 @Component({
8   selector: 'app-sqlcomponent',
9   templateUrl: './sql.component.html',
10   styleUrls: ['./sql.component.css']
11 })
12 export class SQLComponent implements OnInit, AfterViewInit {
13
14   @Input("reportId") reportId1 : string;
15   @Input("reportMode") reportMode : string;
16
17
18   showSaveSQLDialog: boolean;
19   SQLPostResponse : any;
20   ValidatePostResponse : any;
21   showValidateSQLDialog : boolean;
22   SQLstatus : string;
23   Validatestatus : string;
24   SQLmessage : string;
25   Validatemessage : string;
26   sqlText : string;
27   showModal : boolean;
28   ValidateResponseString : string;
29
30   finalGetObj : any;
31
32   @Input() SQLclosable = true;
33   @Input() Validateclosable = true;
34   
35
36   constructor(private _http : HttpClient, private _router : Router, private _sqlService : SqlService) { 
37     this.showSaveSQLDialog = false;
38     this.SQLPostResponse = true;
39     this.ValidatePostResponse = {};
40
41     this._sqlService.getSQLTabData(this.reportId1)
42     .subscribe((response) => {
43         console.log(response);
44
45         this.finalGetObj = response;
46         this.sqlText = this.finalGetObj.query;
47     });
48   }
49
50   ngOnInit() {
51     this.showSaveSQLDialog = false;
52     this.SQLPostResponse = true;
53     this.ValidatePostResponse = {};
54
55     this._sqlService.getSQLTabData(this.reportId1)
56     .subscribe((response) => {
57         console.log(response);
58
59         this.finalGetObj = response;
60         this.sqlText = this.finalGetObj.query;
61     });
62   }
63
64   ngAfterViewInit() {
65     console.log("Reached SQL TAB On Changes.");
66     this.showSaveSQLDialog = false;
67     this.SQLPostResponse = true;
68     this.ValidatePostResponse = {};
69
70     this._sqlService.getSQLTabData(this.reportId1)
71     .subscribe((response) => {
72         console.log(response);
73
74         this.finalGetObj = response;
75         this.sqlText = this.finalGetObj.query;
76     });
77   }
78
79   ngOnChanges()
80   {
81     console.log("Reached SQL TAB On Changes.");
82     this.showSaveSQLDialog = false;
83     this.SQLPostResponse = true;
84     this.ValidatePostResponse = {};
85
86     this._sqlService.getSQLTabData(this.reportId1)
87     .subscribe((response) => {
88         console.log(response);
89
90         this.finalGetObj = response;
91         this.sqlText = this.finalGetObj.query;
92     });
93   }
94
95
96   saveSQL()
97   {
98
99     this.SQLPostResponse = true;
100     // this._http.post(environment.baseUrl + "report/wizard/retrieve_data/true", 
101     // {
102     //   "query": "SELECT       region region,  market market,  site_state state,       count(usid) num_site from cssng_data_addition_5g@e911_e911pro where  cell_active='Y' group by region,market,site_state",
103     //   "tabId": "Sql",
104     //   "tabName": "Sql"
105     // } 
106     // , { headers: new HttpHeaders({'Content-Type': 'application/json'})})
107     // .subscribe((response) => {
108     //     console.log(response);
109     // });
110
111   if(this.SQLPostResponse === true)
112   {
113     this.SQLstatus = "Success!";
114     this.SQLmessage = "Your change has been saved! Definition is updated.";
115       this.showSaveSQLDialog = !this.showSaveSQLDialog;
116       this.SQLclosable = true; 
117   }
118   else
119   {
120     this.SQLstatus = "Failure!";
121     this.SQLmessage = "Definition could not be updated.";
122       this.showSaveSQLDialog = !this.showSaveSQLDialog;
123       this.SQLclosable = true;
124   }
125 }
126
127
128
129 validate()
130 {
131   //console.log(this.sqlText);
132
133   this._sqlService.postSQLValidateAndSave(this.sqlText)
134     .subscribe((response) => {
135         
136
137         this.ValidateResponseString = response["data"]["elements"];
138         this.SetValidateResponseString(this.ValidateResponseString);
139         console.log(this.ValidateResponseString);
140
141         this.ValidatePostResponse = JSON.parse(response["data"]["elements"]);
142         
143         //console.log(this.ValidatePostResponse["errormessage"]);
144
145         if(this.ValidatePostResponse["query"] !== undefined)
146         {
147             this.showModal = true;
148             this.Validatestatus = "SQL Test Run - Executed!";
149             this.showValidateSQLDialog = !this.showValidateSQLDialog;
150             this.Validateclosable = true; 
151         }
152         else
153         {
154           this.showModal = false;
155             this.Validatestatus = "SQL Test Run - Failed!";
156             this.showValidateSQLDialog = !this.showValidateSQLDialog;
157             this.Validateclosable = true;
158         }
159
160     });
161
162
163
164 }
165
166 closeSaveModal() {
167   this.showSaveSQLDialog = !this.showSaveSQLDialog;
168   this.SQLclosable = false;
169 }
170
171 closeValidateModal() {
172   this.showValidateSQLDialog = !this.showValidateSQLDialog;
173   this.Validateclosable = false;
174
175   console.log(this.reportMode);
176
177   if(this.reportMode === "Create")
178   {
179     this._http.get(environment.baseUrl + "report/wizard/retrieve_def_tab_wise_data/InSession")
180     .subscribe((response) => {
181       this._router.navigate(["v2/reports", "Edit",response["reportId"]]); 
182       });
183   }
184   //this._router.navigate(["v2/reports", this.reportId1]);
185 }
186
187 SetValidateResponseString(ValidateResponseString1 : string)
188 {
189   this.ValidateResponseString = ValidateResponseString1;
190 }
191
192 GetValidateResponseString()
193 {
194   return this.ValidateResponseString;
195 }
196
197 }