Catalog alignment
[sdc.git] / catalog-ui / src / app / models / graph / relationship.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 * as _ from "lodash";
22 import {Capability} from "../capability";
23 import {Requirement} from "../requirement";
24
25 export class RelationshipModel {
26     fromNode:string;
27     toNode:string;
28     relationships:Array<Relationship>;
29
30     constructor(relationshipModel?:RelationshipModel, singleRelationship?:Relationship) {
31         if (relationshipModel) {
32             this.fromNode = relationshipModel.fromNode;
33             this.toNode = relationshipModel.toNode;
34             this.relationships = [];
35             if (relationshipModel.relationships && !singleRelationship) {
36                 _.forEach(relationshipModel.relationships, (relation:Relationship):void => {
37                     this.relationships.push(new Relationship(relation));
38                 });
39             } else if (singleRelationship) {
40                 this.relationships.push(singleRelationship);
41             }
42         }
43     }
44
45     public setRelationshipModelParams(fromNode:string, toNode:string, relationships:Array<Relationship>) {
46         this.fromNode = fromNode;
47         this.toNode = toNode;
48         this.relationships = relationships;
49     }
50 }
51
52 export class RelationType {
53     type:string;
54
55     constructor(type?:string) {
56         if (type) {
57             this.type = type;
58         }
59     }
60 }
61
62 export class RelationshipType {
63     id:string;
64     capability:string;
65     capabilityOwnerId:string;
66     capabilityUid:string;
67     relationship:RelationType;
68     requirement:string;
69     requirementOwnerId:string;
70     requirementUid:string;
71
72     constructor(relationship?:RelationshipType) {
73         if (relationship) {
74             this.id = relationship.id;
75             this.capability = relationship.capability;
76             this.capabilityOwnerId = relationship.capabilityOwnerId;
77             this.capabilityUid = relationship.capabilityUid;
78             this.relationship = new RelationType(relationship.relationship.type);
79             this.requirement = relationship.requirement;
80             this.requirementOwnerId = relationship.requirementOwnerId;
81             this.requirementUid = relationship.requirementUid;
82         } else {
83             this.relationship = new RelationType();
84         }
85     }
86
87     public setRelationProperties = (capability?:Capability, requirement?:Requirement)=> {
88         if (capability) {
89             this.capability = capability.name;
90             this.capabilityOwnerId = capability.ownerId;
91             this.capabilityUid = capability.uniqueId;
92             this.relationship = new RelationType(capability.type);
93         }
94         if (requirement) {
95             this.requirement = requirement.name;
96             this.requirementOwnerId = requirement.ownerId;
97             this.requirementUid = requirement.uniqueId;
98         }
99     };
100 }
101
102 export class Relationship {
103     relation: RelationshipType;
104     capability?: Capability;
105     requirement?: Requirement;
106
107     constructor(fullRelationship?:Relationship) {
108         if (fullRelationship) {
109             this.relation = new RelationshipType(fullRelationship.relation);
110             this.capability = fullRelationship.capability && new Capability(fullRelationship.capability);
111             this.requirement = fullRelationship.requirement && new Requirement(fullRelationship.requirement);
112         } else {
113             this.relation = new RelationshipType();
114         }
115     }
116
117     public setRelationProperties(capability?:Capability, requirement?:Requirement) {
118         this.relation.setRelationProperties(capability, requirement);
119         this.capability = capability;
120         this.requirement = requirement;
121     };
122
123     public toJSON = ():any => {
124         let temp = angular.copy(this);
125         temp.capability = undefined;
126         temp.requirement = undefined;
127         return temp;
128     }
129 }