Domain model change
[portal.git] / portal-BE / src / main / java / org / onap / portal / domain / dto / transport / Role.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.transport;
42
43 import com.fasterxml.jackson.annotation.JsonIgnore;
44 import java.io.Serializable;
45 import java.time.LocalDateTime;
46 import java.util.Set;
47 import javax.persistence.CascadeType;
48 import javax.persistence.Column;
49 import javax.persistence.Entity;
50 import javax.persistence.FetchType;
51 import javax.persistence.Index;
52 import javax.persistence.Inheritance;
53 import javax.persistence.InheritanceType;
54 import javax.persistence.OneToMany;
55 import javax.persistence.Table;
56 import javax.validation.constraints.Digits;
57 import javax.validation.constraints.NotNull;
58 import javax.validation.constraints.Size;
59 import lombok.AllArgsConstructor;
60 import lombok.Builder;
61 import lombok.Getter;
62 import lombok.NoArgsConstructor;
63 import lombok.Setter;
64 import org.hibernate.validator.constraints.SafeHtml;
65 import org.onap.portal.domain.db.fn.FnRoleComposite;
66 import org.onap.portal.domain.db.fn.FnRoleFunction;
67 import org.onap.portal.domain.db.DomainVo;
68 import org.onap.portalsdk.core.domain.RoleFunction;
69
70 @Table(name = "role", indexes = {
71     @Index(name = "fn_role_name_app_id_idx", columnList = "role_name, app_id", unique = true)
72 })
73 @Getter
74 @Setter
75 @Entity
76 @NoArgsConstructor
77 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
78 public class Role extends DomainVo {
79
80   private static final long serialVersionUID = 1L;
81
82   @Column(name = "role_name", length = 300, nullable = false)
83   @Size(max = 300)
84   @NotNull
85   @SafeHtml
86   private String roleName;
87   @Column(name = "app_Id", length = 11, columnDefinition = "int(11) default null")
88   @Digits(integer = 11, fraction = 0)
89   private Long appId;
90   @Column(name = "active_yn", length = 1, columnDefinition = "boolean default true", nullable = false)
91   @NotNull
92   private Boolean activeYn = true;
93   @Column(name = "priority", length = 4, columnDefinition = "decimal(4,0) DEFAULT NULL")
94   @Digits(integer = 4, fraction = 0)
95   private Integer priority;
96   @OneToMany(
97       targetEntity = FnRoleFunction.class,
98       mappedBy = "role",
99       cascade = CascadeType.MERGE,
100       fetch = FetchType.LAZY
101   )
102   private Set<FnRoleFunction> fnRoleFunctions;
103   @OneToMany(
104       targetEntity = FnRoleComposite.class,
105       mappedBy = "childRoles",
106       cascade = CascadeType.MERGE,
107       fetch = FetchType.LAZY
108   )
109   private Set<FnRoleComposite> childRoles;
110   @JsonIgnore
111   @OneToMany(
112       targetEntity = FnRoleComposite.class,
113       mappedBy = "parentRoles",
114       cascade = CascadeType.MERGE,
115       fetch = FetchType.LAZY
116   )
117   private Set<FnRoleComposite> parentRoles;
118
119   public String getEditUrl() {
120     return "/role.htm?role_id=" + this.getId();
121   }
122
123   public String getToggleActiveImage() {
124     return "/static/fusion/images/" + (this.activeYn ? "active.png" : "inactive.png");
125   }
126
127   public String getToggleActiveAltText() {
128     return this.activeYn ? "Click to Deactivate Role" : "Click to Activate Role";
129   }
130
131   public void removeChildRole(Long roleId) {
132
133     for (FnRoleComposite role : this.childRoles) {
134       Role childRole = role.getChildRoles();
135       if (childRole.getId().equals(roleId)) {
136         this.childRoles.remove(childRole);
137         break;
138       }
139     }
140
141   }
142
143   public void removeParentRole(Long roleId) {
144
145     for (Object role : this.parentRoles) {
146       org.onap.portalsdk.core.domain.Role parentRole = (org.onap.portalsdk.core.domain.Role) role;
147       if (parentRole.getId().equals(roleId)) {
148         this.parentRoles.remove(parentRole);
149         break;
150       }
151     }
152
153   }
154
155   public void removeRoleFunction(String roleFunctionCd) {
156
157     for (Object function : this.fnRoleFunctions) {
158       RoleFunction roleFunction = (RoleFunction) function;
159       if (roleFunction.getCode().equals(roleFunctionCd)) {
160         this.fnRoleFunctions.remove(roleFunction);
161         break;
162       }
163     }
164
165   }
166
167   public int compareTo(Object obj) {
168     String c1 = this.getRoleName();
169     String c2 = ((org.onap.portalsdk.core.domain.Role) obj).getName();
170     return c1 != null && c2 != null ? c1.compareTo(c2) : 1;
171   }
172
173   public Role( Long id, LocalDateTime created,
174       LocalDateTime modified, Long rowNum, Serializable auditUserId,
175       DomainVo createdId, DomainVo modifiedId, Set<DomainVo> fnUsersCreatedId,
176       Set<DomainVo> fnUsersModifiedId, String roleName, Long appId, Boolean activeYn, Integer priority,
177       Set<FnRoleFunction> fnRoleFunctions, Set<FnRoleComposite> childRoles,
178       Set<FnRoleComposite> parentRoles) {
179     super(id, created, modified, rowNum, auditUserId, createdId, modifiedId, fnUsersCreatedId, fnUsersModifiedId);
180     this.roleName = roleName;
181     this.appId = appId;
182     this.activeYn = activeYn;
183     this.priority = priority;
184     this.fnRoleFunctions = fnRoleFunctions;
185     this.childRoles = childRoles;
186     this.parentRoles = parentRoles;
187   }
188 }