backend/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java

230 lines
7.1 KiB
Java
Raw Normal View History

2020-07-25 10:38:19 +02:00
package com.plannaplan.services;
2021-01-21 16:08:53 +01:00
import java.util.Iterator;
2020-10-19 12:13:02 +02:00
import java.util.List;
2020-09-25 17:01:38 +02:00
import java.util.Optional;
import java.util.UUID;
2021-01-02 14:35:00 +01:00
import java.util.stream.Collectors;
2021-01-21 16:08:53 +01:00
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.models.UserApiResponse;
2021-01-21 15:41:56 +01:00
import com.plannaplan.repositories.CommisionRepository;
2020-07-25 10:38:19 +02:00
import com.plannaplan.repositories.UserRepository;
2020-10-19 12:13:02 +02:00
import com.plannaplan.types.UserRoles;
2020-07-25 10:38:19 +02:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
2020-11-20 13:20:44 +01:00
/**
2020-12-07 21:46:47 +01:00
* Service of UserService which can get(By Email), login, save user.
2020-11-20 13:20:44 +01:00
*/
2020-07-25 10:38:19 +02:00
@Service
public class UserService {
2020-07-25 10:38:19 +02:00
@Autowired
2020-07-25 10:56:11 +02:00
private UserRepository repo;
2020-07-25 10:38:19 +02:00
@Autowired
private UsosApiService service;
2021-01-21 15:41:56 +01:00
@Autowired
private CommisionRepository comRepo;
2021-01-21 16:08:53 +01:00
@Autowired
private CommisionService comService;
2020-07-27 16:44:04 +02:00
public UserService() {
super();
2020-07-25 10:38:19 +02:00
}
/**
* checks if user exist and return him or creates new one with student role
* otherwise
*
* @param email user email in usos
* @param usosId user id in usos
* @return user entity instace containing changes saved in database
*/
public User checkForUser(String email, String usosId) {
return this.checkForUser(email, usosId, UserRoles.STUDENT);
}
/**
* checks if user exist and creates new one if doesn't
*
* @param email user email in usos
* @param usosId user id in usos
* @param roleIfNotExist role to be set in case user is not in database yet
* @return user entity instace containing changes saved in database
*/
public User checkForUser(String email, String usosId, UserRoles roleIfNotExist) {
if (usosId == null) {
Optional<User> user = this.repo.getByEmail(email.replace("\n", "").trim());
2020-12-07 21:46:47 +01:00
if (user.isPresent()) {
return user.get();
2020-12-07 21:46:47 +01:00
} else {
final User newUser = new User(null, null, email.replace("\n", "").trim(), roleIfNotExist);
return this.repo.save(newUser);
}
2020-12-07 21:46:47 +01:00
} else {
Optional<User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
if (user.isPresent()) {
return user.get();
2020-12-07 21:46:47 +01:00
} else {
final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, roleIfNotExist);
return this.repo.save(newUser);
}
}
}
/**
* generates token for user and if user don't have name in database it will
* attemp to obtain these from usos api and saves changes in database
*
* @param authority user we want to login
* @return user with changed values after save in db
* @throws UserNotFoundException throwed if user doesn't exist
*/
2020-12-07 21:46:47 +01:00
public User login(User authority) throws UserNotFoundException {
2020-10-19 12:13:02 +02:00
final String token = UUID.randomUUID().toString();
if ((authority.getName() == null || authority.getSurname() == null) && authority.getUsosId() != null) {
final UserApiResponse resp = this.service.getUserData(authority.getUsosId());
authority.updateWithUsosData(resp);
}
2020-12-07 21:46:47 +01:00
try {
authority.setToken(token);
this.repo.save(authority);
2020-12-07 21:46:47 +01:00
} catch (Exception e) {
throw new UserNotFoundException(e.getMessage());
}
2020-12-07 21:46:47 +01:00
return authority;
}
/**
* sacves user to databse and return instatnce with id
*
* @param user to be saved
* @return instatnce with bd id
*/
2020-11-17 19:36:56 +01:00
public User save(User user) {
return this.repo.save(user);
}
/**
*
* @param email of user to be find
* @return user with given mail
* @throws UserNotFoundException throwed if user doesn't exist
*/
public User getUserByEmail(String email) throws UserNotFoundException {
return this.repo.getByEmail(email.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
}
/**
* return user by given authority
*
* @param authority user usosId or email
* @return optional with user if found
*/
public Optional<User> getByAuthority(String authority) {
return this.repo.getByAuthority(authority);
}
2020-09-25 17:01:38 +02:00
public Optional<User> getByToken(String token) {
2020-09-14 14:02:05 +02:00
return this.repo.getByToken(token);
}
/**
* search for user with given query
*
* @param query string that will be matched to users name and surname
* @return list opf results
*/
2020-10-19 12:13:02 +02:00
public List<User> searchForStudents(String query) {
return this.repo.searchForUsers(query, UserRoles.STUDENT);
}
2020-10-29 16:25:55 +01:00
public Optional<User> getById(Long userId) {
return this.repo.findById(userId);
}
2020-12-18 15:24:01 +01:00
public List<User> getAllStudents() {
return this.repo.getAllByRole(UserRoles.STUDENT);
}
2020-12-23 11:51:17 +01:00
public Optional<User> getUserByRefreshToken(String refreshToken) {
return this.repo.getByRefreshToken(refreshToken);
}
public boolean adminExists() {
return this.repo.getAllByRole(UserRoles.ADMIN).size() > 0;
}
2021-01-03 17:57:32 +01:00
public void saveAll(List<User> users) {
this.repo.saveAll(users);
}
2021-01-02 14:35:00 +01:00
/**
* get students sorted by their ranking
*
2021-01-02 14:35:00 +01:00
* @return list of students
*/
public List<User> getStudentsSortedByRanking() {
return this.repo.getAllByRole(UserRoles.STUDENT).stream().sorted((u1, u2) -> {
return -1 * u1.getRanking().compareTo(u2.getRanking());
}).collect(Collectors.toList());
}
2021-01-21 15:41:56 +01:00
/**
* @return ammount of how many users created an assignment
*/
public int getAmmountOfUsersWithAssignedGroups() {
int response = 0;
final Object dbResponse = this.comRepo.getUsersAssigned()[0];
if (dbResponse != null) {
response = ((Long) dbResponse).intValue();
}
return response;
}
2021-01-21 15:50:20 +01:00
/**
* @return ammount of how many users haven't created an assignment yet
*/
public Integer getAmmountOfUsersWithNoAssignedGroups() {
return this.getAllStudents().size() - this.getAmmountOfUsersWithAssignedGroups();
}
2021-01-21 16:08:53 +01:00
/**
* @return ammount of how many users have full schedule accepted
*/
public Integer getAmmountOfUsersWithAcceptedSchedules() {
final List<User> students = this.getAllStudents();
Integer accepted = 0;
final Iterator<User> it = students.iterator();
while (it.hasNext()) {
final User user = it.next();
final Optional<Commision> com = this.comService.getNewestCommision(user);
if (com.isPresent() && user.getStudentRegisteredGrups().size() == com.get().getAssignments().size()) {
accepted += 1;
}
}
return accepted;
}
2021-01-21 16:15:01 +01:00
/**
* @return ammount of how many users have partly or none schedule accepted
*/
public Integer getAmmountOfUsersWithNoAcceptedSchedules() {
return this.getAllStudents().size() - this.getAmmountOfUsersWithAcceptedSchedules();
}
}