2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.rest.jpa;
24 import com.fasterxml.jackson.annotation.JsonManagedReference;
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
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;
56 * The Entity class to persist a policy object and its configuration data.
60 // Add a non-unique index and a constraint that says the combo of policyName and scopeId must be unique
61 @Table(name = "GroupEntity")
65 @NamedQuery(name = "GroupEntity.findAll", query = "SELECT e FROM GroupEntity e "),
66 @NamedQuery(name = "GroupEntity.deleteAll", query = "DELETE FROM GroupEntity WHERE 1=1")
74 public class GroupEntity implements Serializable {
75 private static final long serialVersionUID = 1L;
78 @Column(name = "groupKey", nullable = false)
79 @GeneratedValue(strategy = GenerationType.AUTO)
80 private long groupKey;
82 @Column(name = "groupId", nullable = false)
83 private String groupId;
85 @Column(name = "groupName", nullable = false, unique = false, length = 255)
86 private String groupName;
89 @Column(name = "version")
92 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
93 @JoinTable(name = "PolicyGroupEntity", joinColumns =
94 { @JoinColumn(name = "groupKey") }, inverseJoinColumns =
95 { @JoinColumn(name = "policyId") })
97 private List<PolicyEntity> policies;
99 @Column(name = "created_by", nullable = false, length = 255)
100 private String createdBy = "guest";
102 @Temporal(TemporalType.TIMESTAMP)
103 @Column(name = "created_date", updatable = false)
104 private Date createdDate;
106 @Column(name = "description", nullable = false, length = 2048)
107 private String description = "NoDescription";
109 @Column(name = "modified_by", nullable = false, length = 255)
110 private String modifiedBy = "guest";
112 @Temporal(TemporalType.TIMESTAMP)
113 @Column(name = "modified_date", nullable = false)
114 private Date modifiedDate;
116 @Column(name = "defaultGroup", nullable = false)
117 private boolean defaultGroup = false;
118 @Column(name = "deleted", nullable = false)
119 private boolean deleted = false;
122 * Instantiates a new group entity.
124 public GroupEntity() {
129 * Called before an instance is persisted.
132 public void prePersist() {
133 Date date = new Date();
134 this.createdDate = date;
135 this.modifiedDate = date;
139 * Called before an instance is updated.
142 public void preUpdate() {
143 this.modifiedDate = new Date();
147 * Adds the policy to group.
149 * @param policy the policy
151 public void addPolicyToGroup(PolicyEntity policy) {
152 if (policies == null) {
153 policies = new ArrayList<>();
156 if (!this.policies.contains(policy)) {
157 this.policies.add(policy);
162 * Removes the policy from group.
164 * @param policy the policy
166 public void removePolicyFromGroup(PolicyEntity policy) {
167 this.policies.remove(policy);
169 if (policies.isEmpty()) {