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.openecomp.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;
41 import com.fasterxml.jackson.annotation.JsonIgnore;
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 if (xacmlId.matches(".*:attribute\\-category:.*")) {
188 String[] parts = xacmlId.split("[:]");
189 if (parts != null && parts.length > 0) {
190 return parts[parts.length - 1];
192 } else if (xacmlId.matches(".*:[a-zA-Z]+[\\-]category:.*")) {
193 String[] parts = xacmlId.split("[:]");
194 if (parts != null && parts.length > 0) {
195 for (String part : parts) {
196 int index = part.indexOf("-category");
198 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 + "]";