Fix bugs in attribute outputs page
[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 * as _ from 'lodash';
26 import { PropertyModel } from './properties';
27
28 export interface RequirementCapabilityModel {}
29
30 // this is an object contains keys, when each key has matching array.
31 // for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects
32 export class CapabilitiesGroup {
33     constructor(capabilityGroupObj?: CapabilitiesGroup) {
34         _.forEach(capabilityGroupObj, (capabilitiesArrayObj: Capability[], instance) => {
35             this[instance] = [];
36             _.forEach(capabilitiesArrayObj, (capability: Capability): void => {
37                 this[instance].push(new Capability(capability));
38             });
39         });
40     }
41     
42     public static getFlattenedCapabilities(capabilitiesGroup: CapabilitiesGroup): Array<Capability> {
43         return _.reduce(
44             _.toArray(capabilitiesGroup),
45             (allCaps, capArr) => allCaps.concat(capArr),
46             []);
47     }
48 }
49
50 export class Capability implements RequirementCapabilityModel {
51
52     // server data
53     name: string;
54     ownerId: string;
55     ownerName: string;
56     type: string;
57     uniqueId: string;
58     capabilitySources: string[];
59     leftOccurrences: string;
60     minOccurrences: string | number;
61     maxOccurrences: string;
62     description: string;
63     validSourceTypes: string[];
64     properties: PropertyModel[];
65     external: boolean;
66
67     // custom
68     selected: boolean;
69     filterTerm: string;
70
71     constructor(capability?: Capability) {
72
73         if (capability) {
74             // server data
75             this.name = capability.name;
76             this.ownerId = capability.ownerId;
77             this.ownerName = capability.ownerName;
78             this.type = capability.type;
79             this.uniqueId = capability.uniqueId;
80             this.capabilitySources = capability.capabilitySources;
81             this.leftOccurrences = capability.leftOccurrences;
82             this.minOccurrences = capability.minOccurrences;
83             this.maxOccurrences = capability.maxOccurrences;
84             this.properties = capability.properties;
85             this.description = capability.description;
86             this.validSourceTypes = capability.validSourceTypes;
87             this.selected = capability.selected;
88             this.external = capability.external;
89             this.initFilterTerm();
90
91         }
92     }
93
94     public getTitle(): string {
95         return this.ownerName + ': ' + this.name;
96     }
97
98     public getFullTitle(): string {
99         const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
100         return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
101     }
102
103     public toJSON = (): any => {
104         this.selected = undefined;
105         this.filterTerm = undefined;
106         return this;
107     }
108
109     public isFulfilled() {
110         return parseInt(this.leftOccurrences) === 0;
111     }
112
113     private initFilterTerm = (): void => {
114         this.filterTerm = this.name + ' ' +
115             (this.type ? (this.type.replace('tosca.capabilities.', '') + ' ' ) : '') +
116             (this.description || '') + ' ' +
117             (this.ownerName || '') + ' ' +
118             (this.validSourceTypes ? (this.validSourceTypes.join(',') + ' ') : '') +
119             this.minOccurrences + ',' + this.maxOccurrences;
120         if (this.properties && this.properties.length) {
121             _.forEach(this.properties, (prop: PropertyModel) => {
122                 this.filterTerm += ' ' + prop.name +
123                     ' ' + (prop.description || '') +
124                     ' ' + prop.type +
125                     (prop.schema && prop.schema.property ? (' ' + prop.schema.property.type) : '');
126             });
127         }
128     }
129 }
130
131 export class CapabilityUI extends Capability {
132     isCreatedManually: boolean;
133
134     constructor(input: Capability, componentUniqueId: string) {
135         super(input);
136         this.isCreatedManually = input.ownerId === componentUniqueId;
137     }
138 }
139
140