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