Fix bugs in attribute outputs page
[sdc.git] / catalog-ui / src / app / models / requirement.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 {RequirementCapabilityModel} from "./capability";
27
28 //this is an object contains keys, when each key has matching array.
29 // for example: key = tosca.capabilities.network. and the match array is array of requirements objects
30 export class RequirementsGroup {
31     [key: string]: Array<Requirement>;
32     constructor(requirementGroupObj?:RequirementsGroup) {
33         _.forEach(requirementGroupObj, (requirementsArrayObj:Array<Requirement>, instance) => {
34             this[instance] = [];
35             _.forEach(requirementsArrayObj, (requirement:Requirement):void => {
36                 this[instance].push(new Requirement(requirement));
37             });
38         });
39     }
40 }
41
42 export class Requirement implements RequirementCapabilityModel{
43
44     //server data
45     capability:string;
46     name:string;
47     ownerId:string;
48     ownerName:string;
49     parentName: string;
50     node:string;
51     uniqueId:string;
52     relationship:string;
53     leftOccurrences:string;
54     minOccurrences:string | number;
55     maxOccurrences:string;
56     external:boolean;
57     //custom
58     filterTerm:string;
59
60     constructor(requirement?:Requirement) {
61
62         if (requirement) {
63             this.capability = requirement.capability;
64             this.name = requirement.name;
65             this.ownerId = requirement.ownerId;
66             this.ownerName = requirement.ownerName;
67             this.parentName = requirement.parentName;
68             this.node = requirement.node;
69             this.uniqueId = requirement.uniqueId;
70             this.relationship = requirement.relationship;
71             this.leftOccurrences = requirement.leftOccurrences;
72             this.minOccurrences = requirement.minOccurrences;
73             this.maxOccurrences = requirement.maxOccurrences;
74             this.external = requirement.external;
75             this.initFilterTerm();
76
77         }
78     }
79
80     public getTitle():string {
81         return this.ownerName + ': ' + this.name;
82     }
83
84     public getFullTitle():string {
85          return this.getTitle() + ': [' + this.minOccurrences + ', ' + this.maxOccurrences + ']';
86     }
87
88     public toJSON = ():any => {
89         this.filterTerm = undefined;
90         return this;
91     };
92
93     private initFilterTerm = ():void => {
94         this.filterTerm = (this.name + " ") +
95             (this.ownerName + " " ) +
96             (this.capability ? (this.capability.substring("tosca.capabilities.".length) + " " ) : "") +
97             (this.node ? (this.node.substring("tosca.nodes.".length) + " ") : "") +
98             (this.relationship ? (this.relationship.substring("tosca.relationships.".length) + " ") : "") +
99             this.minOccurrences + "," + this.maxOccurrences;
100     }
101
102     public isFulfilled() {
103         return parseInt(this.leftOccurrences) === 0;
104     }
105 }
106
107 // tslint:disable-next-line:max-classes-per-file
108 export class RequirementUI extends Requirement {
109     isCreatedManually: boolean;
110
111     constructor(input: Requirement, componentUniqueId: string) {
112         super(input);
113         this.isCreatedManually = input.ownerId === componentUniqueId;
114     }
115 }
116
117