Domain model change
[portal.git] / portal-BE / src / main / java / org / onap / portal / domain / dto / ecomp / EPRole.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.domain.dto.ecomp;
42
43 import com.fasterxml.jackson.annotation.JsonIgnore;
44 import java.util.SortedSet;
45 import java.util.TreeSet;
46 import javax.validation.Valid;
47 import lombok.AllArgsConstructor;
48 import lombok.Getter;
49 import lombok.NoArgsConstructor;
50 import lombok.Setter;
51 import org.hibernate.validator.constraints.SafeHtml;
52 import org.onap.portalsdk.core.domain.RoleFunction;
53 import org.onap.portal.domain.db.DomainVo;
54
55 @Getter
56 @Setter
57 @NoArgsConstructor
58 @AllArgsConstructor
59 public class EPRole extends DomainVo {
60
61        private static final long serialVersionUID = 1L;
62        @SafeHtml
63        private String name;
64        private boolean active;
65        private Integer priority;
66        private Long appId;     // used by ONAP only
67        private Long appRoleId; // used by ONAP only
68        private SortedSet<RoleFunction> roleFunctions = new TreeSet<>();
69        @Valid
70        private SortedSet<EPRole> childRoles = new TreeSet<>();
71        @JsonIgnore
72        private SortedSet<EPRole> parentRoles = new TreeSet<>();
73
74
75        public void addRoleFunction(RoleFunction roleFunction) {
76               this.roleFunctions.add(roleFunction);
77        }
78
79        public void addChildRole(EPRole role) {
80               this.childRoles.add(role);
81        }
82
83        public void addParentRole(EPRole role) {
84               this.parentRoles.add(role);
85        }
86
87        public String getEditUrl() {
88               return "/role.htm?role_id=" + getId();
89        }
90
91        public String getToggleActiveImage() {
92               return "/static/fusion/images/" + (isActive() ? "active.png" : "inactive.png");
93        }
94
95        public String getToggleActiveAltText() {
96               return isActive() ? "Click to Deactivate Role" : "Click to Activate Role";
97        }
98
99        public void removeChildRole(Long roleId) {
100
101               for (EPRole childRole : this.childRoles) {
102                      if (childRole.getId().equals(roleId)) {
103                             this.childRoles.remove(childRole);
104                             break;
105                      }
106               }
107        }
108
109        public void removeParentRole(Long roleId) {
110
111               for (EPRole parentRole : this.parentRoles) {
112                      if (parentRole.getId().equals(roleId)) {
113                             this.parentRoles.remove(parentRole);
114                             break;
115                      }
116               }
117        }
118
119        public void removeRoleFunction(String roleFunctionCd) {
120
121               for (RoleFunction roleFunction : this.roleFunctions) {
122                      if (roleFunction.getCode().equals(roleFunctionCd)) {
123                             this.roleFunctions.remove(roleFunction);
124                             break;
125                      }
126               }
127        }
128
129        public int compareTo(Object obj) {
130               EPRole other = (EPRole) obj;
131
132               if (this.appId == null) {
133                      if (other.getAppId() == null) {
134                             return compareByName(other); //equal
135                      } else {
136                             return -1;
137                      }
138               } else if (other.getAppId() == null) {
139                      return 1;
140               } else {
141                      int appIdCompareResult = appId.compareTo(other.getAppId());
142                      return appIdCompareResult == 0 ? compareByName(other) : appIdCompareResult;
143               }
144        }
145
146        private int compareByName(EPRole other) {
147               String c1 = getName();
148               String c2 = other.getName();
149
150               return (c1 == null || c2 == null) ? 1 : c1.compareTo(c2);
151        }
152
153        @Override
154        public String toString() {
155               return "[Id = " + super.getId() + ", name = " + name + "]";
156        }
157
158 }