Fixed the Sonar technical debt.
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / jpa / Category.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.rest.jpa;
22
23 import java.io.Serializable;
24 import java.util.HashSet;
25 import java.util.Set;
26
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;
36
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
42
43 /**
44  * The persistent class for the Categories database table.
45  * 
46  */
47 @Entity
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;
52         
53         public static final char STANDARD = 'S';
54         public static final char CUSTOM = 'C';
55
56         @Id
57         @GeneratedValue(strategy = GenerationType.AUTO)
58         @Column(name="id")
59         private int id;
60
61         @Column(name="grouping", nullable=false, length=64)
62         private String grouping;
63
64         @Column(name="is_standard", nullable=false)
65         private char isStandard;
66
67         @Column(name="xacml_id", nullable=false, unique=true, length=255)
68         private String xacmlId;
69         
70         @Column(name="short_name", nullable=false, length=64)
71         private String shortName;
72         
73         //bi-directional many-to-one association to Attribute
74         @OneToMany(mappedBy="categoryBean")
75         @JsonBackReference
76         private Set<Attribute> attributes = new HashSet<>();
77
78         public Category() {
79                 this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
80                 this.grouping = "subject";
81                 this.isStandard = Category.STANDARD;
82                 this.shortName = "subject";
83         }
84
85         public Category(Identifier cat, String grouping, char isStandard) {
86                 if (cat != null) {
87                         this.xacmlId = cat.stringValue();
88                 }
89                 this.isStandard = isStandard;
90                 if (grouping != null) {
91                         this.grouping = grouping;
92                 } else {
93                         this.grouping = Category.extractGrouping(this.xacmlId);
94                 }
95         }
96
97         public Category(Identifier cat, String grouping) {
98                 this(cat, grouping, Category.STANDARD);
99         }
100
101         public Category(Identifier cat, char standard) {
102                 this(cat, null, standard);
103         }
104
105         public Category(Identifier cat) {
106                 this(cat, Category.STANDARD);
107         }
108
109         public int getId() {
110                 return this.id;
111         }
112
113         public void setId(int id) {
114                 this.id = id;
115         }
116
117         public String getGrouping() {
118                 return this.grouping;
119         }
120
121         public void setGrouping(String grouping) {
122                 this.grouping = grouping;
123         }
124
125         public char getIsStandard() {
126                 return this.isStandard;
127         }
128
129         public void setIsStandard(char isStandard) {
130                 this.isStandard = isStandard;
131         }
132
133         public String getXacmlId() {
134                 return this.xacmlId;
135         }
136
137         public void setXacmlId(String xacmlId) {
138                 this.xacmlId = xacmlId;
139         }
140
141         public String getShortName() {
142                 return this.shortName;
143         }
144
145         public void setShortName(String shortName) {
146                 this.shortName = shortName;
147         }
148
149         public Set<Attribute> getAttributes() {
150                 return this.attributes;
151         }
152
153         public void setAttributes(Set<Attribute> attributes) {
154                 this.attributes = attributes;
155         }
156
157         public Attribute addAttribute(Attribute attribute) {
158                 getAttributes().add(attribute);
159                 attribute.setCategoryBean(this);
160
161                 return attribute;
162         }
163
164         public Attribute removeAttribute(Attribute attribute) {
165                 getAttributes().remove(attribute);
166                 attribute.setCategoryBean(null);
167
168                 return attribute;
169         }
170
171         @Transient
172         public boolean isStandard() {
173                 return this.isStandard == Category.STANDARD;
174         }
175         
176         @Transient
177         public boolean isCustom() {
178                 return this.isStandard == Category.CUSTOM;
179         }
180         
181         @Transient
182         public static String    extractGrouping(String xacmlId) {
183                 if (xacmlId == null) {
184                         return null;
185                 }
186                 if (xacmlId.matches(".*:attribute\\-category:.*")) {
187                         String[] parts = xacmlId.split("[:]");
188                         if (parts != null && parts.length > 0) {
189                                 return parts[parts.length - 1];
190                         }
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");
196                                         if (index > 0) {
197                                                 return part.substring(0, index);
198                                         }
199                                 }
200                         }
201                 }
202                 return null;
203         }
204         
205         @Transient
206         public Identifier getIdentifer() {
207                 return new IdentifierImpl(this.xacmlId);
208         }
209
210         @Transient
211         @Override
212         public String toString() {
213                 return "Category [id=" + id + ", grouping=" + grouping
214                                 + ", isStandard=" + isStandard + ", xacmlId=" + xacmlId
215                                 + ", attributes=" + attributes + "]";
216         }
217
218 }