JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / Datatype.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.jpa;
23
24 import com.att.research.xacml.api.Identifier;
25 import com.att.research.xacml.api.XACML3;
26 import com.att.research.xacml.std.IdentifierImpl;
27 import com.fasterxml.jackson.annotation.JsonBackReference;
28 import com.fasterxml.jackson.annotation.JsonIgnore;
29
30 import java.io.Serializable;
31 import java.util.HashSet;
32 import java.util.Set;
33
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.GeneratedValue;
37 import javax.persistence.GenerationType;
38 import javax.persistence.Id;
39 import javax.persistence.NamedQuery;
40 import javax.persistence.OneToMany;
41 import javax.persistence.Table;
42 import javax.persistence.Transient;
43
44 /**
45  * The persistent class for the Datatype database table.
46  *
47  */
48 @Entity
49 @Table(name = "Datatype")
50 @NamedQuery(name = "Datatype.findAll", query = "SELECT d FROM Datatype d")
51 public class Datatype implements Serializable {
52     private static final long serialVersionUID = 1L;
53
54     public static final char STANDARD = 'S';
55     public static final char CUSTOM = 'C';
56
57     @Id
58     @GeneratedValue(strategy = GenerationType.AUTO)
59     @Column(name = "id")
60     private int id;
61
62     @Column(name = "is_standard", nullable = false)
63     private char isStandard;
64
65     @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
66     private String xacmlId;
67
68     @Column(name = "short_name", nullable = false, length = 64)
69     private String shortName;
70
71     // bi-directional many-to-one association to Attribute
72     @OneToMany(mappedBy = "datatypeBean")
73     @JsonBackReference
74     private Set<Attribute> attributes = new HashSet<>();
75
76     // bi-directional many-to-one association to Attribute
77     @OneToMany(mappedBy = "datatypeBean")
78     @JsonIgnore
79     private Set<FunctionDefinition> functions = new HashSet<>();
80
81     // bi-directional many-to-one association to Attribute
82     @OneToMany(mappedBy = "datatypeBean")
83     @JsonIgnore
84     private Set<FunctionArgument> arguments = new HashSet<>();
85
86     /**
87      * Instantiates a new datatype.
88      */
89     public Datatype() {
90         this.xacmlId = XACML3.ID_DATATYPE_STRING.stringValue();
91         this.isStandard = Datatype.STANDARD;
92     }
93
94     /**
95      * Instantiates a new datatype.
96      *
97      * @param id the id
98      * @param dt the dt
99      */
100     public Datatype(int id, Datatype dt) {
101         this.id = id;
102         this.isStandard = dt.isStandard;
103         this.xacmlId = dt.xacmlId;
104         this.shortName = dt.shortName;
105         //
106         // Make a copy?
107         //
108         this.attributes = new HashSet<>();
109     }
110
111     /**
112      * Instantiates a new datatype.
113      *
114      * @param identifier the identifier
115      * @param standard the standard
116      */
117     public Datatype(Identifier identifier, char standard) {
118         if (identifier != null) {
119             this.xacmlId = identifier.stringValue();
120
121         }
122         this.isStandard = standard;
123     }
124
125     /**
126      * Instantiates a new datatype.
127      *
128      * @param identifier the identifier
129      */
130     public Datatype(Identifier identifier) {
131         this(identifier, Datatype.STANDARD);
132     }
133
134     /**
135      * Gets the id.
136      *
137      * @return the id
138      */
139     public int getId() {
140         return this.id;
141     }
142
143     /**
144      * Sets the id.
145      *
146      * @param id the new id
147      */
148     public void setId(int id) {
149         this.id = id;
150     }
151
152     /**
153      * Gets the checks if is standard.
154      *
155      * @return the checks if is standard
156      */
157     public char getIsStandard() {
158         return this.isStandard;
159     }
160
161     /**
162      * Sets the checks if is standard.
163      *
164      * @param isStandard the new checks if is standard
165      */
166     public void setIsStandard(char isStandard) {
167         this.isStandard = isStandard;
168     }
169
170     /**
171      * Gets the xacml id.
172      *
173      * @return the xacml id
174      */
175     public String getXacmlId() {
176         return this.xacmlId;
177     }
178
179     /**
180      * Sets the xacml id.
181      *
182      * @param xacmlId the new xacml id
183      */
184     public void setXacmlId(String xacmlId) {
185         this.xacmlId = xacmlId;
186     }
187
188     /**
189      * Gets the short name.
190      *
191      * @return the short name
192      */
193     public String getShortName() {
194         return shortName;
195     }
196
197     /**
198      * Sets the short name.
199      *
200      * @param shortName the new short name
201      */
202     public void setShortName(String shortName) {
203         this.shortName = shortName;
204     }
205
206     /**
207      * Gets the attributes.
208      *
209      * @return the attributes
210      */
211     public Set<Attribute> getAttributes() {
212         return this.attributes;
213     }
214
215     /**
216      * Sets the attributes.
217      *
218      * @param attributes the new attributes
219      */
220     public void setAttributes(Set<Attribute> attributes) {
221         this.attributes = attributes;
222     }
223
224     /**
225      * Adds the attribute.
226      *
227      * @param attribute the attribute
228      * @return the attribute
229      */
230     public Attribute addAttribute(Attribute attribute) {
231         getAttributes().add(attribute);
232         attribute.setDatatypeBean(this);
233
234         return attribute;
235     }
236
237     /**
238      * Removes the attribute.
239      *
240      * @param attribute the attribute
241      * @return the attribute
242      */
243     public Attribute removeAttribute(Attribute attribute) {
244         getAttributes().remove(attribute);
245         attribute.setDatatypeBean(null);
246
247         return attribute;
248     }
249
250     /**
251      * Removes the attribute.
252      *
253      * @param function the function
254      * @return the function definition
255      */
256     public FunctionDefinition removeAttribute(FunctionDefinition function) {
257         getFunctions().remove(function);
258         function.setDatatypeBean(null);
259
260         return function;
261     }
262
263     /**
264      * Gets the functions.
265      *
266      * @return the functions
267      */
268     public Set<FunctionDefinition> getFunctions() {
269         return this.functions;
270     }
271
272     /**
273      * Sets the functions.
274      *
275      * @param functions the new functions
276      */
277     public void setFunctions(Set<FunctionDefinition> functions) {
278         this.functions = functions;
279     }
280
281     /**
282      * Adds the function.
283      *
284      * @param function the function
285      * @return the function definition
286      */
287     public FunctionDefinition addFunction(FunctionDefinition function) {
288         getFunctions().add(function);
289         function.setDatatypeBean(this);
290
291         return function;
292     }
293
294     /**
295      * Gets the arguments.
296      *
297      * @return the arguments
298      */
299     public Set<FunctionArgument> getArguments() {
300         return this.arguments;
301     }
302
303     /**
304      * Sets the arguments.
305      *
306      * @param argument the new arguments
307      */
308     public void setArguments(Set<FunctionArgument> argument) {
309         this.arguments = argument;
310     }
311
312     /**
313      * Adds the argument.
314      *
315      * @param argument the argument
316      * @return the function argument
317      */
318     public FunctionArgument addArgument(FunctionArgument argument) {
319         getArguments().add(argument);
320         argument.setDatatypeBean(this);
321
322         return argument;
323     }
324
325     /**
326      * Removes the argument.
327      *
328      * @param argument the argument
329      * @return the function argument
330      */
331     public FunctionArgument removeArgument(FunctionArgument argument) {
332         getArguments().remove(argument);
333         argument.setDatatypeBean(null);
334
335         return argument;
336     }
337
338     /**
339      * Gets the identifer.
340      *
341      * @return the identifer
342      */
343     @Transient
344     public Identifier getIdentifer() {
345         return new IdentifierImpl(this.xacmlId);
346     }
347
348     /**
349      * Gets the identifer by short name.
350      *
351      * @return the identifer by short name
352      */
353     @Transient
354     public Identifier getIdentiferByShortName() {
355         return new IdentifierImpl(this.shortName);
356     }
357
358     /**
359      * Checks if is standard.
360      *
361      * @return true, if is standard
362      */
363     @Transient
364     public boolean isStandard() {
365         return this.isStandard == Datatype.STANDARD;
366     }
367
368     /**
369      * Checks if is custom.
370      *
371      * @return true, if is custom
372      */
373     @Transient
374     public boolean isCustom() {
375         return this.isStandard == Datatype.CUSTOM;
376     }
377
378     /**
379      * To string.
380      *
381      * @return the string
382      */
383     @Transient
384     @Override
385     public String toString() {
386         return "Datatype [id=" + id + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId + ", shortName=" + shortName
387                         + ", attributes=" + attributes + ", functions=" + functions + ", arguments=" + arguments + "]";
388     }
389
390 }