Added emial to exchanges

This commit is contained in:
Filip Izydorczyk 2021-01-13 16:52:18 +01:00
commit ab32ad8561
4 changed files with 160 additions and 45 deletions

View 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();
}
}

View File

@ -1,7 +1,13 @@
package com.plannaplan.services; 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.entities.User;
import com.plannaplan.models.EmailAcceptedData; import com.plannaplan.models.EmailAcceptedData;
import com.plannaplan.models.EmailExchangesData;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -18,6 +24,9 @@ public class EmailService {
@Autowired @Autowired
private JavaMailSender emailSender; private JavaMailSender emailSender;
@Autowired
private UserService userService;
@Value("${plannaplan.email}") @Value("${plannaplan.email}")
private String appEmail; private String appEmail;
@ -54,4 +63,39 @@ public class EmailService {
mailMessage.setText(data.getEmailMessage()); mailMessage.setText(data.getEmailMessage());
emailSender.send(mailMessage); 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);
}
}
} }

View File

@ -6,6 +6,7 @@ import java.util.concurrent.ScheduledFuture;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.CronTrigger;
@ -14,6 +15,9 @@ import org.springframework.stereotype.Service;
@Service @Service
public class EventService { public class EventService {
@Autowired
private ExchangeService exchangeService;
public static final int FIRST_TOUR_SCHEDULE = 0; public static final int FIRST_TOUR_SCHEDULE = 0;
public static final int SECOND_TOUR_SCHEDULE = 1; public static final int SECOND_TOUR_SCHEDULE = 1;
@ -25,6 +29,12 @@ public class EventService {
System.out.println("Checking for groups"); 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 * Schedule provided task to perform
* *

View File

@ -12,6 +12,7 @@ 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.entities.User;
import com.plannaplan.models.EmailExchangesData;
import com.plannaplan.models.MatchData; import com.plannaplan.models.MatchData;
import com.plannaplan.repositories.ExchangeRepository; import com.plannaplan.repositories.ExchangeRepository;
@ -30,6 +31,9 @@ public class ExchangeService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private EmailService emailService;
/** /**
* @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
@ -77,11 +81,18 @@ public class ExchangeService {
public void performExchange() { public void performExchange() {
final List<MatchData> matchData = this.getMatches(); final List<MatchData> matchData = this.getMatches();
final List<Long> performedAssignmentExchanges = new ArrayList<>(); final List<Long> performedAssignmentExchanges = new ArrayList<>();
final EmailExchangesData emailData = new EmailExchangesData();
final List<Exchange> exchangesToDelete = new ArrayList<>();
matchData.forEach(m -> { matchData.forEach(m -> {
final Assignment assignmentOne = m.getAssignmentOne(); final Assignment assignmentOne = m.getAssignmentOne();
final Assignment assignmentTwo = m.getAssignmentTwo(); final Assignment assignmentTwo = m.getAssignmentTwo();
if (!(performedAssignmentExchanges.contains(assignmentOne.getId()) || performedAssignmentExchanges.contains(assignmentTwo.getId()))){ final Exchange exchange1 = m.getExchangeOne();
final Exchange exchange2 = m.getExchangeTwo();
if (!(performedAssignmentExchanges.contains(assignmentOne.getId())
|| performedAssignmentExchanges.contains(assignmentTwo.getId()))) {
final Commision commisionOne = assignmentOne.getCommision(); final Commision commisionOne = assignmentOne.getCommision();
final User userOne = commisionOne.getCommisionOwner(); final User userOne = commisionOne.getCommisionOwner();
final Commision commisionTwo = assignmentTwo.getCommision(); final Commision commisionTwo = assignmentTwo.getCommision();
@ -104,11 +115,16 @@ public class ExchangeService {
performedAssignmentExchanges.add(assignmentOne.getId()); performedAssignmentExchanges.add(assignmentOne.getId());
performedAssignmentExchanges.add(assignmentTwo.getId()); performedAssignmentExchanges.add(assignmentTwo.getId());
emailData.addExchange(exchange1.getOwnedAssignment().getCommision().getCommisionOwner(), exchange1);
emailData.addExchange(exchange2.getOwnedAssignment().getCommision().getCommisionOwner(), exchange2);
} }
this.deleteExchange(m.getExchangeOne()); exchangesToDelete.add(exchange1);
this.deleteExchange(m.getExchangeTwo()); exchangesToDelete.add(exchange2);
}); });
this.emailService.sendExchangesResults(emailData);
this.repo.deleteAll(exchangesToDelete);
} }
public List<MatchData> getMatches() { public List<MatchData> getMatches() {