2 * ============LICENSE_START=======================================================
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
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.att.research.xacml.api.Identifier;
26 import java.io.Serializable;
27 import java.util.Date;
28 import java.util.HashSet;
31 import javax.persistence.CascadeType;
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.NamedQuery;
38 import javax.persistence.OneToMany;
39 import javax.persistence.PrePersist;
40 import javax.persistence.PreUpdate;
41 import javax.persistence.Table;
42 import javax.persistence.Temporal;
43 import javax.persistence.TemporalType;
44 import javax.persistence.Transient;
50 * The persistent class for the Obadvice database table.
53 @Table(name = "Obadvice")
54 @NamedQuery(name = "Obadvice.findAll", query = "SELECT o FROM Obadvice o")
57 public class Obadvice implements Serializable {
58 private static final long serialVersionUID = 1L;
60 public static final String OBLIGATION = "Obligation";
61 public static final String ADVICE = "Advice";
62 public static final String EFFECT_PERMIT = "Permit";
63 public static final String EFFECT_DENY = "Deny";
65 @GeneratedValue(strategy = GenerationType.AUTO)
69 @Column(name = "type", nullable = false)
72 @Column(name = "xacml_id", nullable = false, length = 255)
73 private String xacmlId;
75 @Column(name = "fulfill_on", nullable = true, length = 32)
76 private String fulfillOn;
78 @Column(name = "description", nullable = true, length = 2048)
79 private String description;
81 // bi-directional one-to-many association to Attribute Assignment
82 @OneToMany(mappedBy = "obadvice", orphanRemoval = true, cascade = CascadeType.REMOVE)
83 private Set<ObadviceExpression> obadviceExpressions = new HashSet<>(2);
85 @Column(name = "created_by", nullable = false, length = 255)
86 private String createdBy;
88 @Temporal(TemporalType.TIMESTAMP)
89 @Column(name = "created_date", nullable = false, updatable = false)
90 private Date createdDate;
92 @Column(name = "modified_by", nullable = false, length = 255)
93 private String modifiedBy;
95 @Temporal(TemporalType.TIMESTAMP)
96 @Column(name = "modified_date", nullable = false)
97 private Date modifiedDate;
100 * Instantiates a new obadvice.
103 this.type = Obadvice.OBLIGATION;
104 this.fulfillOn = Obadvice.EFFECT_PERMIT;
108 * Instantiates a new obadvice.
110 * @param domain the domain
111 * @param userid the userid
113 public Obadvice(String domain, String userid) {
114 this.xacmlId = domain;
115 this.type = Obadvice.OBLIGATION;
116 this.fulfillOn = Obadvice.EFFECT_PERMIT;
117 this.createdBy = userid;
118 this.modifiedBy = userid;
122 * Instantiates a new obadvice.
125 * @param userid the userid
127 public Obadvice(Identifier id, String userid) {
128 this(id.stringValue(), userid);
135 public void prePersist() {
136 Date date = new Date();
137 this.createdDate = date;
138 this.modifiedDate = date;
145 public void preUpdate() {
146 this.modifiedDate = new Date();
150 * Adds the obadvice expression.
152 * @param obadviceExpression the obadvice expression
153 * @return the obadvice expression
155 public ObadviceExpression addObadviceExpression(ObadviceExpression obadviceExpression) {
156 this.obadviceExpressions.add(obadviceExpression);
157 obadviceExpression.setObadvice(this);
159 return obadviceExpression;
163 * Removes the obadvice expression.
165 * @param obadviceExpression the obadvice expression
166 * @return the obadvice expression
168 public ObadviceExpression removeObadviceExpression(ObadviceExpression obadviceExpression) {
169 this.obadviceExpressions.remove(obadviceExpression);
170 obadviceExpression.setObadvice(null);
172 return obadviceExpression;
176 * Removes the all expressions.
178 public void removeAllExpressions() {
179 if (this.obadviceExpressions == null) {
182 for (ObadviceExpression expression : this.obadviceExpressions) {
183 expression.setObadvice(null);
185 this.obadviceExpressions.clear();
191 * @return the obadvice
195 public Obadvice clone() {
196 Obadvice obadvice = new Obadvice();
198 obadvice.type = this.type;
199 obadvice.xacmlId = this.xacmlId;
200 obadvice.fulfillOn = this.fulfillOn;
201 obadvice.description = this.description;
202 obadvice.createdBy = this.createdBy;
203 obadvice.modifiedBy = this.modifiedBy;
204 for (ObadviceExpression exp : this.obadviceExpressions) {
205 obadvice.addObadviceExpression(exp.clone());