Fix bugs in attribute outputs page
[sdc.git] / catalog-ui / src / app / models / data-types.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by rcohen on 9/25/2016.
23  */
24 'use strict';
25 import {PropertyBEModel} from "./properties-inputs/property-be-model";
26 import {AttributeBEModel} from "./attributes-outputs/attribute-be-model";
27 import {Model} from "./model";
28 import {PROPERTY_DATA} from "../utils/constants";
29
30 export class DataTypeModel {
31
32     name: string;
33     uniqueId: string;
34     derivedFromName: string;
35     derivedFrom: DataTypeModel;
36     description: string;
37     creationTime: string;
38     modificationTime: string;
39     properties: Array<PropertyBEModel>;
40     attributes: Array<AttributeBEModel>;
41     model: Model;
42     normative: boolean;
43
44     constructor(dataType?: DataTypeModel) {
45         if (!dataType) {
46             return;
47         }
48
49         this.uniqueId = dataType.uniqueId;
50         this.name = dataType.name;
51         this.description = dataType.description;
52         this.derivedFromName = dataType.derivedFromName;
53         if (dataType.derivedFrom) {
54             this.derivedFrom = new DataTypeModel(dataType.derivedFrom);
55         }
56         this.creationTime = dataType.creationTime;
57         this.modificationTime = dataType.modificationTime;
58         if (dataType.properties) {
59             this.properties = [];
60             dataType.properties.forEach(property => {
61                 this.properties.push(new PropertyBEModel(property));
62             });
63         }
64         this.attributes = dataType.attributes;
65         this.model = dataType.model;
66         this.normative = dataType.normative;
67     }
68
69     public toJSON = ():any => {
70
71         return this;
72     };
73
74     /**
75      * Parses the default value to JSON.
76      */
77     public parseDefaultValueToJson(): any {
78         if (PROPERTY_DATA.TYPES.indexOf(this.name) > -1) {
79             return undefined;
80         }
81         const defaultValue = {};
82         if (this.properties) {
83             this.properties.forEach(property => {
84                 const propertyDefaultValue = property.parseDefaultValueToJson();
85                 if (propertyDefaultValue != undefined) {
86                     defaultValue[property.name] = propertyDefaultValue;
87                 }
88             });
89         }
90
91         return defaultValue === {} ? undefined : defaultValue;
92     }
93 }
94