Sync Integ to Master
[sdc.git] / catalog-ui / src / app / models / graph / match-relation.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 'use strict';
21 import {Requirement} from "../requirement";
22 import {Capability} from "../capability";
23 import {Relationship, RelationshipModel} from "./relationship";
24 import {PropertyModel} from "../properties";
25
26 export class Match {
27     requirement:Requirement;
28     capability:Capability;
29     isFromTo:boolean;
30     fromNode:string;
31     toNode:string;
32     private _relationship:Relationship;
33
34     constructor(requirement:Requirement, capability:Capability, isFromTo:boolean, fromNode:string, toNode:string) {
35         this.requirement = requirement;
36         this.capability = capability;
37         this.isFromTo = isFromTo;
38         this.fromNode = fromNode;
39         this.toNode = toNode;
40     }
41
42     // NOTE: Hold the relationship instance for cases capability / requirement are not available (when fulfilled).
43     //      In case relationship instance is not manually saved to here, then build the relationship from the given capability and requirement.
44     public get relationship():Relationship {
45         if (!this._relationship) {
46             this._relationship = this.matchToRelation();
47         }
48         return this._relationship;
49     }
50     public set relationship(relationship) {
51         this._relationship = relationship;
52     }
53
54     public matchToRelation = ():Relationship => {
55         const relationship:Relationship = new Relationship();
56         relationship.setRelationProperties(this.capability, this.requirement);
57         return relationship;
58     };
59
60     public getDisplayText = (menuSide:string):string => {
61         if (this.isFromTo && 'left' == menuSide || !this.isFromTo && 'right' == menuSide) {
62             return this.requirement.getFullTitle();
63         }
64         return this.capability.getFullTitle();
65     };
66
67     public isOwner = (id:string):boolean => {
68         return this.capability.ownerId === id || this.requirement.ownerId === id;
69     };
70
71     public matchToRelationModel = ():RelationshipModel => {
72         let relationshipModel:RelationshipModel = new RelationshipModel();
73         let relationship:Relationship = this.matchToRelation();
74         relationshipModel.setRelationshipModelParams(this.fromNode, this.toNode, [relationship]);
75         return relationshipModel;
76     };
77 }
78