Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / context-model / src / main / java / org / onap / policy / apex / model / contextmodel / concepts / AxContextAlbums.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.apex.model.contextmodel.concepts;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import javax.persistence.CascadeType;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinTable;
35 import javax.persistence.OneToMany;
36 import javax.persistence.Table;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.bind.annotation.XmlAccessType;
39 import javax.xml.bind.annotation.XmlAccessorType;
40 import javax.xml.bind.annotation.XmlElement;
41 import javax.xml.bind.annotation.XmlType;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
50 import org.onap.policy.common.utils.validation.Assertions;
51
52 /**
53  * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
54  * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
55  * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
56  * be retrieved by calling methods directly on this class without referencing the contained map.
57  *
58  * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
59  * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
60  * map key matches the key in the map value for all album entries. Each context album entry is then validated
61  * individually.
62  */
63 @Entity
64 @Table(name = "AxContextAlbums")
65
66 @XmlAccessorType(XmlAccessType.FIELD)
67 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
68     { "key", "albums" })
69
70 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
71     private static final long serialVersionUID = -4844259809024470975L;
72
73     @EmbeddedId
74     @XmlElement(name = "key", required = true)
75     private AxArtifactKey key;
76
77     // @formatter:off
78     @OneToMany(cascade = CascadeType.ALL)
79     @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
80             @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
81     @XmlElement(name = "albums", required = true)
82     private Map<AxArtifactKey, AxContextAlbum> albums;
83     // @formatter:on
84
85     /**
86      * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
87      * context album map.
88      */
89     public AxContextAlbums() {
90         this(new AxArtifactKey());
91     }
92
93     /**
94      * Copy constructor.
95      *
96      * @param copyConcept the concept to copy from
97      */
98     public AxContextAlbums(final AxContextAlbums copyConcept) {
99         super(copyConcept);
100     }
101
102     /**
103      * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
104      * context album map.
105      *
106      * @param key the key of the context album container
107      */
108     public AxContextAlbums(final AxArtifactKey key) {
109         this(key, new TreeMap<AxArtifactKey, AxContextAlbum>());
110     }
111
112     /**
113      * Constructor that creates the context album map with the given albums and key.
114      *
115      * @param key the key of the context album container
116      * @param albums the context albums to place in this context album container
117      */
118     public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
119         super();
120         Assertions.argumentNotNull(key, "key may not be null");
121         Assertions.argumentNotNull(albums, "albums may not be null");
122
123         this.key = key;
124         this.albums = new TreeMap<>();
125         this.albums.putAll(albums);
126     }
127
128     /**
129      * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
130      * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
131      * that it will work with the {@link AxConceptGetter} interface.
132      *
133      * @param unmarsaller the unmarshaler that is unmarshaling the model
134      * @param parent the parent object of this object in the unmarshaler
135      */
136     public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
137         Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
138         Assertions.argumentNotNull(parent, "parent should not be null");
139
140         // The map must be navigable to allow name and version searching, unmarshaling returns a
141         // hash map
142         final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
143         navigableAlbums.putAll(albums);
144         albums = navigableAlbums;
145     }
146
147     /**
148      * {@inheritDoc}.
149      */
150     @Override
151     public AxArtifactKey getKey() {
152         return key;
153     }
154
155     /**
156      * {@inheritDoc}.
157      */
158     @Override
159     public List<AxKey> getKeys() {
160         final List<AxKey> keyList = key.getKeys();
161
162         for (final AxContextAlbum contextAlbum : albums.values()) {
163             keyList.addAll(contextAlbum.getKeys());
164         }
165
166         return keyList;
167     }
168
169     /**
170      * Sets the key of the context album container.
171      *
172      * @param key the context album container key
173      */
174     public void setKey(final AxArtifactKey key) {
175         Assertions.argumentNotNull(key, "key may not be null");
176         this.key = key;
177     }
178
179     /**
180      * Gets the map of context albums from the context album container.
181      *
182      * @return the context album map
183      */
184     public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
185         return albums;
186     }
187
188     /**
189      * Sets the map of context albums from the context album container.
190      *
191      * @param albumsMap the map of context albums to place in the container
192      */
193     public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
194         Assertions.argumentNotNull(albumsMap, "albums may not be null");
195         this.albums = new TreeMap<>();
196         this.albums.putAll(albumsMap);
197     }
198
199     /**
200      * {@inheritDoc}.
201      */
202     @Override
203     public void clean() {
204         key.clean();
205         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
206             contextAlbumEntry.getKey().clean();
207             contextAlbumEntry.getValue().clean();
208         }
209     }
210
211     /**
212      * {@inheritDoc}.
213      */
214     @Override
215     public String toString() {
216         final StringBuilder builder = new StringBuilder();
217         builder.append(this.getClass().getSimpleName());
218         builder.append(":(");
219         builder.append(this.getClass().getSimpleName());
220         builder.append(":(");
221         builder.append("key=");
222         builder.append(key);
223         builder.append(",albums=");
224         builder.append(albums);
225         builder.append(")");
226         return builder.toString();
227     }
228
229     /**
230      * {@inheritDoc}.
231      */
232     @Override
233     public AxValidationResult validate(final AxValidationResult resultIn) {
234         AxValidationResult result = resultIn;
235
236         if (key.equals(AxArtifactKey.getNullKey())) {
237             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238                             "key is a null key"));
239         }
240
241         result = key.validate(result);
242
243         if (albums.size() == 0) {
244             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
245                             "albums are empty"));
246         } else {
247             for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
248                 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
249                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
250                                     "key on context album entry " + contextAlbumEntry.getKey()
251                                                     + " may not be the null key"));
252                 } else if (contextAlbumEntry.getValue() == null) {
253                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
254                                     "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
255                 } else {
256                     validateContextAlbumKey(result, contextAlbumEntry);
257
258                     result = contextAlbumEntry.getValue().validate(result);
259                 }
260             }
261         }
262
263         return result;
264     }
265
266     private void validateContextAlbumKey(final AxValidationResult result,
267                     final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
268         if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
269             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
270                             "key on context album entry key " + contextAlbumEntry.getKey()
271                                             + " does not equal context album value key "
272                                             + contextAlbumEntry.getValue().getKey()));
273         }
274     }
275
276     /**
277      * {@inheritDoc}.
278      */
279     @Override
280     public AxConcept copyTo(final AxConcept target) {
281         Assertions.argumentNotNull(target, "target may not be null");
282
283         final Object copyObject = target;
284         Assertions.instanceOf(copyObject, AxContextAlbums.class);
285
286         final AxContextAlbums copy = ((AxContextAlbums) copyObject);
287         copy.setKey(key);
288         final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
289         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
290             newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
291                             new AxContextAlbum(contextAlbumEntry.getValue()));
292         }
293         copy.setAlbumsMap(newContextAlbum);
294
295         return copy;
296     }
297
298     /**
299      * {@inheritDoc}.
300      */
301     @Override
302     public int hashCode() {
303         final int prime = 31;
304         int result = 1;
305         result = prime * result + key.hashCode();
306         result = prime * result + albums.hashCode();
307         return result;
308     }
309
310     /**
311      * {@inheritDoc}.
312      */
313     @Override
314     public boolean equals(final Object obj) {
315         if (obj == null) {
316             return false;
317         }
318         if (this == obj) {
319             return true;
320         }
321
322         if (getClass() != obj.getClass()) {
323             return false;
324         }
325
326         final AxContextAlbums other = (AxContextAlbums) obj;
327         if (!key.equals(other.key)) {
328             return false;
329         }
330         return albums.equals(other.albums);
331     }
332
333     /**
334      * {@inheritDoc}.
335      */
336     @Override
337     public int compareTo(final AxConcept otherObj) {
338         if (otherObj == null) {
339             return -1;
340         }
341         if (this == otherObj) {
342             return 0;
343         }
344         if (getClass() != otherObj.getClass()) {
345             return this.hashCode() - otherObj.hashCode();
346         }
347
348         final AxContextAlbums other = (AxContextAlbums) otherObj;
349         if (!key.equals(other.key)) {
350             return key.compareTo(other.key);
351         }
352         if (!albums.equals(other.albums)) {
353             return (albums.hashCode() - other.albums.hashCode());
354         }
355
356         return 0;
357     }
358
359     /**
360      * {@inheritDoc}.
361      */
362     @Override
363     public AxContextAlbum get(final AxArtifactKey conceptKey) {
364         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
365     }
366
367     /**
368      * {@inheritDoc}.
369      */
370     @Override
371     public AxContextAlbum get(final String conceptKeyName) {
372         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
373     }
374
375     /**
376      * {@inheritDoc}.
377      */
378     @Override
379     public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
380         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
381                         conceptKeyVersion);
382     }
383
384     /**
385      * {@inheritDoc}.
386      */
387     @Override
388     public Set<AxContextAlbum> getAll(final String conceptKeyName) {
389         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
390     }
391
392     /**
393      * {@inheritDoc}.
394      */
395     @Override
396     public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
397         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,
398                         conceptKeyVersion);
399     }
400 }