Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / event-model / src / main / java / org / onap / policy / apex / model / eventmodel / concepts / AxField.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.eventmodel.concepts;
23
24 import java.util.List;
25 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
27 import javax.persistence.Column;
28 import javax.persistence.Embedded;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
34 import javax.xml.bind.annotation.XmlAccessType;
35 import javax.xml.bind.annotation.XmlAccessorType;
36 import javax.xml.bind.annotation.XmlElement;
37 import javax.xml.bind.annotation.XmlRootElement;
38 import javax.xml.bind.annotation.XmlType;
39 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
48 import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
49 import org.onap.policy.common.utils.validation.Assertions;
50
51 /**
52  * In Apex, a field is an input or output parameter to or from a concept. For example, the parameters of an event are
53  * fields and the input and output of a task is defined as a collection of fields.
54  *
55  * <p>A field has an {@link AxReferenceKey} key that defines its name and parent, and a {@link AxArtifactKey} key to a
56  * context schema that defines the structure of the data atom that holds the value of the field. Fields can be specified
57  * as being optional but are mandatory by default.
58  *
59  * <p>Validation checks that the field key and the field schema reference key are not null.
60  */
61 @Entity
62 @Table(name = "AxField")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlRootElement(name = "apexField", namespace = "http://www.onap.org/policy/apex-pdp")
67 @XmlType(name = "AxField", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
68     { "key", "fieldSchemaKey", "optional" })
69
70 public class AxField extends AxConcept {
71     private static final String KEY_MAY_NOT_BE_NULL = "key may not be null";
72     private static final String FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL = "fieldSchemaKey may not be null";
73
74     private static final long serialVersionUID = -6443016863162692288L;
75
76     private static final int HASH_PRIME_0 = 1231;
77     private static final int HASH_PRIME_1 = 1237;
78
79     @EmbeddedId()
80     @XmlElement(name = "key", required = true)
81     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
82     private AxReferenceKey key;
83
84     // @formatter:off
85     @Embedded
86     @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "fieldSchemaName")),
87             @AttributeOverride(name = "version", column = @Column(name = "fieldSchemaVersion")) })
88     @Column(name = "fieldSchemaKey")
89     @XmlElement(required = true)
90     private AxArtifactKey fieldSchemaKey;
91     // @formatter:on
92
93     @Column(name = "optional")
94     @XmlElement(required = false)
95     private boolean optional;
96
97     /**
98      * The default constructor creates a field with a null artifact and schema key.
99      */
100     public AxField() {
101         this(new AxReferenceKey());
102         optional = false;
103     }
104
105     /**
106      * The default constructor creates a field with the given artifact key and a null schema key.
107      *
108      * @param key the field key
109      */
110     public AxField(final AxReferenceKey key) {
111         this(key, new AxArtifactKey());
112     }
113
114     /**
115      * Copy constructor.
116      *
117      * @param copyConcept the concept to copy from
118      */
119     public AxField(final AxField copyConcept) {
120         super(copyConcept);
121     }
122
123     /**
124      * Constructor to create the field with both its keys defined.
125      *
126      * @param key the field key
127      * @param fieldSchemaKey the key of the field schema to use for this field
128      */
129     public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey) {
130         super();
131         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
132         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
133
134         this.key = key;
135         this.fieldSchemaKey = fieldSchemaKey;
136     }
137
138     /**
139      * Constructor to create the field with all its fields defined.
140      *
141      * @param key the field key
142      * @param fieldSchemaKey the key of the field schema to use for this field
143      * @param optional true if this field is optional
144      */
145     public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey, final boolean optional) {
146         super();
147         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
148         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
149
150         this.key = key;
151         this.fieldSchemaKey = fieldSchemaKey;
152         this.optional = optional;
153     }
154
155     /**
156      * Constructor to create the field with the local name of its reference key defined and its schema key defined.
157      *
158      * @param localName the local name of the field reference key
159      * @param fieldSchemaKey the key of the field schema to use for this field
160      */
161     public AxField(final String localName, final AxArtifactKey fieldSchemaKey) {
162         super();
163         Assertions.argumentNotNull(localName, "localName may not be null");
164         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
165
166         key = new AxReferenceKey();
167         key.setLocalName(localName);
168         this.fieldSchemaKey = fieldSchemaKey;
169     }
170
171     /**
172      * Constructor to create the field with the local name of its reference key defined, its schema key and optionality
173      * defined.
174      *
175      * @param localName the local name of the field reference key
176      * @param fieldSchemaKey the key of the field schema to use for this field
177      * @param optional true if this field is optional
178      */
179     public AxField(final String localName, final AxArtifactKey fieldSchemaKey, final boolean optional) {
180         super();
181         Assertions.argumentNotNull(localName, "localName may not be null");
182         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
183
184         key = new AxReferenceKey();
185         key.setLocalName(localName);
186         this.fieldSchemaKey = fieldSchemaKey;
187         this.optional = optional;
188     }
189
190     /**
191      * {@inheritDoc}.
192      */
193     @Override
194     public AxReferenceKey getKey() {
195         return key;
196     }
197
198     /**
199      * {@inheritDoc}.
200      */
201     @Override
202     public List<AxKey> getKeys() {
203         final List<AxKey> keyList = key.getKeys();
204         keyList.add(new AxKeyUse(fieldSchemaKey));
205         return keyList;
206     }
207
208     /**
209      * Sets the reference key of the field.
210      *
211      * @param key the field reference key
212      */
213     public void setKey(final AxReferenceKey key) {
214         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
215         this.key = key;
216     }
217
218     /**
219      * Gets the key of the field schema.
220      *
221      * @return the field schema key
222      */
223     public AxArtifactKey getSchema() {
224         return fieldSchemaKey;
225     }
226
227     /**
228      * Sets the key of the field schema.
229      *
230      * @param schema the field schema key
231      */
232     public void setSchema(final AxArtifactKey schema) {
233         Assertions.argumentNotNull(schema, "schema may not be null");
234         this.fieldSchemaKey = schema;
235     }
236
237     /**
238      * Gets the optionality of the field.
239      *
240      * @return the field optional flag
241      */
242     public boolean getOptional() {
243         return optional;
244     }
245
246     /**
247      * Sets the optionality of the field.
248      *
249      * @param optional the optionality of the field
250      */
251     public void setOptional(final boolean optional) {
252         this.optional = optional;
253     }
254
255     /**
256      * {@inheritDoc}.
257      */
258     @Override
259     public AxValidationResult validate(final AxValidationResult resultIn) {
260         AxValidationResult result = resultIn;
261
262         if (key.equals(AxReferenceKey.getNullKey())) {
263             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264                             "key is a null key"));
265         }
266
267         result = key.validate(result);
268
269         if (fieldSchemaKey.equals(AxArtifactKey.getNullKey())) {
270             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271                             "fieldSchemaKey is a null key: " + fieldSchemaKey));
272         }
273         return fieldSchemaKey.validate(result);
274     }
275
276     /**
277      * {@inheritDoc}.
278      */
279     @Override
280     public void clean() {
281         key.clean();
282         fieldSchemaKey.clean();
283     }
284
285     /**
286      * {@inheritDoc}.
287      */
288     @Override
289     public String toString() {
290         final StringBuilder builder = new StringBuilder();
291         builder.append(this.getClass().getSimpleName());
292         builder.append(":(");
293         builder.append("key=");
294         builder.append(key);
295         builder.append(",fieldSchemaKey=");
296         builder.append(fieldSchemaKey);
297         builder.append(",optional=");
298         builder.append(optional);
299         builder.append(")");
300         return builder.toString();
301     }
302
303     /**
304      * {@inheritDoc}.
305      */
306     @Override
307     public AxConcept copyTo(final AxConcept targetObject) {
308         Assertions.argumentNotNull(targetObject, "target may not be null");
309
310         final Object copyObject = targetObject;
311         Assertions.instanceOf(copyObject, AxField.class);
312
313         final AxField copy = ((AxField) copyObject);
314         copy.setKey(new AxReferenceKey(key));
315         copy.setSchema(new AxArtifactKey(fieldSchemaKey));
316         copy.setOptional(optional);
317         return copy;
318     }
319
320     /**
321      * {@inheritDoc}.
322      */
323     @Override
324     public int hashCode() {
325         final int prime = 31;
326         int result = 1;
327         result = prime * result + key.hashCode();
328         result = prime * result + fieldSchemaKey.hashCode();
329         result = prime * result + (optional ? HASH_PRIME_0 : HASH_PRIME_1);
330         return result;
331     }
332
333     /*
334      * (nonJavadoc)
335      *
336      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
337      */
338     @Override
339     public boolean equals(final Object obj) {
340         if (obj == null) {
341             return false;
342         }
343         if (this == obj) {
344             return true;
345         }
346
347         if (!(obj instanceof AxField)) {
348             return false;
349         }
350
351         final AxField other = (AxField) obj;
352         if (!key.getLocalName().equals(other.key.getLocalName())) {
353             return false;
354         }
355         if (optional != other.optional) {
356             return false;
357         }
358         return fieldSchemaKey.equals(other.fieldSchemaKey);
359     }
360
361     /**
362      * {@inheritDoc}.
363      */
364     @Override
365     public int compareTo(final AxConcept otherObj) {
366         if (otherObj == null) {
367             return 1;
368         }
369         if (this == otherObj) {
370             return 0;
371         }
372         if (!(otherObj instanceof AxField)) {
373             return this.hashCode() - otherObj.hashCode();
374         }
375
376         final AxField other = (AxField) otherObj;
377         if (!key.getLocalName().equals(other.key.getLocalName())) {
378             return key.getLocalName().compareTo(other.key.getLocalName());
379         }
380         if (optional != other.optional) {
381             return (optional ? 1 : -1);
382         }
383         return fieldSchemaKey.compareTo(other.fieldSchemaKey);
384     }
385 }