Fixed the Sonar technical debt.
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.rest.jpa;
22
23 import java.io.Serializable;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import javax.persistence.Column;
28 import javax.persistence.Entity;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.OneToMany;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36
37 import com.att.research.xacml.api.Identifier;
38 import com.att.research.xacml.api.XACML3;
39 import com.att.research.xacml.std.IdentifierImpl;
40 import com.fasterxml.jackson.annotation.JsonBackReference;
41 import com.fasterxml.jackson.annotation.JsonIgnore;
42
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         public Datatype() {
87                 this.xacmlId = XACML3.ID_DATATYPE_STRING.stringValue();
88                 this.isStandard = Datatype.STANDARD;
89         }
90         
91         
92         public Datatype(int id, Datatype dt) {
93                 this.id = id;
94                 this.isStandard = dt.isStandard;
95                 this.xacmlId = dt.xacmlId;
96                 this.shortName = dt.shortName;
97                 //
98                 // Make a copy?
99                 //
100                 this.attributes = new HashSet<>();
101         }
102         
103         public Datatype(Identifier identifier, char standard) {
104                 if (identifier != null) {
105                         this.xacmlId = identifier.stringValue();
106                         
107                 }
108                 this.isStandard = standard;
109         }
110         
111         public Datatype(Identifier identifier) {
112                 this(identifier, Datatype.STANDARD);
113         }
114         
115         public int getId() {
116                 return this.id;
117         }
118
119         public void setId(int id) {
120                 this.id = id;
121         }
122
123         public char getIsStandard() {
124                 return this.isStandard;
125         }
126
127         public void setIsStandard(char isStandard) {
128                 this.isStandard = isStandard;
129         }
130
131         public String getXacmlId() {
132                 return this.xacmlId;
133         }
134
135         public void setXacmlId(String xacmlId) {
136                 this.xacmlId = xacmlId;
137         }
138
139         public String getShortName() {
140                 return shortName;
141         }
142
143         public void setShortName(String shortName) {
144                 this.shortName = shortName;
145         }       
146
147         public Set<Attribute> getAttributes() {
148                 return this.attributes;
149         }
150
151         public void setAttributes(Set<Attribute> attributes) {
152                 this.attributes = attributes;
153         }
154
155         public Attribute addAttribute(Attribute attribute) {
156                 getAttributes().add(attribute);
157                 attribute.setDatatypeBean(this);
158
159                 return attribute;
160         }
161
162         public Attribute removeAttribute(Attribute attribute) {
163                 getAttributes().remove(attribute);
164                 attribute.setDatatypeBean(null);
165
166                 return attribute;
167         }
168
169         public Set<FunctionDefinition> getFunctions() {
170                 return this.functions;
171         }
172
173         public void setFunctions(Set<FunctionDefinition> functions) {
174                 this.functions = functions;
175         }
176
177         public FunctionDefinition addFunction(FunctionDefinition function) {
178                 getFunctions().add(function);
179                 function.setDatatypeBean(this);
180
181                 return function;
182         }
183
184         public FunctionDefinition removeAttribute(FunctionDefinition function) {
185                 getFunctions().remove(function);
186                 function.setDatatypeBean(null);
187
188                 return function;
189         }
190
191         public Set<FunctionArgument> getArguments() {
192                 return this.arguments;
193         }
194
195         public void setArguments(Set<FunctionArgument> argument) {
196                 this.arguments = argument;
197         }
198
199         public FunctionArgument addArgument(FunctionArgument argument) {
200                 getArguments().add(argument);
201                 argument.setDatatypeBean(this);
202
203                 return argument;
204         }
205
206         public FunctionArgument removeArgument(FunctionArgument argument) {
207                 getArguments().remove(argument);
208                 argument.setDatatypeBean(null);
209
210                 return argument;
211         }
212
213         @Transient
214         public Identifier getIdentifer() {
215                 return new IdentifierImpl(this.xacmlId);
216         }
217         
218         @Transient
219         public Identifier getIdentiferByShortName() {
220                 return new IdentifierImpl(this.shortName);
221         }
222
223         @Transient
224         public boolean isStandard() {
225                 return this.isStandard == Datatype.STANDARD;
226         }
227         
228         @Transient
229         public boolean isCustom() {
230                 return this.isStandard == Datatype.CUSTOM;
231         }
232
233         @Transient
234         @Override
235         public String toString() {
236                 return "Datatype [id=" + id + ", isStandard=" + isStandard
237                                 + ", xacmlId=" + xacmlId + ", shortName=" + shortName
238                                 + ", attributes=" + attributes + ", functions=" + functions
239                                 + ", arguments=" + arguments + "]";
240         }
241
242 }