2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.rest.jpa;
24 import java.io.Serializable;
25 import java.util.Date;
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;
47 import com.fasterxml.jackson.annotation.JsonBackReference;
48 import com.fasterxml.jackson.annotation.JsonManagedReference;
51 * The Entity class to persist a policy object and its configuration data
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"}))
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)
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")
75 public class PolicyEntity implements Serializable {
76 private static final long serialVersionUID = 1L;
79 //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seqPolicy")
80 @GeneratedValue(strategy = GenerationType.AUTO)
81 @Column (name="policyId")
83 private long policyId;
85 @Column(name="policyName", nullable=false, unique=false, length=255)
86 private String policyName;
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)
93 @Column(name="version")
96 //not going to be used
97 @Column(name="policyVersion")
98 private int policyVersion = 0;
101 @Column(name="policyData", nullable=false, columnDefinition="TEXT")
102 private String policyData = "NoData";
104 @OneToOne(optional=true, orphanRemoval=true)
105 @JoinColumn(name="configurationDataId", referencedColumnName="configurationDataId")
106 @JsonManagedReference
107 private ConfigurationDataEntity configurationDataEntity;
109 @OneToOne(optional=true, orphanRemoval=true)
110 @JoinColumn(name="actionBodyId", referencedColumnName="actionBodyId")
111 @JsonManagedReference
112 private ActionBodyEntity actionBodyEntity;
114 @Column(name="created_by", nullable=false, length=255)
115 private String createdBy = "guest";
117 @Temporal(TemporalType.TIMESTAMP)
118 @Column(name="created_date", updatable=false)
119 private Date createdDate;
121 @Column(name="description", nullable=false, length=2048)
122 private String description = "NoDescription";
124 @Column(name="modified_by", nullable=false, length=255)
125 private String modifiedBy = "guest";
127 @Temporal(TemporalType.TIMESTAMP)
128 @Column(name="modified_date", nullable=false)
129 private Date modifiedDate;
131 @Column(name="deleted", nullable=false)
132 private boolean deleted = false;
134 public PolicyEntity() {
139 public void prePersist() {
140 Date date = new Date();
141 this.createdDate = date;
142 this.modifiedDate = date;
146 public void preUpdate() {
147 this.modifiedDate = new Date();
151 public void resetPolicyVersion(){
152 this.policyVersion = 1;
154 public void advancePolicyVersion(){
155 this.policyVersion++;
157 public int getPolicyVersion(){
158 return this.policyVersion;
160 public void setPolicyVersion(int polVer){
161 this.policyVersion = polVer;
166 * @return the policyId
168 public long getPolicyId() {
173 * @param policyId cannot be set
176 public String getPolicyName() {
180 public void setPolicyName(String policyName) {
181 this.policyName = policyName;
185 * @return the policyData
187 public String getPolicyData() {
192 * @param policyData the policyData to set
194 public void setPolicyData(String policyData) {
195 this.policyData = policyData;
199 * @return the configurationDataEntity
201 public ConfigurationDataEntity getConfigurationData() {
202 return configurationDataEntity;
206 * @param configurationDataEntity the configurationDataEntity to set
208 public void setConfigurationData(ConfigurationDataEntity configurationDataEntity) {
209 this.configurationDataEntity = configurationDataEntity;
213 * @return the actionBodyEntity
215 public ActionBodyEntity getActionBodyEntity() {
216 return actionBodyEntity;
220 * @param actionBodyEntity the actionBodyEntity to set
222 public void setActionBodyEntity(ActionBodyEntity actionBodyEntity) {
223 this.actionBodyEntity = actionBodyEntity;
229 public String getScope() {
234 * @param scope the scope to set
236 public void setScope(String scope) {
241 * @return the createdBy
243 public String getCreatedBy() {
248 * @param createdBy the createdBy to set
250 public void setCreatedBy(String createdBy) {
251 this.createdBy = createdBy;
255 * @return the description
257 public String getDescription() {
262 * @param description the description to set
264 public void setDescription(String description) {
265 this.description = description;
269 * @return the modifiedBy
271 public String getModifiedBy() {
276 * @param modifiedBy the modifiedBy to set
278 public void setModifiedBy(String modifiedBy) {
279 this.modifiedBy = modifiedBy;
283 * @return the version
285 public int getVersion() {
290 * @return the createdDate
292 public Date getCreatedDate() {
297 * @return the modifiedDate
299 public Date getModifiedDate() {
304 * @return the deleted
306 public boolean isDeleted() {
311 * @param deleted the deleted to set
313 public void setDeleted(boolean deleted) {
314 this.deleted = deleted;