3a38eba4f42120816ae7cfc964871572003508e9
[portal/sdk.git] /
1 import { Component, OnInit, Input, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
2 import { GridsterConfig, GridType, GridsterItem } from 'angular-gridster2';
3 import { MatTableDataSource } from '@angular/material/table';
4 import { MatPaginator } from '@angular/material/paginator';
5 import { DashboardReportService } from './dashboard-report.service';
6 import { MatSort } from '@angular/material';
7 import { Router } from '@angular/router';
8 import { environment } from '../../../../../../../../environments/environment';
9 import { DisplayHtml } from '../../../display-html';
10 import { RunService } from '../../run.service';
11 import {FormControl} from '@angular/forms';
12
13
14
15 export interface PeriodicElement {
16
17 }
18
19 const ELEMENT_DATA: PeriodicElement[] = [{}];
20
21 @Component({
22     selector: 'app-run-dashboard-report',
23     templateUrl: './run-dashboard-report.component.html',
24     styleUrls: ['./run-dashboard-report.component.css']
25 })
26
27 export class RunDashboardReportComponent implements AfterViewInit {
28
29     @Input('reportId') inputReportId: string;
30     @Input('queryString') queryString: string;
31     @Input('hitCnt') hitCnt: number;
32     @Input('reportType') reportType: string;
33     @Input('parentId') parentId: string;
34     dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
35     @ViewChild(MatPaginator, { static: false } as any) paginator: MatPaginator;
36     @ViewChild(MatSort, { static: false } as any) sort: MatSort;
37     @ViewChild('iframe') iframe: ElementRef;
38     options: GridsterConfig;
39     dashboard: Array<GridsterItem> = new Array();
40     displayedColumns: string[];
41     IncomingReportId: string;
42     displayedColumnsArr: string[];
43     displayedRowObj: {}[];
44     formFieldPresent: boolean;
45     showSpinner: boolean;
46     formFieldList: {}[];
47     isReady: boolean;
48     responseFormFieldListLength: number;
49     NEWdisplayedColumns: string[];
50     initCnt: number;
51     reportName: string;
52     showDashboardReport: boolean;
53     checkCnt: number;
54     initialQueryString: string;
55     initCounter: number;
56     runButtonHitCounter: number;
57     showChart = false;
58     displayColumValue: string;
59     displayTotal: any[];
60     private chartRunUrl: string;
61     replaceDisplayValue: String;
62 saveResponseObj:any;
63
64     constructor(private _dashboardReportService: DashboardReportService, private _router: Router,
65         private _runService: RunService) {
66         this.initCounter = 0;
67         this.runButtonHitCounter = 0;
68     }
69
70     ngOnInit() {
71         this.initialQueryString = this.queryString;
72         this.initCounter++;
73         this.runButtonHitCounter = this.hitCnt;
74         if (this.reportType === 'Chart') {
75             this.showChart = true;
76         }
77
78         this.initialProcesses();
79     }
80
81     initialProcesses() {
82         this.dataSource.paginator = this.paginator;
83     }
84
85     ngOnChanges() {
86         if (this.initialQueryString !== this.queryString && this.initCounter > 0 && this.runButtonHitCounter !== this.hitCnt) {
87             this.initialQueryString = this.queryString;
88             this.runButtonHitCounter = this.hitCnt;
89             this.initialProcesses();
90             this.afterViewInitProcesses();
91         } else {
92             this.runButtonHitCounter = this.hitCnt;
93             this.initialQueryString = this.queryString;
94         }
95     }
96
97     ngAfterViewInit() {
98         this.afterViewInitProcesses();
99     }
100
101     afterViewInitProcesses() {
102         if (this.showChart) {
103             this.chartRunUrl = environment.baseUrl + 'raptor.htm?action=chart.run&c_master=' +
104                 this.inputReportId + this.queryString + '&refresh=Y&display_content=Y&r_page=0';
105             this.iframe.nativeElement.setAttribute('src', this.chartRunUrl);
106             this.showSpinner = false;
107         } else {
108
109             this.displayedColumnsArr = new Array();
110             this.displayedRowObj = new Array();
111             this.displayedColumns = new Array();
112             this.formFieldList = new Array();
113             this.showSpinner = true;
114             this.isReady = false;
115             this.NEWdisplayedColumns = new Array();
116             this.displayTotal = [];
117             if (localStorage.getItem(this.inputReportId)) {
118                 this.postFetchingReportDataFn(JSON.parse(localStorage.getItem(this.inputReportId)));
119                 localStorage.removeItem(this.inputReportId);
120             } else {
121             this._dashboardReportService.getReportDataWithFormFields(this.queryString, this.inputReportId)
122                 .subscribe((response) => {
123                         this.postFetchingReportDataFn(response);
124                     });
125             }
126         }
127     }
128
129     postFetchingReportDataFn(response: any){
130         this.saveResponseObj = response;
131                     this.formFieldPresent = false;
132                     this.responseFormFieldListLength = 0;
133                     this.reportName = response['reportName'];
134                     let columnCntr = 0;
135                     while (response['reportDataColumns'][columnCntr]) {
136                         this.displayedColumnsArr.push(response['reportDataColumns'][columnCntr]['columnTitle'] + ','
137                             + response['reportDataColumns'][columnCntr]['colId']);
138                         columnCntr++;
139                     }
140                     let totalCnt = 0;
141                     while (response['reportTotalDataRows'][totalCnt]) {
142                         this.displayTotal.push(response['reportTotalDataRows'][totalCnt]);
143                         totalCnt++;
144                     }
145                     let rdr_cntr = 0;
146                     while (response['reportDataRows'][rdr_cntr]) {
147                         let dca_cntr = 0;
148                         const obj = {};
149                         const reportDataRows = response['reportDataRows'][rdr_cntr];
150                         while (this.displayedColumnsArr[dca_cntr]) {
151                             const rowColumnId = this.displayedColumnsArr[dca_cntr].split(',')[1];
152                             if (reportDataRows[rowColumnId]) {
153                                 let drillDownHtml = '';
154                                 let displayValue = '';
155                                 drillDownHtml = reportDataRows[rowColumnId]['drillDownURL'];
156                                 displayValue = reportDataRows[rowColumnId]['displayValue'];
157                                 if (drillDownHtml !== null &&
158                                     drillDownHtml.length > 0 &&
159                                     !displayValue.includes('linkToReport')) {
160                                     const value = this.convertToLinkToReport(drillDownHtml);
161                                     if (value.length > 0) {
162                                         this.replaceDisplayValue = value + ',' +
163                                             reportDataRows[rowColumnId]['displayValue'];
164                                     } else {
165                                         this.replaceDisplayValue = reportDataRows[rowColumnId]['displayValue'];
166                                     }
167                                 } else {
168                                     this.replaceDisplayValue = reportDataRows[rowColumnId]['displayValue'];
169                                 }
170                                 let displayObj: DisplayHtml = new class implements DisplayHtml {
171                                     'background-color': string;
172                                     'font-family': string;
173                                     'font-size': string;
174                                     'font-style': string;
175                                     'font-weight': string;
176                                     'text-align': string;
177                                     'text-decoration': string;
178                                     color: string;
179                                 };
180                                 if (reportDataRows[rowColumnId]['displayValueHtml'].includes('{')) {
181                                     displayObj = JSON.parse(reportDataRows[rowColumnId]['displayValueHtml']);
182                                 }
183                                 displayObj['text-align'] = reportDataRows[rowColumnId]['alignment'];
184                                 if (this.replaceDisplayValue.includes('linkToReport') || this.replaceDisplayValue.includes('linkToFeedback') || this.replaceDisplayValue.includes('linkToMail')) {
185                                     obj[reportDataRows[rowColumnId]['colId']] = this.replaceDisplayValue.split(',').join('|')
186                                         + '|' + JSON.stringify(displayObj);
187                                 } else {
188                                     obj[reportDataRows[rowColumnId]['colId']] = this.replaceDisplayValue
189                                         + '|' + JSON.stringify(displayObj);
190                                 }
191                             }
192                             dca_cntr++;
193                         }
194                         this.displayedRowObj.push(obj);
195                         rdr_cntr++;
196                     }
197                     for (let pushCounter = 0; pushCounter < this.displayedColumnsArr.length; pushCounter++) {
198                         this.displayedColumns.push(this.displayedColumnsArr[pushCounter].split(',')[1]);
199                     }
200                     this.showSpinner = false;
201                     this.dataSource = new MatTableDataSource<PeriodicElement>(this.displayedRowObj);
202                     this.dataSource.sort = this.sort;
203                     this.dataSource.paginator = this.paginator;
204     }
205
206     linkToReport(reportID: string, queryParameters: string) {
207         localStorage.setItem(this.inputReportId, JSON.stringify(this.saveResponseObj));
208         if (sessionStorage.length === 0) {
209             sessionStorage.setItem('1', this.parentId + '|' + this.queryString);
210         } else {
211             let length = sessionStorage.length;
212             length++;
213             sessionStorage.setItem(length.toString(), this.parentId + '|' + this.queryString);
214         }
215         this._router.navigate(['v2/run', reportID, queryParameters]);
216     }
217
218     linkToFeedback(feedBackId: string, queryParameters: string) {
219         this._router.navigate(['v2/feedback', feedBackId]);
220     }
221
222     linkToMail(mailId: string) {
223         const email = 'mailto:' + mailId;
224         window.location.href = email;
225     }
226
227     applyFilter(filterValue: string) {
228         this.dataSource.filter = filterValue.trim().toLowerCase();
229     }
230
231     setStyle(styles: string) {
232         if (styles.includes('{')) {
233             return JSON.parse(styles);
234         } else {
235             return {};
236         }
237     }
238
239     getDisplayTotal(keys: any) {
240         if (this.displayTotal.length > 0) {
241             return this.displayTotal[0][keys].displayValue;
242         } else {
243             return '';
244         }
245     }
246
247     convertToLinkToReport(value: string) {
248         value = value.replace(/;/g, '');
249         let outPut = '';
250         while (value.includes('c_master=')) {
251             const index = value.indexOf('c_master=');
252             if (index > 0) {
253                 value = value.substring(index, value.length);
254             } else if (index === 0) {
255                 value = value.replace('c_master=', '');
256             }
257         }
258         const split = value.split('&');
259         // const spltFirst = split[0].split('=');
260         if (split[1].length <= 0) {
261             return outPut;
262         }
263         outPut = 'linkToReport,' + split[0] + ',';
264         let splitCounter = 1;
265         for (splitCounter = 1; splitCounter < split.length; splitCounter++) {
266             if (!split[splitCounter].includes('LOGIN_ID=') &&
267                 !split[splitCounter].includes('display_content=') &&
268                 !split[splitCounter].includes('drilldown_index=') &&
269                 !split[splitCounter].includes('show_back_btn=') &&
270                 !split[splitCounter].includes('r_action')) {
271                 outPut = outPut + '&' + split[splitCounter];
272             }
273         }
274         return outPut;
275     }
276
277 }