1 import { Injectable } from '@angular/core';
2 import { DataTypeModel, PropertyFEModel, PropertyBEModel, InstanceBePropertiesMap, InstanceFePropertiesMap, SchemaProperty, DerivedFEProperty, DerivedFEPropertyMap, DerivedPropertyType, InputFEModel} from "app/models";
3 import { DataTypeService } from "app/ng2/services/data-type.service";
4 import { PropertiesService } from "app/ng2/services/properties.service";
5 import { PROPERTY_TYPES } from "app/utils";
6 import { UUID } from "angular2-uuid";
9 export class PropertiesUtils {
11 constructor(private dataTypeService:DataTypeService, private propertiesService: PropertiesService) {}
14 * Entry point when getting properties from server
15 * For each instance, loop through each property, and:
16 * 1. Create flattened children
17 * 2. Check against inputs to see if any props are declared and disable them
18 * 3. Initialize valueObj (which also creates any new list/map flattened children as needed)
19 * Returns InstanceFePropertiesMap
21 public convertPropertiesMapToFEAndCreateChildren = (instancePropertiesMap:InstanceBePropertiesMap, inputs:Array<InputFEModel>): InstanceFePropertiesMap => {
22 let instanceFePropertiesMap:InstanceFePropertiesMap = new InstanceFePropertiesMap();
23 angular.forEach(instancePropertiesMap, (properties:Array<PropertyBEModel>, instanceName:string) => {
24 let instanceInputs: Array<InputFEModel> = inputs.filter(input => input.instanceName == instanceName.split('.').pop());
25 let propertyFeArray: Array<PropertyFEModel> = [];
26 _.forEach(properties, (property: PropertyBEModel) => {
28 if (!this.dataTypeService.getDataTypeByTypeName(property.type)) { // if type not exist in data types remove property from list
29 console.log("ERROR: missing type " + property.type + " in dataTypes , of property ", property);
32 let newFEProp: PropertyFEModel = new PropertyFEModel(property); //Convert property to FE
34 if (newFEProp.derivedDataType == DerivedPropertyType.COMPLEX) { //Create children if prop is not simple, list, or map.
35 newFEProp.flattenedChildren = this.createFlattenedChildren(newFEProp.type, newFEProp.name);
37 if (instanceInputs.length) { //if this prop (or any children) are declared, set isDeclared and disable checkbox on parents/children
38 instanceInputs.filter(input => input.propertyName == newFEProp.name).forEach((input) => {
39 newFEProp.setAsDeclared(input.inputPath); //if a path was sent, its a child prop. this param is optional
40 this.propertiesService.disableRelatedProperties(newFEProp, input.inputPath);
43 this.initValueObjectRef(newFEProp); //initialize valueObj.
44 propertyFeArray.push(newFEProp);
45 newFEProp.updateExpandedChildPropertyId(newFEProp.name); //display only the first level of children
48 instanceFePropertiesMap[instanceName] = propertyFeArray;
51 return instanceFePropertiesMap;
53 private createListOrMapChildrenFromValueObj = (property: PropertyFEModel) => {
54 if ((property.derivedDataType == DerivedPropertyType.LIST || property.derivedDataType == DerivedPropertyType.MAP)
55 && Object.keys(property.valueObj).length) {
57 Object.keys(property.valueObj).forEach((key) => {
58 let newProps: Array<DerivedFEProperty> = this.createListOrMapChildren(property, key, property.valueObj[key]);
59 property.flattenedChildren.push(...newProps);
65 public createListOrMapChildren = (property:PropertyBEModel, key: string, valueObj: any): Array<DerivedFEProperty> => {
66 let newProps: Array<DerivedFEProperty> = [];
67 let parentProp = new DerivedFEProperty(property, property.propertiesName, true, key, valueObj);
68 newProps.push(parentProp);
70 if (!property.schema.property.isSimpleType) {
71 let additionalChildren:Array<DerivedFEProperty> = this.createFlattenedChildren(property.schema.property.type, parentProp.propertiesName);
72 this.assignFlattenedChildrenValues(parentProp.valueObj, additionalChildren, parentProp.propertiesName);
73 additionalChildren.forEach(prop => prop.canBeDeclared = false);
74 newProps.push(...additionalChildren);
80 * Creates derivedFEProperties of a specified type and returns them.
82 private createFlattenedChildren = (type: string, parentName: string):Array<DerivedFEProperty> => {
83 let tempProps: Array<DerivedFEProperty> = [];
84 let dataTypeObj: DataTypeModel = this.dataTypeService.getDataTypeByTypeName(type);
85 this.dataTypeService.getDerivedDataTypeProperties(dataTypeObj, tempProps, parentName);
89 /* Sets the valueObj of parent property and its children.
90 * Note: This logic is different than assignflattenedchildrenvalues - here we merge values, there we pick either the parents value, props value, or default value - without merging.
92 public initValueObjectRef = (property: PropertyFEModel): void => {
93 if (property.derivedDataType == DerivedPropertyType.SIMPLE || property.isDeclared) { //if property is declared, it gets a simple input instead. List and map values and pseudo-children will be handled in property component
94 property.valueObj = property.value || property.defaultValue;
96 if (property.isDeclared && typeof property.valueObj == 'object') property.valueObj = JSON.stringify(property.valueObj);
98 if (property.derivedDataType == DerivedPropertyType.LIST) {
99 property.valueObj = _.merge([], JSON.parse(property.defaultValue || '[]'), JSON.parse(property.value || '[]')); //value object should be merged value and default value. Value takes higher precendence. Set valueObj to empty obj if undefined.
101 property.valueObj = _.merge({}, JSON.parse(property.defaultValue || '{}'), JSON.parse(property.value || '{}')); //value object should be merged value and default value. Value takes higher precendence. Set valueObj to empty obj if undefined.
103 if (property.derivedDataType == DerivedPropertyType.COMPLEX) {
104 this.assignFlattenedChildrenValues(property.valueObj, property.flattenedChildren, property.name);
106 this.createListOrMapChildrenFromValueObj(property);
112 * Loops through flattened properties array and to assign values
113 * Then, convert any neccessary strings to objects, and vis-versa
114 * For list or map property, creates new children props if valueObj has values
116 public assignFlattenedChildrenValues = (parentValueJSON: any, derivedPropArray: Array<DerivedFEProperty>, parentName: string) => {
117 if (!derivedPropArray || !parentName) return;
118 derivedPropArray.forEach((prop, index) => {
120 let propNameInObj = prop.propertiesName.substring(prop.propertiesName.indexOf(parentName) + parentName.length + 1).split('#').join('.'); //extract everything after parent name
121 prop.valueObj = _.get(parentValueJSON, propNameInObj, prop.value || prop.defaultValue); //assign value -first value of parent if exists. If not, prop.value if not, prop.defaultvalue
123 if ((prop.derivedDataType == DerivedPropertyType.SIMPLE || prop.isDeclared) && typeof prop.valueObj == 'object') { //Stringify objects that should be strings
124 prop.valueObj = JSON.stringify(prop.valueObj);
125 } else { //parse strings that should be objects
126 if ((prop.derivedDataType == DerivedPropertyType.COMPLEX || prop.derivedDataType == DerivedPropertyType.MAP) && typeof prop.valueObj != 'object') {
127 prop.valueObj = JSON.parse(prop.valueObj || '{}');
128 } else if (prop.derivedDataType == DerivedPropertyType.LIST && typeof prop.valueObj != 'object') {
129 prop.valueObj = JSON.parse(prop.valueObj || '[]');
131 if ((prop.derivedDataType == DerivedPropertyType.LIST || prop.derivedDataType == DerivedPropertyType.MAP) && Object.keys(prop.valueObj).length) {
132 let newProps: Array<DerivedFEProperty> = [];
133 Object.keys(prop.valueObj).forEach((key) => {
134 newProps.push(...this.createListOrMapChildren(prop, key, prop.valueObj[key]));//create new children, assign their values, and then add to array
136 derivedPropArray.splice(index + 1, 0, ...newProps);
142 public resetPropertyValue = (property: PropertyFEModel, newValue: string, inputPath?: string): void => {
143 property.value = newValue;
145 let newProp = property.flattenedChildren.find(prop => prop.propertiesName == inputPath);
146 newProp && this.assignFlattenedChildrenValues(JSON.parse(newValue), [newProp], property.name);
148 this.initValueObjectRef(property);