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