2020-01-02 17:50:56 +01:00
|
|
|
package envoy.server.data;
|
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
import java.time.Instant;
|
2020-04-09 21:01:19 +02:00
|
|
|
import java.util.Set;
|
2020-04-02 16:32:23 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-28 19:25:30 +01:00
|
|
|
import javax.persistence.*;
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-04-09 14:15:39 +02:00
|
|
|
import envoy.data.User.UserStatus;
|
|
|
|
|
2020-01-02 17:50:56 +01:00
|
|
|
/**
|
2020-04-09 14:15:39 +02:00
|
|
|
* 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>
|
2020-01-03 16:21:35 +01:00
|
|
|
* <br>
|
2020-01-02 17:50:56 +01:00
|
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
|
|
* File: <strong>User.java</strong><br>
|
|
|
|
* Created: <strong>02.01.2020</strong><br>
|
2020-01-03 16:21:35 +01:00
|
|
|
*
|
2020-01-02 17:50:56 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2020-02-10 20:02:05 +01:00
|
|
|
* @author Maximilian Käfer
|
2020-01-02 17:50:56 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
@Entity
|
2020-04-24 21:24:19 +02:00
|
|
|
@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"
|
2020-06-11 11:17:22 +02:00
|
|
|
),
|
|
|
|
@NamedQuery(
|
2020-04-24 21:24:19 +02:00
|
|
|
name = User.searchByName,
|
2020-06-11 11:17:22 +02:00
|
|
|
query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND :context NOT MEMBER OF u.contacts)"
|
2020-04-24 21:24:19 +02:00
|
|
|
)
|
|
|
|
})
|
2020-04-09 14:15:39 +02:00
|
|
|
public class User extends Contact {
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-04-24 21:24:19 +02:00
|
|
|
/**
|
|
|
|
* 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";
|
|
|
|
|
2020-06-15 11:59:48 +02:00
|
|
|
@Column(name = "password_hash")
|
2020-07-10 23:25:13 +02:00
|
|
|
private String passwordHash;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-06-15 11:59:48 +02:00
|
|
|
@Column(name = "last_seen")
|
2020-07-16 17:04:35 +02:00
|
|
|
private Instant lastSeen;
|
2020-04-09 14:15:39 +02:00
|
|
|
|
|
|
|
private UserStatus status;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-03-25 16:34:55 +01:00
|
|
|
@Override
|
2020-04-02 16:32:23 +02:00
|
|
|
public envoy.data.User toCommon() {
|
2020-04-09 21:01:19 +02:00
|
|
|
return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet()));
|
2020-04-02 16:32:23 +02:00
|
|
|
}
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-04-09 21:01:19 +02:00
|
|
|
@Override
|
|
|
|
protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); }
|
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
2020-04-09 14:15:39 +02:00
|
|
|
* @return the password hash
|
2020-07-10 23:25:13 +02:00
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
2020-01-03 16:21:35 +01:00
|
|
|
*/
|
2020-07-10 23:25:13 +02:00
|
|
|
public String getPasswordHash() { return passwordHash; }
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
|
|
|
* @param passwordHash the password hash to set
|
2020-07-10 23:25:13 +02:00
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
2020-01-03 16:21:35 +01:00
|
|
|
*/
|
2020-07-10 23:25:13 +02:00
|
|
|
public void setPasswordHash(String passwordHash) { this.passwordHash = passwordHash; }
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
2020-04-09 14:15:39 +02:00
|
|
|
* @return the last date the user has been online
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Server Standalone v0.2-beta
|
2020-01-03 16:21:35 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public Instant getLastSeen() { return lastSeen; }
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
2020-04-09 14:15:39 +02:00
|
|
|
* @param lastSeen the latest date at which the user has been online to set
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Server Standalone v0.2-beta
|
2020-01-03 16:21:35 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public void setLastSeen(Instant lastSeen) { this.lastSeen = lastSeen; }
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
2020-04-09 14:15:39 +02:00
|
|
|
* @return the status
|
2020-01-03 16:21:35 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-04-09 14:15:39 +02:00
|
|
|
public UserStatus getStatus() { return status; }
|
2020-01-02 17:50:56 +01:00
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
|
|
|
* @param status the status to set
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-04-09 14:15:39 +02:00
|
|
|
public void setStatus(UserStatus status) { this.status = status; }
|
2020-01-02 17:50:56 +01:00
|
|
|
}
|