JPA concepts for TOSCA
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTimeInterval.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import com.google.gson.annotations.SerializedName;
27 import java.util.Date;
28 import java.util.List;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.models.base.PfConcept;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.base.PfUtils;
41 import org.onap.policy.models.base.PfValidationMessage;
42 import org.onap.policy.models.base.PfValidationResult;
43 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
44
45 /**
46  * Class to represent the TimeInterval in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  *
51  */
52 @Entity
53 @Table(name = "ToscaTimeInterval")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = false)
57 public class JpaToscaTimeInterval extends PfConcept {
58     private static final long serialVersionUID = 9151467029611969980L;
59
60     @EmbeddedId
61     private PfReferenceKey key;
62
63     @SerializedName("start_time")
64     private Date startTime;
65
66     @SerializedName("end_time")
67     private Date endTime;
68
69     /**
70      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
71      */
72     public JpaToscaTimeInterval() {
73         this(new PfReferenceKey());
74     }
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
78      *
79      * @param key the key
80      */
81     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
82         this(key, new Date(0), new Date(0));
83     }
84
85     /**
86      * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
87      *
88      * @param key the key
89      */
90     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime,
91             @NonNull final Date endTime) {
92         this.key = key;
93         this.startTime = startTime;
94         this.endTime = endTime;
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
103         super(copyConcept);
104         this.key = new PfReferenceKey(copyConcept.key);
105         this.startTime = copyConcept.startTime;
106         this.endTime = copyConcept.endTime;
107     }
108
109     @Override
110     public List<PfKey> getKeys() {
111         return getKey().getKeys();
112     }
113
114     @Override
115     public void clean() {
116         key.clean();
117     }
118
119     @Override
120     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
121         PfValidationResult result = resultIn;
122
123         if (key.isNullKey()) {
124             result.addValidationMessage(
125                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
126         }
127
128         result = key.validate(result);
129
130         if (startTime == null || startTime.getTime() == 0) {
131             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
132                     "start time on time interval may not be null or zero"));
133         }
134
135         if (endTime == null || endTime.getTime() == 0) {
136             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
137                     "end time on time interval may not be null or zero"));
138         }
139
140         if (startTime != null && endTime != null && endTime.before(startTime)) {
141             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
142                     "end time \"" + endTime.toString() + "\" on time interval may not be before start time \""
143                             + startTime.toString() + "\""));
144         }
145
146         return result;
147     }
148
149     @Override
150     public int compareTo(final PfConcept otherConcept) {
151         if (otherConcept == null) {
152             return -1;
153         }
154         if (this == otherConcept) {
155             return 0;
156         }
157         if (getClass() != otherConcept.getClass()) {
158             return getClass().getName().compareTo(otherConcept.getClass().getName());
159         }
160
161         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
162         int result = key.compareTo(other.key);
163         if (result != 0) {
164             return result;
165         }
166
167         result = PfUtils.compareObjects(startTime, other.startTime);
168         if (result != 0) {
169             return result;
170         }
171
172         return PfUtils.compareObjects(endTime, other.endTime);
173     }
174 }