Fixed the Sonar technical debt.
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / PolicyVersion.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 import java.io.Serializable;
24 import java.sql.Timestamp;
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.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.PrePersist;
36 import javax.persistence.PreUpdate;
37 import javax.persistence.Table;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40
41
42 @Entity
43 @Table(name="PolicyVersion")
44 @NamedQueries({
45         @NamedQuery(name="PolicyVersion.findAll", query="SELECT p FROM PolicyVersion p"),
46         @NamedQuery(name="PolicyVersion.deleteAll", query="DELETE FROM PolicyVersion WHERE 1=1"),
47         @NamedQuery(name="PolicyVersion.findByPolicyName", query="Select p from PolicyVersion p where p.policyName=:pname"),
48         @NamedQuery(name="PolicyVersion.findAllCount", query="SELECT COUNT(p) FROM PolicyVersion p")
49 })
50 public class PolicyVersion implements Serializable {
51         private static final long serialVersionUID = 1L;
52
53         @Id
54         @GeneratedValue(strategy = GenerationType.AUTO)
55         
56         @Column(name="id")
57         private int id;
58
59         @Column(name="POLICY_NAME", nullable=false, length=255)
60         private String policyName;      
61         
62         @Column(name="ACTIVE_VERSION")
63         private int activeVersion;
64         
65         @Column(name="HIGHEST_VERSION")
66         private int higherVersion;
67         
68         @Temporal(TemporalType.TIMESTAMP)
69         @Column(name="created_date", nullable=false)
70         private Date createdDate;
71         
72         
73         public int getActiveVersion() {
74                 return activeVersion;
75         }
76
77         public void setActiveVersion(int activeVersion) {
78                 this.activeVersion = activeVersion;
79         }
80
81         public int getHigherVersion() {
82                 return higherVersion;
83         }
84
85         public void setHigherVersion(int higherVersion) {
86                 this.higherVersion = higherVersion;
87         }
88
89         @Column(name="CREATED_BY", nullable=false, length=45)
90         private String createdBy;
91         
92         @Temporal(TemporalType.TIMESTAMP)
93         @Column(name="modified_date", nullable=false)
94         private Date modifiedDate;
95         
96         
97         @Column(name="modified_by", nullable=false, length=45)
98         private String modifiedBy;
99
100         public PolicyVersion(String domain, String loginUserId) {
101                 this(domain);
102                 this.createdBy = loginUserId;
103                 this.modifiedBy = loginUserId;
104         }
105         
106         public PolicyVersion(String domain) {
107                 this.policyName = domain;
108         }
109         
110         public PolicyVersion(){
111                 // Empty constructor
112         }
113         
114         @PrePersist
115         public void     prePersist() {
116                 Date date = new Date();
117                 this.createdDate = date;
118                 this.modifiedDate = date;
119         }
120
121         @PreUpdate
122         public void preUpdate() {
123                 this.modifiedDate =  new Date();
124         }
125         
126         public int getId() {
127                 return id;
128         }
129
130         public void setId(int id) {
131                 this.id = id;
132         }
133
134         public String getPolicyName() {
135                 return policyName;
136         }
137
138         public void setPolicyName(String policyName) {
139                 this.policyName = policyName;
140         }
141         
142         public Date getCreatedDate() {
143                 return createdDate;
144         }
145
146         public void setCreatedDate(Timestamp createdDate) {
147                 this.createdDate = createdDate;
148         }
149
150         public String getCreatedBy() {
151                 return createdBy;
152         }
153
154         public void setCreatedBy(String createdBy) {
155                 this.createdBy = createdBy;
156         }
157
158         public Date getModifiedDate() {
159                 return modifiedDate;
160         }
161
162         public void setModifiedDate(Date modifiedDate) {
163                 this.modifiedDate = modifiedDate;
164         }
165
166         public String getModifiedBy() {
167                 return modifiedBy;
168         }
169
170         public void setModifiedBy(String modifiedBy) {
171                 this.modifiedBy = modifiedBy;
172         }
173         
174         @Override
175         public int hashCode() {
176         return Objects.hash(id, policyName,     activeVersion, higherVersion, createdDate, 
177                         createdBy, modifiedDate, modifiedBy);
178         }
179
180         @Override
181         public boolean equals(Object obj) {
182                 if(obj == null){
183                         return false;
184                 }
185                 if(obj == this){
186                         return true;
187                 }
188                 if(!(obj instanceof PolicyVersion)){
189                         return false;
190                 }
191
192                 PolicyVersion p = (PolicyVersion) obj;
193                 
194                 return id == p.id &&
195                                 policyName.equals(p.policyName) &&
196                                 activeVersion == p.activeVersion &&
197                                 higherVersion == p.higherVersion &&
198                                 createdDate.equals(p.createdDate) &&
199                                 createdBy.equals(p.createdBy) &&
200                                 modifiedDate.equals(p.modifiedDate) &&
201                                 modifiedBy.equals(p.modifiedBy);
202         }
203
204 }
205