Use annotations to do validation
[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-2020 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.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.base.validation.annotations.VerifyKey;
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     @VerifyKey
62     @NotNull
63     private PfReferenceKey key;
64
65     @SerializedName("start_time")
66     private Date startTime;
67
68     @SerializedName("end_time")
69     private Date endTime;
70
71     /**
72      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
73      */
74     public JpaToscaTimeInterval() {
75         this(new PfReferenceKey());
76     }
77
78     /**
79      * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
80      *
81      * @param key the key
82      */
83     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
84         this(key, new Date(0), new Date(0));
85     }
86
87     /**
88      * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
89      *
90      * @param key the key
91      */
92     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime,
93             @NonNull final Date endTime) {
94         this.key = key;
95         this.startTime = startTime;
96         this.endTime = endTime;
97     }
98
99     /**
100      * Copy constructor.
101      *
102      * @param copyConcept the concept to copy from
103      */
104     public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
105         super(copyConcept);
106         this.key = new PfReferenceKey(copyConcept.key);
107         this.startTime = copyConcept.startTime;
108         this.endTime = copyConcept.endTime;
109     }
110
111     @Override
112     public List<PfKey> getKeys() {
113         return getKey().getKeys();
114     }
115
116     @Override
117     public void clean() {
118         key.clean();
119     }
120
121     @Override
122     public BeanValidationResult validate(@NonNull String fieldName) {
123         BeanValidationResult result = super.validate(fieldName);
124
125         if (startTime == null || startTime.getTime() == 0) {
126             addResult(result, "startTime", startTime, "is null or zero");
127         }
128
129         if (endTime == null || endTime.getTime() == 0) {
130             addResult(result, "endTime", endTime, "is null or zero");
131         }
132
133         if (startTime != null && endTime != null && endTime.before(startTime)) {
134             addResult(result, "endTime", endTime, "is before startTime");
135         }
136
137         return result;
138     }
139
140     @Override
141     public int compareTo(final PfConcept otherConcept) {
142         if (otherConcept == null) {
143             return -1;
144         }
145         if (this == otherConcept) {
146             return 0;
147         }
148         if (getClass() != otherConcept.getClass()) {
149             return getClass().getName().compareTo(otherConcept.getClass().getName());
150         }
151
152         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
153         int result = key.compareTo(other.key);
154         if (result != 0) {
155             return result;
156         }
157
158         result = PfUtils.compareObjects(startTime, other.startTime);
159         if (result != 0) {
160             return result;
161         }
162
163         return PfUtils.compareObjects(endTime, other.endTime);
164     }
165 }