2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. 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
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.tosca.simple.concepts;
23 import java.util.LinkedHashMap;
25 import javax.persistence.ElementCollection;
26 import javax.persistence.Lob;
27 import javax.persistence.MappedSuperclass;
29 import lombok.EqualsAndHashCode;
30 import lombok.NonNull;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.common.parameters.annotations.NotNull;
33 import org.onap.policy.models.base.PfAuthorative;
34 import org.onap.policy.models.base.PfConcept;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfUtils;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties;
40 * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
44 @EqualsAndHashCode(callSuper = true)
45 public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProperties> extends JpaToscaEntityType<T>
46 implements PfAuthorative<T> {
48 private static final long serialVersionUID = 2785481541573683089L;
52 private Map<@NotNull String, @NotNull String> properties;
55 * The Default Constructor creates a {@link JpaToscaWithStringProperties} object with
58 protected JpaToscaWithStringProperties() {
59 this(new PfConceptKey());
63 * The Key Constructor creates a {@link JpaToscaWithStringProperties} object with the
68 protected JpaToscaWithStringProperties(@NonNull final PfConceptKey key) {
75 * @param copyConcept the concept to copy from
77 protected JpaToscaWithStringProperties(@NonNull final JpaToscaWithStringProperties<T> copyConcept) {
79 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
83 * Authorative constructor.
85 * @param authorativeConcept the authorative concept to copy from
87 protected JpaToscaWithStringProperties(final T authorativeConcept) {
88 super(new PfConceptKey());
89 this.fromAuthorative(authorativeConcept);
93 public T toAuthorative() {
94 T tosca = super.toAuthorative();
96 tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
102 public void fromAuthorative(@NonNull final T authorativeConcept) {
103 super.fromAuthorative(authorativeConcept);
105 properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
109 * Deserializes a property value.
111 * @param propValue value to be deserialized
112 * @return the deserialized property value
114 protected abstract Object deserializePropertyValue(String propValue);
117 * Serializes a property value.
119 * @param propValue value to be serialized
120 * @return the serialized property value
122 protected abstract String serializePropertyValue(Object propValue);
126 public void clean() {
129 properties = PfUtils.mapMap(properties, String::trim);
133 * Validates the fields of the object, including its key.
135 * @param fieldName name of the field containing this
136 * @return the result, or {@code null}
138 protected BeanValidationResult validateWithKey(String fieldName) {
139 BeanValidationResult result = super.validate(fieldName);
141 validateKeyVersionNotNull(result, "key", getKey());
147 public int compareTo(final PfConcept otherConcept) {
148 if (this == otherConcept) {
152 int result = super.compareTo(otherConcept);
157 @SuppressWarnings("unchecked")
158 final JpaToscaWithStringProperties<T> other = (JpaToscaWithStringProperties<T>) otherConcept;
160 return PfUtils.compareMaps(properties, other.properties);