JUnit/SONAR/Checkstyle in ONAP-REST
[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  * 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.rest.jpa;
24
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;
29
30 import java.io.Serializable;
31 import java.util.HashSet;
32 import java.util.Set;
33
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;
43
44 import lombok.Getter;
45 import lombok.Setter;
46 import lombok.ToString;
47
48 /**
49  * The persistent class for the Categories database table.
50  *
51  */
52 @Entity
53 @Table(name = "Category")
54 @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c")
55 @Getter
56 @Setter
57 @ToString
58 public class Category implements Serializable {
59     private static final long serialVersionUID = 1L;
60
61     public static final char STANDARD = 'S';
62     public static final char CUSTOM = 'C';
63
64     @Id
65     @GeneratedValue(strategy = GenerationType.AUTO)
66     @Column(name = "id")
67     private int id;
68
69     @Column(name = "grouping", nullable = false, length = 64)
70     private String grouping;
71
72     @Column(name = "is_standard", nullable = false)
73     private char isStandard;
74
75     @Column(name = "xacml_id", nullable = false, unique = true, length = 255)
76     private String xacmlId;
77
78     @Column(name = "short_name", nullable = false, length = 64)
79     private String shortName;
80
81     // bi-directional many-to-one association to Attribute
82     @OneToMany(mappedBy = "categoryBean")
83     @JsonBackReference
84     private Set<Attribute> attributes = new HashSet<>();
85
86     /**
87      * Instantiates a new category.
88      */
89     public Category() {
90         this.xacmlId = XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue();
91         this.grouping = "subject";
92         this.isStandard = Category.STANDARD;
93         this.shortName = "subject";
94     }
95
96     /**
97      * Instantiates a new category.
98      *
99      * @param cat the cat
100      * @param grouping the grouping
101      * @param isStandard the is standard
102      */
103     public Category(Identifier cat, String grouping, char isStandard) {
104         if (cat != null) {
105             this.xacmlId = cat.stringValue();
106         }
107         this.isStandard = isStandard;
108         if (grouping != null) {
109             this.grouping = grouping;
110         } else {
111             this.grouping = Category.extractGrouping(this.xacmlId);
112         }
113     }
114
115     /**
116      * Instantiates a new category.
117      *
118      * @param cat the cat
119      * @param grouping the grouping
120      */
121     public Category(Identifier cat, String grouping) {
122         this(cat, grouping, Category.STANDARD);
123     }
124
125     /**
126      * Instantiates a new category.
127      *
128      * @param cat the cat
129      * @param standard the standard
130      */
131     public Category(Identifier cat, char standard) {
132         this(cat, null, standard);
133     }
134
135     /**
136      * Instantiates a new category.
137      *
138      * @param cat the cat
139      */
140     public Category(Identifier cat) {
141         this(cat, Category.STANDARD);
142     }
143
144     /**
145      * Adds the attribute.
146      *
147      * @param attribute the attribute
148      * @return the attribute
149      */
150     public Attribute addAttribute(Attribute attribute) {
151         getAttributes().add(attribute);
152         attribute.setCategoryBean(this);
153
154         return attribute;
155     }
156
157     /**
158      * Removes the attribute.
159      *
160      * @param attribute the attribute
161      * @return the attribute
162      */
163     public Attribute removeAttribute(Attribute attribute) {
164         getAttributes().remove(attribute);
165         attribute.setCategoryBean(null);
166
167         return attribute;
168     }
169
170     /**
171      * Checks if is standard.
172      *
173      * @return true, if is standard
174      */
175     @Transient
176     public boolean isStandard() {
177         return this.isStandard == Category.STANDARD;
178     }
179
180     /**
181      * Checks if is custom.
182      *
183      * @return true, if is custom
184      */
185     @Transient
186     public boolean isCustom() {
187         return this.isStandard == Category.CUSTOM;
188     }
189
190     /**
191      * Extract grouping.
192      *
193      * @param xacmlId the xacml id
194      * @return the string
195      */
196     @Transient
197     public static String extractGrouping(String xacmlId) {
198         if (xacmlId == null) {
199             return null;
200         }
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");
207                 if (index > 0) {
208                     return part.substring(0, index);
209                 }
210             }
211         }
212         return null;
213     }
214
215     /**
216      * Gets the identifer.
217      *
218      * @return the identifer
219      */
220     @Transient
221     public Identifier getIdentifer() {
222         return new IdentifierImpl(this.xacmlId);
223     }
224 }