Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / domain / EPRole.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software 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  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.domain;
39
40 import java.util.Iterator;
41 import java.util.SortedSet;
42 import java.util.TreeSet;
43
44 import javax.validation.Valid;
45 import org.hibernate.validator.constraints.SafeHtml;
46 import org.onap.portalsdk.core.domain.RoleFunction;
47 import org.onap.portalsdk.core.domain.support.DomainVo;
48 import com.fasterxml.jackson.annotation.JsonIgnore;
49
50 public class EPRole extends DomainVo {
51
52         private static final long serialVersionUID = 1L;
53         @SafeHtml
54         private String  name;
55     private boolean active;
56     private Integer priority;
57     
58     // ONAP will identify the specific remote application role id by appID;appRoleId among all the application roles it persists.
59     private Long appId;     // used by ONAP only 
60     private Long appRoleId; // used by ONAP only
61
62     private SortedSet<RoleFunction>     roleFunctions = new TreeSet<RoleFunction>();
63     @Valid
64     private SortedSet<EPRole> childRoles = new TreeSet<EPRole>();
65     
66     @JsonIgnore
67     private SortedSet<EPRole> parentRoles = new TreeSet<EPRole>();
68
69     public EPRole() {}
70
71     public String getName() {
72         return name;
73     }
74
75     public boolean getActive() {
76         return active;
77     }
78
79     public SortedSet<RoleFunction> getRoleFunctions() {
80         return roleFunctions;
81     }
82
83     public Integer getPriority() {
84         return priority;
85     }
86
87     public SortedSet<EPRole> getChildRoles() {
88         return childRoles;
89     }
90
91     public SortedSet<EPRole> getParentRoles() {
92         return parentRoles;
93     }
94
95     public void setName(String name) {
96         this.name = name;
97     }
98
99     public void setActive(boolean active) {
100         this.active = active;
101     }
102
103     public void setRoleFunctions(SortedSet<RoleFunction> roleFunctions) {
104         this.roleFunctions = roleFunctions;
105     }
106
107     public void setPriority(Integer priority) {
108         this.priority = priority;
109     }
110
111     public void setChildRoles(SortedSet<EPRole> childRoles) {
112         this.childRoles = childRoles;
113     }
114
115     public void setParentRoles(SortedSet<EPRole> parentRoles) {
116         this.parentRoles = parentRoles;
117     }
118
119     public void addRoleFunction(RoleFunction roleFunction) {
120         this.roleFunctions.add(roleFunction);
121     }
122
123     public void addChildRole(EPRole role) {
124         this.childRoles.add(role);
125     }
126
127     public void addParentRole(EPRole role) {
128         this.parentRoles.add(role);
129     }
130
131     public String getEditUrl() {
132         return "/role.htm?role_id=" + getId();          
133     }
134     
135         public String getToggleActiveImage() {
136                 return "/static/fusion/images/" + (getActive() ? "active.png" : "inactive.png" );
137         }
138
139         public String getToggleActiveAltText() {
140                 return getActive() ? "Click to Deactivate Role" : "Click to Activate Role";
141         }
142     
143     public void removeChildRole(Long roleId) {
144       Iterator<EPRole> i = this.childRoles.iterator();
145
146       while (i.hasNext()) {
147         EPRole childRole = (EPRole)i.next();
148         if (childRole.getId().equals(roleId)) {
149           this.childRoles.remove(childRole);
150           break;
151         }
152       }
153     }
154
155     public void removeParentRole(Long roleId) {
156         Iterator<EPRole> i = this.parentRoles.iterator();
157
158         while (i.hasNext()) {
159           EPRole parentRole = (EPRole)i.next();
160           if (parentRole.getId().equals(roleId)) {
161             this.parentRoles.remove(parentRole);
162             break;
163           }
164         }
165       }
166
167     public void removeRoleFunction(String roleFunctionCd) {
168       Iterator<RoleFunction> i = this.roleFunctions.iterator();
169
170       while (i.hasNext()) {
171         RoleFunction roleFunction = (RoleFunction)i.next();
172         if (roleFunction.getCode().equals(roleFunctionCd)) {
173           this.roleFunctions.remove(roleFunction);
174           break;
175         }
176       }
177     }
178
179     public int compareTo(Object obj){
180         EPRole other = (EPRole)obj;
181
182         if(this.appId == null)
183                 if(other.getAppId() == null)
184                         return compareByName(other); //equal
185                 else
186                         return -1; 
187         else // this.appId != null
188                 if(other.getAppId() == null)
189                         return 1;  // appId != null, but others is null
190                 else{
191                         int appIdCompareResult = appId.compareTo(other.getAppId());
192                         return appIdCompareResult == 0? compareByName(other) : appIdCompareResult;
193                 }
194     }
195
196         private int compareByName(EPRole other) {
197                 String c1 = getName();
198         String c2 = other.getName();
199
200         return (c1 == null || c2 == null) ? 1 : c1.compareTo(c2);
201         }
202
203         public Long getAppId() {
204                 return appId;
205         }
206
207         public void setAppId(Long appId) {
208                 this.appId = appId;
209         }
210
211         public Long getAppRoleId() {
212                 return appRoleId;
213         }
214
215         public void setAppRoleId(Long appRoleId) {
216                 this.appRoleId = appRoleId;
217         }
218         
219         @Override
220         public String toString() {
221                 return "[Id = " + id + ", name = " + name + "]";
222         }
223
224 }