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