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