JUnit/SONAR/Checkstyle in ONAP-REST
[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-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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.rest.jpa;
23
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25
26 import java.io.Serializable;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.Set;
30
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;
48
49 import lombok.Getter;
50 import lombok.NoArgsConstructor;
51 import lombok.Setter;
52
53 /**
54  * The persistent class for the Attribute database table.
55  *
56  */
57 @Entity
58 @Table(name = "Attribute")
59 @NamedQuery(name = "Attribute.findAll", query = "SELECT a FROM Attribute a order by  a.priority asc, a.xacmlId asc")
60
61 @Getter
62 @Setter
63 @NoArgsConstructor
64 public class Attribute implements Serializable {
65     private static final long serialVersionUID = 1L;
66
67     @Id
68     @GeneratedValue(strategy = GenerationType.AUTO)
69     @Column(name = "id")
70     private int id;
71
72     // bi-directional many-to-one association to Category
73     @ManyToOne
74     @JoinColumn(name = "constraint_type", nullable = true)
75     @JsonIgnore
76     private ConstraintType constraintType;
77
78     @Temporal(TemporalType.TIMESTAMP)
79     @Column(name = "created_date", updatable = false)
80     private Date createdDate;
81
82     @Column(name = "description", nullable = true, length = 2048)
83     private String description;
84
85     @Temporal(TemporalType.TIMESTAMP)
86     @Column(name = "modified_date", nullable = false)
87     private Date modifiedDate;
88
89     @Column(name = "PRIORITY", nullable = true)
90     @OrderBy("asc")
91     private String priority;
92
93     @Column(name = "ATTRIBUTE_VALUE", nullable = true)
94     @OrderBy("asc")
95     private String attributeValue;
96
97     @Column(name = "xacml_id", unique = true, nullable = false)
98     @OrderBy("asc")
99     private String xacmlId = "urn";
100
101     // bi-directional many-to-one association to ConstraintValue
102     @OneToMany(mappedBy = "attribute", orphanRemoval = true, cascade = CascadeType.REMOVE)
103     @JsonIgnore
104     private Set<ConstraintValue> constraintValues = new HashSet<>();
105
106     // bi-directional many-to-one association to Category
107     @ManyToOne
108     @JoinColumn(name = "category")
109     @JsonIgnore
110     private Category categoryBean;
111
112     // bi-directional many-to-one association to Datatype
113     @ManyToOne
114     @JoinColumn(name = "datatype")
115     private Datatype datatypeBean;
116
117     @Column(name = "is_designator", nullable = false)
118     @JsonIgnore
119     private char isDesignator = '1';
120
121     @Column(name = "selector_path", nullable = true, length = 2048)
122     private String selectorPath;
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     /**
139      * Constructor with domain.
140      *
141      * @param domain the domain to use to construct the object
142      */
143     public Attribute(String domain) {
144         this.xacmlId = domain;
145     }
146
147     /**
148      * Copy constructor.
149      *
150      * @param copy the copy to copy from
151      */
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);
162         }
163     }
164
165     /**
166      * Called before an instance is persisted.
167      */
168     @PrePersist
169     public void prePersist() {
170         Date date = new Date();
171         this.createdDate = date;
172         this.modifiedDate = date;
173     }
174
175     /**
176      * Called before an instance is updated.
177      */
178     @PreUpdate
179     public void preUpdate() {
180         this.modifiedDate = new Date();
181     }
182
183     /**
184      * Sets the constraint values.
185      *
186      * @param constraintValues the new constraint values
187      */
188     public void setConstraintValues(Set<ConstraintValue> constraintValues) {
189         if (this.constraintValues == null) {
190             this.constraintValues = new HashSet<>();
191         }
192         for (ConstraintValue value : this.constraintValues) {
193             value.setAttribute(this);
194         }
195         this.constraintValues = constraintValues;
196     }
197
198     /**
199      * Adds the constraint value.
200      *
201      * @param constraintValue the constraint value
202      * @return the constraint value
203      */
204     public ConstraintValue addConstraintValue(ConstraintValue constraintValue) {
205         if (this.constraintValues == null) {
206             this.constraintValues = new HashSet<>();
207         }
208         this.constraintValues.add(constraintValue);
209         constraintValue.setAttribute(this);
210
211         return constraintValue;
212     }
213
214     /**
215      * Removes the constraint value.
216      *
217      * @param constraintValue the constraint value
218      * @return the constraint value
219      */
220     public ConstraintValue removeConstraintValue(ConstraintValue constraintValue) {
221         this.constraintValues.remove(constraintValue);
222         constraintValue.setAttribute(null);
223
224         return constraintValue;
225     }
226
227     /**
228      * Removes the all constraint values.
229      */
230     public void removeAllConstraintValues() {
231         if (this.constraintValues == null) {
232             return;
233         }
234         for (ConstraintValue value : this.constraintValues) {
235             value.setAttribute(null);
236         }
237         this.constraintValues.clear();
238     }
239
240     /**
241      * Gets the issuer.
242      *
243      * @return the issuer
244      */
245     @Transient
246     public String getIssuer() {
247         return issuer;
248     }
249
250     /**
251      * Sets the issuer.
252      *
253      * @param issuer the new issuer
254      */
255     @Transient
256     public void setIssuer(String issuer) {
257         this.issuer = issuer;
258     }
259
260     /**
261      * Checks if is must be present.
262      *
263      * @return true, if is must be present
264      */
265     @Transient
266     public boolean isMustBePresent() {
267         return mustBePresent;
268     }
269
270     /**
271      * Sets the must be present.
272      *
273      * @param mustBePresent the new must be present
274      */
275     @Transient
276     public void setMustBePresent(boolean mustBePresent) {
277         this.mustBePresent = mustBePresent;
278     }
279
280     /**
281      * Checks if is designator.
282      *
283      * @return true, if is designator
284      */
285     @Transient
286     public boolean isDesignator() {
287         return this.isDesignator == '1';
288     }
289
290     /**
291      * Sets the checks if is designator.
292      *
293      * @param is the new checks if is designator
294      */
295     @Transient
296     public void setIsDesignator(boolean is) {
297         if (is) {
298             this.isDesignator = '1';
299         } else {
300             this.isDesignator = '0';
301         }
302     }
303 }