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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
26 import javax.persistence.GeneratedValue;
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;
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.
38 * <p/>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
39 * regular expressions respectively.
43 @EqualsAndHashCode(callSuper = false)
44 public class PfGeneratedIdKey extends PfKeyImpl {
46 private static final long serialVersionUID = 1L;
48 private static final String ID_TOKEN = "ID";
50 @Column(name = NAME_TOKEN, length = 120)
51 @Pattern(regexp = NAME_REGEXP)
54 @Column(name = VERSION_TOKEN, length = 20)
55 @Pattern(regexp = VERSION_REGEXP)
56 private String version;
58 @Column(name = ID_TOKEN)
60 private Long generatedId;
63 * The default constructor creates a null concept key.
65 public PfGeneratedIdKey() {
66 this(NULL_KEY_NAME, NULL_KEY_VERSION);
70 * Constructor to create a key with the specified name and version.
72 * @param name the key name
73 * @param version the key version
75 public PfGeneratedIdKey(final String name, final String version) {
82 * @param copyConcept the concept to copy from
84 public PfGeneratedIdKey(final PfGeneratedIdKey copyConcept) {
86 this.generatedId = copyConcept.getGeneratedId();
90 * Constructor to create a key with the specified name and version.
92 * @param name the key name
93 * @param version the key version
94 * @param generatedId the conceptId of key
96 public PfGeneratedIdKey(@NonNull final String name, @NonNull final String version,
97 final Long generatedId) {
99 this.generatedId = generatedId;
103 * Constructor to create a key using the key and version from the specified key ID.
105 * @param id the key ID in a format that respects the KEY_ID_REGEXP
107 public PfGeneratedIdKey(final String id) {
108 super(id.substring(0, id.lastIndexOf(':')));
109 this.generatedId = Long.parseLong(id.substring(id.lastIndexOf(':') + 1));
113 public int compareTo(@NonNull final PfConcept otherObj) {
114 int result = super.compareTo(otherObj);
116 final PfGeneratedIdKey other = (PfGeneratedIdKey) otherObj;
117 return generatedId.compareTo(other.generatedId);
123 public String getId() {
124 return getName() + ':' + getVersion() + ':' + getGeneratedId();
128 public boolean isNewerThan(@NonNull PfKey otherKey) {
129 Assertions.instanceOf(otherKey, PfGeneratedIdKey.class);
131 final PfGeneratedIdKey otherConceptKey = (PfGeneratedIdKey) otherKey;
133 if (this.equals(otherConceptKey)) {
137 if (!generatedId.equals(otherConceptKey.generatedId)) {
138 return generatedId.compareTo(otherConceptKey.generatedId) >= 1;
141 return super.isNewerThan(otherKey);
145 public boolean isNullKey() {
146 return super.isNullKey() && getGeneratedId() == null;
149 public void setName(@NonNull final String name) {
150 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
153 public void setVersion(@NonNull final String version) {
154 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
158 * Get a null concept key.
160 * @return a null concept key
162 public static final PfGeneratedIdKey getNullKey() {
163 return new PfGeneratedIdKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);