1 package fr.tiogars.inception.settings.user.entities;
2
3 import java.time.Instant;
4
5 import org.hibernate.annotations.OnDelete;
6 import org.hibernate.annotations.OnDeleteAction;
7
8 import fr.tiogars.inception.domains.auth.user.entities.UserEntity;
9 import jakarta.persistence.Column;
10 import jakarta.persistence.Entity;
11 import jakarta.persistence.FetchType;
12 import jakarta.persistence.GeneratedValue;
13 import jakarta.persistence.GenerationType;
14 import jakarta.persistence.Id;
15 import jakarta.persistence.JoinColumn;
16 import jakarta.persistence.OneToOne;
17 import jakarta.persistence.Table;
18
19
20 @Entity
21 @Table(name = "user_settings")
22 public class UserSettingsEntity {
23
24 @Id
25 @GeneratedValue(strategy = GenerationType.IDENTITY)
26 private Long id;
27
28 @OneToOne(fetch = FetchType.LAZY)
29 @JoinColumn(name = "user_id", unique = true)
30 @OnDelete(action = OnDeleteAction.CASCADE)
31 private UserEntity user;
32
33 @Column(name = "role_code", unique = true, length = 64)
34 private String roleCode;
35
36 @Column(name = "view_mode", nullable = false, length = 16)
37 private String viewMode;
38
39 @Column(nullable = false, updatable = false)
40 private Instant createdDate;
41
42 @Column(nullable = false)
43 private Instant lastModifiedDate;
44
45 protected UserSettingsEntity() {
46
47 }
48
49 private UserSettingsEntity(UserEntity user, String roleCode, String viewMode, Instant createdDate,
50 Instant lastModifiedDate) {
51 this.user = user;
52 this.roleCode = roleCode;
53 this.viewMode = viewMode;
54 this.createdDate = createdDate;
55 this.lastModifiedDate = lastModifiedDate;
56 }
57
58 public static UserSettingsEntity forUser(UserEntity user, String viewMode, Instant timestamp) {
59 return new UserSettingsEntity(user, null, viewMode, timestamp, timestamp);
60 }
61
62 public static UserSettingsEntity forRole(String roleCode, String viewMode, Instant timestamp) {
63 return new UserSettingsEntity(null, roleCode, viewMode, timestamp, timestamp);
64 }
65
66 public void update(String viewMode, Instant lastModifiedDate) {
67 this.viewMode = viewMode;
68 this.lastModifiedDate = lastModifiedDate;
69 }
70
71 public Long getId() {
72 return id;
73 }
74
75 public UserEntity getUser() {
76 return user;
77 }
78
79 public String getRoleCode() {
80 return roleCode;
81 }
82
83 public String getViewMode() {
84 return viewMode;
85 }
86
87 public Instant getCreatedDate() {
88 return createdDate;
89 }
90
91 public Instant getLastModifiedDate() {
92 return lastModifiedDate;
93 }
94 }