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