Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-REST / src / main / java / org / openecomp / policy / rest / jpa / DecisionSettings.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
26 import javax.persistence.Column;
27 import javax.persistence.Entity;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.ManyToOne;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OrderBy;
35 import javax.persistence.PrePersist;
36 import javax.persistence.PreUpdate;
37 import javax.persistence.Table;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40 import javax.persistence.Transient;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.openecomp.policy.rest.XacmlAdminAuthorization;
45
46 import com.att.research.xacml.api.Identifier;
47 import com.att.research.xacml.std.IdentifierImpl;
48 import com.fasterxml.jackson.annotation.JsonManagedReference;
49
50 import org.openecomp.policy.common.logging.eelf.MessageCodes;
51 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
52
53
54 @Entity
55 @Table(name="DecisionSettings")
56 @NamedQuery(name="DecisionSettings.findAll", query="SELECT a FROM DecisionSettings a order by  a.priority asc, a.xacmlId asc")
57 public class DecisionSettings implements Serializable {
58         private static final long serialVersionUID = 1L;
59
60         @Id
61         @GeneratedValue(strategy = GenerationType.AUTO)
62         @Column(name="id")
63         private int id;
64
65         @Temporal(TemporalType.TIMESTAMP)
66         @Column(name="created_date", updatable=false)
67         private Date createdDate;
68
69         @Column(name="description", nullable=true, length=2048)
70         private String description;
71
72         @Temporal(TemporalType.TIMESTAMP)
73         @Column(name="modified_date", nullable=false)
74         private Date modifiedDate;
75
76         @Column(name="PRIORITY", nullable=true)
77         @OrderBy("asc")
78         private String priority;
79         
80         @Column(name="xacml_id", unique = true, nullable=false)
81         @OrderBy("asc")
82         private String xacmlId = "urn";
83
84         //bi-directional many-to-one association to Datatype
85         @ManyToOne
86         @JoinColumn(name="datatype")
87         private Datatype datatypeBean;
88         
89         @Transient
90         private String issuer = null;
91         
92         @Transient
93         private boolean mustBePresent = false;
94
95         @ManyToOne(optional = false)
96         @JoinColumn(name="created_by")
97         private UserInfo userCreatedBy;
98
99         @ManyToOne(optional = false)
100         @JoinColumn(name="modified_by")
101         private UserInfo userModifiedBy;
102         
103         public UserInfo getUserCreatedBy() {
104                 return userCreatedBy;
105         }
106
107         public void setUserCreatedBy(UserInfo userCreatedBy) {
108                 this.userCreatedBy = userCreatedBy;
109         }
110
111         public UserInfo getUserModifiedBy() {
112                 return userModifiedBy;
113         }
114
115         public void setUserModifiedBy(UserInfo userModifiedBy) {
116                 this.userModifiedBy = userModifiedBy;
117         }
118
119         private static Log logger = LogFactory.getLog(DecisionSettings.class);
120         public DecisionSettings() {
121         }
122         
123         public DecisionSettings(String domain) {
124                 this.xacmlId = domain;
125         }
126         
127         public DecisionSettings(String domain, String user) {
128                 this(domain);
129         }
130         public DecisionSettings(DecisionSettings copy, String user) {
131                 this(copy.getXacmlId() + ":(0)", user);
132                 this.datatypeBean = copy.getDatatypeBean();
133                 this.description = copy.getDescription();
134                 
135         }
136         
137         public String getDecisionSettings(){
138                 return this.xacmlId;
139         }
140         
141         public String setDecisionSettings(){
142                 return this.xacmlId = xacmlId;
143         }
144         @PrePersist
145         public void     prePersist() {
146                 Date date = new Date();
147                 this.createdDate = date;
148                 this.modifiedDate = date;
149         }
150         
151         @PreUpdate
152         public void preUpdate() {
153                 this.modifiedDate = new Date();
154                 try {
155                         this.userModifiedBy = XacmlAdminAuthorization.getUserId();;
156                 } catch (Exception e) {
157                         logger.error("Exception caused While adding Modified by Role"+e);
158                         // TODO:EELF Cleanup - Remove logger
159                         PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "DecisionSettings", "Exception caused While adding Modified by Role");
160                 }
161         }
162
163         public int getId() {
164                 return this.id;
165         }
166
167         public void setId(int id) {
168                 this.id = id;
169         }
170
171         
172         public Date getCreatedDate() {
173                 return this.createdDate;
174         }
175
176         public void setCreatedDate(Date createdDate) {
177                 this.createdDate = createdDate;
178         }
179
180         public String getDescription() {
181                 return this.description;
182         }
183
184         public void setDescription(String description) {
185                 this.description = description;
186         }
187
188         public Date getModifiedDate() {
189                 return this.modifiedDate;
190         }
191
192         public void setModifiedDate(Date modifiedDate) {
193                 this.modifiedDate = modifiedDate;
194         }
195
196         public String getXacmlId() {
197                 return this.xacmlId;
198         }
199         
200 /*      @Transient
201         public Identifier getXacmlIdentifier() {
202                 return new IdentifierImpl(this.xacmlId);
203         }*/
204
205         public void setXacmlId(String xacmlId) {
206                 this.xacmlId = xacmlId;
207         }
208         
209         public Datatype getDatatypeBean() {
210                 return this.datatypeBean;
211         }
212
213         public void setDatatypeBean(Datatype datatypeBean) {
214                 this.datatypeBean = datatypeBean;
215         }
216
217         @Transient
218         public String getIssuer() {
219                 return issuer;
220         }
221
222         @Transient
223         public void setIssuer(String issuer) {
224                 this.issuer = issuer;
225         }
226
227         @Transient
228         public boolean isMustBePresent() {
229                 return mustBePresent;
230         }
231
232         @Transient
233         public void setMustBePresent(boolean mustBePresent) {
234                 this.mustBePresent = mustBePresent;
235         }
236
237         public String getPriority() {
238                 return priority;
239         }
240
241         public void setPriority(String priority) {
242                 this.priority = priority;
243         }
244 }
245