Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-REST / src / main / java / org / openecomp / policy / rest / jpa / Datatype.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.HashSet;
25 import java.util.Set;
26
27 import javax.persistence.Column;
28 import javax.persistence.Entity;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.OneToMany;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36
37 import com.att.research.xacml.api.Identifier;
38 import com.att.research.xacml.api.XACML3;
39 import com.att.research.xacml.std.IdentifierImpl;
40 import com.fasterxml.jackson.annotation.JsonBackReference;
41 import com.fasterxml.jackson.annotation.JsonIgnore;
42 import com.fasterxml.jackson.annotation.JsonManagedReference;
43
44
45 /**
46  * The persistent class for the Datatype database table.
47  * 
48  */
49 @Entity
50 @Table(name="Datatype")
51 @NamedQuery(name="Datatype.findAll", query="SELECT d FROM Datatype d")
52 public class Datatype implements Serializable {
53         private static final long serialVersionUID = 1L;
54         
55         public static final char STANDARD = 'S';
56         public static final char CUSTOM = 'C';
57
58         @Id
59         @GeneratedValue(strategy = GenerationType.AUTO)
60         @Column(name="id")
61         private int id;
62
63         @Column(name="is_standard", nullable=false)
64         private char isStandard;
65
66         @Column(name="xacml_id", nullable=false, unique=true, length=255)
67         private String xacmlId;
68
69         @Column(name="short_name", nullable=false, length=64)
70         private String shortName;
71
72         //bi-directional many-to-one association to Attribute
73         @OneToMany(mappedBy="datatypeBean")
74         @JsonBackReference
75         private Set<Attribute> attributes = new HashSet<>();
76
77         //bi-directional many-to-one association to Attribute
78         @OneToMany(mappedBy="datatypeBean")
79         @JsonIgnore
80         private Set<FunctionDefinition> functions = new HashSet<>();
81
82         //bi-directional many-to-one association to Attribute
83         @OneToMany(mappedBy="datatypeBean")
84         @JsonIgnore
85         private Set<FunctionArgument> arguments = new HashSet<>();
86
87         public Datatype() {
88                 this.xacmlId = XACML3.ID_DATATYPE_STRING.stringValue();
89                 this.isStandard = Datatype.STANDARD;
90         }
91         
92         
93         public Datatype(int id, Datatype dt) {
94                 this.id = id;
95                 this.isStandard = dt.isStandard;
96                 this.xacmlId = dt.xacmlId;
97                 this.shortName = dt.shortName;
98                 //
99                 // Make a copy?
100                 //
101                 this.attributes = new HashSet<>();
102         }
103         
104         public Datatype(Identifier identifier, char standard) {
105                 if (identifier != null) {
106                         this.xacmlId = identifier.stringValue();
107                         
108                 }
109                 this.isStandard = standard;
110         }
111         
112         public Datatype(Identifier identifier) {
113                 this(identifier, Datatype.STANDARD);
114         }
115         
116         public int getId() {
117                 return this.id;
118         }
119
120         public void setId(int id) {
121                 this.id = id;
122         }
123
124         public char getIsStandard() {
125                 return this.isStandard;
126         }
127
128         public void setIsStandard(char isStandard) {
129                 this.isStandard = isStandard;
130         }
131
132         public String getXacmlId() {
133                 return this.xacmlId;
134         }
135
136         public void setXacmlId(String xacmlId) {
137                 this.xacmlId = xacmlId;
138         }
139
140         public String getShortName() {
141                 return shortName;
142         }
143
144         public void setShortName(String shortName) {
145                 this.shortName = shortName;
146         }       
147
148         public Set<Attribute> getAttributes() {
149                 return this.attributes;
150         }
151
152         public void setAttributes(Set<Attribute> attributes) {
153                 this.attributes = attributes;
154         }
155
156         public Attribute addAttribute(Attribute attribute) {
157                 getAttributes().add(attribute);
158                 attribute.setDatatypeBean(this);
159
160                 return attribute;
161         }
162
163         public Attribute removeAttribute(Attribute attribute) {
164                 getAttributes().remove(attribute);
165                 attribute.setDatatypeBean(null);
166
167                 return attribute;
168         }
169
170         public Set<FunctionDefinition> getFunctions() {
171                 return this.functions;
172         }
173
174         public void setFunctions(Set<FunctionDefinition> functions) {
175                 this.functions = functions;
176         }
177
178         public FunctionDefinition addFunction(FunctionDefinition function) {
179                 getFunctions().add(function);
180                 function.setDatatypeBean(this);
181
182                 return function;
183         }
184
185         public FunctionDefinition removeAttribute(FunctionDefinition function) {
186                 getFunctions().remove(function);
187                 function.setDatatypeBean(null);
188
189                 return function;
190         }
191
192         public Set<FunctionArgument> getArguments() {
193                 return this.arguments;
194         }
195
196         public void setArguments(Set<FunctionArgument> argument) {
197                 this.arguments = argument;
198         }
199
200         public FunctionArgument addArgument(FunctionArgument argument) {
201                 getArguments().add(argument);
202                 argument.setDatatypeBean(this);
203
204                 return argument;
205         }
206
207         public FunctionArgument removeArgument(FunctionArgument argument) {
208                 getArguments().remove(argument);
209                 argument.setDatatypeBean(null);
210
211                 return argument;
212         }
213
214         @Transient
215         public Identifier getIdentifer() {
216                 return new IdentifierImpl(this.xacmlId);
217         }
218         
219         @Transient
220         public Identifier getIdentiferByShortName() {
221                 return new IdentifierImpl(this.shortName);
222         }
223
224         @Transient
225         public boolean isStandard() {
226                 return (this.isStandard == Datatype.STANDARD);
227         }
228         
229         @Transient
230         public boolean isCustom() {
231                 return (this.isStandard == Datatype.CUSTOM);
232         }
233
234         @Transient
235         @Override
236         public String toString() {
237                 return "Datatype [id=" + id + ", isStandard=" + isStandard
238                                 + ", xacmlId=" + xacmlId + ", shortName=" + shortName
239                                 + ", attributes=" + attributes + ", functions=" + functions
240                                 + ", arguments=" + arguments + "]";
241                                 
242                 //return "Datatype [shortName=" + shortName + "]";
243         }
244
245 }