Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-REST / src / main / java / org / openecomp / policy / rest / jpa / Obadvice.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.rest.jpa;
22
23 import java.io.Serializable;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.GeneratedValue;
32 import javax.persistence.GenerationType;
33 import javax.persistence.Id;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToMany;
36 import javax.persistence.PrePersist;
37 import javax.persistence.PreUpdate;
38 import javax.persistence.Table;
39 import javax.persistence.Temporal;
40 import javax.persistence.TemporalType;
41 import javax.persistence.Transient;
42
43 import com.att.research.xacml.api.Identifier;
44
45 /**
46  * The persistent class for the Obadvice database table.
47  * 
48  */
49 @Entity
50 @Table(name="Obadvice")
51 @NamedQuery(name="Obadvice.findAll", query="SELECT o FROM Obadvice o")
52 public class Obadvice implements Serializable {
53         private static final long serialVersionUID = 1L;
54
55         public static final String OBLIGATION = "Obligation";
56         public static final String ADVICE = "Advice";
57         public static final String EFFECT_PERMIT = "Permit";
58         public static final String EFFECT_DENY = "Deny";
59         @Id
60         @GeneratedValue(strategy = GenerationType.AUTO)
61         @Column(name="id")
62         private int id;
63
64         @Column(name="type", nullable=false)
65         private String type;
66
67         @Column(name="xacml_id", nullable=false, length=255)
68         private String xacmlId;
69
70         @Column(name="fulfill_on", nullable=true, length=32)
71         private String fulfillOn;
72
73         @Column(name="description", nullable=true, length=2048)
74         private String description;
75
76         //bi-directional one-to-many association to Attribute Assignment
77         @OneToMany(mappedBy="obadvice", orphanRemoval=true, cascade=CascadeType.REMOVE)
78         private Set<ObadviceExpression> obadviceExpressions = new HashSet<>(2);
79
80         @Column(name="created_by", nullable=false, length=255)
81         private String createdBy;
82
83         @Temporal(TemporalType.TIMESTAMP)
84         @Column(name="created_date", nullable=false, updatable=false)
85         private Date createdDate;
86
87         @Column(name="modified_by", nullable=false, length=255)
88         private String modifiedBy;
89
90         @Temporal(TemporalType.TIMESTAMP)
91         @Column(name="modified_date", nullable=false)
92         private Date modifiedDate;
93
94         public Obadvice() {
95                 this.type = Obadvice.OBLIGATION;
96                 this.fulfillOn = Obadvice.EFFECT_PERMIT;
97         }
98         
99         public Obadvice(String domain, String userid) {
100                 this.xacmlId = domain;
101                 this.type = Obadvice.OBLIGATION;
102                 this.fulfillOn = Obadvice.EFFECT_PERMIT;
103                 this.createdBy = userid;
104                 this.modifiedBy = userid;
105         }
106
107         public Obadvice(Identifier id, String userid) {
108                 this(id.stringValue(), userid);
109         }
110
111         @PrePersist
112         public void     prePersist() {
113                 Date date = new Date();
114                 this.createdDate = date;
115                 this.modifiedDate = date;
116         }
117         
118         @PreUpdate
119         public void preUpdate() {
120                 this.modifiedDate = new Date();
121         }
122
123         public int getId() {
124                 return this.id;
125         }
126
127         public void setId(int id) {
128                 this.id = id;
129         }
130
131         public String getCreatedBy() {
132                 return this.createdBy;
133         }
134
135         public void setCreatedBy(String createdBy) {
136                 this.createdBy = createdBy;
137         }
138
139         public String getDescription() {
140                 return this.description;
141         }
142
143         public void setDescription(String description) {
144                 this.description = description;
145         }
146
147         public String getFulfillOn() {
148                 return this.fulfillOn;
149         }
150
151         public void setFulfillOn(String fulfillOn) {
152                 this.fulfillOn = fulfillOn;
153         }
154
155         public String getModifiedBy() {
156                 return this.modifiedBy;
157         }
158
159         public void setModifiedBy(String modifiedBy) {
160                 this.modifiedBy = modifiedBy;
161         }
162
163         public String getType() {
164                 return this.type;
165         }
166
167         public void setType(String type) {
168                 this.type = type;
169         }
170
171         public String getXacmlId() {
172                 return this.xacmlId;
173         }
174
175         public void setXacmlId(String xacmlId) {
176                 this.xacmlId = xacmlId;
177         }
178
179         public Set<ObadviceExpression> getObadviceExpressions() {
180                 return this.obadviceExpressions;
181         }
182
183         public void setObadviceExpressions(Set<ObadviceExpression> obadviceExpressions) {
184                 this.obadviceExpressions = obadviceExpressions;
185         }
186
187         public ObadviceExpression addObadviceExpression(ObadviceExpression obadviceExpression) {
188                 this.obadviceExpressions.add(obadviceExpression);
189                 obadviceExpression.setObadvice(this);
190
191                 return obadviceExpression;
192         }
193
194         public ObadviceExpression removeObadviceExpression(ObadviceExpression obadviceExpression) {
195                 this.obadviceExpressions.remove(obadviceExpression);
196                 obadviceExpression.setObadvice(null);
197
198                 return obadviceExpression;
199         }
200         
201         public void removeAllExpressions() {
202                 if (this.obadviceExpressions == null) {
203                         return;
204                 }
205                 for (ObadviceExpression expression : this.obadviceExpressions) {
206                         expression.setObadvice(null);
207                 }
208                 this.obadviceExpressions.clear();
209         }
210
211         @Transient
212         public Obadvice clone() {
213                 Obadvice obadvice = new Obadvice();
214                 
215                 obadvice.type = this.type;
216                 obadvice.xacmlId = this.xacmlId;
217                 obadvice.fulfillOn = this.fulfillOn;
218                 obadvice.description = this.description;
219                 obadvice.createdBy = this.createdBy;
220                 obadvice.modifiedBy = this.modifiedBy;
221                 for (ObadviceExpression exp: this.obadviceExpressions) {
222                         obadvice.addObadviceExpression(exp.clone());
223                 }
224
225                 return obadvice;
226         }
227 }