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 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.jpa;
24 import java.io.Serializable;
25 import java.util.HashSet;
28 import javax.persistence.Column;
29 import javax.persistence.Entity;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.GenerationType;
32 import javax.persistence.Id;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToMany;
35 import javax.persistence.Table;
36 import javax.persistence.Transient;
38 import com.att.research.xacml.api.Identifier;
39 import com.att.research.xacml.api.XACML3;
40 import com.att.research.xacml.std.IdentifierImpl;
41 import com.fasterxml.jackson.annotation.JsonBackReference;
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 this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
81 this.grouping = "subject";
82 this.isStandard = Category.STANDARD;
83 this.shortName = "subject";
86 public Category(Identifier cat, String grouping, char isStandard) {
88 this.xacmlId = cat.stringValue();
90 this.isStandard = isStandard;
91 if (grouping != null) {
92 this.grouping = grouping;
94 this.grouping = Category.extractGrouping(this.xacmlId);
98 public Category(Identifier cat, String grouping) {
99 this(cat, grouping, Category.STANDARD);
102 public Category(Identifier cat, char standard) {
103 this(cat, null, standard);
106 public Category(Identifier cat) {
107 this(cat, Category.STANDARD);
114 public void setId(int id) {
118 public String getGrouping() {
119 return this.grouping;
122 public void setGrouping(String grouping) {
123 this.grouping = grouping;
126 public char getIsStandard() {
127 return this.isStandard;
130 public void setIsStandard(char isStandard) {
131 this.isStandard = isStandard;
134 public String getXacmlId() {
138 public void setXacmlId(String xacmlId) {
139 this.xacmlId = xacmlId;
142 public String getShortName() {
143 return this.shortName;
146 public void setShortName(String shortName) {
147 this.shortName = shortName;
150 public Set<Attribute> getAttributes() {
151 return this.attributes;
154 public void setAttributes(Set<Attribute> attributes) {
155 this.attributes = attributes;
158 public Attribute addAttribute(Attribute attribute) {
159 getAttributes().add(attribute);
160 attribute.setCategoryBean(this);
165 public Attribute removeAttribute(Attribute attribute) {
166 getAttributes().remove(attribute);
167 attribute.setCategoryBean(null);
173 public boolean isStandard() {
174 return this.isStandard == Category.STANDARD;
178 public boolean isCustom() {
179 return this.isStandard == Category.CUSTOM;
183 public static String extractGrouping(String xacmlId) {
184 if (xacmlId == null) {
187 String[] parts = xacmlId.split("[:]");
188 if (xacmlId.matches(".*:attribute\\-category:.*")) {
189 if (parts.length > 0) {
190 return parts[parts.length - 1];
192 } else if (xacmlId.matches(".*:[a-zA-Z]+[\\-]category:.*")) {
193 if (parts.length <= 0) {
196 for (String part : parts) {
197 int index = part.indexOf("-category");
199 return part.substring(0, index);
207 public Identifier getIdentifer() {
208 return new IdentifierImpl(this.xacmlId);
213 public String toString() {
214 return "Category [id=" + id + ", grouping=" + grouping
215 + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId
216 + ", attributes=" + attributes + "]";