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