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;
46 import lombok.ToString;
49 * The persistent class for the Categories database table.
53 @Table(name = "Category")
54 @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c")
58 public class Category implements Serializable {
59 private static final long serialVersionUID = 1L;
61 public static final char STANDARD = 'S';
62 public static final char CUSTOM = 'C';
65 @GeneratedValue(strategy = GenerationType.AUTO)
69 @Column(name = "grouping", nullable = false, length = 64)
70 private String grouping;
72 @Column(name = "is_standard", nullable = false)
73 private char isStandard;
75 @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
76 private String xacmlId;
78 @Column(name = "short_name", nullable = false, length = 64)
79 private String shortName;
81 // bi-directional many-to-one association to Attribute
82 @OneToMany(mappedBy = "categoryBean")
84 private Set<Attribute> attributes = new HashSet<>();
87 * Instantiates a new category.
90 this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
91 this.grouping = "subject";
92 this.isStandard = Category.STANDARD;
93 this.shortName = "subject";
97 * Instantiates a new category.
100 * @param grouping the grouping
101 * @param isStandard the is standard
103 public Category(Identifier cat, String grouping, char isStandard) {
105 this.xacmlId = cat.stringValue();
107 this.isStandard = isStandard;
108 if (grouping != null) {
109 this.grouping = grouping;
111 this.grouping = Category.extractGrouping(this.xacmlId);
116 * Instantiates a new category.
119 * @param grouping the grouping
121 public Category(Identifier cat, String grouping) {
122 this(cat, grouping, Category.STANDARD);
126 * Instantiates a new category.
129 * @param standard the standard
131 public Category(Identifier cat, char standard) {
132 this(cat, null, standard);
136 * Instantiates a new category.
140 public Category(Identifier cat) {
141 this(cat, Category.STANDARD);
145 * Adds the attribute.
147 * @param attribute the attribute
148 * @return the attribute
150 public Attribute addAttribute(Attribute attribute) {
151 getAttributes().add(attribute);
152 attribute.setCategoryBean(this);
158 * Removes the attribute.
160 * @param attribute the attribute
161 * @return the attribute
163 public Attribute removeAttribute(Attribute attribute) {
164 getAttributes().remove(attribute);
165 attribute.setCategoryBean(null);
171 * Checks if is standard.
173 * @return true, if is standard
176 public boolean isStandard() {
177 return this.isStandard == Category.STANDARD;
181 * Checks if is custom.
183 * @return true, if is custom
186 public boolean isCustom() {
187 return this.isStandard == Category.CUSTOM;
193 * @param xacmlId the xacml id
197 public static String extractGrouping(String xacmlId) {
198 if (xacmlId == null) {
201 String[] parts = xacmlId.split("[:]");
202 if (xacmlId.matches(".*:attribute\\-category:.*")) {
203 return parts[parts.length - 1];
204 } else if (xacmlId.matches(".*:[a-zA-Z]+[\\-]category:.*")) {
205 for (String part : parts) {
206 int index = part.indexOf("-category");
208 return part.substring(0, index);
216 * Gets the identifer.
218 * @return the identifer
221 public Identifier getIdentifer() {
222 return new IdentifierImpl(this.xacmlId);