2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest.jpa;
23 import java.io.Serializable;
24 import java.util.HashSet;
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;
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;
44 * The persistent class for the Categories database table.
48 @Table(name="Category")
49 @NamedQuery(name="Category.findAll", query="SELECT c FROM Category c")
50 public class Category implements Serializable {
51 private static final long serialVersionUID = 1L;
53 public static final char STANDARD = 'S';
54 public static final char CUSTOM = 'C';
57 @GeneratedValue(strategy = GenerationType.AUTO)
61 @Column(name="grouping", nullable=false, length=64)
62 private String grouping;
64 @Column(name="is_standard", nullable=false)
65 private char isStandard;
67 @Column(name="xacml_id", nullable=false, unique=true, length=255)
68 private String xacmlId;
70 @Column(name="short_name", nullable=false, length=64)
71 private String shortName;
73 //bi-directional many-to-one association to Attribute
74 @OneToMany(mappedBy="categoryBean")
76 private Set<Attribute> attributes = new HashSet<>();
79 this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
80 this.grouping = "subject";
81 this.isStandard = Category.STANDARD;
82 this.shortName = "subject";
85 public Category(Identifier cat, String grouping, char isStandard) {
87 this.xacmlId = cat.stringValue();
89 this.isStandard = isStandard;
90 if (grouping != null) {
91 this.grouping = grouping;
93 this.grouping = Category.extractGrouping(this.xacmlId);
97 public Category(Identifier cat, String grouping) {
98 this(cat, grouping, Category.STANDARD);
101 public Category(Identifier cat, char standard) {
102 this(cat, null, standard);
105 public Category(Identifier cat) {
106 this(cat, Category.STANDARD);
113 public void setId(int id) {
117 public String getGrouping() {
118 return this.grouping;
121 public void setGrouping(String grouping) {
122 this.grouping = grouping;
125 public char getIsStandard() {
126 return this.isStandard;
129 public void setIsStandard(char isStandard) {
130 this.isStandard = isStandard;
133 public String getXacmlId() {
137 public void setXacmlId(String xacmlId) {
138 this.xacmlId = xacmlId;
141 public String getShortName() {
142 return this.shortName;
145 public void setShortName(String shortName) {
146 this.shortName = shortName;
149 public Set<Attribute> getAttributes() {
150 return this.attributes;
153 public void setAttributes(Set<Attribute> attributes) {
154 this.attributes = attributes;
157 public Attribute addAttribute(Attribute attribute) {
158 getAttributes().add(attribute);
159 attribute.setCategoryBean(this);
164 public Attribute removeAttribute(Attribute attribute) {
165 getAttributes().remove(attribute);
166 attribute.setCategoryBean(null);
172 public boolean isStandard() {
173 return (this.isStandard == Category.STANDARD);
177 public boolean isCustom() {
178 return (this.isStandard == Category.CUSTOM);
182 public static String extractGrouping(String xacmlId) {
183 if (xacmlId == null) {
186 if (xacmlId.matches(".*:attribute\\-category:.*")) {
187 String[] parts = xacmlId.split("[:]");
188 if (parts != null && parts.length > 0) {
189 return parts[parts.length - 1];
191 } else if (xacmlId.matches(".*:[a-zA-Z]+[\\-]category:.*")) {
192 String[] parts = xacmlId.split("[:]");
193 if (parts != null && parts.length > 0) {
194 for (String part : parts) {
195 int index = part.indexOf("-category");
197 return part.substring(0, index);
206 public Identifier getIdentifer() {
207 return new IdentifierImpl(this.xacmlId);
212 public String toString() {
213 return "Category [id=" + id + ", grouping=" + grouping
214 + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId
215 + ", attributes=" + attributes + "]";