7285e2e36148af106070721d83861a7027188a6b
[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         }
155         
156         public Attribute(String domain) {
157                 this.xacmlId = domain;
158         }
159         
160         public Attribute(Attribute copy) {
161                 this(copy.getXacmlId() + ":(0)");
162                 this.constraintType = copy.getConstraintType();
163                 this.categoryBean = copy.getCategoryBean();
164                 this.datatypeBean = copy.getDatatypeBean();
165                 this.description = copy.getDescription();
166                 for (ConstraintValue value : copy.getConstraintValues()) {
167                         ConstraintValue newValue = new ConstraintValue(value);
168                         newValue.setAttribute(this);
169                         this.addConstraintValue(newValue);
170                 }
171         }
172
173         @PrePersist
174         public void     prePersist() {
175                 Date date = new Date();
176                 this.createdDate = date;
177                 this.modifiedDate = date;
178         }
179         
180         @PreUpdate
181         public void preUpdate() {
182                 this.modifiedDate = new Date();
183                 try {
184                         this.userModifiedBy = XacmlAdminAuthorization.getUserId();
185                 } catch (Exception e) {
186                         logger.error("Exception caused While adding Modified by Role"+e);
187                 }
188         }
189
190         public int getId() {
191                 return this.id;
192         }
193
194         public void setId(int id) {
195                 this.id = id;
196         }
197
198         public ConstraintType getConstraintType() {
199                 return this.constraintType;
200         }
201
202         public void setConstraintType(ConstraintType constraintType) {
203                 this.constraintType = constraintType;
204         }
205
206         public Date getCreatedDate() {
207                 return this.createdDate;
208         }
209
210         public void setCreatedDate(Date createdDate) {
211                 this.createdDate = createdDate;
212         }
213
214         public String getDescription() {
215                 return this.description;
216         }
217
218         public void setDescription(String description) {
219                 this.description = description;
220         }
221
222         public Date getModifiedDate() {
223                 return this.modifiedDate;
224         }
225
226         public void setModifiedDate(Date modifiedDate) {
227                 this.modifiedDate = modifiedDate;
228         }
229
230         public String getXacmlId() {
231                 return this.xacmlId;
232         }
233         
234 /*      @Transient
235         public Identifier getXacmlIdentifier() {
236                 return new IdentifierImpl(this.xacmlId);
237         }*/
238
239         public void setXacmlId(String xacmlId) {
240                 this.xacmlId = xacmlId;
241         }
242
243         public Set<ConstraintValue> getConstraintValues() {
244                 return this.constraintValues;
245         }
246
247         public void setConstraintValues(Set<ConstraintValue> constraintValues) {
248                 for (ConstraintValue value : this.constraintValues) {
249                         value.setAttribute(this);
250                 }
251                 this.constraintValues = constraintValues;
252         }
253
254         public ConstraintValue addConstraintValue(ConstraintValue constraintValue) {
255                 if (this.constraintValues == null) {
256                         this.constraintValues = new HashSet<>();
257                 }
258                 this.constraintValues.add(constraintValue);
259                 constraintValue.setAttribute(this);
260
261                 return constraintValue;
262         }
263
264         public ConstraintValue removeConstraintValue(ConstraintValue constraintValue) {
265                 this.constraintValues.remove(constraintValue);
266                 constraintValue.setAttribute(null);
267
268                 return constraintValue;
269         }
270         
271         public void removeAllConstraintValues() {
272                 if (this.constraintValues == null) {
273                         return;
274                 }
275                 for (ConstraintValue value : this.constraintValues) {
276                         value.setAttribute(null);
277                 }
278                 this.constraintValues.clear();
279         }
280
281         public Category getCategoryBean() {
282                 return this.categoryBean;
283         }
284
285         public void setCategoryBean(Category categoryBean) {
286                 this.categoryBean = categoryBean;
287         }
288
289         public Datatype getDatatypeBean() {
290                 return this.datatypeBean;
291         }
292
293         public void setDatatypeBean(Datatype datatypeBean) {
294                 this.datatypeBean = datatypeBean;
295         }
296
297         public char getIsDesignator() {
298                 return this.isDesignator;
299         }
300         
301         public void setIsDesignator(char is) {
302                 this.isDesignator = is;
303         }
304         
305         public String getSelectorPath() {
306                 return this.selectorPath;
307         }
308         
309         public void setSelectorPath(String path) {
310                 this.selectorPath = path;
311         }
312         
313         @Transient
314         public String getIssuer() {
315                 return issuer;
316         }
317
318         @Transient
319         public void setIssuer(String issuer) {
320                 this.issuer = issuer;
321         }
322
323         @Transient
324         public boolean isMustBePresent() {
325                 return mustBePresent;
326         }
327
328         @Transient
329         public void setMustBePresent(boolean mustBePresent) {
330                 this.mustBePresent = mustBePresent;
331         }
332
333         @Transient
334         public boolean isDesignator() {
335                 return (this.isDesignator == '1');
336         }
337         
338         @Transient
339         public void setIsDesignator(boolean is) {
340                 if (is) {
341                         this.isDesignator = '1';
342                 } else {
343                         this.isDesignator = '0';
344                 }
345         }       
346         
347         public String getPriority() {
348                 return priority;
349         }
350
351         public void setPriority(String priority) {
352                 this.priority = priority;
353         }
354         
355         public String getAttributeValue() {
356                 return attributeValue;
357         }
358
359         public void setAttributeValue(String attributeValue) {
360                 this.attributeValue = attributeValue;
361         }
362 }
363