Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / PolicyEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.jpa;
23
24 /*
25  */
26 import com.fasterxml.jackson.annotation.JsonBackReference;
27 import com.fasterxml.jackson.annotation.JsonManagedReference;
28
29 import java.io.Serializable;
30 import java.util.Date;
31
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.GeneratedValue;
35 import javax.persistence.GenerationType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.Lob;
39 import javax.persistence.NamedQueries;
40 import javax.persistence.NamedQuery;
41 import javax.persistence.OneToOne;
42 import javax.persistence.PrePersist;
43 import javax.persistence.PreUpdate;
44 import javax.persistence.Table;
45 import javax.persistence.Temporal;
46 import javax.persistence.TemporalType;
47 import javax.persistence.Version;
48
49 import lombok.EqualsAndHashCode;
50 import lombok.Getter;
51 import lombok.Setter;
52
53 /**
54  * The Entity class to persist a policy object and its configuration data.
55  */
56 // @formatter:off
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")
60 @NamedQueries(
61     {
62         @NamedQuery(
63             name = "PolicyEntity.findAll",
64             query = "SELECT e FROM PolicyEntity e "
65         ),
66         @NamedQuery(
67             name = "PolicyEntity.findAllByDeletedFlag",
68             query = "SELECT e FROM PolicyEntity e WHERE e.deleted = :deleted"
69         ),
70         @NamedQuery(
71             name = "PolicyEntity.FindById",
72             query = "SELECT e FROM PolicyEntity e WHERE e.policyId = :id"
73         ),
74         @NamedQuery(
75             name = "PolicyEntity.deleteAll",
76             query = "DELETE FROM PolicyEntity WHERE 1=1"
77         ),
78         @NamedQuery(
79             name = "PolicyEntity.findByNameAndScope",
80             query = "SELECT e FROM PolicyEntity e WHERE e.policyName = :name AND e.scope = :scope"
81         )
82     }
83 )
84 @Getter
85 @Setter
86 @EqualsAndHashCode
87 // @formatter:on
88 public class PolicyEntity implements Serializable {
89     private static final long serialVersionUID = 1L;
90
91     @Id
92     @GeneratedValue(strategy = GenerationType.AUTO)
93     @Column(name = "policyId")
94     @JsonBackReference
95     private long policyId;
96
97     @Column(name = "policyName", nullable = false, unique = false, length = 255)
98     private String policyName;
99
100     // The scope is the directory structure in dot notation. For example: org.onap.myproject
101     @Column(name = "scope", nullable = false, unique = false, length = 255)
102     private String scope;
103
104     @Version
105     @Column(name = "version")
106     private int version;
107
108     // not going to be used
109     @Column(name = "policyVersion")
110     private int policyVersion = 0;
111
112     @Lob
113     @Column(name = "policyData", nullable = false, columnDefinition = "TEXT")
114     private String policyData = "NoData";
115
116     @OneToOne(optional = true, orphanRemoval = true)
117     @JoinColumn(name = "configurationDataId")
118     @JsonManagedReference
119     private ConfigurationDataEntity configurationDataEntity;
120
121     @OneToOne(optional = true, orphanRemoval = true)
122     @JoinColumn(name = "actionBodyId")
123     @JsonManagedReference
124     private ActionBodyEntity actionBodyEntity;
125
126     @Column(name = "created_by", nullable = false, length = 255)
127     private String createdBy = "guest";
128
129     @Temporal(TemporalType.TIMESTAMP)
130     @Column(name = "created_date", updatable = false)
131     private Date createdDate;
132
133     @Column(name = "description", nullable = false, length = 2048)
134     private String description = "NoDescription";
135
136     @Column(name = "modified_by", nullable = false, length = 255)
137     private String modifiedBy = "guest";
138
139     @Temporal(TemporalType.TIMESTAMP)
140     @Column(name = "modified_date", nullable = false)
141     private Date modifiedDate;
142
143     @Column(name = "deleted", nullable = false)
144     private boolean deleted = false;
145
146     @Column(name = "delete_reason_code", nullable = true, length = 100)
147     private String deleteReasonCode;
148
149     @Column(name = "deleted_by", nullable = true, length = 45)
150     private String deletedBy;
151
152     public PolicyEntity() {
153         super();
154     }
155
156     /**
157      * Set default values.
158      */
159     @PrePersist
160     public void prePersist() {
161         Date date = new Date();
162         this.createdDate = date;
163         this.modifiedDate = date;
164     }
165
166     @PreUpdate
167     public void preUpdate() {
168         this.modifiedDate = new Date();
169     }
170
171     /**
172      * Returns configurationDataEntity.
173      *
174      * @return the configurationDataEntity
175      */
176     public ConfigurationDataEntity getConfigurationData() {
177         return configurationDataEntity;
178     }
179
180     /**
181      * Set configurationDataEntity.
182      *
183      * @param configurationDataEntity the configurationDataEntity to set
184      */
185     public void setConfigurationData(ConfigurationDataEntity configurationDataEntity) {
186         this.configurationDataEntity = configurationDataEntity;
187     }
188 }