2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.rest.jpa;
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;
30 import java.io.Serializable;
31 import java.util.HashSet;
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;
45 * The persistent class for the Categories database table.
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;
54 public static final char STANDARD = 'S';
55 public static final char CUSTOM = 'C';
58 @GeneratedValue(strategy = GenerationType.AUTO)
62 @Column(name = "grouping", nullable = false, length = 64)
63 private String grouping;
65 @Column(name = "is_standard", nullable = false)
66 private char isStandard;
68 @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
69 private String xacmlId;
71 @Column(name = "short_name", nullable = false, length = 64)
72 private String shortName;
74 // bi-directional many-to-one association to Attribute
75 @OneToMany(mappedBy = "categoryBean")
77 private Set<Attribute> attributes = new HashSet<>();
80 * Instantiates a new category.
83 this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
84 this.grouping = "subject";
85 this.isStandard = Category.STANDARD;
86 this.shortName = "subject";
90 * Instantiates a new category.
93 * @param grouping the grouping
94 * @param isStandard the is standard
96 public Category(Identifier cat, String grouping, char isStandard) {
98 this.xacmlId = cat.stringValue();
100 this.isStandard = isStandard;
101 if (grouping != null) {
102 this.grouping = grouping;
104 this.grouping = Category.extractGrouping(this.xacmlId);
109 * Instantiates a new category.
112 * @param grouping the grouping
114 public Category(Identifier cat, String grouping) {
115 this(cat, grouping, Category.STANDARD);
119 * Instantiates a new category.
122 * @param standard the standard
124 public Category(Identifier cat, char standard) {
125 this(cat, null, standard);
129 * Instantiates a new category.
133 public Category(Identifier cat) {
134 this(cat, Category.STANDARD);
149 * @param id the new id
151 public void setId(int id) {
158 * @return the grouping
160 public String getGrouping() {
161 return this.grouping;
167 * @param grouping the new grouping
169 public void setGrouping(String grouping) {
170 this.grouping = grouping;
174 * Gets the checks if is standard.
176 * @return the checks if is standard
178 public char getIsStandard() {
179 return this.isStandard;
183 * Sets the checks if is standard.
185 * @param isStandard the new checks if is standard
187 public void setIsStandard(char isStandard) {
188 this.isStandard = isStandard;
194 * @return the xacml id
196 public String getXacmlId() {
203 * @param xacmlId the new xacml id
205 public void setXacmlId(String xacmlId) {
206 this.xacmlId = xacmlId;
210 * Gets the short name.
212 * @return the short name
214 public String getShortName() {
215 return this.shortName;
219 * Sets the short name.
221 * @param shortName the new short name
223 public void setShortName(String shortName) {
224 this.shortName = shortName;
228 * Gets the attributes.
230 * @return the attributes
232 public Set<Attribute> getAttributes() {
233 return this.attributes;
237 * Sets the attributes.
239 * @param attributes the new attributes
241 public void setAttributes(Set<Attribute> attributes) {
242 this.attributes = attributes;
246 * Adds the attribute.
248 * @param attribute the attribute
249 * @return the attribute
251 public Attribute addAttribute(Attribute attribute) {
252 getAttributes().add(attribute);
253 attribute.setCategoryBean(this);
259 * Removes the attribute.
261 * @param attribute the attribute
262 * @return the attribute
264 public Attribute removeAttribute(Attribute attribute) {
265 getAttributes().remove(attribute);
266 attribute.setCategoryBean(null);
272 * Checks if is standard.
274 * @return true, if is standard
277 public boolean isStandard() {
278 return this.isStandard == Category.STANDARD;
282 * Checks if is custom.
284 * @return true, if is custom
287 public boolean isCustom() {
288 return this.isStandard == Category.CUSTOM;
294 * @param xacmlId the xacml id
298 public static String extractGrouping(String xacmlId) {
299 if (xacmlId == null) {
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");
309 return part.substring(0, index);
317 * Gets the identifer.
319 * @return the identifer
322 public Identifier getIdentifer() {
323 return new IdentifierImpl(this.xacmlId);
333 public String toString() {
334 return "Category [id=" + id + ", grouping=" + grouping + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId
335 + ", attributes=" + attributes + "]";