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