Add policy metadataSet handling as node templates
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfGeneratedIdKey.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
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
26 import javax.persistence.GeneratedValue;
27 import lombok.Data;
28 import lombok.EqualsAndHashCode;
29 import lombok.NonNull;
30 import org.onap.policy.common.parameters.annotations.Pattern;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 /**
34  * A concept key uniquely identifies every first order entity in the system. Every first order concept in the system
35  * must have a {@link PfGeneratedIdKey} to identify it. Concepts that are wholly contained in another concept are
36  * identified using a {@link PfReferenceKey} key.
37  *
38  * <p/>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
39  * regular expressions respectively.
40  */
41 @Embeddable
42 @Data
43 @EqualsAndHashCode(callSuper = false)
44 public class PfGeneratedIdKey extends PfKeyImpl {
45
46     private static final long serialVersionUID = 1L;
47
48     private static final String ID_TOKEN = "ID";
49
50     @Column(name = NAME_TOKEN, length = 120)
51     @Pattern(regexp = NAME_REGEXP)
52     private String name;
53
54     @Column(name = VERSION_TOKEN, length = 20)
55     @Pattern(regexp = VERSION_REGEXP)
56     private String version;
57
58     @Column(name = ID_TOKEN)
59     @GeneratedValue
60     private Long generatedId;
61
62     /**
63      * The default constructor creates a null concept key.
64      */
65     public PfGeneratedIdKey() {
66         this(NULL_KEY_NAME, NULL_KEY_VERSION);
67     }
68
69     /**
70      * Constructor to create a key with the specified name and version.
71      *
72      * @param name the key name
73      * @param version the key version
74      */
75     public PfGeneratedIdKey(final String name, final String version) {
76         super(name, version);
77     }
78
79     /**
80      * Copy constructor.
81      *
82      * @param copyConcept the concept to copy from
83      */
84     public PfGeneratedIdKey(final PfGeneratedIdKey copyConcept) {
85         super(copyConcept);
86         this.generatedId = copyConcept.getGeneratedId();
87     }
88
89     /**
90      * Constructor to create a key with the specified name and version.
91      *
92      * @param name the key name
93      * @param version the key version
94      * @param generatedId the conceptId of key
95      */
96     public PfGeneratedIdKey(@NonNull final String name, @NonNull final String version,
97             final Long generatedId) {
98         super(name, version);
99         this.generatedId = generatedId;
100     }
101
102     /**
103      * Constructor to create a key using the key and version from the specified key ID.
104      *
105      * @param id the key ID in a format that respects the KEY_ID_REGEXP
106      */
107     public PfGeneratedIdKey(final String id) {
108         super(id.substring(0, id.lastIndexOf(':')));
109         this.generatedId = Long.parseLong(id.substring(id.lastIndexOf(':') + 1));
110     }
111
112     @Override
113     public int compareTo(@NonNull final PfConcept otherObj) {
114         int result = super.compareTo(otherObj);
115         if (0 == result) {
116             final PfGeneratedIdKey other = (PfGeneratedIdKey) otherObj;
117             return generatedId.compareTo(other.generatedId);
118         }
119         return result;
120     }
121
122     @Override
123     public String getId() {
124         return getName() + ':' + getVersion() + ':' + getGeneratedId();
125     }
126
127     @Override
128     public boolean isNewerThan(@NonNull PfKey otherKey) {
129         Assertions.instanceOf(otherKey, PfGeneratedIdKey.class);
130
131         final PfGeneratedIdKey otherConceptKey = (PfGeneratedIdKey) otherKey;
132
133         if (this.equals(otherConceptKey)) {
134             return false;
135         }
136
137         if (!generatedId.equals(otherConceptKey.generatedId)) {
138             return generatedId.compareTo(otherConceptKey.generatedId) >= 1;
139         }
140
141         return super.isNewerThan(otherKey);
142     }
143
144     @Override
145     public boolean isNullKey() {
146         return super.isNullKey() && getGeneratedId() == null;
147     }
148
149     public void setName(@NonNull final String name) {
150         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
151     }
152
153     public void setVersion(@NonNull final String version) {
154         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
155     }
156
157     /**
158      * Get a null concept key.
159      *
160      * @return a null concept key
161      */
162     public static final PfGeneratedIdKey getNullKey() {
163         return new PfGeneratedIdKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
164     }
165
166 }