Hibernate db fix
[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.UUID;
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.NamedNativeQueries;
60 import javax.persistence.NamedNativeQuery;
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.Getter;
72 import lombok.NoArgsConstructor;
73 import lombok.Setter;
74 import org.hibernate.annotations.DynamicUpdate;
75 import org.hibernate.annotations.GenericGenerator;
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 @NamedNativeQueries({
152         @NamedNativeQuery(
153                 name = "getUsersByOrgUserId",
154                 query = "SELECT * FROM FnUser WHERE where org_user_id in :orgIds"
155         )
156 })
157
158 @Table(name = "fn_user", indexes = {
159         @Index(name = "fn_user_address_id", columnList = "address_id"),
160         @Index(name = "fn_user_alert_method_cd", columnList = "alert_method_cd"),
161         @Index(name = "fn_user_org_id", columnList = "org_id"),
162         @Index(name = "fk_fn_user_ref_197_fn_user", columnList = "manager_id"),
163         @Index(name = "fk_fn_user_ref_198_fn_user", columnList = "created_id"),
164         @Index(name = "fk_fn_user_ref_199_fn_user", columnList = "modified_id"),
165         @Index(name = "fk_timezone", columnList = "timezone")
166 },
167         uniqueConstraints = {
168                 @UniqueConstraint(name = "fn_user_hrid", columnNames = "hrid"),
169                 @UniqueConstraint(name = "fn_user_login_id", columnNames = "login_id")
170
171         })
172
173 @Getter
174 @Setter
175 @Entity
176 @NoArgsConstructor
177 @AllArgsConstructor
178 @DynamicUpdate
179 @SequenceGenerator(name = "seq", initialValue = 1000, allocationSize = 100000)
180 public class FnUser implements UserDetails {
181
182        @Id
183        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
184        @Column(name = "user_id", length = 11, nullable = false)
185        @Digits(integer = 11, fraction = 0)
186        private Long userId;
187        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
188        @JoinColumn(name = "org_id", columnDefinition = "int(11) DEFAULT NULL")
189        private FnOrg orgId;
190        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
191        @JoinColumn(name = "manager_id")
192        private FnUser managerId;
193        @Column(name = "first_name", length = 50, columnDefinition = "varchar(50) DEFAULT NULL")
194        @Size(max = 50)
195        @SafeHtml
196        private String firstName;
197        @Column(name = "middle_name", length = 50, columnDefinition = "varchar(50) DEFAULT NULL")
198        @Size(max = 50)
199        @SafeHtml
200        private String middleName;
201        @Column(name = "last_name", length = 50, columnDefinition = "varchar(50) DEFAULT NULL")
202        @Size(max = 50)
203        @SafeHtml
204        private String lastName;
205        @Column(name = "phone", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
206        @Size(max = 25)
207        @SafeHtml
208        private String phone;
209        @Column(name = "fax", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
210        @Size(max = 25)
211        @SafeHtml
212        private String fax;
213        @Column(name = "cellular", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
214        @Size(max = 25)
215        @SafeHtml
216        private String cellular;
217        @Column(name = "email", length = 50, columnDefinition = "varchar(50) DEFAULT NULL")
218        @Size(max = 50)
219        @Email
220        @SafeHtml
221        private String email;
222        @Column(name = "address_id", columnDefinition = "decimal(11,0) DEFAULT NULL")
223        @Digits(integer = 11, fraction = 0)
224        private Long addressId;
225        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
226        @JoinColumn(name = "alert_method_cd", columnDefinition = "varchar(10) DEFAULT NULL")
227        private FnLuAlertMethod alertMethodCd;
228        @Column(name = "hrid", length = 20, columnDefinition = "varchar(20) DEFAULT NULL")
229        @Size(max = 20)
230        @SafeHtml
231        private String hrid;
232        @Column(name = "org_user_id", length = 20, columnDefinition = "varchar(20) DEFAULT NULL")
233        @Size(max = 20)
234        @SafeHtml
235        private String orgUserId;
236        @Column(name = "org_code", length = 30, columnDefinition = "varchar(30) DEFAULT NULL")
237        @Size(max = 30)
238        @SafeHtml
239        private String org_code;
240        @Column(name = "login_id", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
241        @Size(max = 25)
242        @SafeHtml
243        private String loginId;
244        @Column(name = "login_pwd", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
245        @Size(max = 100)
246        @SafeHtml
247        private String loginPwd;
248        @Column(name = "last_login_date", nullable = false, columnDefinition = "datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()")
249        @PastOrPresent
250        protected LocalDateTime lastLoginDate;
251        @Column(name = "active_yn", length = 1, columnDefinition = "character varying(1) default 'y'", nullable = false)
252        @Size(max = 1)
253        @SafeHtml
254        @NotNull(message = "activeYn must not be null")
255        private String activeYn;
256        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
257        @JoinColumn(name = "created_id")
258        private FnUser createdId;
259        @Column(name = "created_date", columnDefinition = "datetime DEFAULT current_timestamp()", nullable = false)
260        @PastOrPresent
261        protected LocalDateTime createdDate;
262        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
263        @JoinColumn(name = "modified_id")
264        private FnUser modifiedId;
265        @Column(name = "modified_date", nullable = false, columnDefinition = "datetime default now()")
266        @PastOrPresent
267        protected LocalDateTime modifiedDate;
268        @Column(name = "is_internal_yn", length = 1, columnDefinition = "character varying(1) default 'n'", nullable = false)
269        @Size(max = 1)
270        @SafeHtml
271        @NotNull(message = "isInternalYn must not be null")
272        private String isInternalYn;
273        @Column(name = "address_line_1", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
274        @Size(max = 100)
275        @SafeHtml
276        private String addressLine1;
277        @Column(name = "address_line_2", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
278        @Size(max = 100)
279        @SafeHtml
280        private String addressLine2;
281        @Column(name = "city", length = 50, columnDefinition = "varchar(50) DEFAULT NULL")
282        @Size(max = 50)
283        @SafeHtml
284        private String city;
285        @Column(name = "state_cd", length = 3, columnDefinition = "varchar(3) DEFAULT NULL")
286        @Size(max = 3)
287        @SafeHtml
288        private String stateCd;
289        @Column(name = "zip_code", length = 11, columnDefinition = "varchar(11) DEFAULT NULL")
290        @Size(max = 11)
291        @SafeHtml
292        private String zipCode;
293        @Column(name = "country_cd", length = 3, columnDefinition = "varchar(3) DEFAULT NULL")
294        @Size(max = 3)
295        @SafeHtml
296        private String countryCd;
297        @Column(name = "location_clli", length = 8, columnDefinition = "varchar(8) DEFAULT NULL")
298        @Size(max = 8)
299        @SafeHtml
300        private String locationClli;
301        @Column(name = "org_manager_userid", length = 20, columnDefinition = "varchar(20) DEFAULT NULL")
302        @Size(max = 20)
303        @SafeHtml
304        private String orgManagerUserId;
305        @Column(name = "company", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
306        @Size(max = 100)
307        @SafeHtml
308        private String company;
309        @Column(name = "department_name", length = 200, columnDefinition = "varchar(100) DEFAULT NULL")
310        @Size(max = 200)
311        @SafeHtml
312        private String departmentName;
313        @Column(name = "job_title", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
314        @Size(max = 100)
315        @SafeHtml
316        private String jobTitle;
317        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
318        @JoinColumn(name = "timezone", columnDefinition = "int(11) DEFAULT NULL")
319        private FnLuTimezone timezone;
320        @Column(name = "department", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
321        @Size(max = 25)
322        @SafeHtml
323        private String department;
324        @Column(name = "business_unit", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
325        @Size(max = 25)
326        @SafeHtml
327        private String businessUnit;
328        @Column(name = "business_unit_name", length = 100, columnDefinition = "varchar(100) DEFAULT NULL")
329        @Size(max = 100)
330        @SafeHtml
331        private String businessUnitName;
332        @Column(name = "cost_center", length = 25, columnDefinition = "varchar(25) DEFAULT NULL")
333        @Size(max = 25)
334        @SafeHtml
335        private String cost_center;
336        @Column(name = "fin_loc_code", length = 10, columnDefinition = "varchar(10) DEFAULT NULL")
337        @Size(max = 10)
338        @SafeHtml
339        private String finLocCode;
340        @Column(name = "silo_status", length = 10, columnDefinition = "varchar(10) DEFAULT NULL")
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", columnDefinition = "boolean default 0", nullable = false)
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.LAZY)
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 }