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,12 +31,15 @@ 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
*/ */
public Exchange save(Exchange exchange) { 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 * @return Optional Exchange if found
*/ */
public Optional<Exchange> getById(Long id) { public Optional<Exchange> getById(Long id) {
return this.repo.findById(id); return this.repo.findById(id);
} }
public List<Exchange> getAllExchanges() { public List<Exchange> getAllExchanges() {
return this.repo.findAll(); return this.repo.findAll();
} }
/** /**
@ -55,14 +59,14 @@ public class ExchangeService {
* @return List of exchanges that belong to user * @return List of exchanges that belong to user
*/ */
public List<Exchange> getByUserId(Long id) { 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 * @param entity Exchange entity which we would like to delete
*/ */
public void deleteExchange(Exchange entity) { 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 * @return Optional with Exchange if exist
*/ */
public Optional<Exchange> checkForExchange(Assignment assignment, Groups group) { public Optional<Exchange> checkForExchange(Assignment assignment, Groups group) {
return this.repo.checkForExchange(assignment, group); return this.repo.checkForExchange(assignment, group);
} }
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<>();
matchData.forEach( m -> { final EmailExchangesData emailData = new EmailExchangesData();
final Assignment assignmentOne = m.getAssignmentOne(); final List<Exchange> exchangesToDelete = new ArrayList<>();
final Assignment assignmentTwo = m.getAssignmentTwo();
if (!(performedAssignmentExchanges.contains(assignmentOne.getId()) || performedAssignmentExchanges.contains(assignmentTwo.getId()))){ matchData.forEach(m -> {
final Commision commisionOne = assignmentOne.getCommision(); final Assignment assignmentOne = m.getAssignmentOne();
final User userOne = commisionOne.getCommisionOwner(); final Assignment assignmentTwo = m.getAssignmentTwo();
final Commision commisionTwo = assignmentTwo.getCommision();
final User userTwo = commisionTwo.getCommisionOwner();
assignmentOne.setCommision(commisionTwo); final Exchange exchange1 = m.getExchangeOne();
assignmentTwo.setCommision(commisionOne); final Exchange exchange2 = m.getExchangeTwo();
userOne.removeGroup(assignmentOne.getGroup().getId()); if (!(performedAssignmentExchanges.contains(assignmentOne.getId())
userTwo.removeGroup(assignmentTwo.getGroup().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()); assignmentOne.setCommision(commisionTwo);
userTwo.claimGroup(assignmentOne.getGroup()); assignmentTwo.setCommision(commisionOne);
this.assignmentService.save(assignmentOne); userOne.removeGroup(assignmentOne.getGroup().getId());
this.assignmentService.save(assignmentTwo); userTwo.removeGroup(assignmentTwo.getGroup().getId());
this.userService.save(userOne); userOne.claimGroup(assignmentTwo.getGroup());
this.userService.save(userTwo); userTwo.claimGroup(assignmentOne.getGroup());
performedAssignmentExchanges.add(assignmentOne.getId()); this.assignmentService.save(assignmentOne);
performedAssignmentExchanges.add(assignmentTwo.getId()); this.assignmentService.save(assignmentTwo);
}
this.deleteExchange(m.getExchangeOne()); this.userService.save(userOne);
this.deleteExchange(m.getExchangeTwo()); 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(){ public List<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, exchangeTwo); return new MatchData(exchangeOne, exchangeTwo);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
final Set<MatchData> uniqData = new HashSet<>(matches); final Set<MatchData> uniqData = new HashSet<>(matches);
final List<MatchData> matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2)) final List<MatchData> matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2))
.collect(Collectors.toList()); .collect(Collectors.toList());
return matchDataListSorted; return matchDataListSorted;
} }
} }