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