JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / Category.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * Modifications Copyright (C) 2019 Nordix Foundation.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.rest.jpa;
24
25 import com.att.research.xacml.api.Identifier;
26 import com.att.research.xacml.api.XACML3;
27 import com.att.research.xacml.std.IdentifierImpl;
28 import com.fasterxml.jackson.annotation.JsonBackReference;
29
30 import java.io.Serializable;
31 import java.util.HashSet;
32 import java.util.Set;
33
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.GeneratedValue;
37 import javax.persistence.GenerationType;
38 import javax.persistence.Id;
39 import javax.persistence.NamedQuery;
40 import javax.persistence.OneToMany;
41 import javax.persistence.Table;
42 import javax.persistence.Transient;
43
44 /**
45  * The persistent class for the Categories database table.
46  *
47  */
48 @Entity
49 @Table(name = "Category")
50 @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c")
51 public class Category implements Serializable {
52     private static final long serialVersionUID = 1L;
53
54     public static final char STANDARD = 'S';
55     public static final char CUSTOM = 'C';
56
57     @Id
58     @GeneratedValue(strategy = GenerationType.AUTO)
59     @Column(name = "id")
60     private int id;
61
62     @Column(name = "grouping", nullable = false, length = 64)
63     private String grouping;
64
65     @Column(name = "is_standard", nullable = false)
66     private char isStandard;
67
68     @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
69     private String xacmlId;
70
71     @Column(name = "short_name", nullable = false, length = 64)
72     private String shortName;
73
74     // bi-directional many-to-one association to Attribute
75     @OneToMany(mappedBy = "categoryBean")
76     @JsonBackReference
77     private Set<Attribute> attributes = new HashSet<>();
78
79     /**
80      * Instantiates a new category.
81      */
82     public Category() {
83         this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
84         this.grouping = "subject";
85         this.isStandard = Category.STANDARD;
86         this.shortName = "subject";
87     }
88
89     /**
90      * Instantiates a new category.
91      *
92      * @param cat the cat
93      * @param grouping the grouping
94      * @param isStandard the is standard
95      */
96     public Category(Identifier cat, String grouping, char isStandard) {
97         if (cat != null) {
98             this.xacmlId = cat.stringValue();
99         }
100         this.isStandard = isStandard;
101         if (grouping != null) {
102             this.grouping = grouping;
103         } else {
104             this.grouping = Category.extractGrouping(this.xacmlId);
105         }
106     }
107
108     /**
109      * Instantiates a new category.
110      *
111      * @param cat the cat
112      * @param grouping the grouping
113      */
114     public Category(Identifier cat, String grouping) {
115         this(cat, grouping, Category.STANDARD);
116     }
117
118     /**
119      * Instantiates a new category.
120      *
121      * @param cat the cat
122      * @param standard the standard
123      */
124     public Category(Identifier cat, char standard) {
125         this(cat, null, standard);
126     }
127
128     /**
129      * Instantiates a new category.
130      *
131      * @param cat the cat
132      */
133     public Category(Identifier cat) {
134         this(cat, Category.STANDARD);
135     }
136
137     /**
138      * Gets the id.
139      *
140      * @return the id
141      */
142     public int getId() {
143         return this.id;
144     }
145
146     /**
147      * Sets the id.
148      *
149      * @param id the new id
150      */
151     public void setId(int id) {
152         this.id = id;
153     }
154
155     /**
156      * Gets the grouping.
157      *
158      * @return the grouping
159      */
160     public String getGrouping() {
161         return this.grouping;
162     }
163
164     /**
165      * Sets the grouping.
166      *
167      * @param grouping the new grouping
168      */
169     public void setGrouping(String grouping) {
170         this.grouping = grouping;
171     }
172
173     /**
174      * Gets the checks if is standard.
175      *
176      * @return the checks if is standard
177      */
178     public char getIsStandard() {
179         return this.isStandard;
180     }
181
182     /**
183      * Sets the checks if is standard.
184      *
185      * @param isStandard the new checks if is standard
186      */
187     public void setIsStandard(char isStandard) {
188         this.isStandard = isStandard;
189     }
190
191     /**
192      * Gets the xacml id.
193      *
194      * @return the xacml id
195      */
196     public String getXacmlId() {
197         return this.xacmlId;
198     }
199
200     /**
201      * Sets the xacml id.
202      *
203      * @param xacmlId the new xacml id
204      */
205     public void setXacmlId(String xacmlId) {
206         this.xacmlId = xacmlId;
207     }
208
209     /**
210      * Gets the short name.
211      *
212      * @return the short name
213      */
214     public String getShortName() {
215         return this.shortName;
216     }
217
218     /**
219      * Sets the short name.
220      *
221      * @param shortName the new short name
222      */
223     public void setShortName(String shortName) {
224         this.shortName = shortName;
225     }
226
227     /**
228      * Gets the attributes.
229      *
230      * @return the attributes
231      */
232     public Set<Attribute> getAttributes() {
233         return this.attributes;
234     }
235
236     /**
237      * Sets the attributes.
238      *
239      * @param attributes the new attributes
240      */
241     public void setAttributes(Set<Attribute> attributes) {
242         this.attributes = attributes;
243     }
244
245     /**
246      * Adds the attribute.
247      *
248      * @param attribute the attribute
249      * @return the attribute
250      */
251     public Attribute addAttribute(Attribute attribute) {
252         getAttributes().add(attribute);
253         attribute.setCategoryBean(this);
254
255         return attribute;
256     }
257
258     /**
259      * Removes the attribute.
260      *
261      * @param attribute the attribute
262      * @return the attribute
263      */
264     public Attribute removeAttribute(Attribute attribute) {
265         getAttributes().remove(attribute);
266         attribute.setCategoryBean(null);
267
268         return attribute;
269     }
270
271     /**
272      * Checks if is standard.
273      *
274      * @return true, if is standard
275      */
276     @Transient
277     public boolean isStandard() {
278         return this.isStandard == Category.STANDARD;
279     }
280
281     /**
282      * Checks if is custom.
283      *
284      * @return true, if is custom
285      */
286     @Transient
287     public boolean isCustom() {
288         return this.isStandard == Category.CUSTOM;
289     }
290
291     /**
292      * Extract grouping.
293      *
294      * @param xacmlId the xacml id
295      * @return the string
296      */
297     @Transient
298     public static String extractGrouping(String xacmlId) {
299         if (xacmlId == null) {
300             return null;
301         }
302         String[] parts = xacmlId.split("[:]");
303         if (xacmlId.matches(".*:attribute\\-category:.*")) {
304             return parts[parts.length - 1];
305         } else if (xacmlId.matches(".*:[a-zA-Z]+[\\-]category:.*")) {
306             for (String part : parts) {
307                 int index = part.indexOf("-category");
308                 if (index > 0) {
309                     return part.substring(0, index);
310                 }
311             }
312         }
313         return null;
314     }
315
316     /**
317      * Gets the identifer.
318      *
319      * @return the identifer
320      */
321     @Transient
322     public Identifier getIdentifer() {
323         return new IdentifierImpl(this.xacmlId);
324     }
325
326     /**
327      * To string.
328      *
329      * @return the string
330      */
331     @Transient
332     @Override
333     public String toString() {
334         return "Category [id=" + id + ", grouping=" + grouping + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId
335                         + ", attributes=" + attributes + "]";
336     }
337
338 }