package envoy.server.data; import java.time.LocalDateTime; import java.util.Set; import java.util.stream.Collectors; import javax.persistence.*; import envoy.data.User.UserStatus; /** * This class enables the storage of user specific data inside a database using * Hibernate. Its objects will be referred to as database users as opposed to * the common user objects present on both the client and the server.
*
* Project: envoy-server-standalone
* File: User.java
* Created: 02.01.2020
* * @author Kai S. K. Engelbart * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha */ @Entity @NamedQueries({ @NamedQuery( name = User.findByName, query = "SELECT u FROM User u WHERE u.name = :name" ), @NamedQuery( name = User.findContacts, query = "SELECT u.contacts FROM User u WHERE u = :user" ), @NamedQuery( name = User.searchByName, query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND :context NOT MEMBER OF u.contacts)" ) }) public class User extends Contact { /** * Named query retrieving a user by name (parameter {@code :name}). * * @since Envoy Server Standalone v0.1-beta */ public static final String findByName = "User.findByName"; /** * Named query retrieving the contacts of a given user (parameter * {@code :user}). * * @since Envoy Server Standalone v0.1-beta */ public static final String findContacts = "User.findContacts"; /** * Named query searching for users with a name like a search phrase (parameter * {@code :searchPhrase}) that are not in the contact list of a given user * (parameter {@code :context}). * * @since Envoy Server Standalone v0.1-beta */ public static final String searchByName = "User.searchByName"; @Column(name = "password_hash") private String passwordHash; @Column(name = "last_seen") private LocalDateTime lastSeen; private UserStatus status; @Override public envoy.data.User toCommon() { return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet())); } @Override protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); } /** * @return the password hash * @since Envoy Server Standalone v0.1-beta */ public String getPasswordHash() { return passwordHash; } /** * @param passwordHash the password hash to set * @since Envoy Server Standalone v0.1-beta */ public void setPasswordHash(String passwordHash) { this.passwordHash = passwordHash; } /** * @return the last date the user has been online * @since Envoy Server Standalone v0.1-alpha */ public LocalDateTime getLastSeen() { return lastSeen; } /** * @param lastSeen the latest date at which the user has been online to set * @since Envoy Server Standalone v0.1-alpha */ public void setLastSeen(LocalDateTime lastSeen) { this.lastSeen = lastSeen; } /** * @return the status * @since Envoy Server Standalone v0.1-alpha */ public UserStatus getStatus() { return status; } /** * @param status the status to set * @since Envoy Server Standalone v0.1-alpha */ public void setStatus(UserStatus status) { this.status = status; } }