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.onap.policy.rest.jpa;
24 import java.io.Serializable;
25 import java.util.Date;
26 import java.util.Objects;
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.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.Table;
41 import javax.persistence.Temporal;
42 import javax.persistence.TemporalType;
43 import javax.persistence.Version;
45 import com.fasterxml.jackson.annotation.JsonBackReference;
46 import com.fasterxml.jackson.annotation.JsonManagedReference;
49 * The Entity class to persist a policy object and its configuration data
56 //Add a non-unique index and a constraint that says the combo of policyName and scopeId must be unique
57 @Table(name="PolicyEntity")
60 @NamedQuery(name="PolicyEntity.findAll", query="SELECT e FROM PolicyEntity e "),
61 @NamedQuery(name="PolicyEntity.findAllByDeletedFlag", query="SELECT e FROM PolicyEntity e WHERE e.deleted = :deleted"),
62 @NamedQuery(name="PolicyEntity.FindById", query="SELECT e FROM PolicyEntity e WHERE e.policyId = :id"),
63 @NamedQuery(name="PolicyEntity.deleteAll", query="DELETE FROM PolicyEntity WHERE 1=1"),
64 @NamedQuery(name="PolicyEntity.findByNameAndScope", query="SELECT e FROM PolicyEntity e WHERE e.policyName = :name AND e.scope = :scope")
67 public class PolicyEntity implements Serializable {
68 private static final long serialVersionUID = 1L;
71 @GeneratedValue(strategy = GenerationType.AUTO)
72 @Column (name="policyId")
74 private long policyId;
76 @Column(name="policyName", nullable=false, unique=false, length=255)
77 private String policyName;
79 //The scope is the directory structure in dot notation. For example: org.onap.myproject
80 @Column(name="scope", nullable=false, unique=false, length=255)
84 @Column(name="version")
87 //not going to be used
88 @Column(name="policyVersion")
89 private int policyVersion = 0;
92 @Column(name="policyData", nullable=false, columnDefinition="TEXT")
93 private String policyData = "NoData";
95 @OneToOne(optional=true, orphanRemoval=true)
96 @JoinColumn(name="configurationDataId")
98 private ConfigurationDataEntity configurationDataEntity;
100 @OneToOne(optional=true, orphanRemoval=true)
101 @JoinColumn(name="actionBodyId")
102 @JsonManagedReference
103 private ActionBodyEntity actionBodyEntity;
105 @Column(name="created_by", nullable=false, length=255)
106 private String createdBy = "guest";
108 @Temporal(TemporalType.TIMESTAMP)
109 @Column(name="created_date", updatable=false)
110 private Date createdDate;
112 @Column(name="description", nullable=false, length=2048)
113 private String description = "NoDescription";
115 @Column(name="modified_by", nullable=false, length=255)
116 private String modifiedBy = "guest";
118 @Temporal(TemporalType.TIMESTAMP)
119 @Column(name="modified_date", nullable=false)
120 private Date modifiedDate;
122 @Column(name="deleted", nullable=false)
123 private boolean deleted = false;
125 public PolicyEntity() {
130 public void prePersist() {
131 Date date = new Date();
132 this.createdDate = date;
133 this.modifiedDate = date;
137 public void preUpdate() {
138 this.modifiedDate = new Date();
142 * @return the policyId
144 public long getPolicyId() {
149 * @param policyId cannot be set
152 public String getPolicyName() {
156 public void setPolicyName(String policyName) {
157 this.policyName = policyName;
161 * @return the policyData
163 public String getPolicyData() {
168 * @param policyData the policyData to set
170 public void setPolicyData(String policyData) {
171 this.policyData = policyData;
175 * @return the configurationDataEntity
177 public ConfigurationDataEntity getConfigurationData() {
178 return configurationDataEntity;
182 * @param configurationDataEntity the configurationDataEntity to set
184 public void setConfigurationData(ConfigurationDataEntity configurationDataEntity) {
185 this.configurationDataEntity = configurationDataEntity;
189 * @return the actionBodyEntity
191 public ActionBodyEntity getActionBodyEntity() {
192 return actionBodyEntity;
196 * @param actionBodyEntity the actionBodyEntity to set
198 public void setActionBodyEntity(ActionBodyEntity actionBodyEntity) {
199 this.actionBodyEntity = actionBodyEntity;
205 public String getScope() {
210 * @param scope the scope to set
212 public void setScope(String scope) {
217 * @return the createdBy
219 public String getCreatedBy() {
224 * @param createdBy the createdBy to set
226 public void setCreatedBy(String createdBy) {
227 this.createdBy = createdBy;
231 * @return the description
233 public String getDescription() {
238 * @param description the description to set
240 public void setDescription(String description) {
241 this.description = description;
245 * @return the modifiedBy
247 public String getModifiedBy() {
252 * @param modifiedBy the modifiedBy to set
254 public void setModifiedBy(String modifiedBy) {
255 this.modifiedBy = modifiedBy;
259 * @return the version
261 public int getVersion() {
266 * @return the createdDate
268 public Date getCreatedDate() {
273 * @return the modifiedDate
275 public Date getModifiedDate() {
280 * @return the deleted
282 public boolean isDeleted() {
287 * @param deleted the deleted to set
289 public void setDeleted(boolean deleted) {
290 this.deleted = deleted;
294 public int hashCode() {
295 return Objects.hash(policyId, policyName, scope, version, policyVersion, policyData, configurationDataEntity,
296 actionBodyEntity, createdBy, createdDate, description, modifiedBy, modifiedDate, deleted);
300 public boolean equals(Object obj) {
307 if(!(obj instanceof PolicyEntity)){
311 PolicyEntity p = (PolicyEntity) obj;
314 policyId == p.policyId &&
315 policyName.equals(p.policyName) &&
316 scope.equals(p.scope) &&
317 version == p.version &&
318 policyVersion == p.policyVersion &&
319 policyData.equals(p.policyData) &&
320 ((configurationDataEntity == null && p.configurationDataEntity == null) || (configurationDataEntity!=null && configurationDataEntity.equals(p.configurationDataEntity))) &&
321 ((actionBodyEntity == null && p.actionBodyEntity == null) || (actionBodyEntity!=null && actionBodyEntity.equals(p.actionBodyEntity))) &&
322 createdBy.equals(p.createdBy) &&
323 createdDate.equals(p.createdDate) &&
324 description.equals(p.description) &&
325 modifiedBy.equals(p.modifiedBy) &&
326 modifiedDate.equals(p.modifiedDate) &&