Due to a bug in the JPA validator columns with camel case names are flagged as missing (probably due to the case-insensitive nature of SQL). This has been circumvented by assigning every column with a camel case name a new name with underscores. The inheritance strategy of the Contacts class has been changed to single table for performance reasons.
124 lines
3.3 KiB
Java
Executable File
124 lines
3.3 KiB
Java
Executable File
package envoy.server.data;
|
|
|
|
import java.util.Date;
|
|
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.<br>
|
|
* <br>
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
* File: <strong>User.java</strong><br>
|
|
* Created: <strong>02.01.2020</strong><br>
|
|
*
|
|
* @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 byte[] passwordHash;
|
|
|
|
@Column(name = "last_seen")
|
|
@Temporal(TemporalType.TIMESTAMP)
|
|
private Date lastSeen;
|
|
|
|
private UserStatus status;
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public envoy.data.User toCommon() {
|
|
return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet()));
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@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-alpha
|
|
*/
|
|
public byte[] getPasswordHash() { return passwordHash; }
|
|
|
|
/**
|
|
* @param passwordHash the password hash to set
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void setPasswordHash(byte[] passwordHash) { this.passwordHash = passwordHash; }
|
|
|
|
/**
|
|
* @return the last date the user has been online
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public Date 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(Date 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; }
|
|
}
|