import { Injectable } from '@angular/core'; import { DropdownField } from '../../app/form/fields/dropdown/field-dropdown'; import { FieldBase } from '../../app/form/fields/field-base'; import { TextboxField } from '../../app/form/fields/textbox/field-textbox'; import { CheckBoxField } from '../../app/form/fields/checkbox/field-checkbox'; import { RadioBoxField } from '../../app/form/fields/radiobox/field-radiobox'; import { DataModel } from '../entities/DataModel'; import {Rule} from '../entities/common/rule'; import { GroupBase } from '../form/dynamic-form-group/group-base'; import { Attribute } from '../entities/model/attribute'; import { Param } from '../entities/model/param'; import { Section } from '../entities/model/section'; @Injectable() export class dataModelBuilder { private dataModel : DataModel; private fields : FieldBase[]; public getDataModel(data){ if(this.dataModel != null) return this.dataModel; this.dataModel = new DataModel(); this.fields = this.buildFields(data.dataset.profile.viewstyle.definition.root.fields.field); this.dataModel.groups = this.getGroups(data.dataset.profile.viewstyle.definition.root.fieldGroups.fieldGroup, this.fields); this.dataModel.semanticAttr = new Array(new Attribute); //this.dataModel.semanticAttr = data.dataset.profile.definition.root.fields.field; this.dataModel.semanticAttr = this.getFieldsAttributes(data.dataset.profile.definition.root.fields.field, this.fields) ; this.dataModel.sections = this.getSections(data.dataset.profile.viewstyle.definition.root.sections.section, this.dataModel.groups) ; this.dataModel.buildIndex(); return this.dataModel; } private buildFields(fields:any[]) { let fieldsVisible :FieldBase[] =[]; fields.forEach(element => { if (element.viewStyle._renderstyle == "freetext"){ let newfield:FieldBase ; let rule = new Rule(); newfield = new TextboxField({ label: element.title.__cdata, key:element._id, value: element.value, required: true, order:element._ordinal, rules: element.visible.rule != undefined ? element.visible.rule: rule, visible: element._defaultVisibility, group : element._group, description: element.description.__cdata }); fieldsVisible.push(newfield); } else if (element.viewStyle._renderstyle == "dropdown" ||element.viewStyle._renderstyle == "combobox"){//to choose one of these in xml let newfield:DropdownField; let rule = new Rule(); newfield = new DropdownField({ label: element.title.__cdata, key:element._id, value: element.value, required: true, order:element._ordinal, rules: element.visible.rule != undefined ? element.visible.rule: rule, visible: element._defaultVisibility, description: element.description.__cdata, group : element._group }); fieldsVisible.push(newfield); } else if (element.viewStyle._renderstyle == "checkbox"||element.viewStyle._renderstyle == "checkBox"){ let newfield:CheckBoxField; let rule = new Array(); newfield = new CheckBoxField({ label: element.title.__cdata, key:element._id, value: true, required: true, order:element._ordinal, rules: element.visible.rule != undefined ? element.visible.rule: rule, visible: element._defaultVisibility, group : element._group, description: element.description.__cdata, type: "checkbox" }); fieldsVisible.push(newfield); } else if (element.viewStyle._renderstyle == "booleanDesicion"){ let newfield:RadioBoxField; let rule = new Array(); newfield = new RadioBoxField({ label: element.title.__cdata, key:element._id, value: true, required: true, order:element._ordinal, rules: element.visible.rule != undefined ? element.visible.rule: rule, visible: element._defaultVisibility, group : element._group, type: "radio", description: element.description.__cdata, answers: [ { id: 1, answer: "Yes", value: true }, { id: 2, answer: "No", value:false }] }); fieldsVisible.push(newfield); } }); fieldsVisible.sort((a, b) => a.order - b.order); return fieldsVisible; } private getGroups(fieldGroups:any, fields:any[]){ let groups :GroupBase[] =[]; if(fieldGroups.length>1){ fieldGroups.forEach(fieldGroup =>{ // each fiedgroup fills with its fields from json let newfldGroup = new GroupBase(); newfldGroup.groupFields = new Array(); fields.forEach(field => { if(fieldGroup._id == field.group){ newfldGroup.groupFields.push(field); }else{ //this.dataModel.fields.push(field); } }); newfldGroup.title = fieldGroup.title.__cdata; newfldGroup.key = fieldGroup._id; newfldGroup.section = fieldGroup._section; groups.push(newfldGroup) }); } else{ let newfldGroup = new GroupBase(); newfldGroup.groupFields = new Array(); fields.forEach(field => { //for one fieldgroup, because xml to json transformation doesn't create array of one fieldfroup if(fieldGroups._id == field.group){ newfldGroup.groupFields.push(field); }else this.dataModel.fields.push(field); }); newfldGroup.title = fieldGroups.title.__cdata; newfldGroup.key = fieldGroups._id; newfldGroup.section = fieldGroups._section; groups.push(newfldGroup) } return groups; } private getDummyGroups(){ let groups :GroupBase[] =[]; let group: GroupBase; group = new GroupBase({ key: "adfgadfsg", title: "GroupA", rules: new Array(), groupFields: new Array(), value: "value", visible: true, order: 1 //, //controlType: "none" }); let newfield1:FieldBase ; newfield1 = new TextboxField({ key:"tf1-455", label: "Field from group1", value: 'this is a groupfield', required: true, ordinal:1 }); group.groupFields.push(newfield1); let newfield2:FieldBase ; newfield2 = new TextboxField({ key:"tf1-443", label: "Another field from group1", value: 'this is a groupfield', required: true, ordinal:2 }); group.groupFields.push(newfield2); groups.push(group); return groups; } private getFieldsAttributes(attributes:any, fields:any[]){ let attribute:Attribute[]=[]; attributes.forEach(attr => { let newAttribute = new Attribute(); newAttribute.datatype = attr._datatype; newAttribute.defaultValue = attr.defaultValue._value; newAttribute.id = attr._id; newAttribute.multiplicityMax = attr.multiplicity._max; newAttribute.multiplicityMin = attr.multiplicity._min; newAttribute.ordinal = attr._ordinal; newAttribute.sources = new Array(); newAttribute.sources.push(attr.sources.source); newAttribute.sources.forEach(src => { src.params=new Array(); for (var i=0, len=attr.sources.source.value.length; i x.key == newAttribute.id).attributes.sources = newAttribute.sources; }); return attribute; } private getSections(sections:any, fieldGroups:GroupBase[]){ let sects: Section[]= []; if(sections.length){ sections.forEach(section => { let newSection = new Section(); newSection.defaultVisibility = section.defaultVisibility; newSection.description = section.description; newSection.id = section._id; newSection.title = section.title; newSection.ordinal = section._ordinal; newSection.groupFields = new Array(); fieldGroups.forEach(fldgroup => { if(fldgroup.section == newSection.id) newSection.groupFields.push(fldgroup); }) sects.push(newSection); }); } sects.sort((a, b) => a.ordinal - b.ordinal); return sects; } }