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