Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / Obadvice.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.att.research.xacml.api.Identifier;
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.NamedQuery;
38 import javax.persistence.OneToMany;
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 lombok.Getter;
47 import lombok.Setter;
48
49 /**
50  * The persistent class for the Obadvice database table.
51  */
52 @Entity
53 @Table(name = "Obadvice")
54 @NamedQuery(name = "Obadvice.findAll", query = "SELECT o FROM Obadvice o")
55 @Getter
56 @Setter
57 public class Obadvice implements Serializable {
58     private static final long serialVersionUID = 1L;
59
60     public static final String OBLIGATION = "Obligation";
61     public static final String ADVICE = "Advice";
62     public static final String EFFECT_PERMIT = "Permit";
63     public static final String EFFECT_DENY = "Deny";
64     @Id
65     @GeneratedValue(strategy = GenerationType.AUTO)
66     @Column(name = "id")
67     private int id;
68
69     @Column(name = "type", nullable = false)
70     private String type;
71
72     @Column(name = "xacml_id", nullable = false, length = 255)
73     private String xacmlId;
74
75     @Column(name = "fulfill_on", nullable = true, length = 32)
76     private String fulfillOn;
77
78     @Column(name = "description", nullable = true, length = 2048)
79     private String description;
80
81     // bi-directional one-to-many association to Attribute Assignment
82     @OneToMany(mappedBy = "obadvice", orphanRemoval = true, cascade = CascadeType.REMOVE)
83     private Set<ObadviceExpression> obadviceExpressions = new HashSet<>(2);
84
85     @Column(name = "created_by", nullable = false, length = 255)
86     private String createdBy;
87
88     @Temporal(TemporalType.TIMESTAMP)
89     @Column(name = "created_date", nullable = false, updatable = false)
90     private Date createdDate;
91
92     @Column(name = "modified_by", nullable = false, length = 255)
93     private String modifiedBy;
94
95     @Temporal(TemporalType.TIMESTAMP)
96     @Column(name = "modified_date", nullable = false)
97     private Date modifiedDate;
98
99     /**
100      * Instantiates a new obadvice.
101      */
102     public Obadvice() {
103         this.type = Obadvice.OBLIGATION;
104         this.fulfillOn = Obadvice.EFFECT_PERMIT;
105     }
106
107     /**
108      * Instantiates a new obadvice.
109      *
110      * @param domain the domain
111      * @param userid the userid
112      */
113     public Obadvice(String domain, String userid) {
114         this.xacmlId = domain;
115         this.type = Obadvice.OBLIGATION;
116         this.fulfillOn = Obadvice.EFFECT_PERMIT;
117         this.createdBy = userid;
118         this.modifiedBy = userid;
119     }
120
121     /**
122      * Instantiates a new obadvice.
123      *
124      * @param id the id
125      * @param userid the userid
126      */
127     public Obadvice(Identifier id, String userid) {
128         this(id.stringValue(), userid);
129     }
130
131     /**
132      * Pre persist.
133      */
134     @PrePersist
135     public void prePersist() {
136         Date date = new Date();
137         this.createdDate = date;
138         this.modifiedDate = date;
139     }
140
141     /**
142      * Pre update.
143      */
144     @PreUpdate
145     public void preUpdate() {
146         this.modifiedDate = new Date();
147     }
148
149     /**
150      * Adds the obadvice expression.
151      *
152      * @param obadviceExpression the obadvice expression
153      * @return the obadvice expression
154      */
155     public ObadviceExpression addObadviceExpression(ObadviceExpression obadviceExpression) {
156         this.obadviceExpressions.add(obadviceExpression);
157         obadviceExpression.setObadvice(this);
158
159         return obadviceExpression;
160     }
161
162     /**
163      * Removes the obadvice expression.
164      *
165      * @param obadviceExpression the obadvice expression
166      * @return the obadvice expression
167      */
168     public ObadviceExpression removeObadviceExpression(ObadviceExpression obadviceExpression) {
169         this.obadviceExpressions.remove(obadviceExpression);
170         obadviceExpression.setObadvice(null);
171
172         return obadviceExpression;
173     }
174
175     /**
176      * Removes the all expressions.
177      */
178     public void removeAllExpressions() {
179         if (this.obadviceExpressions == null) {
180             return;
181         }
182         for (ObadviceExpression expression : this.obadviceExpressions) {
183             expression.setObadvice(null);
184         }
185         this.obadviceExpressions.clear();
186     }
187
188     /**
189      * Clone.
190      *
191      * @return the obadvice
192      */
193     @Transient
194     @Override
195     public Obadvice clone() {
196         Obadvice obadvice = new Obadvice();
197
198         obadvice.type = this.type;
199         obadvice.xacmlId = this.xacmlId;
200         obadvice.fulfillOn = this.fulfillOn;
201         obadvice.description = this.description;
202         obadvice.createdBy = this.createdBy;
203         obadvice.modifiedBy = this.modifiedBy;
204         for (ObadviceExpression exp : this.obadviceExpressions) {
205             obadvice.addObadviceExpression(exp.clone());
206         }
207
208         return obadvice;
209     }
210 }