Add template and tosca model entities and repositories
[clamp.git] / src / main / java / org / onap / clamp / loop / template / TemplateMicroServiceModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.template;
25
26 import com.google.gson.annotations.Expose;
27
28 import java.io.Serializable;
29
30 import javax.persistence.CascadeType;
31 import javax.persistence.Column;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.JoinColumn;
36 import javax.persistence.ManyToOne;
37 import javax.persistence.MapsId;
38 import javax.persistence.Table;
39
40 @Entity
41 @Table(name = "templates_microservicemodels")
42 public class TemplateMicroServiceModel implements Serializable, Comparable<TemplateMicroServiceModel> {
43
44     /**
45      * Serial ID.
46      */
47     private static final long serialVersionUID = 5924989899078094245L;
48
49     @EmbeddedId
50     private TemplateMicroServiceModelId templateMicroServiceModelId;
51
52     @ManyToOne(fetch = FetchType.LAZY)
53     @MapsId("loopTemplateName")
54     @JoinColumn(name = "loop_template_name")
55     private LoopTemplate loopTemplate;
56
57     @Expose
58     @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
59     @MapsId("microServiceModelName")
60     @JoinColumn(name = "micro_service_model_name")
61     private MicroServiceModel microServiceModel;
62
63     @Expose
64     @Column(nullable = false, name = "flow_order")
65     private Integer flowOrder;
66
67     /**
68      * Default constructor for serialization.
69      */
70     public TemplateMicroServiceModel() {
71
72     }
73
74     /**
75      * Constructor.
76      * 
77      * @param loopTemplate      The loop template object
78      * @param microServiceModel The microServiceModel object
79      * @param flowOrder         The position of the micro service in the flow
80      */
81     public TemplateMicroServiceModel(LoopTemplate loopTemplate, MicroServiceModel microServiceModel,
82             Integer flowOrder) {
83         this.loopTemplate = loopTemplate;
84         this.microServiceModel = microServiceModel;
85         this.flowOrder = flowOrder;
86         this.templateMicroServiceModelId = new TemplateMicroServiceModelId(loopTemplate.getName(),
87                 microServiceModel.getName());
88     }
89
90     /**
91      * loopTemplate getter.
92      * 
93      * @return the loopTemplate
94      */
95     public LoopTemplate getLoopTemplate() {
96         return loopTemplate;
97     }
98
99     /**
100      * loopTemplate setter.
101      * 
102      * @param loopTemplate the loopTemplate to set
103      */
104     public void setLoopTemplate(LoopTemplate loopTemplate) {
105         this.loopTemplate = loopTemplate;
106     }
107
108     /**
109      * microServiceModel getter.
110      * 
111      * @return the microServiceModel
112      */
113     public MicroServiceModel getMicroServiceModel() {
114         return microServiceModel;
115     }
116
117     /**
118      * microServiceModel setter.
119      * 
120      * @param microServiceModel the microServiceModel to set
121      */
122     public void setMicroServiceModel(MicroServiceModel microServiceModel) {
123         this.microServiceModel = microServiceModel;
124     }
125
126     /**
127      * flowOrder getter.
128      * 
129      * @return the flowOrder
130      */
131     public Integer getFlowOrder() {
132         return flowOrder;
133     }
134
135     /**
136      * flowOrder setter.
137      * 
138      * @param flowOrder the flowOrder to set
139      */
140     public void setFlowOrder(Integer flowOrder) {
141         this.flowOrder = flowOrder;
142     }
143
144     @Override
145     public int compareTo(TemplateMicroServiceModel arg0) {
146         // Reverse it, so that by default we have the latest
147         if (getFlowOrder() == null) {
148             return 1;
149         }
150         if (arg0.getFlowOrder() == null) {
151             return -1;
152         }
153         return arg0.getFlowOrder().compareTo(this.getFlowOrder());
154     }
155
156     @Override
157     public int hashCode() {
158         final int prime = 31;
159         int result = 1;
160         result = prime * result + ((loopTemplate == null) ? 0 : loopTemplate.hashCode());
161         result = prime * result + ((microServiceModel == null) ? 0 : microServiceModel.hashCode());
162         return result;
163     }
164
165     @Override
166     public boolean equals(Object obj) {
167         if (this == obj) {
168             return true;
169         }
170         if (obj == null) {
171             return false;
172         }
173         if (getClass() != obj.getClass()) {
174             return false;
175         }
176         TemplateMicroServiceModel other = (TemplateMicroServiceModel) obj;
177         if (loopTemplate == null) {
178             if (other.loopTemplate != null) {
179                 return false;
180             }
181         } else if (!loopTemplate.equals(other.loopTemplate)) {
182             return false;
183         }
184         if (microServiceModel == null) {
185             if (other.microServiceModel != null) {
186                 return false;
187             }
188         } else if (!microServiceModel.equals(other.microServiceModel)) {
189             return false;
190         }
191         return true;
192     }
193
194 }