40ddb430809c791047e0ef6900831c0c3ff2151b
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / PolicyEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.onap.policy.rest.jpa;
22
23 /*
24  */
25 import com.fasterxml.jackson.annotation.JsonBackReference;
26 import com.fasterxml.jackson.annotation.JsonManagedReference;
27 import java.io.Serializable;
28 import java.util.Date;
29 import java.util.Objects;
30 import javax.persistence.Column;
31 import javax.persistence.Entity;
32 import javax.persistence.GeneratedValue;
33 import javax.persistence.GenerationType;
34 import javax.persistence.Id;
35 import javax.persistence.JoinColumn;
36 import javax.persistence.Lob;
37 import javax.persistence.NamedQueries;
38 import javax.persistence.NamedQuery;
39 import javax.persistence.OneToOne;
40 import javax.persistence.PrePersist;
41 import javax.persistence.PreUpdate;
42 import javax.persistence.Table;
43 import javax.persistence.Temporal;
44 import javax.persistence.TemporalType;
45 import javax.persistence.Version;
46
47
48 /*
49  * The Entity class to persist a policy object and its configuration data
50  */
51
52 @Entity
53 // Add a non-unique index and a constraint that says the combo of policyName and scopeId must be unique
54 @Table(name = "PolicyEntity")
55 @NamedQueries({
56     @NamedQuery(name = "PolicyEntity.findAll", query = "SELECT e FROM PolicyEntity e "),
57     @NamedQuery(name = "PolicyEntity.findAllByDeletedFlag", 
58         query = "SELECT e FROM PolicyEntity e WHERE e.deleted = :deleted"),
59     @NamedQuery(name = "PolicyEntity.FindById", query = "SELECT e FROM PolicyEntity e WHERE e.policyId = :id"),
60     @NamedQuery(name = "PolicyEntity.deleteAll", query = "DELETE FROM PolicyEntity WHERE 1=1"),
61     @NamedQuery(name = "PolicyEntity.findByNameAndScope", 
62         query = "SELECT e FROM PolicyEntity e WHERE e.policyName = :name AND e.scope = :scope")
63 })
64 public class PolicyEntity implements Serializable {
65     private static final long serialVersionUID = 1L;
66
67     @Id
68     @GeneratedValue(strategy = GenerationType.AUTO)
69     @Column(name = "policyId")
70     @JsonBackReference
71     private long policyId;
72
73     @Column(name = "policyName", nullable = false, unique = false, length = 255)
74     private String policyName;
75
76     // The scope is the directory structure in dot notation. For example: org.onap.myproject
77     @Column(name = "scope", nullable = false, unique = false, length = 255)
78     private String scope;
79
80     @Version
81     @Column(name = "version")
82     private int version;
83
84     // not going to be used
85     @Column(name = "policyVersion")
86     private int policyVersion = 0;
87
88     @Lob
89     @Column(name = "policyData", nullable = false, columnDefinition = "TEXT")
90     private String policyData = "NoData";
91
92     @OneToOne(optional = true, orphanRemoval = true)
93     @JoinColumn(name = "configurationDataId")
94     @JsonManagedReference
95     private ConfigurationDataEntity configurationDataEntity;
96
97     @OneToOne(optional = true, orphanRemoval = true)
98     @JoinColumn(name = "actionBodyId")
99     @JsonManagedReference
100     private ActionBodyEntity actionBodyEntity;
101
102     @Column(name = "created_by", nullable = false, length = 255)
103     private String createdBy = "guest";
104
105     @Temporal(TemporalType.TIMESTAMP)
106     @Column(name = "created_date", updatable = false)
107     private Date createdDate;
108
109     @Column(name = "description", nullable = false, length = 2048)
110     private String description = "NoDescription";
111
112     @Column(name = "modified_by", nullable = false, length = 255)
113     private String modifiedBy = "guest";
114
115     @Temporal(TemporalType.TIMESTAMP)
116     @Column(name = "modified_date", nullable = false)
117     private Date modifiedDate;
118
119     @Column(name = "deleted", nullable = false)
120     private boolean deleted = false;
121
122     @Column(name = "delete_reason_code", nullable = true, length = 100)
123     private String deleteReasonCode;
124
125     @Column(name = "deleted_by", nullable = true, length = 45)
126     private String deletedBy;
127
128     public PolicyEntity() {
129         super();
130     }
131
132     /**
133      * Set default values.
134      */
135     @PrePersist
136     public void prePersist() {
137         Date date = new Date();
138         this.createdDate = date;
139         this.modifiedDate = date;
140     }
141
142     @PreUpdate
143     public void preUpdate() {
144         this.modifiedDate = new Date();
145     }
146
147     /**
148      * Returns Policy Id.
149      * @return the policyId
150      */
151     public long getPolicyId() {
152         return policyId;
153     }
154
155     /**
156      * Returns policy name.
157      * @return the policy name
158      */
159     public String getPolicyName() {
160         return policyName;
161     }
162
163     public void setPolicyName(String policyName) {
164         this.policyName = policyName;
165     }
166
167     /**
168      * Returns policy data.
169      * @return the policyData
170      */
171     public String getPolicyData() {
172         return policyData;
173     }
174
175     /**
176      * Set policy data.
177      * @param policyData the policyData to set
178      */
179     public void setPolicyData(String policyData) {
180         this.policyData = policyData;
181     }
182
183     /**
184      * Returns configurationDataEntity.
185      * @return the configurationDataEntity
186      */
187     public ConfigurationDataEntity getConfigurationData() {
188         return configurationDataEntity;
189     }
190
191     /**
192      * Set configurationDataEntity.
193      * @param configurationDataEntity the configurationDataEntity to set
194      */
195     public void setConfigurationData(ConfigurationDataEntity configurationDataEntity) {
196         this.configurationDataEntity = configurationDataEntity;
197     }
198
199     /**
200      * Returns actionBodyEntity.
201      * @return the actionBodyEntity
202      */
203     public ActionBodyEntity getActionBodyEntity() {
204         return actionBodyEntity;
205     }
206
207     /**
208      * Set actionBodyEntity.
209      * @param actionBodyEntity the actionBodyEntity to set
210      */
211     public void setActionBodyEntity(ActionBodyEntity actionBodyEntity) {
212         this.actionBodyEntity = actionBodyEntity;
213     }
214
215     /**
216      * Returns scope.
217      * @return the scope
218      */
219     public String getScope() {
220         return scope;
221     }
222
223     /**
224      * Set scope.
225      * @param scope the scope to set
226      */
227     public void setScope(String scope) {
228         this.scope = scope;
229     }
230
231     /**
232      * Returns createdBy.
233      * @return the createdBy
234      */
235     public String getCreatedBy() {
236         return createdBy;
237     }
238
239     /**
240      * Set createdBy. 
241      * @param createdBy the createdBy to set
242      */
243     public void setCreatedBy(String createdBy) {
244         this.createdBy = createdBy;
245     }
246
247     /**
248      * Returns description.
249      * @return the description
250      */
251     public String getDescription() {
252         return description;
253     }
254
255     /**
256      * Set description.
257      * @param description the description to set
258      */
259     public void setDescription(String description) {
260         this.description = description;
261     }
262
263     /**
264      * Returns modifiedBy.
265      * @return the modifiedBy
266      */
267     public String getModifiedBy() {
268         return modifiedBy;
269     }
270
271     /**
272      * Set modifiedBy.
273      * @param modifiedBy the modifiedBy to set
274      */
275     public void setModifiedBy(String modifiedBy) {
276         this.modifiedBy = modifiedBy;
277     }
278
279     /**
280      * Returns version.
281      * @return version
282      */
283     public int getVersion() {
284         return version;
285     }
286
287     /**
288      * Returns createdDate.
289      * @return the createdDate
290      */
291     public Date getCreatedDate() {
292         return createdDate;
293     }
294
295     /**
296      * Returns modifiedDate.
297      * @return the modifiedDate
298      */
299     public Date getModifiedDate() {
300         return modifiedDate;
301     }
302
303     /**
304      * Return deleted.
305      * @return the deleted
306      */
307     public boolean isDeleted() {
308         return deleted;
309     }
310
311     /**
312      * Set deleted.
313      * @param deleted the deleted to set
314      */
315     public void setDeleted(boolean deleted) {
316         this.deleted = deleted;
317     }
318
319     /**
320      * Return the reason code.
321      * @return deleted reason code
322      */
323     public String getDeleteReasonCode() {
324         return deleteReasonCode;
325     }
326
327     /**
328      * Set the reason of deletion.
329      * @param deleteReasonCode String object
330      */
331     public void setDeleteReasonCode(String deleteReasonCode) {
332         this.deleteReasonCode = deleteReasonCode;
333     }
334
335     /**
336      * Return deleted By.
337      * @return deletedBy
338      */
339     public String getDeletedBy() {
340         return deletedBy;
341     }
342
343     /**
344      * Set deleted By.
345      * @param deletedBy String object
346      */
347     public void setDeletedBy(String deletedBy) {
348         this.deletedBy = deletedBy;
349     }
350
351     @Override
352     public int hashCode() {
353         return Objects.hash(policyId, policyName, scope, version, policyVersion, policyData, configurationDataEntity,
354                 actionBodyEntity, createdBy, createdDate, description, modifiedBy, modifiedDate, deleted);
355     }
356
357     @Override
358     public boolean equals(Object obj) {
359         if (obj == null) {
360             return false;
361         }
362         if (obj == this) {
363             return true;
364         }
365         if (!(obj instanceof PolicyEntity)) {
366             return false;
367         }
368
369         PolicyEntity p = (PolicyEntity) obj;
370
371         return policyId == p.policyId
372                 && policyName.equals(p.policyName)
373                 && scope.equals(p.scope)
374                 && version == p.version
375                 && policyVersion == p.policyVersion
376                 && policyData.equals(p.policyData)
377                 && ((configurationDataEntity == null && p.configurationDataEntity == null) 
378                         || (configurationDataEntity != null
379                 && configurationDataEntity
380                         .equals(p.configurationDataEntity)))
381                 && ((actionBodyEntity == null && p.actionBodyEntity == null) || (actionBodyEntity != null 
382                 && actionBodyEntity
383                         .equals(p.actionBodyEntity))) && createdBy.equals(p.createdBy)
384                 && createdDate.equals(p.createdDate) && description.equals(p.description)
385                 && modifiedBy.equals(p.modifiedBy) && modifiedDate.equals(p.modifiedDate) && deleted == p.deleted;
386     }
387
388
389 }