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