Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020, 2023 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import jakarta.persistence.CascadeType;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.FetchType;
27 import jakarta.persistence.Inheritance;
28 import jakarta.persistence.InheritanceType;
29 import jakarta.persistence.JoinColumn;
30 import jakarta.persistence.OneToOne;
31 import jakarta.persistence.Table;
32 import java.io.Serial;
33 import java.util.List;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NoArgsConstructor;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
45
46 /**
47  * Class to represent the node type in TOSCA definition.
48  */
49
50 @Entity
51 @Table(name = "ToscaNodeType")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = true)
55 @NoArgsConstructor
56 public class JpaToscaNodeType extends JpaToscaWithToscaProperties<ToscaNodeType> {
57     @Serial
58     private static final long serialVersionUID = -563659852901842616L;
59
60
61     // formatter:off
62     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
63     @JoinColumn(name = "requirementsName", referencedColumnName = "name")
64     @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
65     // @formatter:on
66     @Valid
67     private JpaToscaRequirements requirements;
68
69     /**
70      * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
71      *
72      * @param key the key
73      */
74     public JpaToscaNodeType(@NonNull final PfConceptKey key) {
75         super(key);
76     }
77
78     /**
79      * Copy constructor.
80      *
81      * @param copyConcept the concept to copy from
82      */
83     public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
84         super(copyConcept);
85         this.requirements =
86                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
87     }
88
89     /**
90      * Authorative constructor.
91      *
92      * @param authorativeConcept the authorative concept to copy from
93      */
94     public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
95         super(authorativeConcept);
96     }
97
98     @Override
99     public ToscaNodeType toAuthorative() {
100         var toscaNodeType = new ToscaNodeType();
101         super.setToscaEntity(toscaNodeType);
102         super.toAuthorative();
103
104         if (requirements != null) {
105             toscaNodeType.setRequirements(requirements.toAuthorative());
106         }
107
108         return toscaNodeType;
109     }
110
111     @Override
112     public void fromAuthorative(final ToscaNodeType toscaNodeType) {
113         super.fromAuthorative(toscaNodeType);
114
115         if (toscaNodeType.getRequirements() != null) {
116             requirements = new JpaToscaRequirements();
117             requirements.fromAuthorative(toscaNodeType.getRequirements());
118         }
119     }
120
121     @Override
122     public List<PfKey> getKeys() {
123         final List<PfKey> keyList = super.getKeys();
124
125         if (requirements != null) {
126             keyList.addAll(requirements.getKeys());
127         }
128
129         return keyList;
130     }
131
132     @Override
133     public void clean() {
134         super.clean();
135
136         if (requirements != null) {
137             requirements.clean();
138         }
139     }
140
141     @Override
142     public BeanValidationResult validate(String fieldName) {
143         return validateWithKey(fieldName);
144     }
145
146     @Override
147     public int compareTo(final PfConcept otherConcept) {
148         if (this == otherConcept) {
149             return 0;
150         }
151
152         int result = super.compareTo(otherConcept);
153         if (result != 0) {
154             return result;
155         }
156
157         final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
158
159         return ObjectUtils.compare(requirements, other.requirements);
160     }
161 }