ed79cb2cb18f4ed50986f815edce24c1005d8c40
[portal.git] / portal-BE / src / main / java / org / onap / portal / domain / db / fn / FnUser.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.db.fn;
42
43 import java.io.Serializable;
44 import java.time.LocalDateTime;
45 import java.util.Collection;
46 import java.util.HashSet;
47 import java.util.Set;
48 import java.util.stream.Collectors;
49 import javax.persistence.CascadeType;
50 import javax.persistence.Column;
51 import javax.persistence.Entity;
52 import javax.persistence.FetchType;
53 import javax.persistence.GeneratedValue;
54 import javax.persistence.GenerationType;
55 import javax.persistence.Id;
56 import javax.persistence.Index;
57 import javax.persistence.JoinColumn;
58 import javax.persistence.ManyToMany;
59 import javax.persistence.ManyToOne;
60 import javax.persistence.NamedQueries;
61 import javax.persistence.NamedQuery;
62 import javax.persistence.OneToMany;
63 import javax.persistence.SequenceGenerator;
64 import javax.persistence.Table;
65 import javax.persistence.UniqueConstraint;
66 import javax.validation.constraints.Digits;
67 import javax.validation.constraints.Email;
68 import javax.validation.constraints.NotNull;
69 import javax.validation.constraints.PastOrPresent;
70 import javax.validation.constraints.Size;
71 import lombok.AllArgsConstructor;
72 import lombok.Builder;
73 import lombok.Getter;
74 import lombok.NoArgsConstructor;
75 import lombok.Setter;
76 import org.hibernate.annotations.DynamicUpdate;
77 import org.hibernate.validator.constraints.SafeHtml;
78 import org.onap.portal.domain.db.cr.CrReportFileHistory;
79 import org.onap.portal.domain.db.ep.EpPersUserWidgetPlacement;
80 import org.onap.portal.domain.db.ep.EpPersUserWidgetSel;
81 import org.onap.portal.domain.db.ep.EpUserNotification;
82 import org.onap.portal.domain.db.ep.EpUserRolesRequest;
83 import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter;
84 import org.onap.portal.domain.dto.DomainVo;
85 import org.springframework.security.core.GrantedAuthority;
86 import org.springframework.security.core.authority.SimpleGrantedAuthority;
87 import org.springframework.security.core.userdetails.UserDetails;
88
89 /*
90 CREATE TABLE `fn_user` (
91         `user_id` int(11) NOT NULL AUTO_INCREMENT,
92         `org_id` int(11) DEFAULT NULL,
93         `manager_id` int(11) DEFAULT NULL,
94         `first_name` varchar(50) DEFAULT NULL,
95         `middle_name` varchar(50) DEFAULT NULL,
96         `last_name` varchar(50) DEFAULT NULL,
97         `phone` varchar(25) DEFAULT NULL,
98         `fax` varchar(25) DEFAULT NULL,
99         `cellular` varchar(25) DEFAULT NULL,
100         `email` varchar(50) DEFAULT NULL,
101         `address_id` decimal(11,0) DEFAULT NULL,
102         `alert_method_cd` varchar(10) DEFAULT NULL,
103         `hrid` varchar(20) DEFAULT NULL,
104         `org_user_id` varchar(20) DEFAULT NULL,
105         `org_code` varchar(30) DEFAULT NULL,
106         `login_id` varchar(25) DEFAULT NULL,
107         `login_pwd` varchar(100) DEFAULT NULL,
108         `last_login_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
109         `active_yn` varchar(1) NOT NULL DEFAULT 'y',
110         `created_id` int(11) DEFAULT NULL,
111         `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
112         `modified_id` int(11) DEFAULT NULL,
113         `modified_date` timestamp NOT NULL DEFAULT current_timestamp(),
114         `is_internal_yn` char(1) NOT NULL DEFAULT 'n',
115         `address_line_1` varchar(100) DEFAULT NULL,
116         `address_line_2` varchar(100) DEFAULT NULL,
117         `city` varchar(50) DEFAULT NULL,
118         `state_cd` varchar(3) DEFAULT NULL,
119         `zip_code` varchar(11) DEFAULT NULL,
120         `country_cd` varchar(3) DEFAULT NULL,
121         `location_clli` varchar(8) DEFAULT NULL,
122         `org_manager_userid` varchar(20) DEFAULT NULL,
123         `company` varchar(100) DEFAULT NULL,
124         `department_name` varchar(100) DEFAULT NULL,
125         `job_title` varchar(100) DEFAULT NULL,
126         `timezone` int(11) DEFAULT NULL,
127         `department` varchar(25) DEFAULT NULL,
128         `business_unit` varchar(25) DEFAULT NULL,
129         `business_unit_name` varchar(100) DEFAULT NULL,
130         `cost_center` varchar(25) DEFAULT NULL,
131         `fin_loc_code` varchar(10) DEFAULT NULL,
132         `silo_status` varchar(10) DEFAULT NULL,
133         `language_id` int(2) NOT NULL DEFAULT 1,
134         PRIMARY KEY (`user_id`),
135         UNIQUE KEY `fn_user_hrid` (`hrid`) USING BTREE,
136         UNIQUE KEY `fn_user_login_id` (`login_id`) USING BTREE,
137         KEY `fn_user_address_id` (`address_id`) USING BTREE,
138         KEY `fn_user_alert_method_cd` (`alert_method_cd`) USING BTREE,
139         KEY `fn_user_org_id` (`org_id`) USING BTREE,
140         KEY `fk_fn_user_ref_197_fn_user` (`manager_id`),
141         KEY `fk_fn_user_ref_198_fn_user` (`created_id`),
142         KEY `fk_fn_user_ref_199_fn_user` (`modified_id`),
143         KEY `fk_timezone` (`timezone`),
144         CONSTRAINT `fk_fn_user_ref_110_fn_org` FOREIGN KEY (`org_id`) REFERENCES `fn_org` (`org_id`),
145         CONSTRAINT `fk_fn_user_ref_123_fn_lu_al` FOREIGN KEY (`alert_method_cd`) REFERENCES `fn_lu_alert_method` (`alert_method_cd`),
146         CONSTRAINT `fk_fn_user_ref_197_fn_user` FOREIGN KEY (`manager_id`) REFERENCES `fn_user` (`user_id`),
147         CONSTRAINT `fk_fn_user_ref_198_fn_user` FOREIGN KEY (`created_id`) REFERENCES `fn_user` (`user_id`),
148         CONSTRAINT `fk_fn_user_ref_199_fn_user` FOREIGN KEY (`modified_id`) REFERENCES `fn_user` (`user_id`),
149         CONSTRAINT `fk_timezone` FOREIGN KEY (`timezone`) REFERENCES `fn_lu_timezone` (`timezone_id`)
150         )
151 */
152
153 @NamedQueries({
154         @NamedQuery(
155                 name = "FnUser.getUserWithOrgUserId",
156                 query = "FROM FnUser WHERE orgUserId = :orgId"),
157         @NamedQuery(
158                 name = "FnUser.getActiveUsers",
159                 query = "FROM FnUser WHERE activeYn = 'Y'"),
160         @NamedQuery(
161                 name = "FnUser.getUsersByOrgIds",
162                 query = "FROM FnUser WHERE orgUserId IN :orgIds"
163         )
164 })
165 @Table(name = "fn_user", indexes = {
166         @Index(name = "fn_user_address_id", columnList = "address_id"),
167         @Index(name = "fn_user_alert_method_cd", columnList = "alert_method_cd"),
168         @Index(name = "fn_user_org_id", columnList = "org_id"),
169         @Index(name = "fk_fn_user_ref_197_fn_user", columnList = "manager_id"),
170         @Index(name = "fk_fn_user_ref_198_fn_user", columnList = "created_id"),
171         @Index(name = "fk_fn_user_ref_199_fn_user", columnList = "modified_id"),
172         @Index(name = "fk_timezone", columnList = "timezone")
173 },
174         uniqueConstraints = {
175                 @UniqueConstraint(name = "fn_user_hrid", columnNames = "hrid"),
176                 @UniqueConstraint(name = "fn_user_login_id", columnNames = "login_id")
177
178         })
179
180 @Getter
181 @Setter
182 @Entity
183 @Builder
184 @NoArgsConstructor
185 @AllArgsConstructor
186 @DynamicUpdate
187 public class FnUser extends DomainVo implements UserDetails, Serializable {
188
189        @Id
190        @GeneratedValue(strategy = GenerationType.IDENTITY)
191        @Column(name = "user_id", nullable = false)
192        private Long userId;
193        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
194        @JoinColumn(name = "org_id")
195        private FnOrg orgId;
196        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
197        @JoinColumn(name = "manager_id")
198        private FnUser managerId;
199        @Column(name = "first_name", length = 50)
200        @Size(max = 50)
201        @SafeHtml
202        private String firstName;
203        @Column(name = "middle_name", length = 50)
204        @Size(max = 50)
205        @SafeHtml
206        private String middleName;
207        @Column(name = "last_name", length = 50)
208        @Size(max = 50)
209        @SafeHtml
210        private String lastName;
211        @Column(name = "phone", length = 25)
212        @Size(max = 25)
213        @SafeHtml
214        private String phone;
215        @Column(name = "fax", length = 25)
216        @Size(max = 25)
217        @SafeHtml
218        private String fax;
219        @Column(name = "cellular", length = 25)
220        @Size(max = 25)
221        @SafeHtml
222        private String cellular;
223        @Column(name = "email", length = 50)
224        @Size(max = 50)
225        @Email
226        @SafeHtml
227        private String email;
228        @Column(name = "address_id")
229        @Digits(integer = 11, fraction = 0)
230        private Long addressId;
231        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
232        @JoinColumn(name = "alert_method_cd")
233        private FnLuAlertMethod alertMethodCd;
234        @Column(name = "hrid", length = 20)
235        @Size(max = 20)
236        @SafeHtml
237        private String hrid;
238        @Column(name = "org_user_id", length = 20)
239        @Size(max = 20)
240        @SafeHtml
241        private String orgUserId;
242        @Column(name = "org_code", length = 30)
243        @Size(max = 30)
244        @SafeHtml
245        private String org_code;
246        @Column(name = "login_id", length = 25)
247        @Size(max = 25)
248        @SafeHtml
249        private String loginId;
250        @Column(name = "login_pwd", length = 100)
251        @Size(max = 100)
252        @SafeHtml
253        private String loginPwd;
254        @Column(name = "last_login_date", nullable = false, columnDefinition = "datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()")
255        @PastOrPresent
256        protected LocalDateTime lastLoginDate;
257        @Column(name = "active_yn", nullable = false)
258        private Boolean activeYn;
259        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
260        @JoinColumn(name = "created_id")
261        private FnUser createdId;
262        @Column(name = "created_date", columnDefinition = "datetime DEFAULT current_timestamp()", nullable = false)
263        @PastOrPresent
264        protected LocalDateTime createdDate;
265        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
266        @JoinColumn(name = "modified_id")
267        private FnUser modifiedId;
268        @Column(name = "modified_date", nullable = false, columnDefinition = "datetime default now()")
269        @PastOrPresent
270        protected LocalDateTime modifiedDate;
271        @Column(name = "is_internal_yn", nullable = false, columnDefinition = "bit DEFAULT 0")
272        private Boolean isInternalYn;
273        @Column(name = "address_line_1", length = 100)
274        @Size(max = 100)
275        @SafeHtml
276        private String addressLine1;
277        @Column(name = "address_line_2", length = 100)
278        @Size(max = 100)
279        @SafeHtml
280        private String addressLine2;
281        @Column(name = "city", length = 50)
282        @Size(max = 50)
283        @SafeHtml
284        private String city;
285        @Column(name = "state_cd", length = 3)
286        @Size(max = 3)
287        @SafeHtml
288        private String stateCd;
289        @Column(name = "zip_code", length = 11)
290        @Size(max = 11)
291        @SafeHtml
292        private String zipCode;
293        @Column(name = "country_cd", length = 3)
294        @Size(max = 3)
295        @SafeHtml
296        private String countryCd;
297        @Column(name = "location_clli", length = 8)
298        @Size(max = 8)
299        @SafeHtml
300        private String locationClli;
301        @Column(name = "org_manager_userid", length = 20)
302        @Size(max = 20)
303        @SafeHtml
304        private String orgManagerUserId;
305        @Column(name = "company", length = 100)
306        @Size(max = 100)
307        @SafeHtml
308        private String company;
309        @Column(name = "department_name", length = 200)
310        @Size(max = 200)
311        @SafeHtml
312        private String departmentName;
313        @Column(name = "job_title", length = 100)
314        @Size(max = 100)
315        @SafeHtml
316        private String jobTitle;
317        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
318        @JoinColumn(name = "timezone")
319        private FnLuTimezone timezone;
320        @Column(name = "department", length = 25)
321        @Size(max = 25)
322        @SafeHtml
323        private String department;
324        @Column(name = "business_unit", length = 25)
325        @Size(max = 25)
326        @SafeHtml
327        private String businessUnit;
328        @Column(name = "business_unit_name", length = 100)
329        @Size(max = 100)
330        @SafeHtml
331        private String businessUnitName;
332        @Column(name = "cost_center", length = 25)
333        @Size(max = 25)
334        @SafeHtml
335        private String cost_center;
336        @Column(name = "fin_loc_code", length = 10)
337        @Size(max = 10)
338        @SafeHtml
339        private String finLocCode;
340        @Column(name = "silo_status", length = 10)
341        @Size(max = 10)
342        @SafeHtml
343        private String siloStatus;
344        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
345        @JoinColumn(name = "language_id", nullable = false, columnDefinition = "int(11) DEFAULT 1")
346        @NotNull(message = "languageId must not be null")
347        private FnLanguage languageId;
348        @Column(name = "is_guest", nullable = false, columnDefinition = "bit DEFAULT 0")
349        @NotNull(message = "guest must not be null")
350        private Boolean guest;
351        @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "fnUserList")
352        private Set<CrReportFileHistory> crReportFileHistorie;
353        @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
354        private Set<FnRole> fnRoles;
355        @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
356        private Set<FnMenuFunctional> fnRoleList;
357        @OneToMany(
358                targetEntity = FnAuditLog.class,
359                mappedBy = "userId",
360                cascade = CascadeType.ALL,
361                fetch = FetchType.LAZY
362        )
363        private Set<FnAuditLog> fnAuditLogs;
364        @OneToMany(
365                targetEntity = FnUser.class,
366                mappedBy = "createdId",
367                cascade = CascadeType.ALL,
368                fetch = FetchType.LAZY
369        )
370        private Set<FnUser> fnUsersCreatedId;
371        @OneToMany(
372                targetEntity = FnUser.class,
373                mappedBy = "managerId",
374                cascade = CascadeType.ALL,
375                fetch = FetchType.LAZY
376        )
377        private Set<FnUser> fnUsersManagerId;
378        @OneToMany(
379                targetEntity = FnUser.class,
380                mappedBy = "modifiedId",
381                cascade = CascadeType.ALL,
382                fetch = FetchType.LAZY
383        )
384        private Set<FnUser> fnUsersModifiedId;
385        @OneToMany(
386                targetEntity = EpUserRolesRequest.class,
387                mappedBy = "userId",
388                cascade = CascadeType.ALL,
389                fetch = FetchType.LAZY
390        )
391        private Set<EpUserRolesRequest> epUserRolesRequests;
392        @OneToMany(
393                targetEntity = FnPersUserAppSel.class,
394                mappedBy = "userId",
395                cascade = CascadeType.ALL,
396                fetch = FetchType.LAZY
397        )
398        private Set<FnPersUserAppSel> persUserAppSels;
399        @OneToMany(
400                targetEntity = EpWidgetCatalogParameter.class,
401                mappedBy = "userId",
402                cascade = CascadeType.ALL,
403                fetch = FetchType.LAZY
404        )
405        private Set<EpWidgetCatalogParameter> epWidgetCatalogParameters;
406        @OneToMany(
407                targetEntity = EpPersUserWidgetPlacement.class,
408                mappedBy = "userId",
409                cascade = CascadeType.ALL,
410                fetch = FetchType.LAZY
411        )
412        private Set<EpPersUserWidgetPlacement> epPersUserWidgetPlacements;
413        @OneToMany(
414                targetEntity = EpPersUserWidgetSel.class,
415                mappedBy = "userId",
416                cascade = CascadeType.ALL,
417                fetch = FetchType.LAZY
418        )
419        private Set<EpPersUserWidgetSel> epPersUserWidgetSels;
420        @OneToMany(
421                targetEntity = FnUserRole.class,
422                mappedBy = "userId",
423                cascade = CascadeType.ALL,
424                fetch = FetchType.LAZY
425        )
426        private Set<FnUserRole> fnUserRoles;
427        @OneToMany(
428                targetEntity = EpUserNotification.class,
429                mappedBy = "userId",
430                cascade = CascadeType.ALL,
431                fetch = FetchType.LAZY
432        )
433        private Set<EpUserNotification> epUserNotifications;
434
435        @Override
436        public Collection<? extends GrantedAuthority> getAuthorities() {
437               return fnRoles
438                       .stream()
439                       .map(fnRole -> new SimpleGrantedAuthority("ROLE_" + fnRole.getRoleName()))
440                       .collect(Collectors.toList());
441        }
442
443        @Override
444        public String getPassword() {
445               return this.getLoginPwd();
446        }
447
448        @Override
449        public String getUsername() {
450               return this.getLoginId();
451        }
452
453        @Override
454        public boolean isAccountNonExpired() {
455               return true;
456        }
457
458        @Override
459        public boolean isAccountNonLocked() {
460               return true;
461        }
462
463        @Override
464        public boolean isCredentialsNonExpired() {
465               return true;
466        }
467
468        @Override
469        public boolean isEnabled() {
470               return true;
471        }
472
473        public String getFullName() {
474               return this.firstName + " " + this.lastName;
475        }
476
477 }