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