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