Added emial to exchanges
This commit is contained in:
commit
ab32ad8561
45
buisnesslogic/src/main/java/com/plannaplan/models/EmailExchangesData.java
Executable file
45
buisnesslogic/src/main/java/com/plannaplan/models/EmailExchangesData.java
Executable file
@ -0,0 +1,45 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.entities.User;
|
||||
|
||||
/**
|
||||
* Class to keepm data to be send after exchanges being accept
|
||||
*/
|
||||
public class EmailExchangesData {
|
||||
private Map<Long, List<Exchange>> data;
|
||||
|
||||
public EmailExchangesData() {
|
||||
this.data = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* method to add user and his event to data to be send
|
||||
*
|
||||
* @param user owner of exchange being performed
|
||||
* @param data exchange that was executed
|
||||
*/
|
||||
public void addExchange(User user, Exchange data) {
|
||||
final Long id = user.getId();
|
||||
if (this.data.get(id) == null) {
|
||||
this.data.put(id, new ArrayList<>());
|
||||
}
|
||||
this.data.get(id).add(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* get datas entry set
|
||||
*
|
||||
* @return set with entries that contains Long that is user id and list of hiss
|
||||
* performed Exchanges
|
||||
*/
|
||||
public Set<Map.Entry<Long, List<Exchange>>> getDataEntry() {
|
||||
return this.data.entrySet();
|
||||
}
|
||||
}
|
@ -1,7 +1,13 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.models.EmailAcceptedData;
|
||||
import com.plannaplan.models.EmailExchangesData;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -18,6 +24,9 @@ public class EmailService {
|
||||
@Autowired
|
||||
private JavaMailSender emailSender;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Value("${plannaplan.email}")
|
||||
private String appEmail;
|
||||
|
||||
@ -54,4 +63,39 @@ public class EmailService {
|
||||
mailMessage.setText(data.getEmailMessage());
|
||||
emailSender.send(mailMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* method to send email for students whose groups were swaped
|
||||
*
|
||||
* @param data EmailExchangesData instance that contains pair of datas user and
|
||||
* list of performed exhanges
|
||||
*/
|
||||
public void sendExchangesResults(EmailExchangesData data) {
|
||||
for (Map.Entry<Long, List<Exchange>> entry : data.getDataEntry()) {
|
||||
final User user = this.userService.getById(entry.getKey()).get();
|
||||
String response = "Znaleźliśmy dla Ciebie osobę, która zamieniła się z Tobą przedmiotami!!!\n Zamienione przedmioty: \n";
|
||||
|
||||
final Iterator<Exchange> interator = entry.getValue().iterator();
|
||||
|
||||
while (interator.hasNext()) {
|
||||
final Exchange exchange = interator.next();
|
||||
final String courseFrom = exchange.getDesiredAssignment().getCourseId() != null
|
||||
? exchange.getDesiredAssignment().getCourseId().getName()
|
||||
: "Nieznane zajęcie";
|
||||
final String courseTo = exchange.getOwnedAssignment().getGroup().getCourseId() != null
|
||||
? exchange.getOwnedAssignment().getGroup().getCourseId().getName()
|
||||
: "Nieznane zajęcie";
|
||||
|
||||
response += (" - " + courseFrom + " za " + courseTo.toLowerCase());
|
||||
}
|
||||
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setFrom(appEmail);
|
||||
mailMessage.setTo(user.getEmail());
|
||||
mailMessage.setSubject("[PlanNaPlan] Zamiana przedmiotów");
|
||||
mailMessage.setText(response);
|
||||
emailSender.send(mailMessage);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
@ -14,6 +15,9 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class EventService {
|
||||
|
||||
@Autowired
|
||||
private ExchangeService exchangeService;
|
||||
|
||||
public static final int FIRST_TOUR_SCHEDULE = 0;
|
||||
public static final int SECOND_TOUR_SCHEDULE = 1;
|
||||
|
||||
@ -25,9 +29,15 @@ public class EventService {
|
||||
System.out.println("Checking for groups");
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 0 * * *")
|
||||
public void performExchangeService() {
|
||||
System.out.println("Performing Exchange");
|
||||
this.exchangeService.performExchange();
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule provided task to perform
|
||||
*
|
||||
*
|
||||
* @param taskId static filed of this class that represents to what event
|
||||
* we want to assign task
|
||||
* @param task runnable class that perform task in implemented run method
|
||||
|
@ -12,6 +12,7 @@ import com.plannaplan.entities.Commision;
|
||||
import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.models.EmailExchangesData;
|
||||
import com.plannaplan.models.MatchData;
|
||||
import com.plannaplan.repositories.ExchangeRepository;
|
||||
|
||||
@ -30,12 +31,15 @@ public class ExchangeService {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private EmailService emailService;
|
||||
|
||||
/**
|
||||
* @param exchange Instance to save in database
|
||||
* @return Exchange Instance contains database id
|
||||
*/
|
||||
public Exchange save(Exchange exchange) {
|
||||
return this.repo.save(exchange);
|
||||
return this.repo.save(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,11 +47,11 @@ public class ExchangeService {
|
||||
* @return Optional Exchange if found
|
||||
*/
|
||||
public Optional<Exchange> getById(Long id) {
|
||||
return this.repo.findById(id);
|
||||
return this.repo.findById(id);
|
||||
}
|
||||
|
||||
public List<Exchange> getAllExchanges() {
|
||||
return this.repo.findAll();
|
||||
public List<Exchange> getAllExchanges() {
|
||||
return this.repo.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,14 +59,14 @@ public class ExchangeService {
|
||||
* @return List of exchanges that belong to user
|
||||
*/
|
||||
public List<Exchange> getByUserId(Long id) {
|
||||
return this.repo.getByUserId(id);
|
||||
return this.repo.getByUserId(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entity Exchange entity which we would like to delete
|
||||
*/
|
||||
public void deleteExchange(Exchange entity) {
|
||||
this.repo.delete(entity);
|
||||
this.repo.delete(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,57 +75,69 @@ public class ExchangeService {
|
||||
* @return Optional with Exchange if exist
|
||||
*/
|
||||
public Optional<Exchange> checkForExchange(Assignment assignment, Groups group) {
|
||||
return this.repo.checkForExchange(assignment, group);
|
||||
return this.repo.checkForExchange(assignment, group);
|
||||
}
|
||||
|
||||
public void performExchange() {
|
||||
final List<MatchData> matchData = this.getMatches();
|
||||
final List<Long> performedAssignmentExchanges = new ArrayList<>();
|
||||
matchData.forEach( m -> {
|
||||
final Assignment assignmentOne = m.getAssignmentOne();
|
||||
final Assignment assignmentTwo = m.getAssignmentTwo();
|
||||
final List<MatchData> matchData = this.getMatches();
|
||||
final List<Long> performedAssignmentExchanges = new ArrayList<>();
|
||||
final EmailExchangesData emailData = new EmailExchangesData();
|
||||
final List<Exchange> exchangesToDelete = new ArrayList<>();
|
||||
|
||||
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();
|
||||
matchData.forEach(m -> {
|
||||
final Assignment assignmentOne = m.getAssignmentOne();
|
||||
final Assignment assignmentTwo = m.getAssignmentTwo();
|
||||
|
||||
assignmentOne.setCommision(commisionTwo);
|
||||
assignmentTwo.setCommision(commisionOne);
|
||||
final Exchange exchange1 = m.getExchangeOne();
|
||||
final Exchange exchange2 = m.getExchangeTwo();
|
||||
|
||||
userOne.removeGroup(assignmentOne.getGroup().getId());
|
||||
userTwo.removeGroup(assignmentTwo.getGroup().getId());
|
||||
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();
|
||||
|
||||
userOne.claimGroup(assignmentTwo.getGroup());
|
||||
userTwo.claimGroup(assignmentOne.getGroup());
|
||||
assignmentOne.setCommision(commisionTwo);
|
||||
assignmentTwo.setCommision(commisionOne);
|
||||
|
||||
this.assignmentService.save(assignmentOne);
|
||||
this.assignmentService.save(assignmentTwo);
|
||||
userOne.removeGroup(assignmentOne.getGroup().getId());
|
||||
userTwo.removeGroup(assignmentTwo.getGroup().getId());
|
||||
|
||||
this.userService.save(userOne);
|
||||
this.userService.save(userTwo);
|
||||
userOne.claimGroup(assignmentTwo.getGroup());
|
||||
userTwo.claimGroup(assignmentOne.getGroup());
|
||||
|
||||
performedAssignmentExchanges.add(assignmentOne.getId());
|
||||
performedAssignmentExchanges.add(assignmentTwo.getId());
|
||||
}
|
||||
this.assignmentService.save(assignmentOne);
|
||||
this.assignmentService.save(assignmentTwo);
|
||||
|
||||
this.deleteExchange(m.getExchangeOne());
|
||||
this.deleteExchange(m.getExchangeTwo());
|
||||
});
|
||||
this.userService.save(userOne);
|
||||
this.userService.save(userTwo);
|
||||
|
||||
performedAssignmentExchanges.add(assignmentOne.getId());
|
||||
performedAssignmentExchanges.add(assignmentTwo.getId());
|
||||
|
||||
emailData.addExchange(exchange1.getOwnedAssignment().getCommision().getCommisionOwner(), exchange1);
|
||||
emailData.addExchange(exchange2.getOwnedAssignment().getCommision().getCommisionOwner(), exchange2);
|
||||
}
|
||||
|
||||
exchangesToDelete.add(exchange1);
|
||||
exchangesToDelete.add(exchange2);
|
||||
});
|
||||
this.emailService.sendExchangesResults(emailData);
|
||||
this.repo.deleteAll(exchangesToDelete);
|
||||
}
|
||||
|
||||
public List<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, exchangeTwo);
|
||||
}).collect(Collectors.toList());
|
||||
public List<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, exchangeTwo);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
final Set<MatchData> uniqData = new HashSet<>(matches);
|
||||
final List<MatchData> matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2))
|
||||
.collect(Collectors.toList());
|
||||
final Set<MatchData> uniqData = new HashSet<>(matches);
|
||||
final List<MatchData> matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return matchDataListSorted;
|
||||
}
|
||||
return matchDataListSorted;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user