Replace Eclipselink with Hibernate
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithTypeAndStringProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021,2023 Nordix Foundation.
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 java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import javax.persistence.AttributeOverride;
28 import javax.persistence.AttributeOverrides;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Lob;
32 import javax.persistence.MappedSuperclass;
33 import javax.ws.rs.core.Response;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.models.base.PfAuthorative;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfModelRuntimeException;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
47
48 /**
49  * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
50  */
51 @MappedSuperclass
52 @Data
53 @EqualsAndHashCode(callSuper = true)
54 public abstract class JpaToscaWithTypeAndStringProperties<T extends ToscaWithTypeAndObjectProperties>
55         extends JpaToscaEntityType<T> implements PfAuthorative<T> {
56
57     private static final long serialVersionUID = 2785481541573683089L;
58
59     @Column
60     @AttributeOverrides ({
61         @AttributeOverride(name = "name", column = @Column(name = "type_name")),
62         @AttributeOverride(name = "version", column = @Column(name = "type_version"))
63     })
64     @VerifyKey
65     @NotNull
66     private PfConceptKey type;
67
68     @ElementCollection
69     @Lob
70     private Map<@NotNull String, @NotNull String> properties;
71
72     /**
73      * The Default Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with a null key.
74      */
75     protected JpaToscaWithTypeAndStringProperties() {
76         this(new PfConceptKey());
77     }
78
79     /**
80      * The Key Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with the given concept key.
81      *
82      * @param key the key
83      */
84     protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key) {
85         this(key, new PfConceptKey());
86     }
87
88     /**
89      * The full Constructor creates a {@link JpaToscaWithTypeAndStringProperties} object with all mandatory fields.
90      *
91      * @param key the key
92      * @param type the type of the policy
93      */
94     protected JpaToscaWithTypeAndStringProperties(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
95         super(key);
96         this.type = type;
97     }
98
99     /**
100      * Copy constructor.
101      *
102      * @param copyConcept the concept to copy from
103      */
104     protected JpaToscaWithTypeAndStringProperties(@NonNull final JpaToscaWithTypeAndStringProperties<T> copyConcept) {
105         super(copyConcept);
106         this.type = new PfConceptKey(copyConcept.type);
107         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
108     }
109
110     /**
111      * Authorative constructor.
112      *
113      * @param authorativeConcept the authorative concept to copy from
114      */
115     protected JpaToscaWithTypeAndStringProperties(final T authorativeConcept) {
116         super(new PfConceptKey());
117         type = new PfConceptKey();
118         this.fromAuthorative(authorativeConcept);
119     }
120
121     @Override
122     public T toAuthorative() {
123         var tosca = super.toAuthorative();
124
125         tosca.setType(type.getName());
126
127         if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
128             tosca.setTypeVersion(type.getVersion());
129         } else {
130             tosca.setTypeVersion(null);
131         }
132
133         tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
134
135         return tosca;
136     }
137
138     @Override
139     public void fromAuthorative(@NonNull final T authorativeConcept) {
140         super.fromAuthorative(authorativeConcept);
141
142         if (authorativeConcept.getType() != null) {
143             type.setName(authorativeConcept.getType());
144         } else {
145             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
146                     "Type not specified, the type of this TOSCA entity must be specified in the type field");
147         }
148
149         if (authorativeConcept.getTypeVersion() != null) {
150             type.setVersion(authorativeConcept.getTypeVersion());
151         } else {
152             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
153                     "Version not specified, the version of this TOSCA entity must be specified"
154                             + " in the type_version field");
155         }
156
157         properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
158     }
159
160     /**
161      * Deserializes a property value.
162      *
163      * @param propValue value to be deserialized
164      * @return the deserialized property value
165      */
166     protected abstract Object deserializePropertyValue(String propValue);
167
168     /**
169      * Serializes a property value.
170      *
171      * @param propValue value to be serialized
172      * @return the serialized property value
173      */
174     protected abstract String serializePropertyValue(Object propValue);
175
176     @Override
177     public List<PfKey> getKeys() {
178         final List<PfKey> keyList = super.getKeys();
179
180         keyList.addAll(type.getKeys());
181
182         return keyList;
183     }
184
185     @Override
186     public void clean() {
187         super.clean();
188
189         type.clean();
190
191         properties = PfUtils.mapMap(properties, String::trim);
192     }
193
194     /**
195      * Validates the fields of the object, including its key.
196      *
197      * @param fieldName name of the field containing this
198      * @return the result, or {@code null}
199      */
200     protected BeanValidationResult validateWithKey(String fieldName) {
201         BeanValidationResult result = super.validate(fieldName);
202
203         validateKeyVersionNotNull(result, "key", getKey());
204
205         return result;
206     }
207
208     @Override
209     public int compareTo(final PfConcept otherConcept) {
210         if (this == otherConcept) {
211             return 0;
212         }
213
214         int result = super.compareTo(otherConcept);
215         if (result != 0) {
216             return result;
217         }
218
219         @SuppressWarnings("unchecked")
220         final JpaToscaWithTypeAndStringProperties<T> other = (JpaToscaWithTypeAndStringProperties<T>) otherConcept;
221
222         result = type.compareTo(other.type);
223         if (result != 0) {
224             return result;
225         }
226
227         return PfUtils.compareMaps(properties, other.properties);
228     }
229 }