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