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