package SuMoC.backend.user.models; import SuMoC.backend.gamification.badges.models.UserBadge; import SuMoC.backend.notification.models.NotificationToken; import SuMoC.backend.routing.models.RoutingPreferences; import SuMoC.backend.trip.models.Trip; import SuMoC.backend.user.enums.RoleName; import SuMoC.backend.user.enums.VisibilityPrivacy; import SuMoC.backend.user.stats.StatsList; import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; import lombok.Setter; import java.util.HashSet; import java.util.Set; import java.util.UUID; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; /** * The public name of the user, displayed in public leaderboards etc. */ @Column(unique = true, nullable = false, length = 50) private String publicName; /** * The private name of the user, used in private leaderboards, shown to friends etc. */ @Column(nullable = false) private String privateName; @Column(unique = true, nullable = false) private String email; @Column(nullable = false) private String password; @Enumerated(EnumType.STRING) @Column(nullable = false) private VisibilityPrivacy visibilityPrivacy; @Column(nullable = false, unique = true, length = 24) private String friendCode; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) @JsonManagedReference("user-trips") private Set trips = new HashSet<>(); @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private Set friendships = new HashSet<>(); @Setter @ElementCollection(targetClass = RoleName.class, fetch = FetchType.EAGER) @Enumerated(EnumType.STRING) @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id")) @Column(name = "role") private Set roles = new HashSet<>(); @OneToOne(cascade = CascadeType.REMOVE, orphanRemoval = true) @JoinColumn(name = "id", referencedColumnName = "user_id") private NotificationToken notificationToken; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set badges = new HashSet<>(); @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private StatsList statsList; @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "routing_preferences_id", unique = true) private RoutingPreferences routingPreferences; @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private ProfileImageEntity profileImage; public User() { } // public User(Long id, String publicName, String privateName, String email, String password, VisibilityPrivacy visibilityPrivacy, String friendCode, Set trips, Set friendships, Set roles) { // this.id = id; // this.publicName = publicName; // this.privateName = privateName; // this.email = email; // this.password = password; // this.visibilityPrivacy = visibilityPrivacy; // this.friendCode = friendCode; // this.trips = trips; // this.friendships = friendships; // this.roles = roles; // } public User(String publicName, String privateName, String email, String password) { this.publicName = publicName; this.privateName = privateName; this.email = email; this.password = password; this.visibilityPrivacy = VisibilityPrivacy.NONE; this.friendCode = generateFriendCode(); // generate unique friend code this.routingPreferences = RoutingPreferences.getDefaultPreferences(); // Init with default routing preferences this.roles.add(RoleName.USER); this.statsList = new StatsList(this); this.profileImage = new ProfileImageEntity(); this.profileImage.setUser(this); } @PrePersist private void ensureFriendCode() { if (this.friendCode == null) { this.friendCode = generateFriendCode(); } } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPublicName() { return publicName; } public void setPublicName(String publicName) { this.publicName = publicName; } public String getPrivateName() { return privateName; } public void setPrivateName(String privateName) { this.privateName = privateName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public VisibilityPrivacy getVisibilityPrivacy() { return visibilityPrivacy; } public void setVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) { this.visibilityPrivacy = visibilityPrivacy; } public String getFriendCode() { return friendCode; } public void setFriendCode(String friendCode) { this.friendCode = friendCode; } public Set getTrips() { return trips; } public void setTrips(Set trips) { this.trips = trips; } public Set getFriendships() { return friendships; } public void setFriendships(Set friendships) { this.friendships = friendships; } public Set getRoles() { return roles; } public Set getBadges() { return badges; } public void setBadges(Set badges) { this.badges = badges; } public StatsList getStats() { return statsList; } public void setStats(StatsList statsList) { this.statsList = statsList; } public NotificationToken getNotificationToken() { return notificationToken; } public void setNotificationToken(NotificationToken notificationToken) { this.notificationToken = notificationToken; } public RoutingPreferences getRoutingPreferences() { return routingPreferences; } public void setRoutingPreferences(RoutingPreferences routingPreferences) { this.routingPreferences = routingPreferences; } public ProfileImageEntity getProfileImage() { return profileImage; } public void setProfileImage(ProfileImageEntity profileImage) { this.profileImage = profileImage; } // Friendcode generator private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private static final java.security.SecureRandom RANDOM = new java.security.SecureRandom(); private String generateFriendCode() { StringBuilder sb = new StringBuilder(); for (int group = 0; group < 5; group++) { for (int i = 0; i < 4; i++) { sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); } if (group < 4) { sb.append("-"); } } return sb.toString(); } }