summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/services/properties.service.ts
blob: 638e537cd18770979910e8d39279040a69e1d23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { Injectable } from '@angular/core';
import { DataTypeModel, PropertyFEModel, PropertyBEModel, SchemaProperty, DerivedFEProperty, DerivedFEPropertyMap, DerivedPropertyType, InputFEModel} from "app/models";
import { DataTypeService } from "./data-type.service";
import { PROPERTY_TYPES } from "app/utils";
import { ContentAfterLastDotPipe } from "../pipes/contentAfterLastDot.pipe";
import { UUID } from "angular2-uuid";

@Injectable()
export class PropertiesService {

    constructor(private dataTypeService:DataTypeService, private contentAfterLastDotPipe:ContentAfterLastDotPipe) {
    }

    public getParentPropertyFEModelFromPath = (properties:Array<PropertyFEModel>, path:string) => {
        let parent:PropertyFEModel = _.find(properties, (property:PropertyFEModel):boolean=>{
            return property.name === path.substring(0, path.indexOf('#'));
        });
        return parent;
    }

    //undo disabling of parent and child props=
    public undoDisableRelatedProperties = (property: PropertyFEModel, childPath?: string): void => {
        property.isDisabled = false;
        if (!childPath) {
            property.isSelected = false;
            property.flattenedChildren && property.flattenedChildren.map(child => child.isDisabled = false);
        } else { //QND - unselect everything and then re-do the disabling of declared props. TODO: put a flag on propertyFEModel instead to indicate who's causing them to be disabled instead
            property.flattenedChildren.filter(child => child.isDisabled && !child.isDeclared).map(child => child.isDisabled = false);
            property.flattenedChildren.filter(child => child.isDeclared || child.isSelected).forEach((childProp) => { //handle brothers who are selected - redo their disabled relatives as well
                this.disableRelatedProperties(property, childProp.propertiesName);
            });
        }
    }

    //disable parents and children of prop
    public disableRelatedProperties = (property:PropertyFEModel, childPath?: string): void => {
        if (!childPath) { //selecting the parent property
            property.isSelected = true;
            property.flattenedChildren && property.flattenedChildren.map(child => { child.isSelected = false; child.isDisabled = true; });
        } else {
            property.isSelected = false;
            property.isDisabled = true;
            property.flattenedChildren.filter((childProp: DerivedFEProperty) => {
                return (childProp.propertiesName.indexOf(childPath + "#") > -1 //is child of prop to disable
                    || childPath.indexOf(childProp.propertiesName + "#") > -1); //is parent of prop to disable
            }).map((child: DerivedFEProperty) => { child.isSelected = false; child.isDisabled = true; });
        }
    }

    public getCheckedProperties = (properties:Array<PropertyFEModel>): Array<PropertyBEModel> => {
        let selectedProps: Array<PropertyBEModel> = [];
        properties.forEach(prop => {
            if (prop.isSelected && !prop.isDeclared && !prop.isDisabled) {
                selectedProps.push(new PropertyBEModel(prop));
            } else if(prop.flattenedChildren) {
                prop.flattenedChildren.forEach((child) => {
                    if (child.isSelected && !child.isDeclared && !child.isDisabled) {
                        let childProp = new PropertyBEModel(prop, child); //create it from the parent
                        selectedProps.push(childProp);
                    }
                })
            }
        });
        return selectedProps;
    }

    /**
     * Build hirarchy structure for the tree when user selects on table row.
     * First create Array<SimpleFlatProperty> and insert also the parent (PropertyFEModel) to this array.
     * The Array is flat and contains SimpleFlatProperty that has parentName and uniqueId.
     * Now we build hirarchy from this Array (that includes childrens) and return it for the tree
     *
     * @argument property: PropertyFEModel - property contains flattenedChildren array of DerivedFEProperty
     * @returns  Array<SimpleFlatProperty> - containing childrens Array<SimpleFlatProperty>, augmantin childrens to SimpleFlatProperty.
     */
    public getSimplePropertiesTree(property: PropertyFEModel, instanceName:string):Array<SimpleFlatProperty> {
        // Build Array of SimpleFlatProperty before unflatten function
        let flattenProperties:Array<SimpleFlatProperty> = [];
        flattenProperties.push(new SimpleFlatProperty(property.uniqueId, property.name, property.name, '', instanceName)); // Push the root property
        _.each(property.flattenedChildren, (child:DerivedFEProperty):void => {
            flattenProperties.push(new SimpleFlatProperty(child.uniqueId, child.propertiesName, child.name, child.parentName, instanceName));
        });

        let tree = this.unflatten(flattenProperties, '', []);
        return tree[0].childrens; // Return the childrens without the root.
    }

