JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / GroupEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 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 import com.fasterxml.jackson.annotation.JsonManagedReference;
25
26 /*
27  */
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32
33 import javax.persistence.CascadeType;
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.FetchType;
37 import javax.persistence.GeneratedValue;
38 import javax.persistence.GenerationType;
39 import javax.persistence.Id;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.JoinTable;
42 import javax.persistence.ManyToMany;
43 import javax.persistence.NamedQueries;
44 import javax.persistence.NamedQuery;
45 import javax.persistence.PrePersist;
46 import javax.persistence.PreUpdate;
47 import javax.persistence.Table;
48 import javax.persistence.Temporal;
49 import javax.persistence.TemporalType;
50 import javax.persistence.Version;
51
52 import lombok.Getter;
53 import lombok.Setter;
54
55 /**
56  * The Entity class to persist a policy object and its configuration data.
57  */
58 // @formatter:off
59 @Entity
60 // Add a non-unique index and a constraint that says the combo of policyName and scopeId must be unique
61 @Table(name = "GroupEntity")
62
63 @NamedQueries(
64     {
65         @NamedQuery(name = "GroupEntity.findAll", query = "SELECT e FROM GroupEntity e "),
66         @NamedQuery(name = "GroupEntity.deleteAll", query = "DELETE FROM GroupEntity WHERE 1=1")
67     }
68 )
69
70 @Getter
71 @Setter
72 //@formatter:on
73
74 public class GroupEntity implements Serializable {
75     private static final long serialVersionUID = 1L;
76
77     @Id
78     @Column(name = "groupKey", nullable = false)
79     @GeneratedValue(strategy = GenerationType.AUTO)
80     private long groupKey;
81
82     @Column(name = "groupId", nullable = false)
83     private String groupId;
84
85     @Column(name = "groupName", nullable = false, unique = false, length = 255)
86     private String groupName;
87
88     @Version
89     @Column(name = "version")
90     private int version;
91
92     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
93     @JoinTable(name = "PolicyGroupEntity", joinColumns =
94         { @JoinColumn(name = "groupKey") }, inverseJoinColumns =
95         { @JoinColumn(name = "policyId") })
96     @JsonManagedReference
97     private List<PolicyEntity> policies;
98
99     @Column(name = "created_by", nullable = false, length = 255)
100     private String createdBy = "guest";
101
102     @Temporal(TemporalType.TIMESTAMP)
103     @Column(name = "created_date", updatable = false)
104     private Date createdDate;
105
106     @Column(name = "description", nullable = false, length = 2048)
107     private String description = "NoDescription";
108
109     @Column(name = "modified_by", nullable = false, length = 255)
110     private String modifiedBy = "guest";
111
112     @Temporal(TemporalType.TIMESTAMP)
113     @Column(name = "modified_date", nullable = false)
114     private Date modifiedDate;
115
116     @Column(name = "defaultGroup", nullable = false)
117     private boolean defaultGroup = false;
118     @Column(name = "deleted", nullable = false)
119     private boolean deleted = false;
120
121     /**
122      * Instantiates a new group entity.
123      */
124     public GroupEntity() {
125         super();
126     }
127
128     /**
129      * Called before an instance is persisted.
130      */
131     @PrePersist
132     public void prePersist() {
133         Date date = new Date();
134         this.createdDate = date;
135         this.modifiedDate = date;
136     }
137
138     /**
139      * Called before an instance is updated.
140      */
141     @PreUpdate
142     public void preUpdate() {
143         this.modifiedDate = new Date();
144     }
145
146     /**
147      * Adds the policy to group.
148      *
149      * @param policy the policy
150      */
151     public void addPolicyToGroup(PolicyEntity policy) {
152         if (policies == null) {
153             policies = new ArrayList<>();
154         }
155
156         if (!this.policies.contains(policy)) {
157             this.policies.add(policy);
158         }
159     }
160
161     /**
162      * Removes the policy from group.
163      *
164      * @param policy the policy
165      */
166     public void removePolicyFromGroup(PolicyEntity policy) {
167         this.policies.remove(policy);
168
169         if (policies.isEmpty()) {
170             policies = null;
171         }
172     }
173 }