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