Catalog alignment
[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     //custom
57     filterTerm:string;
58
59     constructor(requirement?:Requirement) {
60
61         if (requirement) {
62             this.capability = requirement.capability;
63             this.name = requirement.name;
64             this.ownerId = requirement.ownerId;
65             this.ownerName = requirement.ownerName;
66             this.parentName = requirement.parentName;
67             this.node = requirement.node;
68             this.uniqueId = requirement.uniqueId;
69             this.relationship = requirement.relationship;
70             this.leftOccurrences = requirement.leftOccurrences;
71             this.minOccurrences = requirement.minOccurrences;
72             this.maxOccurrences = requirement.maxOccurrences;
73             this.initFilterTerm();
74
75         }
76     }
77
78     public getTitle():string {
79         return this.ownerName + ': ' + this.name;
80     }
81
82     public getFullTitle():string {
83          return this.getTitle() + ': [' + this.minOccurrences + ', ' + this.maxOccurrences + ']';
84     }
85
86     public toJSON = ():any => {
87         this.filterTerm = undefined;
88         return this;
89     };
90
91     private initFilterTerm = ():void => {
92         this.filterTerm = (this.name + " ") +
93             (this.ownerName + " " ) +
94             (this.capability ? (this.capability.substring("tosca.capabilities.".length) + " " ) : "") +
95             (this.node ? (this.node.substring("tosca.nodes.".length) + " ") : "") +
96             (this.relationship ? (this.relationship.substring("tosca.relationships.".length) + " ") : "") +
97             this.minOccurrences + "," + this.maxOccurrences;
98     }
99
100     public isFulfilled() {
101         return parseInt(this.leftOccurrences) === 0;
102     }
103 }
104
105 // tslint:disable-next-line:max-classes-per-file
106 export class RequirementUI extends Requirement {
107     isCreatedManually: boolean;
108
109     constructor(input: Requirement, componentUniqueId: string) {
110         super(input);
111         this.isCreatedManually = input.ownerId === componentUniqueId;
112     }
113 }
114
115