6ac71aba256045e2fd5b06b76037e2b0c1e9561d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023-2026 OpenInfra Foundation Europe. All rights reserved.
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.clamp.models.acm.persistence.concepts;
22
23 import jakarta.persistence.Column;
24 import jakarta.persistence.Entity;
25 import jakarta.persistence.Id;
26 import jakarta.persistence.Inheritance;
27 import jakarta.persistence.InheritanceType;
28 import jakarta.persistence.Table;
29 import jakarta.validation.constraints.NotNull;
30 import java.util.UUID;
31 import lombok.Data;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34 import org.apache.commons.lang3.ObjectUtils;
35 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
36 import org.onap.policy.models.base.PfAuthorative;
37 import org.onap.policy.models.base.Validated;
38
39 @Entity
40 @Table(name = "ParticipantSupportedAcElements")
41 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
42 @Data
43 @EqualsAndHashCode(callSuper = false)
44 public class JpaParticipantSupportedElementType extends Validated
45     implements PfAuthorative<ParticipantSupportedElementType>, Comparable<JpaParticipantSupportedElementType> {
46
47     @NotNull
48     @Column
49     @Id
50     private String id;
51
52     @NotNull
53     @Column
54     private String participantId;
55
56     @NotNull
57     @Column
58     private String typeName;
59
60     @NotNull
61     @Column
62     private String typeVersion;
63
64     /**
65      * The default Constructor creates a {@link JpaParticipantSupportedElementType} with a generated id.
66      *
67      */
68     public JpaParticipantSupportedElementType() {
69         this(UUID.randomUUID().toString());
70     }
71
72     /**
73      * The Key Constructor creates a {@link JpaParticipantSupportedElementType} with just a generated id parameter.
74      *
75      * @param id the main id of the element type
76      */
77     public JpaParticipantSupportedElementType(@NonNull String id) {
78         this(id, UUID.randomUUID().toString());
79     }
80
81     /**
82      * The Key Constructor creates a {@link JpaParticipantSupportedElementType} with id and participantId.
83      *
84      * @param id the main id of the element type
85      * @param participantId the participant id
86      */
87     public JpaParticipantSupportedElementType(@NonNull String id, @NonNull String participantId) {
88         this(id, participantId, "", "");
89     }
90
91     /**
92      * The Key Constructor creates a {@link JpaParticipantSupportedElementType} object with all mandatory fields.
93      *
94      * @param id the main id of the element type
95      * @param participantId the participant id
96      * @param typeName the type name of the supported element
97      * @param typeVersion the type version of the supported element
98      */
99     public JpaParticipantSupportedElementType(@NonNull String id,
100                                               @NonNull String participantId,
101                                               @NonNull String typeName,
102                                               @NonNull String typeVersion) {
103         this.id = id;
104         this.participantId = participantId;
105         this.typeName = typeName;
106         this.typeVersion = typeVersion;
107     }
108
109     /**
110      * Copy constructor.
111      *
112      * @param copyConcept the concept to copy from
113      */
114     public JpaParticipantSupportedElementType(@NonNull final JpaParticipantSupportedElementType copyConcept) {
115         this.id = copyConcept.id;
116         this.participantId = copyConcept.participantId;
117         this.typeName = copyConcept.typeName;
118         this.typeVersion = copyConcept.typeVersion;
119     }
120
121     /**
122      * Authorative constructor.
123      *
124      * @param authorativeConcept the authorative concept to copy from
125      */
126     public JpaParticipantSupportedElementType(@NonNull final ParticipantSupportedElementType authorativeConcept) {
127         this.fromAuthorative(authorativeConcept);
128     }
129
130     @Override
131     public int compareTo(final JpaParticipantSupportedElementType other) {
132         if (other == null) {
133             return -1;
134         }
135         if (this == other) {
136             return 0;
137         }
138
139         var result = ObjectUtils.compare(participantId, other.participantId);
140         if (result != 0) {
141             return result;
142         }
143
144         result = ObjectUtils.compare(typeName, other.typeName);
145         if (result != 0) {
146             return result;
147         }
148
149         result = ObjectUtils.compare(id, other.id);
150         if (result != 0) {
151             return result;
152         }
153
154         result = typeVersion.compareTo(other.typeVersion);
155         if (result != 0) {
156             return result;
157         }
158
159         return 0;
160     }
161
162     @Override
163     public ParticipantSupportedElementType toAuthorative() {
164         var element = new ParticipantSupportedElementType();
165         element.setId(UUID.fromString(id));
166         element.setTypeName(typeName);
167         element.setTypeVersion(typeVersion);
168         return element;
169     }
170
171     @Override
172     public void fromAuthorative(@NonNull ParticipantSupportedElementType participantSupportedElementType) {
173         this.id = participantSupportedElementType.getId().toString();
174         this.typeName = participantSupportedElementType.getTypeName();
175         this.typeVersion = participantSupportedElementType.getTypeVersion();
176     }
177 }