    /**
     * Unflatten Array<SimpleFlatProperty> and build hirarchy.
     * The result will be Array<SimpleFlatProperty> that augmantin with childrens for each SimpleFlatProperty.
     */
    private unflatten( array:Array<SimpleFlatProperty>, parent:any, tree?:any ):any {
        tree = typeof tree!=='undefined' ? tree : [];
        parent = typeof parent!=='undefined' && parent!=='' ? parent : { path: '' };

        var childrens = _.filter( array, (child:SimpleFlatProperty):boolean => {
            return child.parentName == parent.path;
        });

        if( !_.isEmpty( childrens )  ){
            if( parent.path == '' ){
                tree = childrens;
            } else {
                parent['childrens'] = childrens;
            }
            _.each( childrens, ( child ):void => {
                this.unflatten( array, child );
            });
        }
        return tree;
    }

    // TODO: To remove
    // public convertPropertiesToFEAndInitialize(properties: Array<PropertyBEModel>): Array<PropertyFEModel> {
    //     let props:Array<PropertyFEModel> = [];
    //     _.forEach(properties, (property: PropertyFEModel, index: number) => {
    //         props[index] = new PropertyFEModel(property);
    //         this.initValueObjectRef(props[index]);
    //         if (props[index].isDataType ||
    //             ((props[index].type === PROPERTY_TYPES.MAP || props[index].type === PROPERTY_TYPES.LIST) && props[index].schema.property.isDataType)) {
    //             props[index].valueObjectRef = props[index].valueObjectRef || {};
    //             let defaultValueObject:any = props[index].defaultValue ? JSON.parse(props[index].defaultValue): null;
    //             this.createPropertiesTreeForProp(props[index], true, defaultValueObject);
    //         }
    //     });
    //     return props;
    // }


    /**
     * Converts a property's map values to properties and adds them to property.childrenProperties
     * @param property - property to add children to
     * @param onlyFirstLevel - recursively retrieve properties for each dataType?
     */
    //TODO: To remove
    // public createPropertyNodesForMapOfDataTypes(property: PropertyFEModel, onlyFirstLevel:boolean):void {
    //     property.childrenProperties = [];
    //     angular.forEach(property.valueObjectRef,function(itemInMap:any, keyInMap:string){
    //         let newProperty: PropertyFEModel = new PropertyFEModel(keyInMap,
    //             property.schema.property.type,
    //             UUID.UUID(),
    //             property,
    //             property.valueObjectRef[keyInMap]);
    //         !onlyFirstLevel && this.createPropertiesTreeForProp(newProperty);
    //         property.childrenProperties.push(newProperty);
    //     },this);
    // }


    /**
     * Converts a property's list values to properties and adds them to property.childrenProperties
     * @param property - property to add children to
     * @param onlyFirstLevel - recursively retrieve properties for each dataType?
     */
    //TODO: To remove
    // public createPropertyNodesForListOfDataTypes(property: PropertyFEModel, onlyFirstLevel:boolean):void {
    //     property.childrenProperties = [];
    //     property.valueObjectRef.forEach((itemInList:any, index:number):void =>{
    //         let newProperty: PropertyFEModel = new PropertyFEModel(this.contentAfterLastDotPipe.transform(property.schema.property.type),
    //             property.schema.property.type,
    //             UUID.UUID(),
    //             property,
    //             property.valueObjectRef[index]);
    //         !onlyFirstLevel && this.createPropertiesTreeForProp(newProperty);
    //         property.childrenProperties.push(newProperty);
    //     });
    // }

    // private checkIfPropertyDerivedFromSimpleAndUpdateProp(property:PropertyFEModel | SchemaProperty): boolean{
    //     property.derivedFromSimpleTypeName = this.dataTypeService.getTypeForDataTypeDerivedFromSimple(property.type);
    //     if(property.derivedFromSimpleTypeName){
    //         property.isSimpleType = true;
    //         property.isDataType = false;
    //     }
    //     return property.isSimpleType;
    // }

