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

118 lines
3.4 KiB
Java
Raw Normal View History

2020-07-25 10:38:19 +02:00
package com.plannaplan.services;
2021-01-03 17:10:36 +01:00
import java.util.ArrayList;
2020-09-30 19:15:32 +02:00
import java.util.List;
import java.util.Optional;
2020-09-30 19:15:32 +02:00
import com.plannaplan.entities.Assignment;
2020-09-30 19:15:32 +02:00
import com.plannaplan.entities.Commision;
2021-01-03 16:21:06 +01:00
import com.plannaplan.entities.Groups;
2021-01-02 14:35:00 +01:00
import com.plannaplan.entities.User;
2021-01-03 17:10:36 +01:00
import com.plannaplan.models.EmailAcceptedData;
2020-07-25 10:38:19 +02:00
import com.plannaplan.repositories.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
2020-11-20 13:20:44 +01:00
/**
2021-01-02 14:35:00 +01:00
* Service of Assignment which can save assignments, diplay assignments, get
* ammount of assigments.
2020-11-20 13:20:44 +01:00
*/
2020-07-25 10:38:19 +02:00
@Service
2020-09-30 19:15:32 +02:00
public class AssignmentService {
2020-07-25 10:38:19 +02:00
@Autowired
2020-07-25 10:56:11 +02:00
private AssignmentRepository repo;
2020-07-25 10:38:19 +02:00
2021-01-03 16:21:06 +01:00
@Autowired
private CommisionService service;
2021-01-02 14:35:00 +01:00
@Autowired
private UserService userService;
2021-01-03 17:10:36 +01:00
@Autowired
private EmailService emailService;
2020-07-27 16:44:04 +02:00
public AssignmentService() {
2020-07-25 10:38:19 +02:00
}
/**
* Save given assignment
2021-01-02 14:35:00 +01:00
*
* @param assignment assignment to save
* @return assignment saved assignment with database id
*/
public Assignment save(Assignment assignment) {
return this.repo.save(assignment);
}
2020-09-30 19:15:32 +02:00
2021-01-05 11:44:54 +01:00
/**
* gets list of assignments of given commision
*
* @param com Commision to get assinments from
* @return list of assignments
*/
2020-09-30 19:15:32 +02:00
public List<Assignment> getCommisionAssignments(Commision com) {
return this.repo.getByCommision(com.getId());
}
2020-10-02 17:18:03 +02:00
2021-01-05 11:44:54 +01:00
/**
* get ammount of all assignments (not only for selected commision)
*
* @return long - ammount of assingments
*/
2020-10-02 17:18:03 +02:00
public long getAssignmentsAmmount() {
return this.repo.count();
}
/**
* Get assigmnent by id
2021-01-02 14:35:00 +01:00
*
* @param id id of assigmnent
* @return Optional of assignment
*/
public Optional<Assignment> getById(Long id) {
return this.repo.findById(id);
}
2021-01-02 14:35:00 +01:00
2021-01-03 17:10:36 +01:00
/**
* this method will activate accept algorythm for all students. Algorythm is
* takeing each student in order defined by ranking and accept for him groups
* that are joinable for him
*/
2021-01-02 14:35:00 +01:00
public void callAcceptAlgorythm() {
final List<User> students = this.userService.getStudentsSortedByRanking();
students.forEach(e -> {
2021-01-03 16:21:06 +01:00
final Optional<Commision> com = this.service.getNewestCommision(e);
2021-01-03 17:10:36 +01:00
final List<Groups> accepted = new ArrayList<>();
final List<Groups> removed = new ArrayList<>();
2021-01-03 16:21:06 +01:00
if (com.isPresent()) {
final List<Assignment> assignments = this.getCommisionAssignments(com.get());
assignments.forEach(a -> {
if (a.isAccepted() == false) {
final Groups group = a.getGroup();
if (group.getCapacity() > group.getRegisteredStudents().size()) {
e.claimGroup(group);
accepted.add(group);
} else {
removed.add(group);
}
2021-01-03 16:21:06 +01:00
}
});
}
2021-01-03 17:57:32 +01:00
this.userService.save(e);
2021-01-03 17:10:36 +01:00
this.emailService.sendAcceptationResult(e, new EmailAcceptedData(accepted, removed));
2021-01-02 14:35:00 +01:00
});
}
2021-01-19 09:32:57 +01:00
/**
* @param toSave list of entites to save to db
* @return list of assignments entities with ids from db
*/
public List<Assignment> saveAll(List<Assignment> toSave) {
return this.repo.saveAll(toSave);
}
}