Buisness logic docs updated

This commit is contained in:
Filip Izydorczyk 2021-01-15 15:54:17 +01:00
parent 8d007c259f
commit 21983fe4f7
15 changed files with 325 additions and 143 deletions

View File

@ -18,10 +18,23 @@ public class FileReader {
private InputStream fileInputStream;
/**
* @param fileInputStream stream of stadarized file contains courses and gropups
* to import. File needs to be .xlsx file that has
* fields: zaj_cyk_id, typ, sym, nazwa, gr_nr, Mc, dzien,
* godz_od, sala, tytul, nazwisko, imie. Order doesn't
* have impact on import. Any change name of given field
* can be performed in FileToDatabaseMigrator class
*/
public FileReader(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
/**
* read data rom file
*
* @return instance of class FileData
*/
public FileData read() {
FileData result = null;

View File

@ -16,7 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* FileToDatabaseMigrator is used for migrate data from file (it reads line by line) and push it into database
* FileToDatabaseMigrator is used for migrate data from file (it reads line by
* line) and push it into database
*/
@Component
public class FileToDatabaseMigrator {
@ -47,6 +48,11 @@ public class FileToDatabaseMigrator {
public FileToDatabaseMigrator() {
}
/**
* insert data to database
*
* @param data FileData imported from file
*/
public void migrate(FileData data) {
Iterator<Row> rows = data.getRows();
int courseNameIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_NAME_STRING);
@ -71,18 +77,26 @@ public class FileToDatabaseMigrator {
String courseName = row.getCell(courseNameIndex).toString().trim();
String symbol = row.getCell(symbolIndex).toString().trim();
String lecturerTitle = row.getCell(titleIndex) != null ? row.getCell(titleIndex).toString().trim() : "";
String lecturerName = row.getCell(nameIndex) != null ? row.getCell(nameIndex).toString().trim() : "";
String lecturerSurname = row.getCell(surnameIndex) != null ? row.getCell(surnameIndex).toString().trim()
String lecturerTitle = row.getCell(titleIndex) != null
? row.getCell(titleIndex).toString().trim()
: "";
String lecturerName = row.getCell(nameIndex) != null ? row.getCell(nameIndex).toString().trim()
: "";
String lecturerSurname = row.getCell(surnameIndex) != null
? row.getCell(surnameIndex).toString().trim()
: "";
Integer zajCykId = row.getCell(zajCykIdIndex) != null ? (int) Double.parseDouble(row.getCell(zajCykIdIndex).toString().trim())
Integer zajCykId = row.getCell(zajCykIdIndex) != null
? (int) Double.parseDouble(row.getCell(zajCykIdIndex).toString().trim())
: null;
Integer grNr = row.getCell(grNrIndex) != null ? (int) Double.parseDouble(row.getCell(grNrIndex).toString().trim())
Integer grNr = row.getCell(grNrIndex) != null
? (int) Double.parseDouble(row.getCell(grNrIndex).toString().trim())
: null;
int day = row.getCell(dayIndex) != null ? (int) Double.parseDouble(row.getCell(dayIndex).toString()) : 0;
int day = row.getCell(dayIndex) != null
? (int) Double.parseDouble(row.getCell(dayIndex).toString())
: 0;
WeekDay groupDay = WeekDay.getDay(day - 1);
int time = parseTimeToInt(row.getCell(timeIndex).toString());
String room = row.getCell(roomIndex).toString().trim();
@ -92,12 +106,13 @@ public class FileToDatabaseMigrator {
Course course = this.courseService.getCourseByName(courseName)
.orElseGet(() -> this.courseService.save(new Course(courseName, symbol)));
Lecturer lecturer = this.lecturerService.getLecturer(lecturerTitle, lecturerName, lecturerSurname)
.orElseGet(() -> this.lecturerService
.save(new Lecturer(lecturerTitle, lecturerName, lecturerSurname)));
Lecturer lecturer = this.lecturerService
.getLecturer(lecturerTitle, lecturerName, lecturerSurname)
.orElseGet(() -> this.lecturerService.save(
new Lecturer(lecturerTitle, lecturerName, lecturerSurname)));
Groups group = this.groupService.find(zajCykId, grNr).orElseGet(
() -> new Groups(capacity, room, course, time, groupDay, lecturer, zajCykId, grNr, typeGroup));
Groups group = this.groupService.find(zajCykId, grNr).orElseGet(() -> new Groups(capacity, room,
course, time, groupDay, lecturer, zajCykId, grNr, typeGroup));
group.update(capacity, room, course, time, null, groupDay, lecturer, typeGroup);
this.groupService.save(group);
@ -106,6 +121,11 @@ public class FileToDatabaseMigrator {
}
/**
*
* @param time time string in formaT hh:mm or hh.mm
* @return int time witch is minutes from 00:00
*/
private static int parseTimeToInt(String time) {
String times[] = time.split("\\.|\\:");
return times.length == 2 ? Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]) : 0;

View File

@ -78,6 +78,9 @@ public class Assignment {
public Assignment() {
}
/**
* @param commision commision to set
*/
public void setCommision(Commision commision) {
this.commision = commision;
}

View File

@ -1,9 +1,9 @@
package com.plannaplan.exceptions;
public class TokenExpiredException extends RuntimeException {
/**
*
* Excepction to be thrown when provided token is expired
*/
public class TokenExpiredException extends RuntimeException {
private static final long serialVersionUID = 1L;
public TokenExpiredException(String message) {

View File

@ -1,9 +1,9 @@
package com.plannaplan.exceptions;
public class UserNotFoundException extends Exception {
/**
*
* Exception to be thrown when provided user does not exist in database
*/
public class UserNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
public UserNotFoundException(String message) {

View File

@ -2,6 +2,9 @@ package com.plannaplan.models;
import java.io.InputStream;
/**
* Config data copntainer to keep tours dates and stream of dasta to import
*/
public class ConfigData {
private TourData firstTour;
private TourData secondTour;

View File

@ -4,6 +4,9 @@ import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Row;
/**
* Wrapper for data readed from file
*/
public class FileData {
private HashMap<String, Integer> keys;
@ -13,6 +16,7 @@ public class FileData {
* FileData
*
* @param keys this is a hashmap of String and Integer
*
* @param rows this is a iterator of rows.
*/
public FileData(HashMap<String, Integer> keys, Iterator<Row> rows) {
@ -31,6 +35,7 @@ public class FileData {
/*
* setRows
*
* @param rows set the rows to given function
*/
public void setRows(Iterator<Row> rows) {
@ -39,6 +44,7 @@ public class FileData {
/*
* getKeys
*
* @return keys
*/
public HashMap<String, Integer> getKeys() {
@ -47,6 +53,7 @@ public class FileData {
/*
* setKeys
*
* @param keys set the key is being a struck of hashmap (String, Integer)
*/
public void setKeys(HashMap<String, Integer> keys) {
@ -55,6 +62,7 @@ public class FileData {
/*
* getIndexOf
*
* @return index
*/
public int getIndexOf(String key) {

View File

@ -3,27 +3,48 @@ package com.plannaplan.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
/**
* Match of users Exchange's to be performed
*/
public class MatchData {
private Exchange exchangeOne;
private Exchange exchangeTwo;
/**
* create MatchData
*
* @param exchangeOne first Exchange of found match
* @param exchangeTwo second Exchange of found match
*/
public MatchData(Exchange exchangeOne, Exchange exchangeTwo) {
this.exchangeOne = exchangeOne;
this.exchangeTwo = exchangeTwo;
}
/**
* @return first Exchange
*/
public Exchange getExchangeOne() {
return this.exchangeOne;
}
/**
* @return second Exchange
*/
public Exchange getExchangeTwo() {
return this.exchangeTwo;
}
/**
* @return second Exchange's owned assignmetn
*/
public Assignment getAssignmentTwo() {
return this.exchangeTwo.getOwnedAssignment();
}
/**
* @return first Exchange's owned assignmetn
*/
public Assignment getAssignmentOne() {
return this.exchangeOne.getOwnedAssignment();
}
@ -36,30 +57,37 @@ public class MatchData {
@Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/*
* Check if o is an instance of Complex or not "null instanceof [type]" also
* returns false
*/
if (!(o instanceof MatchData)) {
return false;
}
// typecast o to Complex so that we can compare data members
MatchData c = (MatchData) o;
// Compare the data members and return accordingly
return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne()));
return (this.getAssignmentOne().equals(c.getAssignmentOne())
&& this.getAssignmentTwo().equals(c.getAssignmentTwo()))
|| (this.getAssignmentOne().equals(c.getAssignmentTwo())
&& this.getAssignmentTwo().equals(c.getAssignmentOne()));
}
/**
* comparator for MatchData. It compare it by sum of both exchange's times. For
* example MatchData with Exchanges 11:00 and 12:00 will be less than with
* Exchanges 12:00 and 12:00
*
* @param m1 MatchData instance to compare to
* @return int <0 if m1 is "less than", 0 if it's equal and >0 otherwise
*/
public int compare(MatchData m1) {
return Long.compare(m1.getExchangesMsValue(), this.getExchangesMsValue());
}
/**
* @return sum of both exchanges java.sql.Timestanp::getTime"
*/
public long getExchangesMsValue() {
return this.exchangeOne.getDataExchange().getTime() + this.exchangeTwo.getDataExchange().getTime();
}

View File

@ -2,6 +2,9 @@ package com.plannaplan.models;
import java.sql.Date;
/**
* Container for Tours dates
*/
public class TourData {
private Date start;

View File

@ -11,18 +11,36 @@ public class UserApiResponse {
public UserApiResponse() {
}
/**
* @return user's Surname
*/
public String getSurname() {
return surname;
}
/**
* setter for name. Reson to have setters for this class is for case if there
* would be name and no surname or otherwise
*
* @param surname name to set that was obtained by api request
*/
public void setSurname(String surname) {
this.surname = surname;
}
/**
* @return user's Name
*/
public String getName() {
return name;
}
/**
* setter for surname. Reson to have setters for this class is for case if there
* would be name and no surname or otherwise
*
* @param surname name to set that was obtained by api request
*/
public void setName(String name) {
this.name = name;
}

View File

@ -29,6 +29,13 @@ public class CommisionService {
public CommisionService() {
}
/**
* save to database commision. It also checks for missing assignments from
* previous commision (you can not get rid of accepted assignment)
*
* @param commision to save to db
* @return Commision instance with id from database
*/
public Commision save(Commision commision) {
Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner());
if (lastCommision.isPresent()) {

View File

@ -10,7 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Service of CourseService which can get(Course By Name, All Courses, Courses Ammount ), save, delete course.
* Service of CourseService which can get(Course By Name, All Courses, Courses
* Ammount ), save, delete course.
*/
@Service
@ -18,42 +19,46 @@ public class CourseService {
@Autowired
private CourseRepository repo;
/*
/**
* getCourseByName
* Return Course By Name
*
* @param name
* @return Course By Name
*/
public Optional<Course> getCourseByName(String name) {
return this.repo.findByName(name);
}
/*
* getAllCourses
* Return List of get courses
/**
*
* @return all courses from db
*/
public List<Course> getAllCourses() {
return this.repo.findAll();
}
/*
* save
/**
* save to db
*
* @param course which course you would like to save
* @return Course instance with id from db
*/
public Course save(Course course) {
this.repo.save(course);
return course;
}
/*
* delete
/**
* delete course from db
*
* @param course which course you would like to delete
*/
public void delete(Course course) {
this.repo.delete(course);
}
/*
* getCoursesAmmount
* Return a ammount of courses
/**
* @return ammount of courses in db
*/
public int getCoursesAmmount() {
return (int) this.repo.count();

View File

@ -12,6 +12,9 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
/**
* Service to manage app events
*/
@Service
public class EventService {
@ -29,6 +32,9 @@ public class EventService {
System.out.println("Checking for groups");
}
/**
* perfroms checks for matching exchanges daily
*/
@Scheduled(cron = "0 0 0 * * *")
public void performExchangeService() {
System.out.println("Performing Exchange");
@ -53,6 +59,10 @@ public class EventService {
jobsMap.put(taskId, scheduledTask);
}
/**
* init resources needed for dynamicly creating new tasks (needed to set tours
* end events)
*/
@PostConstruct
public void initialize() {
this.scheduler = new ThreadPoolTaskScheduler();

View File

@ -19,6 +19,9 @@ import com.plannaplan.repositories.ExchangeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Service to manage Exchanges
*/
@Service
public class ExchangeService {
@ -50,6 +53,9 @@ public class ExchangeService {
return this.repo.findById(id);
}
/**
* @return list of all exchanges in database
*/
public List<Exchange> getAllExchanges() {
return this.repo.findAll();
}
@ -78,6 +84,12 @@ public class ExchangeService {
return this.repo.checkForExchange(assignment, group);
}
/**
* method to perform Exchange algorythm. It search for matches and swap
* assignments between latests user commisions if it can be performed. After
* swap we block users matches that contains switched groups. After algorythm
* email is being sent to all users with information about performed exchanges
*/
public void performExchange() {
final List<MatchData> matchData = this.getMatches();
final List<Long> performedAssignmentExchanges = new ArrayList<>();
@ -127,6 +139,9 @@ public class ExchangeService {
this.repo.deleteAll(exchangesToDelete);
}
/**
* @return list of matches found in database
*/
public List<MatchData> getMatches() {
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
final Exchange exchangeOne = (Exchange) m[0];

View File

@ -18,7 +18,6 @@ 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
@ -27,34 +26,84 @@ public class GroupService {
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) {