Fix Reference Key columns persistence issue in db
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithStringProperties.java
1 /*-
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
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.models.tosca.simple.concepts;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 import javax.persistence.ElementCollection;
26 import javax.persistence.Lob;
27 import javax.persistence.MappedSuperclass;
28 import lombok.Data;
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;
38
39 /**
40  * Class to represent JPA TOSCA classes containing property maps whose values are Strings.
41  */
42 @MappedSuperclass
43 @Data
44 @EqualsAndHashCode(callSuper = true)
45 public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProperties> extends JpaToscaEntityType<T>
46                 implements PfAuthorative<T> {
47
48     private static final long serialVersionUID = 2785481541573683089L;
49
50     @ElementCollection
51     @Lob
52     private Map<@NotNull String, @NotNull String> properties;
53
54     /**
55      * The Default Constructor creates a {@link JpaToscaWithStringProperties} object with
56      * a null key.
57      */
58     protected JpaToscaWithStringProperties() {
59         this(new PfConceptKey());
60     }
61
62     /**
63      * The Key Constructor creates a {@link JpaToscaWithStringProperties} object with the
64      * given concept key.
65      *
66      * @param key the key
67      */
68     protected JpaToscaWithStringProperties(@NonNull final PfConceptKey key) {
69         super(key);
70     }
71
72     /**
73      * Copy constructor.
74      *
75      * @param copyConcept the concept to copy from
76      */
77     protected JpaToscaWithStringProperties(@NonNull final JpaToscaWithStringProperties<T> copyConcept) {
78         super(copyConcept);
79         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
80     }
81
82     /**
83      * Authorative constructor.
84      *
85      * @param authorativeConcept the authorative concept to copy from
86      */
87     protected JpaToscaWithStringProperties(final T authorativeConcept) {
88         super(new PfConceptKey());
89         this.fromAuthorative(authorativeConcept);
90     }
91
92     @Override
93     public T toAuthorative() {
94         T tosca = super.toAuthorative();
95
96         tosca.setProperties(PfUtils.mapMap(properties, this::deserializePropertyValue));
97
98         return tosca;
99     }
100
101     @Override
102     public void fromAuthorative(@NonNull final T authorativeConcept) {
103         super.fromAuthorative(authorativeConcept);
104
105         properties = PfUtils.mapMap(authorativeConcept.getProperties(), this::serializePropertyValue);
106     }
107
108     /**
109      * Deserializes a property value.
110      *
111      * @param propValue value to be deserialized
112      * @return the deserialized property value
113      */
114     protected abstract Object deserializePropertyValue(String propValue);
115
116     /**
117      * Serializes a property value.
118      *
119      * @param propValue value to be serialized
120      * @return the serialized property value
121      */
122     protected abstract String serializePropertyValue(Object propValue);
123
124
125     @Override
126     public void clean() {
127         super.clean();
128
129         properties = PfUtils.mapMap(properties, String::trim);
130     }
131
132     /**
133      * Validates the fields of the object, including its key.
134      *
135      * @param fieldName name of the field containing this
136      * @return the result, or {@code null}
137      */
138     protected BeanValidationResult validateWithKey(String fieldName) {
139         BeanValidationResult result = super.validate(fieldName);
140
141         validateKeyVersionNotNull(result, "key", getKey());
142
143         return result;
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         @SuppressWarnings("unchecked")
158         final JpaToscaWithStringProperties<T> other = (JpaToscaWithStringProperties<T>) otherConcept;
159
160         return PfUtils.compareMaps(properties, other.properties);
161     }
162 }