e8a608493a3d95ccc7ddaf558fdffddbde053db6
[portal/sdk.git] /
1 import {Component, OnInit, Input, Inject} from '@angular/core';
2 import {FormFieldsService} from './form-fields.service';
3 import {MatDialog, MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
4 import {Observable} from 'rxjs';
5 import {MatSnackBar} from '@angular/material/snack-bar';
6
7
8 export interface DialogData {
9     name: string;
10 }
11
12 @Component({
13     selector: 'app-form-fields',
14     templateUrl: './form-fields.component.html',
15     styleUrls: ['./form-fields.component.css']
16 })
17 export class FormFieldsComponent implements OnInit {
18
19     @Input('reportId') reportId1: string;
20
21     formFieldsListObj: any[];
22     postMoveUpwrdObj: {};
23     postDownwardObj: {};
24     orderSeqOfDown: number;
25     orderSeqOfUp: number;
26     showDialog: boolean;
27     closable: boolean;
28     sqlAsDefaultValue: Boolean;
29     showDefaultSQLOption: boolean;
30     verifyFieldValueAs: number;
31     predefinedValueList: any[];
32     predefinedValue: string;
33     tabId: string;
34     tabName: string;
35     validationType: string;
36     visible: string;
37     orderSeq: number;
38     message: any;
39     groupFormField: boolean;
40     fieldType: string;
41     fieldSQL: string;
42     fieldName: string;
43     fieldId: string;
44     fieldDefaultSQL: string;
45     errorStackTrace: any;
46     errorMessage: any;
47     defaultValue: any;
48     finalPOSTObj: any;
49     mode: string;
50     showSpinner: boolean;
51     showDialog1: boolean;
52     queryObj: {};
53     validateResponseString: string;
54     showVerifySpinner: boolean;
55     Groups: {}[] = [];
56     isDefaultSql: boolean;
57     showConfirmButton: boolean;
58
59
60     constructor(private _formFieldsService: FormFieldsService, public dialog: MatDialog, private _snackBar: MatSnackBar) {
61         this.formFieldsListObj = [];
62         this.postMoveUpwrdObj = {};
63         this.postDownwardObj = {};
64         this.showDialog = false;
65         this.showDefaultSQLOption = false;
66         this.closable = true;
67         this.predefinedValueList = [];
68         this.finalPOSTObj = {};
69         this.showSpinner = false;
70         this.showDialog1 = false;
71         this.queryObj = {};
72         this.showVerifySpinner = false;
73         this.Groups = [];
74         this.isDefaultSql = false;
75     }
76
77     ngOnInit() {
78         this._formFieldsService.getListOfFormFields()
79             .subscribe((responseFormFields) => {
80                 for (let m = 0; m < responseFormFields.length; m++) {
81                     responseFormFields[m]['orderSeq'] = m + 1;
82                     this.formFieldsListObj.push(responseFormFields[m]);
83                 }
84
85                 this._formFieldsService.getFormFieldGroupsData(this.reportId1)
86                     .subscribe((responseGroupsData) => {
87                         this.Groups = JSON.parse(responseGroupsData['formFieldGroupsJSON']);
88                     });
89             });
90     }
91
92
93     isLast(n: number) {
94         if (n == this.formFieldsListObj.length - 1) {
95             return true;
96         } else {
97             return false;
98         }
99     }
100
101     isFirst(n: number) {
102         if (n == 0) {
103             return true;
104         } else {
105             return false;
106         }
107     }
108
109
110     moveUpward(n: number) {
111         this.showSpinner = true;
112
113         this._formFieldsService.getFormFieldData(this.formFieldsListObj[n]['id'])
114             .subscribe((responseGetFormFieldn) => {
115                 this.postMoveUpwrdObj = responseGetFormFieldn;
116
117                 this._formFieldsService.getFormFieldData(this.formFieldsListObj[n - 1]['id'])
118                     .subscribe((responseGetFormFieldnmin1) => {
119                         this.postDownwardObj = responseGetFormFieldnmin1;
120
121                         this.orderSeqOfDown = this.postMoveUpwrdObj['orderSeq'];
122                         this.orderSeqOfUp = this.postDownwardObj['orderSeq'];
123
124                         this.postMoveUpwrdObj['orderSeq'] = this.orderSeqOfUp;
125                         this.postDownwardObj['orderSeq'] = this.orderSeqOfDown;
126
127                         console.log(this.postDownwardObj);
128                         console.log(this.postMoveUpwrdObj);
129                         this.postMoveUpwrdObj['lastUpdatedFieldId'] = this.postDownwardObj['fieldId'];
130                         this.postDownwardObj['lastUpdatedFieldId'] = this.postMoveUpwrdObj['fieldId'];
131
132                         this._formFieldsService.postFormFieldData(this.postDownwardObj, this.reportId1)
133                             .subscribe((responsePostn) => {
134                                 if (responsePostn['message']) {
135                                     this._formFieldsService.postFormFieldData(this.postMoveUpwrdObj, this.reportId1)
136                                         .subscribe((responsePostnmin1) => {
137                                             if (responsePostnmin1['message']) {
138
139                                                 this.formFieldsListObj = [];
140                                                 this._formFieldsService.getListOfFormFields()
141                                                     .subscribe((finalListResponse) => {
142                                                         for (let k = 0; k < finalListResponse.length; k++) {
143                                                             //  this.formFieldsListObj.push(finalListResponse[k]);
144                                                             for (let m = 0; m < finalListResponse.length; m++) {
145                                                                 if (finalListResponse[m]['orderSeq'] == k + 1) {
146                                                                     this.formFieldsListObj.push(finalListResponse[m]);
147                                                                 }
148                                                             }
149                                                         }
150                                                         this.showSpinner = false;
151                                                     });
152                                             }
153                                         });
154                                 }
155                             });
156
157                     });
158             });
159
160
161     }
162
163     moveDownward(n: number) {
164         this.showSpinner = true;
165
166         this._formFieldsService.getFormFieldData(this.formFieldsListObj[n]['id'])
167             .subscribe((responseGetFormFieldn) => {
168                 this.postMoveUpwrdObj = responseGetFormFieldn;
169
170                 this._formFieldsService.getFormFieldData(this.formFieldsListObj[n + 1]['id'])
171                     .subscribe((responseGetFormFieldnmin1) => {
172                         this.postDownwardObj = responseGetFormFieldnmin1;
173
174                         this.orderSeqOfDown = this.postMoveUpwrdObj['orderSeq'];
175                         this.orderSeqOfUp = this.postDownwardObj['orderSeq'];
176
177                         this.postMoveUpwrdObj['orderSeq'] = this.orderSeqOfUp;
178                         this.postDownwardObj['orderSeq'] = this.orderSeqOfDown;
179
180                         console.log(this.postDownwardObj);
181                         console.log(this.postMoveUpwrdObj);
182
183                         this.postMoveUpwrdObj['lastUpdatedFieldId'] = this.postDownwardObj['fieldId'];
184                         this.postDownwardObj['lastUpdatedFieldId'] = this.postMoveUpwrdObj['fieldId'];
185
186                         this._formFieldsService.postFormFieldData(this.postDownwardObj, this.reportId1)
187                             .subscribe((responsePostn) => {
188                                 if (responsePostn['message']) {
189                                     this._formFieldsService.postFormFieldData(this.postMoveUpwrdObj, this.reportId1)
190                                         .subscribe((responsePostnmin1) => {
191                                             if (responsePostnmin1['message']) {
192
193                                                 this.formFieldsListObj = [];
194                                                 this._formFieldsService.getListOfFormFields()
195                                                     .subscribe((finalListResponse) => {
196                                                         for (let k = 0; k < finalListResponse.length; k++) {
197                                                             //  this.formFieldsListObj.push(finalListResponse[k]);
198                                                             for (let m = 0; m < finalListResponse.length; m++) {
199                                                                 if (finalListResponse[m]['orderSeq'] == k + 1) {
200                                                                     this.formFieldsListObj.push(finalListResponse[m]);
201                                                                 }
202                                                             }
203                                                         }
204                                                         this.showSpinner = false;
205                                                     });
206                                             }
207                                         });
208                                 }
209
210                             });
211
212                     });
213             });
214
215     }
216
217     ngDoCheck() {
218         if (this.sqlAsDefaultValue) {
219             this.showDefaultSQLOption = true;
220         } else {
221             this.showDefaultSQLOption = false;
222         }
223
224     }
225
226     setDisplayMode(displayMode: string) {
227     }
228
229     editGroup(group: any) {
230         this.openEditDialog(group);
231     }
232
233     edit(id: string) {
234         this._formFieldsService.getFormFieldData(id)
235             .subscribe((responseFormFieldData) => {
236                 this.validationType = responseFormFieldData['validationType'];
237
238                 if (responseFormFieldData['visible'] === true) {
239                     this.visible = 'YES';
240                 } else {
241                     this.visible = 'NO';
242                 }
243
244                 this.orderSeq = responseFormFieldData['orderSeq'];
245                 this.message = responseFormFieldData['message'];
246                 this.groupFormField = responseFormFieldData['groupFormField'];
247                 this.fieldType = responseFormFieldData['fieldType'];
248                 this.fieldSQL = responseFormFieldData['fieldSQL'];
249                 this.fieldName = responseFormFieldData['fieldName'];
250                 this.fieldId = responseFormFieldData['fieldId'];
251                 this.fieldDefaultSQL = responseFormFieldData['fieldDefaultSQL'];
252                 this.errorStackTrace = responseFormFieldData['errorStackTrace'];
253                 this.errorMessage = responseFormFieldData['errorMessage'];
254                 this.defaultValue = responseFormFieldData['defaultValue'];
255                 if (this.fieldDefaultSQL != null || this.fieldDefaultSQL != '') {
256                     this.isDefaultSql = true;
257                 } else {
258                     this.isDefaultSql = false;
259                 }
260
261                 if (this.defaultValue == null || this.defaultValue == '') {
262                     this.isDefaultSql = true;
263                 } else {
264                     this.isDefaultSql = false;
265                 }
266
267                 if ((this.fieldDefaultSQL == null || this.fieldDefaultSQL == '') && (this.defaultValue == null || this.defaultValue == '')) {
268                     this.isDefaultSql = false;
269                 }
270
271                 if (responseFormFieldData['predefinedValueList'] == null) {
272                     this.predefinedValueList = [];
273                 } else {
274                     this.predefinedValueList = responseFormFieldData['predefinedValueList'];
275                 }
276
277                 this.showDialog = true;
278                 this.closable = true;
279                 this.mode = 'Edit';
280             });
281
282     }
283
284     add() {
285         this.showDialog = true;
286         this.closable = true;
287         this.validationType = '';
288         this.visible = 'YES';
289         this.message = '';
290         this.groupFormField = false;
291         this.fieldType = '';
292         this.fieldSQL = '';
293         this.fieldName = '';
294         this.fieldId = '';
295         this.fieldDefaultSQL = '';
296         this.errorStackTrace = '';
297         this.errorMessage = '';
298         this.isDefaultSql = false;
299         this.predefinedValueList = [];
300         this.defaultValue = '';
301
302         this.showDialog = true;
303         this.closable = true;
304
305         this.mode = 'Add';
306
307     }
308
309     close() {
310         this.showDialog = !this.showDialog;
311         this.closable = false;
312
313     }
314
315     closeValidateModal() {
316         this.showDialog1 = !this.showDialog1;
317         this.showVerifySpinner = false;
318     }
319
320     addToList(attrib: string) {
321         this.predefinedValueList.push(
322             {
323                 'id': attrib,
324                 'name': attrib,
325                 'selected': false
326             });
327     }
328
329     deleteFromList(attrib: string) {
330
331         for (let k = 0; k < this.predefinedValueList.length; k++) {
332             if (this.predefinedValueList[k]['id'] == attrib) {
333                 this.predefinedValueList.splice(k, 1);
334             }
335         }
336     }
337
338
339     save() {
340         this.showSpinner = true;
341         this.showDialog = false;
342         this.finalPOSTObj['validationType'] = this.validationType;
343
344         if (this.visible == 'YES') {
345             this.finalPOSTObj['visible'] = true;
346         } else {
347             this.finalPOSTObj['visible'] = false;
348         }
349
350         this.finalPOSTObj['orderSeq'] = this.orderSeq;
351         this.finalPOSTObj['message'] = this.message;
352         this.finalPOSTObj['groupFormField'] = this.groupFormField;
353         this.finalPOSTObj['fieldType'] = this.fieldType;
354         this.finalPOSTObj['fieldSQL'] = this.fieldSQL;
355         this.finalPOSTObj['fieldName'] = this.fieldName;
356         this.finalPOSTObj['fieldId'] = this.fieldId;
357
358         this.finalPOSTObj['errorStackTrace'] = this.errorStackTrace;
359         this.finalPOSTObj['errorMessage'] = this.errorMessage;
360         this.finalPOSTObj['lastUpdatedFieldId'] = this.fieldId;
361
362         this.finalPOSTObj['fieldDefaultSQL'] = this.fieldDefaultSQL;
363
364         this.finalPOSTObj['defaultValue'] = this.defaultValue;
365
366         if (this.fieldDefaultSQL != '' && this.fieldDefaultSQL != null) {
367             this.finalPOSTObj['defaultValue'] = '';
368         }
369
370         this.finalPOSTObj['predefinedValueList'] = this.predefinedValueList;
371
372         if (this.mode == 'Edit') {
373             this._formFieldsService.postFormFieldData(this.finalPOSTObj, this.reportId1)
374                 .subscribe((responsePost) => {
375                     if (responsePost['message']) {
376                         this._formFieldsService.getListOfFormFields()
377                             .subscribe((responseFormFields) => {
378                                 this.formFieldsListObj = [];
379                                 let m = 0;
380                                 while (responseFormFields[m]) {
381                                     this.formFieldsListObj.push(responseFormFields[m]);
382                                     m++;
383                                 }
384
385
386                                 this.showSpinner = false;
387
388                             });
389
390                     }
391                 });
392         } else {
393             this._formFieldsService.addFormFieldData(this.finalPOSTObj, this.reportId1)
394                 .subscribe((responsePost) => {
395                     if (responsePost['message']) {
396                         this._formFieldsService.getListOfFormFields()
397                             .subscribe((responseFormFields) => {
398                                 this.formFieldsListObj = [];
399                                 let m = 0;
400                                 while (responseFormFields[m]) {
401                                     this.formFieldsListObj.push(responseFormFields[m]);
402                                     m++;
403                                 }
404
405                                 this.showSpinner = false;
406
407                             });
408                     }
409                 });
410         }
411     }
412
413     delete(id: string) {
414         this.showSpinner = true;
415         this._formFieldsService.deleteFormField(id)
416             .subscribe((responseDeleted) => {
417                 if (responseDeleted['message'] == 'Formfield  Deleted') {
418                     this._formFieldsService.getListOfFormFields()
419                         .subscribe((responseFormFields) => {
420                             this.formFieldsListObj = [];
421                             let m = 0;
422                             while (responseFormFields[m]) {
423                                 this.formFieldsListObj.push(responseFormFields[m]);
424                                 m++;
425                             }
426
427                             this.showSpinner = false;
428
429                         });
430                 }
431             });
432     }
433
434     verify(SQLType: string) {
435         this.showVerifySpinner = true;
436         this.showDialog1 = true;
437         if (SQLType == 'Default') {
438             this.queryObj = {
439                 query: this.fieldDefaultSQL
440             };
441             this._formFieldsService.verifySQL(this.queryObj)
442                 .subscribe((responseDefaultSQL) => {
443                     this.validateResponseString = responseDefaultSQL['data']['elements'];
444                     this.showVerifySpinner = false;
445                 });
446         } else {
447             this.queryObj = {
448                 query: this.fieldSQL
449             };
450             this._formFieldsService.verifySQL(this.queryObj)
451                 .subscribe((responseSQL) => {
452                     this.validateResponseString = responseSQL['data']['elements'];
453                     this.showVerifySpinner = false;
454                 });
455         }
456
457     }
458
459     deleteFromGroup(formFieldId: String) {
460         for (let k = 0; k < this.Groups.length; k++) {
461             let element = this.Groups[k];
462             for (let l = 0; l < element["formFieldList"].length; l++) {
463                 if (element['formFieldList'][l]['id'] == formFieldId) {
464                     this.Groups[k]['formFieldList'].splice(l, 1);
465                 }
466             }
467         }
468     }
469
470     deleteGroup(groupName: string) {
471         for (let k = 0; k < this.Groups.length; k++) {
472             if (this.Groups[k]['name'] == groupName) {
473                 this.Groups.splice(k, 1);
474             }
475         }
476     }
477
478     createGroup() {
479         this.openDialog();
480     }
481
482     openDialog() {
483         const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
484             width: '400px',
485             height: '600px',
486             data: this.formFieldsListObj,
487         });
488
489         dialogRef.afterClosed().subscribe(result => {
490
491             let groupAlreadyExists = false;
492             if (this.Groups == null || this.Groups == undefined) {
493                 this.Groups = [];
494             }
495             for (let k = 0; k < this.Groups.length; k++) {
496                 if (this.Groups[k]['name'] == result['name']) {
497                     groupAlreadyExists = true;
498                 }
499             }
500
501
502             if (!groupAlreadyExists) {
503                 let UpdatedResult = result;
504                 for (let i = 0; i < result["formFieldList"].length; i++) {
505                     let formFieldId = result['formFieldList'][i];
506                     let j = 0;
507                     while (this.formFieldsListObj[j]) {
508                         if (this.formFieldsListObj[j]['id'] == formFieldId) {
509                             UpdatedResult['formFieldList'][i] = this.formFieldsListObj[j];
510                         }
511                         j++;
512                     }
513
514                 }
515                 if (this.Groups !== null || this.Groups !== undefined) {
516                     this.Groups.push(UpdatedResult);
517                 } else {
518                     this.Groups = [];
519                     this.Groups.push(UpdatedResult);
520                 }
521
522             } else {
523                 this.openSnackBar('Group Already Exists.', 'Try using another name!');
524             }
525
526
527         });
528     }
529
530
531     openEditDialog(groupInfo: any) {
532         const multiSelectArray = [];
533         for (let arr = 0; arr < groupInfo['formFieldList'].length; arr++) {
534             multiSelectArray.push(groupInfo['formFieldList'][arr]['id']);
535         }
536         let editGroupInfo = {};
537         editGroupInfo['formFieldList'] = multiSelectArray;
538         editGroupInfo['name'] = groupInfo['name'];
539         const dialogRef = this.dialog.open(EditGroupDialog, {
540             width: '400px',
541             height: '600px',
542             data: {
543                 formFieldsListObj: this.formFieldsListObj,
544                 editGroupInfo: editGroupInfo
545             }
546         });
547
548         dialogRef.afterClosed().subscribe(result => {
549             for (let k = 0; k < this.Groups.length; k++) {
550                 if (this.Groups[k]['name'] == editGroupInfo['name']) {
551                     let UpdatedResult = editGroupInfo;
552                     for (let i = 0; i < editGroupInfo["formFieldList"].length; i++) {
553                         let formFieldId = editGroupInfo['formFieldList'][i];
554                         let j = 0;
555                         while (this.formFieldsListObj[j]) {
556                             if (this.formFieldsListObj[j]['id'] == formFieldId) {
557                                 UpdatedResult['formFieldList'][i] = this.formFieldsListObj[j];
558                             }
559                             j++;
560                         }
561
562                     }
563
564                     this.Groups[k]['formFieldList'] = UpdatedResult['formFieldList'];
565                 }
566             }
567         });
568     }
569
570
571     openSnackBar(message: string, action: string) {
572         this._snackBar.open(message, action, {
573             duration: 5000,
574         });
575     }
576
577     saveFormFieldGroups() {
578         let formFieldGroupsPostObj =
579             {
580                 reportId: this.reportId1,
581                 formFieldGroupsJSON: JSON.stringify(this.Groups)
582             };
583
584         this._formFieldsService.postFormFieldGroupsData(formFieldGroupsPostObj)
585             .subscribe((responsePost) => {
586                 this.openSnackBar('Successfully Saved!', '');
587             });
588     }
589
590 }
591
592 @Component({
593     selector: 'app-create-group',
594     templateUrl: './create-group.component.html',
595 })
596 export class DialogOverviewExampleDialog {
597     createGroupObj: any = {};
598
599     constructor(
600         public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
601         @Inject(MAT_DIALOG_DATA) public data: DialogData) {
602     }
603
604     onNoClick(): void {
605         this.dialogRef.close();
606     }
607
608 }
609
610
611 @Component({
612     selector: 'app-edit-group',
613     templateUrl: './edit-group.component.html',
614 })
615 export class EditGroupDialog {
616     @Input('mode') mode: string;
617     editGroupInfo: any = {};
618
619     constructor(
620         public dialogRef: MatDialogRef<EditGroupDialog>,
621         @Inject(MAT_DIALOG_DATA) public data: DialogData) {
622     }
623
624     onNoClick(): void {
625         this.dialogRef.close();
626     }
627
628 }
629