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

View File

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

View File

@ -1,27 +1,36 @@
package com.plannaplan.models; package com.plannaplan.models;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
public class MatchData { public class MatchData {
private Assignment assignmentOne; private Exchange exchangeOne;
private Assignment assignmentTwo; private Exchange exchangeTwo;
public MatchData(Assignment assignmentOne, Assignment assignmentTwo) { public MatchData(Exchange exchangeOne, Exchange exchangeTwo) {
this.assignmentOne = assignmentOne; this.exchangeOne = exchangeOne;
this.assignmentTwo = assignmentTwo; this.exchangeTwo = exchangeTwo;
}
public Exchange getExchangeOne() {
return this.exchangeOne;
}
public Exchange getExchangeTwo() {
return this.exchangeTwo;
} }
public Assignment getAssignmentTwo() { public Assignment getAssignmentTwo() {
return assignmentTwo; return this.exchangeTwo.getOwnedAssignment();
} }
public Assignment getAssignmentOne() { public Assignment getAssignmentOne() {
return assignmentOne; return this.exchangeOne.getOwnedAssignment();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode(); return this.getAssignmentOne().hashCode() + this.getAssignmentTwo().hashCode();
} }
@Override @Override
@ -44,6 +53,6 @@ public class MatchData {
MatchData c = (MatchData) o; MatchData c = (MatchData) o;
// Compare the data members and return accordingly // 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; package com.plannaplan.services;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -7,8 +8,10 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.models.MatchData; import com.plannaplan.models.MatchData;
import com.plannaplan.repositories.ExchangeRepository; import com.plannaplan.repositories.ExchangeRepository;
@ -21,6 +24,12 @@ public class ExchangeService {
@Autowired @Autowired
private ExchangeRepository repo; private ExchangeRepository repo;
@Autowired
private AssignmentService assignmentService;
@Autowired
private UserService userService;
/** /**
* @param exchange Instance to save in database * @param exchange Instance to save in database
* @return Exchange Instance contains database id * @return Exchange Instance contains database id
@ -62,14 +71,47 @@ public class ExchangeService {
} }
public void performExchange() { 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(){ public Set<MatchData> getMatches(){
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> { final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeOne = (Exchange) m[0];
final Exchange exchangeTwo = (Exchange) m[1]; final Exchange exchangeTwo = (Exchange) m[1];
return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment()); return new MatchData(exchangeOne, exchangeTwo);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
final Set<MatchData> filterMatches = new HashSet<>(matches); final Set<MatchData> filterMatches = new HashSet<>(matches);