[sdc] rebase update
[sdc.git] / catalog-ui / src / app / models / capability.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 obarda on 4/20/2016.
23  */
24 'use strict';
25 import {PropertyModel} from "./properties";
26
27 //this is an object contains keys, when each key has matching array.
28 // for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects
29 export class CapabilitiesGroup {
30     constructor(capabilityGroupObj?:CapabilitiesGroup) {
31         _.forEach(capabilityGroupObj, (capabilitiesArrayObj:Array<Capability>, instance) => {
32             this[instance] = [];
33             _.forEach(capabilitiesArrayObj, (capability:Capability):void => {
34                 this[instance].push(new Capability(capability));
35             });
36         });
37     }
38
39     public findValueByKey(keySubstring:string):Array<Capability> {
40         let key:string = _.find(Object.keys(this), (key)=> {
41             return _.includes(key.toLowerCase(), keySubstring);
42         });
43         return this[key];
44     }
45 }
46
47 export class Capability {
48
49     //server data
50     name:string;
51     ownerId:string;
52     ownerName:string;
53     type:string;
54     uniqueId:string;
55     capabilitySources:Array<String>;
56     minOccurrences:string;
57     maxOccurrences:string;
58     properties:Array<PropertyModel>;
59     description:string;
60     validSourceTypes:Array<string>;
61     //custom
62     selected:boolean;
63     filterTerm:string;
64
65     constructor(capability?:Capability) {
66
67         if (capability) {
68             //server data
69             this.name = capability.name;
70             this.ownerId = capability.ownerId;
71             this.ownerName = capability.ownerName;
72             this.type = capability.type;
73             this.uniqueId = capability.uniqueId;
74             this.capabilitySources = capability.capabilitySources;
75             this.minOccurrences = capability.minOccurrences;
76             this.maxOccurrences = capability.maxOccurrences;
77             this.properties = capability.properties;
78             this.description = capability.description;
79             this.validSourceTypes = capability.validSourceTypes;
80             this.selected = capability.selected;
81             this.initFilterTerm();
82
83         }
84     }
85
86     public getFullTitle():string {
87         let maxOccurrences:string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
88         return this.ownerName + ': ' + this.name + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
89     }
90
91     public toJSON = ():any => {
92         this.selected = undefined;
93         this.filterTerm = undefined;
94         return this;
95     };
96
97     private initFilterTerm = ():void => {
98         this.filterTerm = this.name + " " +
99             (this.type ? (this.type.replace("tosca.capabilities.", "") + " " ) : "") +
100             (this.description || "") + " " +
101             (this.ownerName || "") + " " +
102             (this.validSourceTypes ? (this.validSourceTypes.join(',') + " ") : "") +
103             this.minOccurrences + "," + this.maxOccurrences;
104         if (this.properties && this.properties.length) {
105             _.forEach(this.properties, (prop:PropertyModel)=> {
106                 this.filterTerm += " " + prop.name +
107                     " " + (prop.description || "") +
108                     " " + prop.type +
109                     (prop.schema && prop.schema.property ? (" " + prop.schema.property.type) : "");
110             });
111         }
112     }
113 }
114
115