Fix Reference Key columns persistence issue in db
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceTimestampKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.sql.Timestamp;
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.List;
27 import javax.persistence.Column;
28 import javax.persistence.Embeddable;
29 import javax.persistence.Embedded;
30 import lombok.Data;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33 import org.onap.policy.common.parameters.annotations.NotNull;
34 import org.onap.policy.common.utils.validation.Assertions;
35
36 /**
37  * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
38  * additional option to have timestamp as a parameter.
39  *
40  */
41
42 @Embeddable
43 @Data
44 @EqualsAndHashCode
45 public class PfReferenceTimestampKey extends PfKey {
46     private static final long serialVersionUID = 1130918285832617215L;
47
48     private static final String TIMESTAMP_TOKEN = "timeStamp";
49
50     @Column(name = TIMESTAMP_TOKEN)
51     @NotNull
52     private Timestamp timeStamp;
53
54     @Embedded
55     @Column
56     private PfReferenceKey referenceKey;
57
58     /**
59      * The default constructor creates a null reference timestamp key.
60      */
61     public PfReferenceTimestampKey() {
62         this.referenceKey = new PfReferenceKey();
63         this.timeStamp = new Timestamp(0);
64     }
65
66     /**
67      * The Copy Constructor creates a key by copying another key.
68      *
69      * @param referenceTimestampKey
70      *        the reference key to copy from
71      */
72     public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
73         this.referenceKey = referenceTimestampKey.getReferenceKey();
74         this.timeStamp = referenceTimestampKey.getTimeStamp();
75     }
76
77     /**
78      * Constructor to create a null reference key for the specified parent concept key.
79      *
80      * @param pfConceptKey
81      *        the parent concept key of this reference key
82      */
83     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
84         this.referenceKey = new PfReferenceKey(pfConceptKey);
85         this.timeStamp = new Timestamp(0);
86     }
87
88     /**
89      * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
90      *
91      * @param pfConceptKey
92      *        the parent concept key of this reference key
93      * @param localName
94      *        the local name of this reference key
95      * @param instant
96      *        the time stamp for this reference key
97      */
98     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
99         this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
100         this.timeStamp = Timestamp.from(instant);
101     }
102
103     /**
104      * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
105      *
106      * @param parentReferenceKey
107      *        the parent reference key of this reference key
108      * @param localName
109      *        the local name of this reference key
110      * @param instant
111      *        the time stamp for this reference key
112      */
113     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
114                                    final Instant instant) {
115         this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
116         this.timeStamp = Timestamp.from(instant);
117     }
118
119     /**
120      * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
121      * reference key's concept key and local name) with the given local name.
122      *
123      * @param pfConceptKey
124      *        the concept key of the parent reference key of this reference key
125      * @param parentLocalName
126      *        the local name of the parent reference key of this reference key
127      * @param localName
128      *        the local name of this reference key
129      * @param instant
130      *        the time stamp for this reference key
131      */
132     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
133                                    final String localName, final Instant instant) {
134         this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
135         this.timeStamp = Timestamp.from(instant);
136     }
137
138     /**
139      * Constructor to create a reference timestamp key for the given parent concept key (specified by the
140      * parent concept key's name and version) with the given local name.
141      *
142      * @param parentKeyName
143      *        the name of the parent concept key of this reference key
144      * @param parentKeyVersion
145      *        the version of the parent concept key of this reference key
146      * @param localName
147      *        the local name of this reference key
148      * @param instant
149      *        the time stamp for this reference key
150      */
151     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
152                                    final Instant instant) {
153         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
154         this.timeStamp = Timestamp.from(instant);
155     }
156
157     /**
158      * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
159      * version and local name) with the given local name.
160      *
161      * @param parentKeyName
162      *        the parent key name of this reference key
163      * @param parentKeyVersion
164      *        the parent key version of this reference key
165      * @param parentLocalName
166      *        the parent local name of this reference key
167      * @param localName
168      *        the local name of this reference key
169      * @param instant
170      *        the instant for this reference key
171      */
172     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
173                                    final String parentLocalName, final String localName, final Instant instant) {
174         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
175         this.timeStamp = Timestamp.from(instant);
176     }
177
178
179     /**
180      * Constructor to create a key using the key and version from the specified key ID.
181      *
182      * @param id the key ID in a format that respects the KEY_ID_REGEXP
183      */
184     public PfReferenceTimestampKey(final String id) {
185         this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
186         this.timeStamp = new Timestamp(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
187     }
188
189
190     /**
191      * Get a null reference timestamp key.
192      *
193      * @return a null reference key
194      */
195     public static PfReferenceTimestampKey getNullKey() {
196         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
197             PfKey.NULL_KEY_NAME, Instant.EPOCH);
198     }
199
200     public Instant getInstant() {
201         return timeStamp.toInstant();
202     }
203
204     public void setInstant(final Instant instant) {
205         setTimeStamp(Timestamp.from(instant));
206     }
207
208     /**
209      * Get the key of this reference.
210      *
211      * @return the pfReferenceTimestamp key
212      */
213     public PfReferenceTimestampKey getKey() {
214         return this;
215     }
216
217     /**
218      * Get the key as a string.
219      * @return pfReferenceTimestamp key.
220      */
221     public String getId() {
222         return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
223     }
224
225
226     /**
227      * Check if this key is a newer version than the other key.
228      *
229      * @param otherKey the key to check against
230      * @return true, if this key is newer than the other key
231      */
232     @Override
233     public boolean isNewerThan(@NonNull PfKey otherKey) {
234         Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
235         final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
236         if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
237             return timeStamp.after(otherReferenceKey.timeStamp);
238         }
239         return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
240     }
241
242     @Override
243     public boolean isNullKey() {
244         return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
245     }
246
247     @Override
248     public int getMajorVersion() {
249         return getReferenceKey().getMajorVersion();
250     }
251
252     @Override
253     public int getMinorVersion() {
254         return getReferenceKey().getMinorVersion();
255     }
256
257     @Override
258     public int getPatchVersion() {
259         return getReferenceKey().getPatchVersion();
260     }
261
262
263     @Override
264     public int compareTo(@NonNull final PfConcept otherObj) {
265         if (this == otherObj) {
266             return 0;
267         }
268         if (getClass() != otherObj.getClass()) {
269             return getClass().getName().compareTo(otherObj.getClass().getName());
270         }
271         int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
272         if (0 == result) {
273             return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
274         }
275         return result;
276     }
277
278     @Override
279     public List<PfKey> getKeys() {
280         return Collections.singletonList(getKey());
281     }
282
283     @Override
284     public void clean() {
285         getReferenceKey().clean();
286     }
287
288     @Override
289     public Compatibility getCompatibility(@NonNull PfKey otherKey) {
290         return getReferenceKey().getCompatibility(otherKey);
291     }
292
293     @Override
294     public boolean isCompatible(@NonNull PfKey otherKey) {
295         if (!(otherKey instanceof PfReferenceTimestampKey)) {
296             return false;
297         }
298         final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
299
300         return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
301             .getParentConceptKey());
302     }
303 }