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