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.Setter;
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
60 /**
61  * Gets the user modified by.
62  *
63  * @return the user modified by
64  */
65 @Getter
66
67 /**
68  * Sets the user modified by.
69  *
70  * @param userModifiedBy the new user modified by
71  */
72 @Setter
73 public class Attribute implements Serializable {
74     private static final long serialVersionUID = 1L;
75
76     @Id
77     @GeneratedValue(strategy = GenerationType.AUTO)
78     @Column(name = "id")
79     private int id;
80
81     // bi-directional many-to-one association to Category
82     @ManyToOne
83     @JoinColumn(name = "constraint_type", nullable = true)
84     @JsonIgnore
85     private ConstraintType constraintType;
86
87     @Temporal(TemporalType.TIMESTAMP)
88     @Column(name = "created_date", updatable = false)
89     private Date createdDate;
90
91     @Column(name = "description", nullable = true, length = 2048)
92     private String description;
93
94     @Temporal(TemporalType.TIMESTAMP)
95     @Column(name = "modified_date", nullable = false)
96     private Date modifiedDate;
97
98     @Column(name = "PRIORITY", nullable = true)
99     @OrderBy("asc")
100     private String priority;
101
102     @Column(name = "ATTRIBUTE_VALUE", nullable = true)
103     @OrderBy("asc")
104     private String attributeValue;
105
106     @Column(name = "xacml_id", unique = true, nullable = false)
107     @OrderBy("asc")
108     private String xacmlId = "urn";
109
110     // bi-directional many-to-one association to ConstraintValue
111     @OneToMany(mappedBy = "attribute", orphanRemoval = true, cascade = CascadeType.REMOVE)
112     @JsonIgnore
113     private Set<ConstraintValue> constraintValues = new HashSet<>();
114
115     // bi-directional many-to-one association to Category
116     @ManyToOne
117     @JoinColumn(name = "category")
118     @JsonIgnore
119     private Category categoryBean;
120
121     // bi-directional many-to-one association to Datatype
122     @ManyToOne
123     @JoinColumn(name = "datatype")
124     private Datatype datatypeBean;
125
126     @Column(name = "is_designator", nullable = false)
127     @JsonIgnore
128     private char isDesignator = '1';
129
130     @Column(name = "selector_path", nullable = true, length = 2048)
131     private String selectorPath;
132
133     @Transient
134     private String issuer = null;
135
136     @Transient
137     private boolean mustBePresent = false;
138
139     @ManyToOne(optional = false)
140     @JoinColumn(name = "created_by")
141     private UserInfo userCreatedBy;
142
143     @ManyToOne(optional = false)
144     @JoinColumn(name = "modified_by")
145     private UserInfo userModifiedBy;
146
147     /**
148      * Default constructor.
149      */
150     public Attribute() {
151         // An empty constructor
152     }
153
154     /**
155      * Constructor with domain.
156      * 
157      * @param domain the domain to use to construct the object
158      */
159     public Attribute(String domain) {
160         this.xacmlId = domain;
161     }
162
163     /**
164      * Copy constructor.
165      * 
166      * @param copy the copy to copy from
167      */
168     public Attribute(Attribute copy) {
169         this(copy.getXacmlId() + ":(0)");
170         this.constraintType = copy.getConstraintType();
171         this.categoryBean = copy.getCategoryBean();
172         this.datatypeBean = copy.getDatatypeBean();
173         this.description = copy.getDescription();
174         for (ConstraintValue value : copy.getConstraintValues()) {
175             ConstraintValue newValue = new ConstraintValue(value);
176             newValue.setAttribute(this);
177             this.addConstraintValue(newValue);
178         }
179     }
180
181     /**
182      * Called before an instance is persisted.
183      */
184     @PrePersist
185     public void prePersist() {
186         Date date = new Date();
187         this.createdDate = date;
188         this.modifiedDate = date;
189     }
190
191     /**
192      * Called before an instance is updated.
193      */
194     @PreUpdate
195     public void preUpdate() {
196         this.modifiedDate = new Date();
197     }
198
199     /**
200      * Sets the constraint values.
201      *
202      * @param constraintValues the new constraint values
203      */
204     public void setConstraintValues(Set<ConstraintValue> constraintValues) {
205         if (this.constraintValues == null) {
206             this.constraintValues = new HashSet<>();
207         }
208         for (ConstraintValue value : this.constraintValues) {
209             value.setAttribute(this);
210         }
211         this.constraintValues = constraintValues;
212     }
213
214     /**
215      * Adds the constraint value.
216      *
217      * @param constraintValue the constraint value
218      * @return the constraint value
219      */
220     public ConstraintValue addConstraintValue(ConstraintValue constraintValue) {
221         if (this.constraintValues == null) {
222             this.constraintValues = new HashSet<>();
223         }
224         this.constraintValues.add(constraintValue);
225         constraintValue.setAttribute(this);
226
227         return constraintValue;
228     }
229
230     /**
231      * Removes the constraint value.
232      *
233      * @param constraintValue the constraint value
234      * @return the constraint value
235      */
236     public ConstraintValue removeConstraintValue(ConstraintValue constraintValue) {
237         this.constraintValues.remove(constraintValue);
238         constraintValue.setAttribute(null);
239
240         return constraintValue;
241     }
242
243     /**
244      * Removes the all constraint values.
245      */
246     public void removeAllConstraintValues() {
247         if (this.constraintValues == null) {
248             return;
249         }
250         for (ConstraintValue value : this.constraintValues) {
251             value.setAttribute(null);
252         }
253         this.constraintValues.clear();
254     }
255
256     /**
257      * Gets the issuer.
258      *
259      * @return the issuer
260      */
261     @Transient
262     public String getIssuer() {
263         return issuer;
264     }
265
266     /**
267      * Sets the issuer.
268      *
269      * @param issuer the new issuer
270      */
271     @Transient
272     public void setIssuer(String issuer) {
273         this.issuer = issuer;
274     }
275
276     /**
277      * Checks if is must be present.
278      *
279      * @return true, if is must be present
280      */
281     @Transient
282     public boolean isMustBePresent() {
283         return mustBePresent;
284     }
285
286     /**
287      * Sets the must be present.
288      *
289      * @param mustBePresent the new must be present
290      */
291     @Transient
292     public void setMustBePresent(boolean mustBePresent) {
293         this.mustBePresent = mustBePresent;
294     }
295
296     /**
297      * Checks if is designator.
298      *
299      * @return true, if is designator
300      */
301     @Transient
302     public boolean isDesignator() {
303         return this.isDesignator == '1';
304     }
305
306     /**
307      * Sets the checks if is designator.
308      *
309      * @param is the new checks if is designator
310      */
311     @Transient
312     public void setIsDesignator(boolean is) {
313         if (is) {
314             this.isDesignator = '1';
315         } else {
316             this.isDesignator = '0';
317         }
318     }
319 }