Checkpoint: added performExchange

This commit is contained in:
Marcin Woźniak 2021-01-12 14:10:48 +01:00
parent 1c12a778af
commit bc8f7407fc
Signed by: y0rune
GPG Key ID: F204C385F57EB348
4 changed files with 99 additions and 40 deletions

View File

@ -10,7 +10,7 @@ import javax.persistence.ManyToOne;
/**
* Entity of Assignment grouping of state associated about group_id and
* commision_id
*
*
*/
@Entity
@ -28,7 +28,7 @@ public class Assignment {
/**
* Assignment
*
*
* @param group group we would like to assign
* @param commision commision that assignment belongs to
* @param isPastAssignment is assignment past or no
@ -40,14 +40,14 @@ public class Assignment {
/**
* If it returns trues it mesans u are assigned to group accepted by algorythm
*
*
* @return boolean isAccepted
*/
public boolean isAccepted() {
return this.group.getRegisteredStudents().contains(this.commision.getCommisionOwner());
}
/**
/**
* Getter of commision
* @return Commision Commision of given assignments
*/
@ -57,7 +57,7 @@ public class Assignment {
/**
* Assignment
*
*
* @param group group we would like to assign
* @param commision commision that assignment belongs to
*/
@ -68,9 +68,12 @@ public class Assignment {
public Assignment() {
}
public void setCommision(Commision commision) {
this.commision = commision;
}
/**
* Id getter
*
*
* @return id id of assignment
*/
@ -80,7 +83,7 @@ public class Assignment {
/**
* getGroup
*
*
* @return group
*/
public Groups getGroup() {
@ -89,7 +92,7 @@ public class Assignment {
/**
* isPastAssignment getter
*
*
* @return isPastAssignment
*/
public boolean isPastAssignment() {
@ -98,10 +101,10 @@ public class Assignment {
/**
* setter isPastAssignment
*
*
* @param isPastAssignment is assignment past or not
*/
public void setPastAssignment(boolean isPastAssignment) {
this.isPastAssignment = isPastAssignment;
}
}
}

View File

@ -47,6 +47,11 @@ public class User {
return this.studentRegisteredGrups;
}
public void removeGroup(Long id) {
final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst().get();
this.studentRegisteredGrups.remove(groupToDelete);
}
public void claimGroup(Groups group) {
if (this.studentRegisteredGrups == null) {
this.studentRegisteredGrups = new HashSet<>();
@ -58,7 +63,7 @@ public class User {
}
/**
*
*
* @param name name given to the user
* @param surname surname given to the user
* @param mail mail given to the user
@ -72,7 +77,7 @@ public class User {
}
/**
*
*
* @param name name given to the user
* @param surname surname given to the user
* @param mail mail given to the user
@ -85,7 +90,7 @@ public class User {
}
/**
*
*
* @param name name given to the user
* @param surname surname given to the user
* @param mail mail given to the user
@ -101,7 +106,7 @@ public class User {
/**
* usos id getter
*
*
* @return usosid
*/
public String getUsosId() {
@ -110,7 +115,7 @@ public class User {
/**
* email getter
*
*
* @return mailof user
*/
public String getEmail() {
@ -119,7 +124,7 @@ public class User {
/**
* email setter
*
*
* @param email user email
*/
public void setEmail(String email) {
@ -128,7 +133,7 @@ public class User {
/**
* token usage getter
*
*
* @return Timestamp when token was used
*/
public Timestamp getTokenUsageDate() {
@ -137,7 +142,7 @@ public class User {
/**
* token getter
*
*
* @return user token
*/
public String getToken() {
@ -153,7 +158,7 @@ public class User {
/**
* token seter. Sets token and automaticly set time when was set
*
*
* @param token token to set
*/
public void setToken(String token) {
@ -164,7 +169,7 @@ public class User {
/**
* name setter
*
*
* @return String user name
*/
public String getName() {
@ -173,7 +178,7 @@ public class User {
/**
* user rolse getter
*
*
* @return UserRoles of user
*/
public UserRoles getRole() {
@ -182,7 +187,7 @@ public class User {
/**
* user role setter
*
*
* @param role to be set
*/
public void setRole(UserRoles role) {
@ -191,7 +196,7 @@ public class User {
/**
* surname getter
*
*
* @return string surname
*/
public String getSurname() {
@ -200,7 +205,7 @@ public class User {
/**
* surname setter
*
*
* @param surname string to be set as surnames
*/
public void setSurname(String surname) {
@ -209,7 +214,7 @@ public class User {
/**
* name stter
*
*
* @param name stirng to be set as name
*/
public void setName(String name) {
@ -218,7 +223,7 @@ public class User {
/**
* id getter
*
*
* @return id in database
*/
public Long getId() {
@ -227,7 +232,7 @@ public class User {
/**
* Ranking points are [100;500]. It's calculated by gradesAvg*100*studiesYear
*
*
* @return ranking points [100;500]
*/
public Integer getRanking() {
@ -237,7 +242,7 @@ public class User {
/**
* Ranking points setter. Ranking points are [100;500]. It's calculated by
* gradesAvg*100*studiesYear
*
*
* @param ranking ranking points [100;500]
*/
public void setRanking(Integer ranking) {
@ -246,7 +251,7 @@ public class User {
/**
* updates user entity with data got by UsosApiService::getUserData
*
*
* @param usosData UserApiResponse model with needed data
*/
public void updateWithUsosData(UserApiResponse usosData) {
@ -257,7 +262,7 @@ public class User {
/**
* 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
*
*
* @return boolena if credentials (token) is expired or not
*/
public boolean isCredentialsNonExpired() {

View File

@ -1,27 +1,36 @@
package com.plannaplan.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
public class MatchData {
private Assignment assignmentOne;
private Assignment assignmentTwo;
private Exchange exchangeOne;
private Exchange exchangeTwo;
public MatchData(Assignment assignmentOne, Assignment assignmentTwo) {
this.assignmentOne = assignmentOne;
this.assignmentTwo = assignmentTwo;
public MatchData(Exchange exchangeOne, Exchange exchangeTwo) {
this.exchangeOne = exchangeOne;
this.exchangeTwo = exchangeTwo;
}
public Exchange getExchangeOne() {
return this.exchangeOne;
}
public Exchange getExchangeTwo() {
return this.exchangeTwo;
}
public Assignment getAssignmentTwo() {
return assignmentTwo;
return this.exchangeTwo.getOwnedAssignment();
}
public Assignment getAssignmentOne() {
return assignmentOne;
return this.exchangeOne.getOwnedAssignment();
}
@Override
public int hashCode() {
return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode();
return this.getAssignmentOne().hashCode() + this.getAssignmentTwo().hashCode();
}
@Override
@ -44,6 +53,6 @@ public class MatchData {
MatchData c = (MatchData) o;
// Compare the data members and return accordingly
return (this.assignmentOne.equals(c.getAssignmentOne()) && this.assignmentTwo.equals(c.getAssignmentTwo())) || (this.assignmentOne.equals(c.getAssignmentTwo()) && this.assignmentTwo.equals(c.getAssignmentOne()));
return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne()));
}
}

View File

@ -1,5 +1,6 @@
package com.plannaplan.services;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
@ -7,8 +8,10 @@ import java.util.Set;
import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.models.MatchData;
import com.plannaplan.repositories.ExchangeRepository;
@ -21,6 +24,12 @@ public class ExchangeService {
@Autowired
private ExchangeRepository repo;
@Autowired
private AssignmentService assignmentService;
@Autowired
private UserService userService;
/**
* @param exchange Instance to save in database
* @return Exchange Instance contains database id
@ -62,14 +71,47 @@ public class ExchangeService {
}
public void performExchange() {
final Set<MatchData> matchData = this.getMatches();
final List<Long> performedAssignmentExchanges = new ArrayList<>();
matchData.forEach( m -> {
final Assignment assignmentOne = m.getAssignmentOne();
final Assignment assignmentTwo = m.getAssignmentTwo();
if (!(performedAssignmentExchanges.contains(assignmentOne.getId()) || performedAssignmentExchanges.contains(assignmentTwo.getId()))){
final Commision commisionOne = assignmentOne.getCommision();
final User userOne = commisionOne.getCommisionOwner();
final Commision commisionTwo = assignmentTwo.getCommision();
final User userTwo = commisionTwo.getCommisionOwner();
assignmentOne.setCommision(commisionTwo);
assignmentTwo.setCommision(commisionOne);
userOne.removeGroup(assignmentOne.getGroup().getId());
userTwo.removeGroup(assignmentTwo.getGroup().getId());
userOne.claimGroup(assignmentTwo.getGroup());
userTwo.claimGroup(assignmentOne.getGroup());
this.assignmentService.save(assignmentOne);
this.assignmentService.save(assignmentTwo);
this.userService.save(userOne);
this.userService.save(userTwo);
performedAssignmentExchanges.add(assignmentOne.getId());
performedAssignmentExchanges.add(assignmentTwo.getId());
}
this.deleteExchange(m.getExchangeOne());
this.deleteExchange(m.getExchangeTwo());
});
}
public Set<MatchData> getMatches(){
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
final Exchange exchangeOne = (Exchange) m[0];
final Exchange exchangeTwo = (Exchange) m[1];
return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment());
return new MatchData(exchangeOne, exchangeTwo);
}).collect(Collectors.toList());
final Set<MatchData> filterMatches = new HashSet<>(matches);