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 import lombok.Getter;
45 import lombok.Setter;
46 import lombok.ToString;
47
48 /**
49  * The persistent class for the Datatype database table.
50  *
51  */
52 @Entity
53 @Table(name = "Datatype")
54 @NamedQuery(name = "Datatype.findAll", query = "SELECT d FROM Datatype d")
55 @Getter
56 @Setter
57 @ToString
58 public class Datatype implements Serializable {
59     private static final long serialVersionUID = 1L;
60
61     public static final char STANDARD = 'S';
62     public static final char CUSTOM = 'C';
63
64     @Id
65     @GeneratedValue(strategy = GenerationType.AUTO)
66     @Column(name = "id")
67     private int id;
68
69     @Column(name = "is_standard", nullable = false)
70     private char isStandard;
71
72     @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
73     private String xacmlId;
74
75     @Column(name = "short_name", nullable = false, length = 64)
76     private String shortName;
77
78     // bi-directional many-to-one association to Attribute
79     @OneToMany(mappedBy = "datatypeBean")
80     @JsonBackReference
81     private Set<Attribute> attributes = new HashSet<>();
82
83     // bi-directional many-to-one association to Attribute
84     @OneToMany(mappedBy = "datatypeBean")
85     @JsonIgnore
86     private Set<FunctionDefinition> functions = new HashSet<>();
87
88     // bi-directional many-to-one association to Attribute
89     @OneToMany(mappedBy = "datatypeBean")
90     @JsonIgnore
91     private Set<FunctionArgument> arguments = new HashSet<>();
92
93     /**
94      * Instantiates a new datatype.
95      */
96     public Datatype() {
97         this.xacmlId = XACML3.ID_DATATYPE_STRING.stringValue();
98         this.isStandard = Datatype.STANDARD;
99     }
100
101     /**
102      * Instantiates a new datatype.
103      *
104      * @param id the id
105      * @param dt the dt
106      */
107     public Datatype(int id, Datatype dt) {
108         this.id = id;
109         this.isStandard = dt.isStandard;
110         this.xacmlId = dt.xacmlId;
111         this.shortName = dt.shortName;
112         //
113         // Make a copy?
114         //
115         this.attributes = new HashSet<>();
116     }
117
118     /**
119      * Instantiates a new datatype.
120      *
121      * @param identifier the identifier
122      * @param standard the standard
123      */
124     public Datatype(Identifier identifier, char standard) {
125         if (identifier != null) {
126             this.xacmlId = identifier.stringValue();
127
128         }
129         this.isStandard = standard;
130     }
131
132     /**
133      * Instantiates a new datatype.
134      *
135      * @param identifier the identifier
136      */
137     public Datatype(Identifier identifier) {
138         this(identifier, Datatype.STANDARD);
139     }
140
141     /**
142      * Adds the attribute.
143      *
144      * @param attribute the attribute
145      * @return the attribute
146      */
147     public Attribute addAttribute(Attribute attribute) {
148         getAttributes().add(attribute);
149         attribute.setDatatypeBean(this);
150
151         return attribute;
152     }
153
154     /**
155      * Removes the attribute.
156      *
157      * @param attribute the attribute
158      * @return the attribute
159      */
160     public Attribute removeAttribute(Attribute attribute) {
161         getAttributes().remove(attribute);
162         attribute.setDatatypeBean(null);
163
164         return attribute;
165     }
166
167     /**
168      * Removes the attribute.
169      *
170      * @param function the function
171      * @return the function definition
172      */
173     public FunctionDefinition removeAttribute(FunctionDefinition function) {
174         getFunctions().remove(function);
175         function.setDatatypeBean(null);
176
177         return function;
178     }
179
180     /**
181      * Adds the function.
182      *
183      * @param function the function
184      * @return the function definition
185      */
186     public FunctionDefinition addFunction(FunctionDefinition function) {
187         getFunctions().add(function);
188         function.setDatatypeBean(this);
189
190         return function;
191     }
192
193     /**
194      * Adds the argument.
195      *
196      * @param argument the argument
197      * @return the function argument
198      */
199     public FunctionArgument addArgument(FunctionArgument argument) {
200         getArguments().add(argument);
201         argument.setDatatypeBean(this);
202
203         return argument;
204     }
205
206     /**
207      * Removes the argument.
208      *
209      * @param argument the argument
210      * @return the function argument
211      */
212     public FunctionArgument removeArgument(FunctionArgument argument) {
213         getArguments().remove(argument);
214         argument.setDatatypeBean(null);
215
216         return argument;
217     }
218
219     /**
220      * Gets the identifer.
221      *
222      * @return the identifer
223      */
224     @Transient
225     public Identifier getIdentifer() {
226         return new IdentifierImpl(this.xacmlId);
227     }
228
229     /**
230      * Gets the identifer by short name.
231      *
232      * @return the identifer by short name
233      */
234     @Transient
235     public Identifier getIdentiferByShortName() {
236         return new IdentifierImpl(this.shortName);
237     }
238
239     /**
240      * Checks if is standard.
241      *
242      * @return true, if is standard
243      */
244     @Transient
245     public boolean isStandard() {
246         return this.isStandard == Datatype.STANDARD;
247     }
248
249     /**
250      * Checks if is custom.
251      *
252      * @return true, if is custom
253      */
254     @Transient
255     public boolean isCustom() {
256         return this.isStandard == Datatype.CUSTOM;
257     }
258 }