Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / concepts / AxPolicyModel.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.Entry;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Entity;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.JoinColumns;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.xml.bind.Marshaller;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.bind.annotation.XmlAccessType;
37 import javax.xml.bind.annotation.XmlAccessorType;
38 import javax.xml.bind.annotation.XmlElement;
39 import javax.xml.bind.annotation.XmlRootElement;
40 import javax.xml.bind.annotation.XmlType;
41 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
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.AxKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
51 import org.onap.policy.apex.model.basicmodel.handling.KeyInfoMarshalFilter;
52 import org.onap.policy.apex.model.basicmodel.service.ModelService;
53 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
54 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
55 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
56 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
57 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
58 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
59 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
60 import org.onap.policy.apex.model.policymodel.handling.EmptyAlbumsAdapter;
61 import org.onap.policy.common.utils.validation.Assertions;
62
63 /**
64  * A container class for an Apex policy model. This class is a container class that allows an Apex
65  * model to be constructed that contains definitions of all the context, event, and policy concepts
66  * required to run policies in Apex. The model contains schema definitions, definitions of events
67  * and context albums that use those schemas, definitions of tasks for policies and definitions of
68  * the policies themselves.
69  *
70  * <p>An Apex policy model is an important artifact in Apex. At editing time, an Apex editor creates
71  * and edits a policy model and a policy model is loaded into and is executed by an Apex engine.
72  * Therefore, an Apex model and the set of policies that it holds is the way that the policy domain
73  * that an Apex engine or a group of Apex engines executes across is expressed, both at design time
74  * and run time. The Apex deployment system is responsible for deploying Apex models to and the
75  * context they need the appropriate engines for execution.
76  *
77  * <p>Model registration is carried out by calling the {@code register()} method, which registers the
78  * policy model and all its constituent containers with the model service. The containers for
79  * context schemas, events, context albums, tasks, policies, and key information are all registered.
80  *
81  * <p>Once a policy model is composed, the overall structure of the policy model and all its references
82  * can be validated. During validation of a policy model, the validation checks listed below are
83  * executed:
84  * <ol>
85  * <li>The policy model is validated as an Apex model, which validates the existence, correctness,
86  * and duplication of all keys in the model as well as validating the key information of the keys,
87  * see validation in {@link AxModel}
88  * <li>The schemas in the model must be valid, see validation in {@link AxContextSchemas}
89  * <li>The context albums in the model must be valid, see validation in {@link AxContextAlbums}
90  * <li>The tasks in the model must be valid, see validation in {@link AxTasks}
91  * <li>The policies in the model must be valid, see validation in {@link AxPolicies}
92  * <li>The events in the model must be valid, see validation in {@link AxEvents}
93  * <li>The context schemas referred to in each field in every event must exist
94  * <li>The context schemas referred to in every context album must exist
95  * <li>The context schemas referred to in every task input field and output field must exist
96  * <li>The context albums referred to in every task must exist
97  * <li>The context albums referred to in every state must exist
98  * <li>The trigger event referred to in every state must exist
99  * <li>The default task referred to in every state must exist
100  * <li>In a state, an event that triggers a task must contain all the input fields required by that
101  * task
102  * <li>In a state, an event that is emitted by a task must contain all the output fields produced by
103  * that task
104  * <li>All tasks referred to by a state must exist
105  * <li>All events referred to on direct state outputs must exist
106  * </ol>
107  */
108 @Entity
109 @Table(name = "AxPolicyModel")
110
111 @XmlRootElement(name = "apexPolicyModel", namespace = "http://www.onap.org/policy/apex-pdp")
112 @XmlAccessorType(XmlAccessType.FIELD)
113 @XmlType(name = "AxPolicyModel", namespace = "http://www.onap.org/policy/apex-pdp",
114     propOrder = {"policies", "tasks", "events", "albums", "schemas"})
115
116 public class AxPolicyModel extends AxModel {
117     private static final String DOES_NOT_EXIST = " does not exist";
118
119     private static final long serialVersionUID = 8800599637708309945L;
120
121     // @formatter:off
122     @OneToOne(cascade = CascadeType.ALL)
123     @JoinColumns({@JoinColumn(name = "policiesName", referencedColumnName = "name"),
124             @JoinColumn(name = "policiesVersion", referencedColumnName = "version")})
125     @XmlElement(name = "policies", required = true)
126     private AxPolicies policies;
127
128     @OneToOne(cascade = CascadeType.ALL)
129     @JoinColumns({@JoinColumn(name = "tasksName", referencedColumnName = "name"),
130             @JoinColumn(name = "tasksVersion", referencedColumnName = "version")})
131     @XmlElement(name = "tasks", required = true)
132     private AxTasks tasks;
133
134     @OneToOne(cascade = CascadeType.ALL)
135     @JoinColumns({@JoinColumn(name = "eventsName", referencedColumnName = "name"),
136             @JoinColumn(name = "eventsVersion", referencedColumnName = "version")})
137     @XmlElement(name = "events", required = true)
138     private AxEvents events;
139
140     @OneToOne(cascade = CascadeType.ALL)
141     @JoinColumns({@JoinColumn(name = "albumsName", referencedColumnName = "name"),
142             @JoinColumn(name = "albumsVersion", referencedColumnName = "version")})
143     @XmlElement(name = "albums", required = false)
144     @XmlJavaTypeAdapter(EmptyAlbumsAdapter.class)
145     private AxContextAlbums albums;
146
147     @OneToOne(cascade = CascadeType.ALL)
148     @JoinColumns({@JoinColumn(name = "schemasName", referencedColumnName = "name"),
149             @JoinColumn(name = "schemasVersion", referencedColumnName = "version")})
150     @XmlElement(name = "schemas", required = true)
151     private AxContextSchemas schemas;
152     // @formatter:on
153
154     /**
155      * The Default Constructor creates a policy model with a null key and empty containers for
156      * schemas, key information, events, context albums, tasks and policies.
157      */
158     public AxPolicyModel() {
159         this(new AxArtifactKey());
160     }
161
162     /**
163      * Copy constructor.
164      *
165      * @param copyConcept the concept to copy from
166      */
167     public AxPolicyModel(final AxPolicyModel copyConcept) {
168         super(copyConcept);
169     }
170
171     /**
172      * The Keyed Constructor creates a policy model with the given key and empty containers for
173      * schemas, key information, events, context albums, tasks and policies.
174      *
175      * @param key the key
176      */
177     public AxPolicyModel(final AxArtifactKey key) {
178         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_Schemas", key.getVersion())),
179             new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())),
180             new AxEvents(new AxArtifactKey(key.getName() + "_Events", key.getVersion())),
181             new AxContextAlbums(new AxArtifactKey(key.getName() + "_Albums", key.getVersion())),
182             new AxTasks(new AxArtifactKey(key.getName() + "_Tasks", key.getVersion())),
183             new AxPolicies(new AxArtifactKey(key.getName() + "_Policies", key.getVersion())));
184     }
185
186     /**
187      * This Constructor creates a policy model with all of its fields specified.
188      *
189      * @param key the key of the policy model
190      * @param schemas the context schema container for the policy model
191      * @param keyInformation the key information container for the policy model
192      * @param events the event container for the policy model
193      * @param albums the context album container for the policy model
194      * @param tasks the task container for the policy model
195      * @param policies the policy container for the policy model
196      */
197     public AxPolicyModel(final AxArtifactKey key, final AxContextSchemas schemas, final AxKeyInformation keyInformation,
198             final AxEvents events, final AxContextAlbums albums, final AxTasks tasks, final AxPolicies policies) {
199         super(key, keyInformation);
200         Assertions.argumentNotNull(schemas, "schemas may not be null");
201         Assertions.argumentNotNull(events, "events may not be null");
202         Assertions.argumentNotNull(albums, "albums may not be null");
203         Assertions.argumentNotNull(tasks, "tasks may not be null");
204         Assertions.argumentNotNull(policies, "policies may not be null");
205
206         this.schemas = schemas;
207         this.events = events;
208         this.albums = albums;
209         this.tasks = tasks;
210         this.policies = policies;
211     }
212
213     /**
214      * {@inheritDoc}.
215      */
216     @Override
217     public void register() {
218         super.register();
219         ModelService.registerModel(AxContextSchemas.class, getSchemas());
220         ModelService.registerModel(AxEvents.class, getEvents());
221         ModelService.registerModel(AxContextAlbums.class, getAlbums());
222         ModelService.registerModel(AxTasks.class, getTasks());
223         ModelService.registerModel(AxPolicies.class, getPolicies());
224         ModelService.registerModel(AxPolicyModel.class, this);
225     }
226
227     /**
228      * {@inheritDoc}.
229      */
230     @Override
231     public List<AxKey> getKeys() {
232         final List<AxKey> keyList = super.getKeys();
233
234         keyList.addAll(schemas.getKeys());
235         keyList.addAll(events.getKeys());
236         keyList.addAll(albums.getKeys());
237         keyList.addAll(tasks.getKeys());
238         keyList.addAll(policies.getKeys());
239
240         return keyList;
241     }
242
243     /**
244      * Gets a context model from the policy model.
245      *
246      * @return the context model
247      */
248     public AxContextModel getContextModel() {
249         return new AxContextModel(new AxArtifactKey(albums.getKey().getName() + "_Model", albums.getKey().getVersion()),
250             getSchemas(), getAlbums(), getKeyInformation());
251     }
252
253     /**
254      * Gets the policy container from the policy model.
255      *
256      * @return the policy container with all the policies in the model
257      */
258     public AxPolicies getPolicies() {
259         return policies;
260     }
261
262     /**
263      * Sets the policy container for the policy model.
264      *
265      * @param policies the policy container with all the policies in the model
266      */
267     public void setPolicies(final AxPolicies policies) {
268         Assertions.argumentNotNull(policies, "policies may not be null");
269         this.policies = policies;
270     }
271
272     /**
273      * Gets the task container from the policy model.
274      *
275      * @return the task container with all the tasks in the model
276      */
277     public AxTasks getTasks() {
278         return tasks;
279     }
280
281     /**
282      * Sets the task container from the policy model.
283      *
284      * @param tasks the task container with all the tasks in the model
285      */
286     public void setTasks(final AxTasks tasks) {
287         Assertions.argumentNotNull(tasks, "tasks may not be null");
288         this.tasks = tasks;
289     }
290
291     /**
292      * Gets the event container from the policy model.
293      *
294      * @return the event container with all the events in the model
295      */
296     public AxEvents getEvents() {
297         return events;
298     }
299
300     /**
301      * Sets the event container from the policy model.
302      *
303      * @param events the event container with all the events in the model
304      */
305     public void setEvents(final AxEvents events) {
306         Assertions.argumentNotNull(events, "events may not be null");
307         this.events = events;
308     }
309
310     /**
311      * Gets the context album container from the policy model.
312      *
313      * @return the context album container with all the context albums in the model
314      */
315     public AxContextAlbums getAlbums() {
316         return albums;
317     }
318
319     /**
320      * Sets the context album container from the policy model.
321      *
322      * @param albums the context album container with all the context albums in the model
323      */
324     public void setAlbums(final AxContextAlbums albums) {
325         Assertions.argumentNotNull(albums, "albums may not be null");
326         this.albums = albums;
327     }
328
329     /**
330      * Gets the context schema container from the policy model.
331      *
332      * @return the context schema container with all the context schemas in the model
333      */
334     public AxContextSchemas getSchemas() {
335         return schemas;
336     }
337
338     /**
339      * Sets the context schema container from the policy model.
340      *
341      * @param schemas the context schema container with all the context schemas in the model
342      */
343     public void setSchemas(final AxContextSchemas schemas) {
344         Assertions.argumentNotNull(schemas, "schemas may not be null");
345         this.schemas = schemas;
346     }
347
348     /**
349      * {@inheritDoc}.
350      */
351     @Override
352     public AxValidationResult validate(final AxValidationResult resultIn) {
353         AxValidationResult result = resultIn;
354
355         result = super.validate(result);
356         result = schemas.validate(result);
357         result = events.validate(result);
358         result = albums.validate(result);
359         result = tasks.validate(result);
360         result = policies.validate(result);
361
362         validateEventKeys(result);
363         validateContextAlbumKeys(result);
364         result = validateAllTaskKeys(result);
365         validatePolicyKeys(result);
366
367         return result;
368     }
369
370     /**
371      * Validate all fundamental concepts keyed in events exist.
372      *
373      * @param result the validation result to return
374      * @return the result
375      */
376     private AxValidationResult validateEventKeys(final AxValidationResult result) {
377         for (final AxEvent event : events.getAll(null)) {
378             for (final AxField field : event.getFields()) {
379                 if (getSchemas().get(field.getSchema()) == null) {
380                     result.addValidationMessage(
381                         new AxValidationMessage(event.getKey(), this.getClass(), ValidationResult.INVALID,
382                             "event field data type " + field.getSchema().getId() + DOES_NOT_EXIST));
383                 }
384             }
385         }
386         return result;
387     }
388
389     /**
390      * Validate all fundamental concepts keyed in concept maps exist.
391      *
392      * @param result the validation result to return
393      * @return the result
394      */
395     private AxValidationResult validateContextAlbumKeys(final AxValidationResult result) {
396         for (final AxContextAlbum contextAlbum : albums.getAll(null)) {
397             if (getSchemas().get(contextAlbum.getItemSchema()) == null) {
398                 result.addValidationMessage(
399                     new AxValidationMessage(contextAlbum.getKey(), this.getClass(), ValidationResult.INVALID,
400                         "context album schema " + contextAlbum.getItemSchema().getId() + DOES_NOT_EXIST));
401             }
402         }
403         return result;
404     }
405
406     /**
407      * Validate all fundamental concepts keyed in tasks exist.
408      *
409      * @param result the validation result to return
410      * @return the result
411      */
412     private AxValidationResult validateAllTaskKeys(AxValidationResult result) {
413         for (final AxTask task : tasks.getAll(null)) {
414             result = validateTaskKeys(task, result);
415         }
416         return result;
417     }
418
419     /**
420      * Validate all fundamental concepts keyed in tasks exist.
421      *
422      * @param task The task to validate the keys of
423      * @param result the validation result to return
424      * @return the result
425      */
426     private AxValidationResult validateTaskKeys(final AxTask task, AxValidationResult result) {
427         for (final AxField field : task.getInputFieldSet()) {
428             if (getSchemas().get(field.getSchema()) == null) {
429                 result.addValidationMessage(new AxValidationMessage(task.getKey(), this.getClass(),
430                     ValidationResult.INVALID, "task input field schema " + field.getSchema().getId() + DOES_NOT_EXIST));
431             }
432         }
433         for (final AxField field : task.getOutputFieldSet()) {
434             if (getSchemas().get(field.getSchema()) == null) {
435                 result.addValidationMessage(
436                     new AxValidationMessage(task.getKey(), this.getClass(), ValidationResult.INVALID,
437                         "task output field schema " + field.getSchema().getId() + DOES_NOT_EXIST));
438             }
439         }
440         for (final AxArtifactKey contextAlbumKey : task.getContextAlbumReferences()) {
441             if (albums.get(contextAlbumKey) == null) {
442                 result.addValidationMessage(new AxValidationMessage(task.getKey(), this.getClass(),
443                     ValidationResult.INVALID, "task context album " + contextAlbumKey.getId() + DOES_NOT_EXIST));
444             }
445         }
446         return result;
447     }
448
449     /**
450      * Validate all fundamental concepts keyed in policies exist.
451      *
452      * @param result the validation result to return
453      * @return the result
454      */
455     private AxValidationResult validatePolicyKeys(final AxValidationResult result) {
456         for (final AxPolicy policy : policies.getAll(null)) {
457             for (final AxState state : policy.getStateMap().values()) {
458                 validateStateReferences(state, result);
459             }
460         }
461
462         return result;
463     }
464
465     /**
466      * Validate that the references used on a state are valid.
467      *
468      * @param state The state to check
469      * @param result the validation result to append to
470      */
471     private void validateStateReferences(AxState state, AxValidationResult result) {
472         for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
473             if (albums.get(contextAlbumKey) == null) {
474                 result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
475                     ValidationResult.INVALID, "state context album " + contextAlbumKey.getId() + DOES_NOT_EXIST));
476             }
477         }
478
479         final AxEvent triggerEvent = events.getEventMap().get(state.getTrigger());
480         if (triggerEvent == null) {
481             result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
482                 ValidationResult.INVALID, "state trigger event " + state.getTrigger().getId() + DOES_NOT_EXIST));
483         }
484
485         final AxTask defaultTask = tasks.getTaskMap().get(state.getDefaultTask());
486         if (defaultTask == null) {
487             result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
488                 ValidationResult.INVALID, "state default task " + state.getDefaultTask().getId() + DOES_NOT_EXIST));
489         }
490
491         // Check task input fields and event fields are compatible for default tasks with no task
492         // selection logic
493         if (state.getTaskSelectionLogic().getKey().equals(AxReferenceKey.getNullKey())
494                 && triggerEvent != null && defaultTask != null) {
495             final Set<AxField> unhandledTaskInputFields = new TreeSet<>(defaultTask.getInputFieldSet());
496             unhandledTaskInputFields.removeAll(triggerEvent.getFields());
497             for (final AxField unhandledTaskInputField : unhandledTaskInputFields) {
498                 result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
499                     ValidationResult.INVALID, "task input field " + unhandledTaskInputField + " for task "
500                         + defaultTask.getId() + " not in trigger event " + triggerEvent.getId()));
501             }
502         }
503
504         for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
505             if (events.getEventMap().get(stateOutput.getOutgingEvent()) == null) {
506                 result.addValidationMessage(new AxValidationMessage(stateOutput.getKey(), this.getClass(),
507                     ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId()
508                         + " for state output " + stateOutput.getId() + DOES_NOT_EXIST));
509             }
510         }
511
512         validateEventTaskFieldCompatibilityOnState(state, result);
513     }
514
515     /**
516      * Validate that the fields on tasks and events that trigger them and are output by them are
517      * compatible for all tasks used on a state.
518      *
519      * @param state The state to check
520      * @param result the validation result to append to
521      */
522     private void validateEventTaskFieldCompatibilityOnState(AxState state, AxValidationResult result) {
523         // Check task output fields and event fields are compatible for tasks that directly
524         // reference state outputs
525         for (final Entry<AxArtifactKey, AxStateTaskReference> taskRefEntry : state.getTaskReferences().entrySet()) {
526             if (!taskRefEntry.getValue().getStateTaskOutputType().equals(AxStateTaskOutputType.DIRECT)) {
527                 continue;
528             }
529
530             final AxTask usedTask = tasks.getTaskMap().get(taskRefEntry.getKey());
531             if (usedTask == null) {
532                 result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
533                     ValidationResult.INVALID, "state task " + taskRefEntry.getKey().getId() + DOES_NOT_EXIST));
534             } else {
535                 AxStateOutput stateOutput =
536                     state.getStateOutputs().get(taskRefEntry.getValue().getOutput().getKey().getLocalName());
537                 validateEventTaskFieldCompatibilityOnStateOutput(state, usedTask, stateOutput, result);
538             }
539         }
540     }
541
542     /**
543      * Validate that the fields on a task of a state output and the events that trigger it are
544      * compatible.
545      *
546      * @param state The state to check
547      * @param task The task to check
548      * @param stateOutput The state output to check
549      * @param result the validation result to append to
550      */
551     private void validateEventTaskFieldCompatibilityOnStateOutput(final AxState state, final AxTask task,
552             final AxStateOutput stateOutput, AxValidationResult result) {
553         if (stateOutput == null) {
554             result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
555                 ValidationResult.INVALID, "state output on task reference for task " + task.getId() + " is null"));
556
557         } else {
558             final AxEvent usedEvent = events.getEventMap().get(stateOutput.getOutgingEvent());
559             if (usedEvent == null) {
560                 result.addValidationMessage(new AxValidationMessage(stateOutput.getKey(), this.getClass(),
561                     ValidationResult.INVALID, "output event " + stateOutput.getOutgingEvent().getId()
562                         + " for state output " + stateOutput.getId() + DOES_NOT_EXIST));
563             }
564
565             if (task != null && usedEvent != null) {
566                 final Set<AxField> unhandledTaskOutputFields = new TreeSet<>(task.getOutputFieldSet());
567                 unhandledTaskOutputFields.removeAll(usedEvent.getFields());
568                 for (final AxField unhandledTaskOutputField : unhandledTaskOutputFields) {
569                     result.addValidationMessage(new AxValidationMessage(state.getKey(), this.getClass(),
570                         ValidationResult.INVALID, "task output field " + unhandledTaskOutputField + " for task "
571                             + task.getId() + " not in output event " + usedEvent.getId()));
572                 }
573             }
574         }
575     }
576
577     /**
578      * When a model is unmarshalled from disk or from the database, if the albums field was missing a blank
579      * with a null key was added. This method is called by JAXB after unmarshalling and is
580      * used to insert an appropriate key
581      *
582      * @param unmarshaller the unmarshaller that is unmarshalling the model
583      * @param parent the parent object of this object in the unmarshaller
584      */
585     public void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
586         new EmptyAlbumsAdapter().doAfterUnmarshal(this);
587     }
588
589     /**
590      * When a model is marshalled from disk or database, if the albums field is empty/null, then the albums field
591      * is not emitted. If the (empty) albums field is not emitted then it's keyinfo should also be suppressed
592      * This method is called by JAXB before marshaling and is used to insert the appropriate filters
593      *
594      * @param marshaller the marshaller that is marshaller the model
595      * @throws Exception if there is a problem with the marshalling
596      */
597     public void beforeMarshal(final Marshaller marshaller) throws Exception {
598         EmptyAlbumsAdapter albumsfilter = new EmptyAlbumsAdapter();
599         marshaller.setAdapter(EmptyAlbumsAdapter.class, albumsfilter);
600         //get/create the keyinfofilter
601         KeyInfoMarshalFilter keyinfoFilter = marshaller.getAdapter(KeyInfoMarshalFilter.class);
602         if (keyinfoFilter == null) {
603             keyinfoFilter = new KeyInfoMarshalFilter();
604         }
605         //if the albumsfilter would filter out this model's albums add the album's key to the keyinfofilter
606         if (albumsfilter.marshal(this.albums) == null && this.albums != null) {
607             keyinfoFilter.addFilterKey(this.albums.getKey());
608         }
609         marshaller.setAdapter(keyinfoFilter);
610     }
611
612     /**
613      * {@inheritDoc}.
614      */
615     @Override
616     public void clean() {
617         super.clean();
618         policies.clean();
619         tasks.clean();
620         events.clean();
621         albums.clean();
622         schemas.clean();
623     }
624
625     /**
626      * {@inheritDoc}.
627      */
628     @Override
629     public String toString() {
630         final StringBuilder builder = new StringBuilder();
631         builder.append(this.getClass().getSimpleName());
632         builder.append(":(");
633         builder.append(super.toString());
634         builder.append(",policies=");
635         builder.append(policies);
636         builder.append(",tasks=");
637         builder.append(tasks);
638         builder.append(",events=");
639         builder.append(events);
640         builder.append(",albums=");
641         builder.append(albums);
642         builder.append(",schemas=");
643         builder.append(schemas);
644         builder.append(")");
645         return builder.toString();
646     }
647
648     /**
649      * {@inheritDoc}.
650      */
651     @Override
652     public AxConcept copyTo(final AxConcept targetObject) {
653         Assertions.argumentNotNull(targetObject, "target may not be null");
654
655         final Object copyObject = targetObject;
656         Assertions.instanceOf(copyObject, AxPolicyModel.class);
657
658         final AxPolicyModel copy = ((AxPolicyModel) copyObject);
659         super.copyTo(targetObject);
660         copy.setPolicies(new AxPolicies(policies));
661         copy.setTasks(new AxTasks(tasks));
662         copy.setEvents(new AxEvents(events));
663         copy.setAlbums(new AxContextAlbums(albums));
664         copy.setSchemas(new AxContextSchemas(schemas));
665
666         return copy;
667     }
668
669     /**
670      * {@inheritDoc}.
671      */
672     @Override
673     public int hashCode() {
674         final int prime = 31;
675         int result = 1;
676         result = prime * result + super.hashCode();
677         result = prime * result + policies.hashCode();
678         result = prime * result + tasks.hashCode();
679         result = prime * result + events.hashCode();
680         result = prime * result + albums.hashCode();
681         result = prime * result + schemas.hashCode();
682         return result;
683     }
684
685     /**
686      * {@inheritDoc}.
687      */
688     @Override
689     public boolean equals(final Object obj) {
690         if (obj == null) {
691             throw new IllegalArgumentException("comparison object may not be null");
692         }
693
694         if (this == obj) {
695             return true;
696         }
697         if (getClass() != obj.getClass()) {
698             return false;
699         }
700
701         final AxPolicyModel other = (AxPolicyModel) obj;
702         if (!super.equals(other)) {
703             return false;
704         }
705         if (!policies.equals(other.policies)) {
706             return false;
707         }
708         if (!tasks.equals(other.tasks)) {
709             return false;
710         }
711         if (!events.equals(other.events)) {
712             return false;
713         }
714         if (!albums.equals(other.albums)) {
715             return false;
716         }
717         return schemas.equals(other.schemas);
718     }
719
720     /**
721      * {@inheritDoc}.
722      */
723     @Override
724     public int compareTo(final AxConcept otherObj) {
725         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
726
727         if (this == otherObj) {
728             return 0;
729         }
730         if (getClass() != otherObj.getClass()) {
731             return this.hashCode() - otherObj.hashCode();
732         }
733
734         final AxPolicyModel other = (AxPolicyModel) otherObj;
735         if (!super.equals(other)) {
736             return super.compareTo(other);
737         }
738         if (!policies.equals(other.policies)) {
739             return policies.compareTo(other.policies);
740         }
741         if (!tasks.equals(other.tasks)) {
742             return tasks.compareTo(other.tasks);
743         }
744         if (!events.equals(other.events)) {
745             return events.compareTo(other.events);
746         }
747         if (!albums.equals(other.albums)) {
748             return albums.compareTo(other.albums);
749         }
750         return schemas.compareTo(other.schemas);
751     }
752 }