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