6740d36ee5be712d82d0aadc475a14d50dddf393
[portal/sdk.git] /
1 import { Component, OnInit, Input, SimpleChange, SystemJsNgModuleLoader } from '@angular/core';
2 import { RunService } from '../run.service';
3 import { ActivatedRoute, Router } from '@angular/router';
4 import { inlineInterpolate } from '@angular/core/src/view';
5
6 @Component({
7   selector: 'app-run-report-form-fields',
8   templateUrl: './run-report-form-fields.component.html',
9   styleUrls: ['./run-report-form-fields.component.css']
10 })
11 export class RunReportFormFieldsComponent implements OnInit {
12
13   @Input("formFieldList") formFieldList : {}[];
14   @Input("reportId") reportId : string;
15   
16
17   staticFormFieldList : {}[] = [];
18   formFieldListValueArr : any[];
19   finalQueryParamsObj : {};
20   navigateToRun : boolean;
21   reportMode : string;
22   queryString : string;
23   reportName : string;
24   showSpinner : boolean;
25   showLabel : boolean;
26   runDashboardReport : boolean;
27   DashboardReportObj : {}[] = new Array();
28   triggerFormFieldArr = new Array();
29   initialObject = new Object();
30   formFieldGroupObjList : {}[] = [];
31   toggleFormFieldRenderArr : {}[] = [];
32   groupSelectValue : string = "";
33   oldGroupSelectValue : string = "";
34   unCommonCnt : number = 0;
35   commonCount : number = 0;
36   totalCount : number = 0;
37   errorMessage : string = "";
38   stackTrace : string = "";
39   error : boolean = false;
40   iSDashboardReport : string = "";
41   hitCnt : number = 0;
42   directCallQueryParams : any = "";
43   calledWithFormFields : boolean = false;
44
45
46   constructor(private _runService : RunService, private _route : ActivatedRoute, private _router : Router) { 
47     this.formFieldListValueArr = new Array();
48     this.finalQueryParamsObj = new Object();
49     this.navigateToRun = false;
50     this.queryString = "";
51     this.showSpinner = false;
52     this.showLabel = false;
53     this.runDashboardReport = false;
54
55     this._router.routeReuseStrategy.shouldReuseRoute = function() {
56       return false;
57   };
58   }
59
60   ngOnInit() {
61
62     this.showSpinner = true;  
63     this.navigateToRun = false;
64
65     this._route.params.subscribe(params => {
66       
67       this.reportId = params["reportId"];
68
69       if(params["queryParameters"])
70       {
71         this.directCallQueryParams = params["queryParameters"];
72         this.calledWithFormFields = true;
73       }
74     });
75
76     
77
78     
79     this._runService.getDefinitionPageDetails(+this.reportId)
80     .subscribe((responseDefPage) => {
81     
82     this.reportName = responseDefPage["reportName"];
83     if(responseDefPage["reportType"] !== "Dashboard")
84     {
85       if(this.calledWithFormFields == false)
86       {
87       this._runService.getReportData(this.reportId)
88       .subscribe((response) => {
89       if(response["errormessage"])
90       {
91         this.showError(response);
92       }
93       else
94       {
95       
96       this.reportName = response["reportName"];
97       if(response["formFieldList"].length > 0)
98       {
99         this.fetchAndPopulateFormFields(response, this.reportId);
100       }
101       else
102       {
103         this.reportMode = "Regular";
104         this.navigateToRun = true;
105         
106       }
107       this.showSpinner = false;
108       }
109     });
110     }
111     else
112     {
113       this.reportMode = "FormField";
114       this.navigateToRun = true;
115       this.showSpinner = false;
116     }
117   }
118   else
119   {
120     this.iSDashboardReport = "Dashboard";
121     this.reportName = responseDefPage["reportName"];
122     this.DashboardReportObj = JSON.parse(responseDefPage["dashboardLayoutJSON"]);
123     let i=0;
124     let subReportId = "";
125
126     while(this.DashboardReportObj[i])
127     {
128       subReportId = this.DashboardReportObj[i]["hasContent"]["id"].split("#")[1];
129       i++;
130       break;
131     }
132
133     this._runService.getReportData(subReportId)
134     .subscribe((response) => {
135       if(response["errormessage"])
136       {
137         this.showError(response);
138       }
139       else
140       {
141       
142       if(response["formFieldList"].length > 0)
143       {
144         this.fetchAndPopulateFormFields(response, subReportId);
145       }
146       this.runDashboardReport = true;      
147       this.showSpinner = false;
148       }
149
150     });
151   }
152
153   });
154
155   
156
157 }
158
159   ngDoCheck()
160   {
161    
162     if(this.formFieldList !== undefined)
163     {
164     if(this.groupSelectValue !== this.oldGroupSelectValue)
165     {
166
167
168       this.oldGroupSelectValue = this.groupSelectValue;
169       for(let formFieldGroupObjItem of this.formFieldGroupObjList)
170       {
171         if(formFieldGroupObjItem["name"] == this.groupSelectValue)
172         {
173           this.toggleFormFieldRenderArr = formFieldGroupObjItem["formFieldList"];
174         }
175
176       }
177
178       if(this.toggleFormFieldRenderArr.length > 0)
179       {
180         for(let i=0; i<this.toggleFormFieldRenderArr.length; i++)
181         {
182           let formFieldObj = this.toggleFormFieldRenderArr[i];
183           if(formFieldObj["triggerOtherFormFields"] === true)
184           {
185             let formFieldId = formFieldObj["fieldId"];
186             this.triggerFormFieldArr.push(formFieldId);
187             this.initialObject[formFieldId] = "1";
188             this.finalQueryParamsObj[formFieldId] = "1";
189           }
190         }
191       }
192
193       for(let fvl=this.commonCount; fvl < this.formFieldListValueArr.length; fvl++)
194       {
195         this.formFieldListValueArr[fvl] = null;
196       }
197
198       if(this.formFieldGroupObjList.length > 0)
199       {
200       for(let k=0; k<this.formFieldList.length; k++)
201       {
202       for(let i=0; i<this.formFieldGroupObjList.length; i++)
203       {
204         let inList = this.formFieldGroupObjList[i]["formFieldList"];
205         
206         for(let j=0; j<inList.length; j++)
207         {
208           let cntr=0;
209           if(inList[j]["fieldId"] == this.formFieldList[k]["fieldId"])
210           {
211             cntr++;
212           }
213           if(cntr == 0)
214           {
215             this.finalQueryParamsObj[inList[j]["fieldId"]] = "1";
216           }
217         }
218         
219
220       }
221       }
222       }
223     }
224
225     
226
227     //Toggle Form Field Block Begins
228     
229     for(let tffr=0; tffr<this.toggleFormFieldRenderArr.length; tffr++)
230     {
231       if(this.formFieldListValueArr[tffr + this.commonCount])
232       {
233         if(this.toggleFormFieldRenderArr[tffr]["validationType"] == "DATE")
234         {
235           this.finalQueryParamsObj[this.toggleFormFieldRenderArr[tffr]["fieldId"]] = this.convertDate(this.formFieldListValueArr[tffr + this.commonCount]);
236         }
237         else
238         {
239           this.finalQueryParamsObj[this.toggleFormFieldRenderArr[tffr]["fieldId"]] = this.formFieldListValueArr[tffr + this.commonCount];
240           for(let trigElement of this.triggerFormFieldArr)
241           {
242             if(this.initialObject[trigElement] !== this.finalQueryParamsObj[trigElement])
243             {
244               this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
245               this.generateQueryString();
246               this._runService.refreshFormFields(this.reportId, this.queryString)
247               .subscribe((responseRefreshFF) => {
248               for(let rrff=0; rrff<responseRefreshFF["formFieldList"].length; rrff++)
249               {
250                 for(let innerTFRR=0; innerTFRR<this.toggleFormFieldRenderArr.length; innerTFRR++)
251                 {
252                   if(responseRefreshFF["formFieldList"][rrff]["fieldId"] == this.toggleFormFieldRenderArr[innerTFRR]["fieldId"])
253                   {
254                     this.toggleFormFieldRenderArr[innerTFRR] = responseRefreshFF["formFieldList"][rrff];
255                   }
256                 }
257               }
258             });
259             this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
260             }
261           }
262           
263         }
264       }
265     }
266
267     //Toggle Form Field Block Ends
268
269     this.formFieldListValueArr = this.formFieldListValueArr;
270     if(this.reportMode !== "Regular" && this.reportMode !== "Dashboard")
271     {
272     for(let ffl=0; ffl<this.formFieldList.length; ffl++)
273     {
274       if(this.formFieldListValueArr[ffl])
275       {
276         if(this.formFieldList[ffl]["validationType"] == "DATE")
277         {
278           this.finalQueryParamsObj[this.formFieldList[ffl]["fieldId"]] = this.convertDate(this.formFieldListValueArr[ffl]);
279         }
280         else
281         {
282           this.finalQueryParamsObj[this.formFieldList[ffl]["fieldId"]] = this.formFieldListValueArr[ffl];
283           for(let trigElement of this.triggerFormFieldArr)
284           {
285             if(this.initialObject[trigElement] !== this.finalQueryParamsObj[trigElement])
286             {
287               this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
288               this.generateQueryString();
289               this._runService.refreshFormFields(this.reportId, this.queryString)
290               .subscribe((responseRefreshFF) => {
291
292                 for(let rrff=0; rrff<responseRefreshFF["formFieldList"].length; rrff++)
293                 {
294                 for(let innerFFL=0; innerFFL<this.formFieldList.length; innerFFL++)
295                 {
296                   if(responseRefreshFF["formFieldList"][rrff]["fieldId"] == this.formFieldList[innerFFL]["fieldId"])
297                   {
298                     this.formFieldList[innerFFL] = responseRefreshFF["formFieldList"][rrff];
299                   }
300                 }
301                 }
302             });
303             }
304           }
305           
306         }
307       }
308     }
309   }
310   }
311   }
312
313   convertDate(str) {
314     var date = new Date(str),
315       mnth = ("0" + (date.getMonth() + 1)).slice(-2),
316       day = ("0" + date.getDate()).slice(-2);
317     return [mnth, day, date.getFullYear()].join("/");
318   }
319
320   getQueryString()
321   {
322     if(this.directCallQueryParams !== "")
323     {
324       return this.directCallQueryParams;
325     }
326     else
327     {
328       return this.queryString;
329     }
330   }
331
332   fetchAndPopulateFormFields(respObj : any, ffReportId : string)
333   {
334     
335     this._runService.getFormFieldGroupsData(ffReportId)
336     .subscribe((responseFormFieldGroups) => {
337       this.formFieldGroupObjList = JSON.parse(responseFormFieldGroups["formFieldGroupsJSON"]);
338     
339     
340     this.formFieldList = respObj["formFieldList"];
341     this.staticFormFieldList = respObj["formFieldList"];
342     this.reportMode = "FormField";
343     this.totalCount = this.formFieldList.length;
344     if(this.formFieldGroupObjList !== null)
345     {
346     for(let ffgl=0; ffgl<this.formFieldGroupObjList.length; ffgl++)
347     {
348       for(let itemFFGL=0; itemFFGL<this.formFieldGroupObjList[ffgl]["formFieldList"].length; itemFFGL++)
349       {
350         let formFieldGroupItem = this.formFieldGroupObjList[ffgl]["formFieldList"][itemFFGL];
351         for(let fflg=0; fflg<this.formFieldList.length; fflg++)
352         {
353           if(formFieldGroupItem["id"] == this.formFieldList[fflg]["fieldId"])
354           {
355             this.formFieldGroupObjList[ffgl]["formFieldList"][itemFFGL] = this.formFieldList[fflg];
356             this.formFieldList.splice(fflg, 1);
357             this.unCommonCnt++;
358           }
359         }
360       }
361     }
362     }
363     
364     this.commonCount = this.totalCount - this.unCommonCnt;
365
366     for(let i=0; i<this.formFieldList.length; i++)
367     {
368       let formFieldObj = this.formFieldList[i];
369       if(formFieldObj["triggerOtherFormFields"] === true)
370       {
371         let formFieldId = formFieldObj["fieldId"];
372         this.triggerFormFieldArr.push(formFieldId);
373         this.initialObject[formFieldId] = [];
374         this.finalQueryParamsObj[formFieldId] = [];
375       }
376     }
377     });
378   }
379
380   generateQueryString()
381   {
382     
383     this.queryString="";
384     for(let k=0; k<Object.keys(this.finalQueryParamsObj).length; k++)
385     {
386       
387       if(typeof(this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]]) == "object")
388       {
389         
390         let key = Object.keys(this.finalQueryParamsObj)[k];
391         let qstr = "";
392         let l=0;
393         while(this.finalQueryParamsObj[key][l])
394         {
395           if(l === 0)
396           {
397             qstr = qstr + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]][l];
398           }
399           else
400           {
401             qstr = qstr + "|" + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]][l];
402           }
403           l++;
404         }
405         if(qstr !== "")
406         {
407           this.queryString = this.queryString + "&" + Object.keys(this.finalQueryParamsObj)[k] + "=" + qstr;
408         }
409       }
410       else
411       {
412         if(typeof(this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]]) == "string")
413         {
414           let stringParam = this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]];
415           if(stringParam.split(",").length > 1)
416           {
417             let l=0;
418             let qstr = "";
419             for(let param of stringParam.split(","))
420             {
421               if(l === 0)
422               {
423                 qstr = qstr + param;
424               }
425               else
426               {
427                 qstr = qstr + "|" + param;
428               }
429               l++;
430             }
431             if(qstr !== "")
432             {
433               this.queryString = this.queryString + "&" + Object.keys(this.finalQueryParamsObj)[k] + "=" + qstr;
434             }
435           }
436           else
437           {
438             this.queryString = this.queryString + "&" + Object.keys(this.finalQueryParamsObj)[k] + "=" + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]];
439           }
440         }
441         else
442         {
443           this.queryString = this.queryString + "&" + Object.keys(this.finalQueryParamsObj)[k] + "=" + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]];
444         }
445         
446       }
447
448     }
449
450
451   }
452
453   showError(Errresponse : any)
454   {
455     this.errorMessage = Errresponse["errormessage"];
456     this.stackTrace = Errresponse["stacktrace"];
457     this.error = true;
458     this.showSpinner = false;
459   }
460
461   runReport()
462   {
463     this.hitCnt++;
464     this.showSpinner = true;
465
466
467     if(this.iSDashboardReport !== "Dashboard")
468     {
469
470       if(this.formFieldList.length > 0)
471       {
472         this.reportMode = "FormField";
473         this.generateQueryString();
474         }
475       else
476       {
477         this.reportMode = "Regular";
478       }
479
480       
481       this.showSpinner = false;
482    
483   }
484   else
485   {
486     this.generateQueryString();
487     
488     this.showSpinner = false;
489   }
490
491   this.navigateToRun = true;
492   }
493
494   editReport(reportId : string)
495   {
496     this._router.navigate(["v2/reports", "Edit", reportId]);
497   }
498
499   showLabelFn()
500   {
501     this.showLabel = !this.showLabel;
502   }
503
504   
505
506 }