2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.jpa;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
26 import java.io.Serializable;
27 import java.util.Date;
28 import java.util.HashSet;
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.GeneratedValue;
35 import javax.persistence.GenerationType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.ManyToOne;
39 import javax.persistence.NamedQuery;
40 import javax.persistence.OneToMany;
41 import javax.persistence.OrderBy;
42 import javax.persistence.PrePersist;
43 import javax.persistence.PreUpdate;
44 import javax.persistence.Table;
45 import javax.persistence.Temporal;
46 import javax.persistence.TemporalType;
47 import javax.persistence.Transient;
50 import lombok.NoArgsConstructor;
54 * The persistent class for the Attribute database table.
58 @Table(name = "Attribute")
59 @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;
68 @GeneratedValue(strategy = GenerationType.AUTO)
72 // bi-directional many-to-one association to Category
74 @JoinColumn(name = "constraint_type", nullable = true)
76 private ConstraintType constraintType;
78 @Temporal(TemporalType.TIMESTAMP)
79 @Column(name = "created_date", updatable = false)
80 private Date createdDate;
82 @Column(name = "description", nullable = true, length = 2048)
83 private String description;
85 @Temporal(TemporalType.TIMESTAMP)
86 @Column(name = "modified_date", nullable = false)
87 private Date modifiedDate;
89 @Column(name = "PRIORITY", nullable = true)
91 private String priority;
93 @Column(name = "ATTRIBUTE_VALUE", nullable = true)
95 private String attributeValue;
97 @Column(name = "xacml_id", unique = true, nullable = false)
99 private String xacmlId = "urn";
101 // bi-directional many-to-one association to ConstraintValue
102 @OneToMany(mappedBy = "attribute", orphanRemoval = true, cascade = CascadeType.REMOVE)
104 private Set<ConstraintValue> constraintValues = new HashSet<>();
106 // bi-directional many-to-one association to Category
108 @JoinColumn(name = "category")
110 private Category categoryBean;
112 // bi-directional many-to-one association to Datatype
114 @JoinColumn(name = "datatype")
115 private Datatype datatypeBean;
117 @Column(name = "is_designator", nullable = false)
119 private char isDesignator = '1';
121 @Column(name = "selector_path", nullable = true, length = 2048)
122 private String selectorPath;
125 private String issuer = null;
128 private boolean mustBePresent = false;
130 @ManyToOne(optional = false)
131 @JoinColumn(name = "created_by")
132 private UserInfo userCreatedBy;
134 @ManyToOne(optional = false)
135 @JoinColumn(name = "modified_by")
136 private UserInfo userModifiedBy;
139 * Constructor with domain.
141 * @param domain the domain to use to construct the object
143 public Attribute(String domain) {
144 this.xacmlId = domain;
150 * @param copy the copy to copy from
152 public Attribute(Attribute copy) {
153 this(copy.getXacmlId() + ":(0)");
154 this.constraintType = copy.getConstraintType();
155 this.categoryBean = copy.getCategoryBean();
156 this.datatypeBean = copy.getDatatypeBean();
157 this.description = copy.getDescription();
158 for (ConstraintValue value : copy.getConstraintValues()) {
159 ConstraintValue newValue = new ConstraintValue(value);
160 newValue.setAttribute(this);
161 this.addConstraintValue(newValue);
166 * Called before an instance is persisted.
169 public void prePersist() {
170 Date date = new Date();
171 this.createdDate = date;
172 this.modifiedDate = date;
176 * Called before an instance is updated.
179 public void preUpdate() {
180 this.modifiedDate = new Date();
184 * Sets the constraint values.
186 * @param constraintValues the new constraint values
188 public void setConstraintValues(Set<ConstraintValue> constraintValues) {
189 if (this.constraintValues == null) {
190 this.constraintValues = new HashSet<>();
192 for (ConstraintValue value : this.constraintValues) {
193 value.setAttribute(this);
195 this.constraintValues = constraintValues;
199 * Adds the constraint value.
201 * @param constraintValue the constraint value
202 * @return the constraint value
204 public ConstraintValue addConstraintValue(ConstraintValue constraintValue) {
205 if (this.constraintValues == null) {
206 this.constraintValues = new HashSet<>();
208 this.constraintValues.add(constraintValue);
209 constraintValue.setAttribute(this);
211 return constraintValue;
215 * Removes the constraint value.
217 * @param constraintValue the constraint value
218 * @return the constraint value
220 public ConstraintValue removeConstraintValue(ConstraintValue constraintValue) {
221 this.constraintValues.remove(constraintValue);
222 constraintValue.setAttribute(null);
224 return constraintValue;
228 * Removes the all constraint values.
230 public void removeAllConstraintValues() {
231 if (this.constraintValues == null) {
234 for (ConstraintValue value : this.constraintValues) {
235 value.setAttribute(null);
237 this.constraintValues.clear();
246 public String getIssuer() {
253 * @param issuer the new issuer
256 public void setIssuer(String issuer) {
257 this.issuer = issuer;
261 * Checks if is must be present.
263 * @return true, if is must be present
266 public boolean isMustBePresent() {
267 return mustBePresent;
271 * Sets the must be present.
273 * @param mustBePresent the new must be present
276 public void setMustBePresent(boolean mustBePresent) {
277 this.mustBePresent = mustBePresent;
281 * Checks if is designator.
283 * @return true, if is designator
286 public boolean isDesignator() {
287 return this.isDesignator == '1';
291 * Sets the checks if is designator.
293 * @param is the new checks if is designator
296 public void setIsDesignator(boolean is) {
298 this.isDesignator = '1';
300 this.isDesignator = '0';