Use ValidationResult for models v2.0
[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.models.base.PfConcept;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfReferenceKey;
41 import org.onap.policy.models.base.PfUtils;
42
43 /**
44  * Class to represent the TimeInterval in TOSCA definition.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  * @author Liam Fallon (liam.fallon@est.tech)
48  *
49  */
50 @Entity
51 @Table(name = "ToscaTimeInterval")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaTimeInterval extends PfConcept {
56     private static final long serialVersionUID = 9151467029611969980L;
57
58     @EmbeddedId
59     private PfReferenceKey key;
60
61     @SerializedName("start_time")
62     private Date startTime;
63
64     @SerializedName("end_time")
65     private Date endTime;
66
67     /**
68      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
69      */
70     public JpaToscaTimeInterval() {
71         this(new PfReferenceKey());
72     }
73
74     /**
75      * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
76      *
77      * @param key the key
78      */
79     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
80         this(key, new Date(0), new Date(0));
81     }
82
83     /**
84      * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
85      *
86      * @param key the key
87      */
88     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime,
89             @NonNull final Date endTime) {
90         this.key = key;
91         this.startTime = startTime;
92         this.endTime = endTime;
93     }
94
95     /**
96      * Copy constructor.
97      *
98      * @param copyConcept the concept to copy from
99      */
100     public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
101         super(copyConcept);
102         this.key = new PfReferenceKey(copyConcept.key);
103         this.startTime = copyConcept.startTime;
104         this.endTime = copyConcept.endTime;
105     }
106
107     @Override
108     public List<PfKey> getKeys() {
109         return getKey().getKeys();
110     }
111
112     @Override
113     public void clean() {
114         key.clean();
115     }
116
117     @Override
118     public BeanValidationResult validate(@NonNull String fieldName) {
119         BeanValidationResult result = new BeanValidationResult(fieldName, this);
120
121         result.addResult(validateKeyNotNull("key", key));
122
123         if (startTime == null || startTime.getTime() == 0) {
124             addResult(result, "startTime", startTime, "is null or zero");
125         }
126
127         if (endTime == null || endTime.getTime() == 0) {
128             addResult(result, "endTime", endTime, "is null or zero");
129         }
130
131         if (startTime != null && endTime != null && endTime.before(startTime)) {
132             addResult(result, "endTime", endTime, "is before startTime");
133         }
134
135         return result;
136     }
137
138     @Override
139     public int compareTo(final PfConcept otherConcept) {
140         if (otherConcept == null) {
141             return -1;
142         }
143         if (this == otherConcept) {
144             return 0;
145         }
146         if (getClass() != otherConcept.getClass()) {
147             return getClass().getName().compareTo(otherConcept.getClass().getName());
148         }
149
150         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
151         int result = key.compareTo(other.key);
152         if (result != 0) {
153             return result;
154         }
155
156         result = PfUtils.compareObjects(startTime, other.startTime);
157         if (result != 0) {
158             return result;
159         }
160
161         return PfUtils.compareObjects(endTime, other.endTime);
162     }
163 }