    // TODO: Remove
    public createPropertiesTreeForProp(property: PropertyFEModel, onlyFirstLevel?: boolean, defaultValueObj?: any): void {
    }

    public getPropertyFEModelFromDerivedPropertyUniqueId(properties: Array<PropertyFEModel>, uniqueId: string): PropertyFEModel {
        // _.each(properties, (property):void => {
        //     property.flattenedChildren

        // });

        // let rootProperty = _.find(properties, (property):boolean => { return property.uniqueId === uniqueId; });
        // if
        return null;
    }

    /**
     * Utilizes the dataTypeService to retrieve children properties from dataTypes recursively.
     * @param property
     * @param onlyFirstLevel
     */
    // TODO: To remove
    // public createPropertiesTreeForProp(property: PropertyFEModel, onlyFirstLevel?:boolean, defaultValueObj?:any ):void{
    //     if (property.isDataType && !this.checkIfPropertyDerivedFromSimpleAndUpdateProp(property)){
    //         let dataType: DataTypeModel = this.dataTypeService.getDataTypeByTypeName(property.type);
    //         property.childrenProperties = [];
    //         let childrenProperties = this.dataTypeService.getDataTypePropertiesRecursively(dataType);
    //         childrenProperties.forEach((childProperty: PropertyBEModel):void => {
    //             let childDataTypePropertyObj: PropertyFEModel = new PropertyFEModel(childProperty.name, childProperty.type,UUID.UUID(),property, {}, childProperty.schema);
    //             //init empty object in valueObjectRef[childProperty.name] because this property's children should has ref to this obj.
    //             if(!property.valueObjectRef[childDataTypePropertyObj.name]){
    //                 if ((childDataTypePropertyObj.isDataType && !this.checkIfPropertyDerivedFromSimpleAndUpdateProp(childDataTypePropertyObj))
    //                     || childDataTypePropertyObj.type === PROPERTY_TYPES.MAP){
    //                     property.valueObjectRef[childDataTypePropertyObj.name] = {};
    //                 }else if(childDataTypePropertyObj.type === PROPERTY_TYPES.LIST){
    //                     property.valueObjectRef[childDataTypePropertyObj.name] = [];
    //                 }
    //             }
    //             childDataTypePropertyObj.valueObjectRef = property.valueObjectRef[childDataTypePropertyObj.name];
    //             property.valueObjectRef[childDataTypePropertyObj.name] =  property.valueObjectRef[childProperty.name] || childDataTypePropertyObj.defaultValue;
    //             if( !childDataTypePropertyObj.isDataType && defaultValueObj ){//init property default value
    //                 childDataTypePropertyObj.defaultValue = JSON.stringify(defaultValueObj[childDataTypePropertyObj.name]);
    //             }
    //             property.childrenProperties.push(childDataTypePropertyObj);
    //             !onlyFirstLevel && this.createPropertiesTreeForProp(childDataTypePropertyObj, false, (defaultValueObj ? defaultValueObj[childDataTypePropertyObj.name] : null));
    //         });
    //     } else if (property.type == PROPERTY_TYPES.MAP && property.schema.property.isDataType && !this.checkIfPropertyDerivedFromSimpleAndUpdateProp(property.schema.property)){
    //         if( property.valueObjectRef && !_.isEmpty(property.valueObjectRef)){
    //             this.createPropertyNodesForMapOfDataTypes(property, onlyFirstLevel);
    //         }
    //     } else if (property.type == PROPERTY_TYPES.LIST && property.schema.property.isDataType && !this.checkIfPropertyDerivedFromSimpleAndUpdateProp(property.schema.property)){
    //         if( property.valueObjectRef && property.valueObjectRef.length){
    //             this.createPropertyNodesForListOfDataTypes(property, onlyFirstLevel);
    //         }
    //     }
    // }
}

export class SimpleFlatProperty {
    uniqueId:string;
    path:string;
    name:string;
    parentName:string;
    instanceName:string;

    constructor(uniqueId?:string, path?:string, name?: string, parentName?:string, instanceName?:string) {
        this.uniqueId = uniqueId;
        this.path = path;
        this.name = name;
        this.parentName = parentName;
        this.instanceName = instanceName;
    }
}