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

175 lines
4.9 KiB
Java
Executable File

package com.plannaplan.services;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.function.Function;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
import com.plannaplan.repositories.GroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Service of GroupService which can find(optional), get(By Course, Groups
* Ammount, Group By Id, find Not Existing Group), save, delete group.
*/
@Service
public class GroupService {
@Autowired
private GroupRepository repo;
public GroupService() {
}
/**
* find group with given properties
*
* @param time scheduled time for group as int of minutes passed from 00:00
* @param capacity capacity of group
* @param room class room
* @return optional with Groups instance if found
*/
public Optional<Groups> find(int time, int capacity, String room) {
return this.repo.find(time, room, capacity);
}
/**
* find group with given properties
*
* @param zajCykId proteprty from usos
* @param nrGr group number
* @return optional with Groups instance if found
*/
public Optional<Groups> find(Integer zajCykId, Integer nrGr) {
return this.repo.find(zajCykId, nrGr);
}
/**
* find group with given properties
*
* @param id course id of groups belogns to
* @return list of found groups
*/
public List<Groups> getGroupsByCourse(Long id) {
return this.repo.getByCourse(id);
}
/**
* save group to database
*
* @param group insatnce to be saved
* @return new instance that has id form database
*/
public Groups save(Groups group) {
return this.repo.save(group);
}
/**
* delete from database
*
* @param groups isntance to delete
*/
public void delete(Groups groups) {
this.repo.delete(groups);
}
/**
* get hom manyh groups are in database in general
*
* @return int - groups ammount
*/
public int getGroupsAmmount() {
return (int) this.repo.count();
}
/**
* find group with given properties
*
* @param id group id
* @return optional with group if found
*/
public Optional<Groups> getGroupById(Long id) {
return this.repo.findById(id);
}
/**
* get wich of provided id is not existind groups
*
* @param ids list of ids to check
* @return optional with id that is not group if found. If there is multiple
* will be returned first found
*/
public Optional<Long> findNotExistingGroup(List<Long> ids) {
for (Long oneId : ids) {
if (this.repo.existsById(oneId) == false) {
return Optional.of(oneId);
}
}
return Optional.empty();
}
/**
*
* @param assignments list of assignments you want to get taken places ammount
* @return HashMap of Long to Integer where Long is group id and Integer is how
* many places in gorup is already taken
*/
public HashMap<Long, Integer> getTakenPlacesOfAssignments(List<Assignment> assignments) {
return getTakenPlaces(assignments.stream().map(Assignment::getGroup).collect(Collectors.toList()));
}
/**
*
* @param groups list of groups you want to get taken places ammount
* @return HashMap of Long to Integer where Long is group id and Integer is how
* many places in gorup is already taken
*/
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Long, Integer> response = new HashMap<>();
if (groups.size() == 0) {
return response;
}
List<Object[]> respoonses = this.repo
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
@Override
public Long apply(Groups p) {
final Long id = p.getId();
response.put(id, 0);
return id;
}
}).collect(Collectors.toList()));
for (Object[] element : respoonses) {
response.put(((Groups) element[0]).getId(), ((Long) element[1]).intValue());
}
return response;
}
/**
* @return amount of groups with full capacity taken
*/
public Integer getFullgroupsAmmount() {
Integer response = 0;
final Iterator<Groups> groups = this.repo.findAll().iterator();
while (groups.hasNext()) {
final Groups group = groups.next();
if (group.getCapacity() <= group.getRegisteredStudents().size()) {
response += 1;
}
}
return response;
}
}