backend/buisnesslogic/src/main/java/com/plannaplan/entities/User.java

284 lines
6.9 KiB
Java
Raw Normal View History

2020-07-25 10:38:19 +02:00
package com.plannaplan.entities;
2020-11-07 16:30:25 +01:00
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
2020-12-23 11:51:17 +01:00
import java.util.UUID;
2020-11-07 16:30:25 +01:00
import java.util.concurrent.TimeUnit;
import javax.persistence.CascadeType;
2020-07-25 10:38:19 +02:00
import javax.persistence.Entity;
2021-01-03 16:21:06 +01:00
import javax.persistence.FetchType;
2020-07-25 10:38:19 +02:00
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
2020-07-25 10:38:19 +02:00
import com.plannaplan.models.UserApiResponse;
2020-07-25 10:38:19 +02:00
import com.plannaplan.types.UserRoles;
2020-09-25 16:43:24 +02:00
/**
2020-12-09 16:57:49 +01:00
* Entity of User grouping of state ssociated about
* id,name,surname,email,role,token,tokenCreatedDate
*/
2020-07-25 10:38:19 +02:00
@Entity
public class User {
2020-11-07 16:30:25 +01:00
private static final float TOKEN_EXPIRE_MINUTES = 15;
2020-07-25 10:38:19 +02:00
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String surname;
private String email;
private String usosId;
2020-07-25 10:38:19 +02:00
private UserRoles role;
private String token;
2020-12-23 11:51:17 +01:00
private String refreshToken;
2020-11-07 16:30:25 +01:00
private Timestamp tokenUsageDate;
2021-01-02 13:45:03 +01:00
private Integer ranking;
2021-01-03 16:21:06 +01:00
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable
private Set<Groups> studentRegisteredGrups;
public Set<Groups> getStudentRegisteredGrups() {
return this.studentRegisteredGrups;
}
2021-01-12 14:10:48 +01:00
public void removeGroup(Long id) {
2021-01-19 12:55:58 +01:00
final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst()
.get();
this.studentRegisteredGrups.remove(groupToDelete);
2021-01-12 14:10:48 +01:00
}
public void claimGroup(Groups group) {
if (this.studentRegisteredGrups == null) {
this.studentRegisteredGrups = new HashSet<>();
}
this.studentRegisteredGrups.add(group);
}
2020-07-25 10:38:19 +02:00
public User() {
}
2020-12-09 16:57:49 +01:00
/**
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param name name given to the user
* @param surname surname given to the user
2020-12-09 16:57:49 +01:00
* @param mail mail given to the user
* @param role user's role
*/
2020-09-25 16:43:24 +02:00
public User(String name, String surname, String mail, UserRoles role) {
this.name = name;
this.surname = surname;
this.email = mail;
this.role = role;
}
2020-12-09 16:57:49 +01:00
/**
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param name name given to the user
* @param surname surname given to the user
2020-12-09 16:57:49 +01:00
* @param mail mail given to the user
* @param usosId id in the USOS system
* @param role user's role
*/
2020-12-09 16:57:49 +01:00
public User(String name, String surname, String mail, String usosId, UserRoles role) {
this(name, surname, mail, role);
this.usosId = usosId;
}
2021-01-02 13:45:03 +01:00
/**
2021-01-12 14:10:48 +01:00
*
2021-01-02 13:45:03 +01:00
* @param name name given to the user
* @param surname surname given to the user
* @param mail mail given to the user
* @param usosId id in the USOS system
* @param role user's role
2021-01-05 11:50:25 +01:00
* @param ranking ranking points [100;500]. It's calculated by
2021-01-02 13:45:03 +01:00
* gradesAvg*100*studiesYear
*/
public User(String name, String surname, String mail, String usosId, UserRoles role, Integer ranking) {
this(name, surname, mail, usosId, role);
this.ranking = ranking;
}
2020-12-09 16:57:49 +01:00
/**
* usos id getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return usosid
*/
public String getUsosId() {
return usosId;
}
2020-12-09 16:57:49 +01:00
/**
* email getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return mailof user
*/
public String getEmail() {
return email;
}
2020-12-09 16:57:49 +01:00
/**
* email setter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param email user email
*/
public void setEmail(String email) {
this.email = email;
}
2020-12-09 16:57:49 +01:00
/**
* token usage getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return Timestamp when token was used
*/
2020-11-07 16:30:25 +01:00
public Timestamp getTokenUsageDate() {
return tokenUsageDate;
}
2020-12-09 16:57:49 +01:00
/**
* token getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return user token
*/
public String getToken() {
return token;
}
2020-12-23 11:51:17 +01:00
/**
* @return token needed to call refresh token after it expired
*/
public String getRefreshToken() {
return refreshToken;
}
2020-12-09 16:57:49 +01:00
/**
* token seter. Sets token and automaticly set time when was set
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param token token to set
*/
public void setToken(String token) {
2020-11-07 16:30:25 +01:00
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
this.token = token;
2021-01-20 16:05:43 +01:00
this.refreshToken = this.refreshToken == null ? UUID.randomUUID().toString() : this.refreshToken;
}
2020-12-09 16:57:49 +01:00
/**
* name setter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return String user name
*/
2020-07-25 10:38:19 +02:00
public String getName() {
return name;
}
2020-12-09 16:57:49 +01:00
/**
* user rolse getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return UserRoles of user
*/
2020-07-25 10:38:19 +02:00
public UserRoles getRole() {
return role;
}
2020-12-09 16:57:49 +01:00
/**
* user role setter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param role to be set
*/
2020-07-25 10:38:19 +02:00
public void setRole(UserRoles role) {
this.role = role;
}
2020-12-09 16:57:49 +01:00
/**
* surname getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return string surname
*/
2020-07-25 10:38:19 +02:00
public String getSurname() {
return surname;
}
2020-12-09 16:57:49 +01:00
/**
* surname setter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param surname string to be set as surnames
*/
2020-07-25 10:38:19 +02:00
public void setSurname(String surname) {
this.surname = surname;
}
2020-12-09 16:57:49 +01:00
/**
* name stter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @param name stirng to be set as name
*/
2020-07-25 10:38:19 +02:00
public void setName(String name) {
this.name = name;
}
2020-12-09 16:57:49 +01:00
/**
* id getter
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return id in database
*/
2020-09-30 17:46:04 +02:00
public Long getId() {
return this.id;
}
2020-11-07 16:30:25 +01:00
2021-01-02 13:45:03 +01:00
/**
2021-01-05 11:50:25 +01:00
* Ranking points are [100;500]. It's calculated by gradesAvg*100*studiesYear
2021-01-12 14:10:48 +01:00
*
2021-01-05 11:50:25 +01:00
* @return ranking points [100;500]
2021-01-02 13:45:03 +01:00
*/
public Integer getRanking() {
2021-01-19 12:55:58 +01:00
if (ranking == null) {
return 100;
}
2021-01-02 13:45:03 +01:00
return ranking;
}
/**
2021-01-05 11:50:25 +01:00
* Ranking points setter. Ranking points are [100;500]. It's calculated by
2021-01-02 13:45:03 +01:00
* gradesAvg*100*studiesYear
2021-01-12 14:10:48 +01:00
*
2021-01-05 11:50:25 +01:00
* @param ranking ranking points [100;500]
2021-01-02 13:45:03 +01:00
*/
public void setRanking(Integer ranking) {
this.ranking = ranking;
}
/**
* updates user entity with data got by UsosApiService::getUserData
2021-01-12 14:10:48 +01:00
*
* @param usosData UserApiResponse model with needed data
*/
public void updateWithUsosData(UserApiResponse usosData) {
this.name = usosData.getName();
this.surname = usosData.getSurname();
}
2020-12-09 16:57:49 +01:00
/**
* it checks if given ammount of time passed since last token usage. If not
* retunr true and reset time otherwise return false and token won work anymore
2021-01-12 14:10:48 +01:00
*
2020-12-09 16:57:49 +01:00
* @return boolena if credentials (token) is expired or not
*/
2020-12-09 16:57:49 +01:00
public boolean isCredentialsNonExpired() {
2020-11-07 16:30:25 +01:00
final long diffInMilliseconds = Math
.abs(this.tokenUsageDate.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
if (minutes > TOKEN_EXPIRE_MINUTES) {
return false;
}
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
return true;
}
}