Added Junis for Policy ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / Attribute.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.Date;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.GeneratedValue;
32 import javax.persistence.GenerationType;
33 import javax.persistence.Id;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.ManyToOne;
36 import javax.persistence.NamedQuery;
37 import javax.persistence.OneToMany;
38 import javax.persistence.OrderBy;
39 import javax.persistence.PrePersist;
40 import javax.persistence.PreUpdate;
41 import javax.persistence.Table;
42 import javax.persistence.Temporal;
43 import javax.persistence.TemporalType;
44 import javax.persistence.Transient;
45
46 import com.fasterxml.jackson.annotation.JsonIgnore;
47
48 /**
49  * The persistent class for the Attribute database table.
50  * 
51  */
52 @Entity
53 @Table(name="Attribute")
54 @NamedQuery(name="Attribute.findAll", query="SELECT a FROM Attribute a order by  a.priority asc, a.xacmlId asc")
55 public class Attribute implements Serializable {
56         private static final long serialVersionUID = 1L;
57         
58         @Id
59         @GeneratedValue(strategy = GenerationType.AUTO)
60         @Column(name="id")
61         private int id;
62
63         //bi-directional many-to-one association to Category
64         @ManyToOne
65         @JoinColumn(name="constraint_type", nullable=true)
66         @JsonIgnore
67         private ConstraintType constraintType;
68
69         @Temporal(TemporalType.TIMESTAMP)
70         @Column(name="created_date", updatable=false)
71         private Date createdDate;
72
73         @Column(name="description", nullable=true, length=2048)
74         private String description;
75
76         @Temporal(TemporalType.TIMESTAMP)
77         @Column(name="modified_date", nullable=false)
78         private Date modifiedDate;
79
80         @Column(name="PRIORITY", nullable=true)
81         @OrderBy("asc")
82         private String priority;
83         
84         @Column(name="ATTRIBUTE_VALUE", nullable=true)
85         @OrderBy("asc")
86         private String attributeValue;
87
88         @Column(name="xacml_id",  unique = true, nullable=false)
89         @OrderBy("asc")
90         private String xacmlId = "urn";
91
92         //bi-directional many-to-one association to ConstraintValue
93         @OneToMany(mappedBy="attribute", orphanRemoval=true, cascade=CascadeType.REMOVE)
94         @JsonIgnore
95         private Set<ConstraintValue> constraintValues = new HashSet<>();
96
97         //bi-directional many-to-one association to Category
98         @ManyToOne
99         @JoinColumn(name="category")
100         @JsonIgnore
101         private Category categoryBean;
102
103         //bi-directional many-to-one association to Datatype
104         @ManyToOne
105         @JoinColumn(name="datatype")
106         private Datatype datatypeBean;
107
108         @Column(name="is_designator", nullable=false)
109         @JsonIgnore
110         private char isDesignator = '1';
111
112         @Column(name="selector_path", nullable=true, length=2048)
113         private String selectorPath;
114         
115
116         
117         @Transient
118         private String issuer = null;
119         
120         @Transient
121         private boolean mustBePresent = false;
122
123         @ManyToOne(optional = false)
124         @JoinColumn(name="created_by")
125         private UserInfo userCreatedBy;
126
127         @ManyToOne(optional = false)
128         @JoinColumn(name="modified_by")
129         private UserInfo userModifiedBy;
130         
131         public UserInfo getUserCreatedBy() {
132                 return userCreatedBy;
133         }
134
135         public void setUserCreatedBy(UserInfo userCreatedBy) {
136                 this.userCreatedBy = userCreatedBy;
137         }
138
139         public UserInfo getUserModifiedBy() {
140                 return userModifiedBy;
141         }
142
143         public void setUserModifiedBy(UserInfo userModifiedBy) {
144                 this.userModifiedBy = userModifiedBy;
145         }
146
147         
148         public Attribute() {
149                 //An empty constructor
150         }
151         
152         public Attribute(String domain) {
153                 this.xacmlId = domain;
154         }
155         
156         public Attribute(Attribute copy) {
157                 this(copy.getXacmlId() + ":(0)");
158                 this.constraintType = copy.getConstraintType();
159                 this.categoryBean = copy.getCategoryBean();
160                 this.datatypeBean = copy.getDatatypeBean();
161                 this.description = copy.getDescription();
162                 for (ConstraintValue value : copy.getConstraintValues()) {
163                         ConstraintValue newValue = new ConstraintValue(value);
164                         newValue.setAttribute(this);
165                         this.addConstraintValue(newValue);
166                 }
167         }
168
169         @PrePersist
170         public void     prePersist() {
171                 Date date = new Date();
172                 this.createdDate = date;
173                 this.modifiedDate = date;
174         }
175         
176         @PreUpdate
177         public void preUpdate() {
178                 this.modifiedDate = new Date();
179         }
180
181         public int getId() {
182                 return this.id;
183         }
184
185         public void setId(int id) {
186                 this.id = id;
187         }
188
189         public ConstraintType getConstraintType() {
190                 return this.constraintType;
191         }
192
193         public void setConstraintType(ConstraintType constraintType) {
194                 this.constraintType = constraintType;
195         }
196
197         public Date getCreatedDate() {
198                 return this.createdDate;
199         }
200
201         public void setCreatedDate(Date createdDate) {
202                 this.createdDate = createdDate;
203         }
204
205         public String getDescription() {
206                 return this.description;
207         }
208
209         public void setDescription(String description) {
210                 this.description = description;
211         }
212
213         public Date getModifiedDate() {
214                 return this.modifiedDate;
215         }
216
217         public void setModifiedDate(Date modifiedDate) {
218                 this.modifiedDate = modifiedDate;
219         }
220
221         public String getXacmlId() {
222                 return this.xacmlId;
223         }
224
225
226         public void setXacmlId(String xacmlId) {
227                 this.xacmlId = xacmlId;
228         }
229
230         public Set<ConstraintValue> getConstraintValues() {
231                 return this.constraintValues;
232         }
233
234         public void setConstraintValues(Set<ConstraintValue> constraintValues) {
235                 for (ConstraintValue value : this.constraintValues) {
236                         value.setAttribute(this);
237                 }
238                 this.constraintValues = constraintValues;
239         }
240
241         public ConstraintValue addConstraintValue(ConstraintValue constraintValue) {
242                 if (this.constraintValues == null) {
243                         this.constraintValues = new HashSet<>();
244                 }
245                 this.constraintValues.add(constraintValue);
246                 constraintValue.setAttribute(this);
247
248                 return constraintValue;
249         }
250
251         public ConstraintValue removeConstraintValue(ConstraintValue constraintValue) {
252                 this.constraintValues.remove(constraintValue);
253                 constraintValue.setAttribute(null);
254
255                 return constraintValue;
256         }
257         
258         public void removeAllConstraintValues() {
259                 if (this.constraintValues == null) {
260                         return;
261                 }
262                 for (ConstraintValue value : this.constraintValues) {
263                         value.setAttribute(null);
264                 }
265                 this.constraintValues.clear();
266         }
267
268         public Category getCategoryBean() {
269                 return this.categoryBean;
270         }
271
272         public void setCategoryBean(Category categoryBean) {
273                 this.categoryBean = categoryBean;
274         }
275
276         public Datatype getDatatypeBean() {
277                 return this.datatypeBean;
278         }
279
280         public void setDatatypeBean(Datatype datatypeBean) {
281                 this.datatypeBean = datatypeBean;
282         }
283
284         public char getIsDesignator() {
285                 return this.isDesignator;
286         }
287         
288         public void setIsDesignator(char is) {
289                 this.isDesignator = is;
290         }
291         
292         public String getSelectorPath() {
293                 return this.selectorPath;
294         }
295         
296         public void setSelectorPath(String path) {
297                 this.selectorPath = path;
298         }
299         
300         @Transient
301         public String getIssuer() {
302                 return issuer;
303         }
304
305         @Transient
306         public void setIssuer(String issuer) {
307                 this.issuer = issuer;
308         }
309
310         @Transient
311         public boolean isMustBePresent() {
312                 return mustBePresent;
313         }
314
315         @Transient
316         public void setMustBePresent(boolean mustBePresent) {
317                 this.mustBePresent = mustBePresent;
318         }
319
320         @Transient
321         public boolean isDesignator() {
322                 return this.isDesignator == '1';
323         }
324         
325         @Transient
326         public void setIsDesignator(boolean is) {
327                 if (is) {
328                         this.isDesignator = '1';
329                 } else {
330                         this.isDesignator = '0';
331                 }
332         }       
333         
334         public String getPriority() {
335                 return priority;
336         }
337
338         public void setPriority(String priority) {
339                 this.priority = priority;
340         }
341         
342         public String getAttributeValue() {
343                 return attributeValue;
344         }
345
346         public void setAttributeValue(String attributeValue) {
347                 this.attributeValue = attributeValue;
348         }
349 }
350