d8e8f58b421d0a20c79909326c9d05ae6b22dc84
[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: './sqlcomponent.component.html',
10   styleUrls: ['./sqlcomponent.component.css']
11 })
12 export class SQLComponentComponent 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._http.post(environment.baseUrl + "report/wizard/retrieve_data/true", 
100     {
101       "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",
102       "tabId": "Sql",
103       "tabName": "Sql"
104     } 
105     , { headers: new HttpHeaders({'Content-Type': 'application/json'})})
106     .subscribe((response) => {
107         console.log(response);
108     });
109
110   if(this.SQLPostResponse === true)
111   {
112     this.SQLstatus = "Success!";
113     this.SQLmessage = "Your change has been saved! Definition is updated.";
114       this.showSaveSQLDialog = !this.showSaveSQLDialog;
115       this.SQLclosable = true; 
116   }
117   else
118   {
119     this.SQLstatus = "Failure!";
120     this.SQLmessage = "Definition could not be updated.";
121       this.showSaveSQLDialog = !this.showSaveSQLDialog;
122       this.SQLclosable = true;
123   }
124 }
125
126
127
128 validate()
129 {
130   //console.log(this.sqlText);
131
132   this._sqlService.postSQLValidateAndSave(this.sqlText)
133     .subscribe((response) => {
134         
135
136         this.ValidateResponseString = response["data"]["elements"];
137         this.SetValidateResponseString(this.ValidateResponseString);
138         console.log(this.ValidateResponseString);
139
140         this.ValidatePostResponse = JSON.parse(response["data"]["elements"]);
141         
142         //console.log(this.ValidatePostResponse["errormessage"]);
143
144         if(this.ValidatePostResponse["query"] !== undefined)
145         {
146             this.showModal = true;
147             this.Validatestatus = "SQL Test Run - Executed!";
148             this.showValidateSQLDialog = !this.showValidateSQLDialog;
149             this.Validateclosable = true; 
150         }
151         else
152         {
153           this.showModal = false;
154             this.Validatestatus = "SQL Test Run - Failed!";
155             this.showValidateSQLDialog = !this.showValidateSQLDialog;
156             this.Validateclosable = true;
157         }
158
159     });
160
161
162
163 }
164
165 closeSaveModal() {
166   this.showSaveSQLDialog = !this.showSaveSQLDialog;
167   this.SQLclosable = false;
168 }
169
170 closeValidateModal() {
171   this.showValidateSQLDialog = !this.showValidateSQLDialog;
172   this.Validateclosable = false;
173
174   console.log(this.reportMode);
175
176   if(this.reportMode === "Create")
177   {
178     this._http.get(environment.baseUrl + "report/wizard/retrieve_def_tab_wise_data/InSession")
179     .subscribe((response) => {
180       this._router.navigate(["v2/reports", "Edit",response["reportId"]]); 
181       });
182   }
183   //this._router.navigate(["v2/reports", this.reportId1]);
184 }
185
186 SetValidateResponseString(ValidateResponseString1 : string)
187 {
188   this.ValidateResponseString = ValidateResponseString1;
189 }
190
191 GetValidateResponseString()
192 {
193   return this.ValidateResponseString;
194 }
195
196 }