Use ValidationResult for models v2.0
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.pdp.persistence.concepts;
25
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import javax.persistence.CascadeType;
32 import javax.persistence.CollectionTable;
33 import javax.persistence.Column;
34 import javax.persistence.ElementCollection;
35 import javax.persistence.EmbeddedId;
36 import javax.persistence.Entity;
37 import javax.persistence.FetchType;
38 import javax.persistence.Inheritance;
39 import javax.persistence.InheritanceType;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.OneToMany;
42 import javax.persistence.Table;
43 import lombok.Data;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.BeanValidationResult;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.Validated;
55 import org.onap.policy.models.pdp.concepts.PdpGroup;
56 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
57 import org.onap.policy.models.pdp.enums.PdpState;
58
59 /**
60  * Class to represent a PDP group in the database.
61  *
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64 @Entity
65 @Table(name = "PdpGroup")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @Data
68 @EqualsAndHashCode(callSuper = false)
69 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
70     private static final long serialVersionUID = -357224425637789775L;
71
72     @EmbeddedId
73     private PfConceptKey key;
74
75     @Column
76     private String description;
77
78     @Column
79     private PdpState pdpGroupState;
80
81     @ElementCollection
82     private Map<String, String> properties;
83
84     // @formatter:off
85     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
86     @CollectionTable(joinColumns = {
87             @JoinColumn(name = "pdpGroupParentKeyName",    referencedColumnName = "parentKeyName"),
88             @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
89             @JoinColumn(name = "pdpGroupParentLocalName",  referencedColumnName = "parentLocalName"),
90             @JoinColumn(name = "pdpGroupLocalName",        referencedColumnName = "localName")
91         })
92     // @formatter:on
93     private List<JpaPdpSubGroup> pdpSubGroups;
94
95     /**
96      * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
97      */
98     public JpaPdpGroup() {
99         this(new PfConceptKey());
100     }
101
102     /**
103      * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
104      *
105      * @param key the key
106      */
107     public JpaPdpGroup(@NonNull final PfConceptKey key) {
108         this(key, PdpState.PASSIVE, new ArrayList<>());
109     }
110
111     /**
112      * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
113      *
114      * @param key the key
115      * @param pdpGroupState State of the PDP group
116      */
117     public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
118             @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
119         this.key = key;
120         this.pdpGroupState = pdpGroupState;
121         this.pdpSubGroups = pdpSubGroups;
122     }
123
124     /**
125      * Copy constructor.
126      *
127      * @param copyConcept the concept to copy from
128      */
129     public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
130         super(copyConcept);
131         this.key = new PfConceptKey(copyConcept.key);
132         this.description = copyConcept.description;
133         this.pdpGroupState = copyConcept.pdpGroupState;
134         this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
135         this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
136     }
137
138     /**
139      * Authorative constructor.
140      *
141      * @param authorativeConcept the authorative concept to copy from
142      */
143     public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
144         this.fromAuthorative(authorativeConcept);
145     }
146
147     @Override
148     public PdpGroup toAuthorative() {
149         PdpGroup pdpGroup = new PdpGroup();
150
151         pdpGroup.setName(getKey().getName());
152         pdpGroup.setVersion(getKey().getVersion());
153         pdpGroup.setDescription(description);
154         pdpGroup.setPdpGroupState(pdpGroupState);
155
156         pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
157
158         pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
159         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
160             pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
161         }
162
163         return pdpGroup;
164     }
165
166     @Override
167     public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
168         if (this.key == null || this.getKey().isNullKey()) {
169             this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
170         }
171
172         this.description = pdpGroup.getDescription();
173         this.pdpGroupState = pdpGroup.getPdpGroupState();
174
175         this.properties =
176                 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
177
178         this.pdpSubGroups = new ArrayList<>();
179         for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
180             JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
181             jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
182             jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
183             this.pdpSubGroups.add(jpaPdpSubGroup);
184         }
185     }
186
187     @Override
188     public List<PfKey> getKeys() {
189         List<PfKey> keyList = getKey().getKeys();
190
191         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
192             keyList.addAll(jpaPdpSubgroup.getKeys());
193         }
194
195         return keyList;
196     }
197
198     @Override
199     public void clean() {
200         key.clean();
201
202         description = (description == null ? null : description.trim());
203
204         if (properties != null) {
205             Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
206             for (Entry<String, String> propertyEntry : properties.entrySet()) {
207                 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
208             }
209             properties = cleanedPropertyMap;
210         }
211
212         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
213             jpaPdpSubgroup.clean();
214         }
215     }
216
217     @Override
218     public BeanValidationResult validate(@NonNull String fieldName) {
219         BeanValidationResult result = new BeanValidationResult(fieldName, this);
220
221         result.addResult(validateKeyNotNull("key", key));
222         result.addResult(validateNotBlank("description", description, false));
223         result.addResult(validateNotNull("pdpGroupState", pdpGroupState));
224
225         validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank);
226
227         result.addResult(validateNotNull("pdpSubGroups", pdpSubGroups));
228         validateList(result, "pdpSubGroups", pdpSubGroups, Validated::validateNotNull);
229
230         return result;
231     }
232
233     @Override
234     public int compareTo(final PfConcept otherConcept) {
235         if (otherConcept == null) {
236             return -1;
237         }
238         if (this == otherConcept) {
239             return 0;
240         }
241         if (getClass() != otherConcept.getClass()) {
242             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
243         }
244
245         final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
246         if (!key.equals(other.key)) {
247             return key.compareTo(other.key);
248         }
249
250         int result = ObjectUtils.compare(description, other.description);
251         if (result != 0) {
252             return result;
253         }
254
255         result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
256         if (result != 0) {
257             return result;
258         }
259
260         result = PfUtils.compareObjects(properties, other.properties);
261         if (result != 0) {
262             return result;
263         }
264
265         return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
266     }
267 }