Author: fdesbois Date: 2012-04-12 12:32:25 +0200 (Thu, 12 Apr 2012) New Revision: 3251 Url: http://chorem.org/repositories/revision/pollen/3251 Log: send email to person on restricted poll update (if email change) Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-04-12 10:32:21 UTC (rev 3250) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-04-12 10:32:25 UTC (rev 3251) @@ -118,6 +118,14 @@ } } + protected <T extends TopiaEntity, D extends TopiaDAO<? super T>> T update(D dao, T entity) { + try { + return (T) dao.update(entity); + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not update entity ", e); + } + } + protected TopiaContext getTransaction() { return serviceContext.getTransaction(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-04-12 10:32:21 UTC (rev 3250) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-04-12 10:32:25 UTC (rev 3251) @@ -113,7 +113,6 @@ String pollTitle = poll.getTitle(); // Mail au créateur - if (StringUtils.isNotEmpty(poll.getCreator().getEmail())) { String subject = l_(locale, "pollen.email.creatorEmail.subject", pollTitle); @@ -135,16 +134,10 @@ List<PollEmail> emails = Lists.newArrayList(); for (PollAccount account : votingPollAccounts) { - String accountId = account.getAccountId(); - PollUrl accountVoteURL = PollUrl.newPollUrl(voteURL, accountId); - String content = - l_(locale, "pollen.email.votingEmail.content", - pollTitle, accountId, accountVoteURL); + PollEmail pollEmail = + createVotingEmail(locale, pollTitle, account, voteURL); - PollEmail pollEmail = createPollEmail( - account.getEmail(), subject, content); - emails.add(pollEmail); } @@ -152,7 +145,45 @@ } } } + + public void onRestrictedPersonAdded(Poll poll, PollAccount account, PollUrl voteURL) { + URL url = serviceContext.getApplicationURL(); + + if (url != null) { + + Locale locale = getLocale(); + + String pollTitle = poll.getTitle(); + + PollEmail pollEmail = + createVotingEmail(locale, pollTitle, account, voteURL); + + sendEmail(pollEmail); + } + } + + private PollEmail createVotingEmail(Locale locale, + String pollTitle, + PollAccount account, + PollUrl voteURL) { + + String subject = l_(locale, "pollen.email.votingEmail.subject", + pollTitle); + + PollUrl accountVoteURL = + PollUrl.newPollUrl(voteURL, account.getAccountId()); + + String content = + l_(locale, "pollen.email.votingEmail.content", + pollTitle, account.getVotingId(), accountVoteURL); + + PollEmail result = createPollEmail( + account.getEmail(), subject, content); + + return result; + } + public void onPollReminder(Poll poll, PollUrl voteURL) { URL url = serviceContext.getApplicationURL(); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-12 10:32:21 UTC (rev 3250) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-04-12 10:32:25 UTC (rev 3251) @@ -23,6 +23,7 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; @@ -291,7 +292,9 @@ // -- VotingList -- // try { for (VotingList votingList : poll.getVotingList()) { - saveVotingList(result, votingList); + // Do not set emailService, all emails will be send during + // onPollCreated + saveVotingList(result, votingList, null); } } catch (TopiaException e) { throw new PollenTechnicalException("Can't save votingLists", e); @@ -353,10 +356,11 @@ // -- VotingLists -- // if (!voteStarted) { + EmailService emailService = newService(EmailService.class); pollToUpdate.clearVotingList(); try { for (VotingList votingList : poll.getVotingList()) { - saveVotingList(pollToUpdate, votingList); + saveVotingList(pollToUpdate, votingList, emailService); } } catch (TopiaException e) { throw new PollenTechnicalException("Can't save votingLists", e); @@ -926,7 +930,7 @@ return result; } - protected void saveVotingList(Poll poll, VotingList votingList) throws TopiaException { + protected void saveVotingList(Poll poll, VotingList votingList, EmailService emailService) throws TopiaException { VotingListDAO votingListDAO = getDAO(VotingList.class); PersonToListDAO personToListDAO = getDAO(PersonToList.class); @@ -947,6 +951,8 @@ result.setName(votingList.getName()); result.setWeight(votingList.getWeight()); + + PollUrl voteURL = getPollVoteUrl(poll); // Merge PersonToList List<PersonToList> personToListsUpdated = Lists.newArrayList(); @@ -974,7 +980,6 @@ pollAccountLoaded = create(pollAccountDAO); pollAccountLoaded.setAccountId(accountId); } - personToListLoaded.setPollAccount(pollAccountLoaded); } else { personToListLoaded = getEntityById(PersonToList.class, personToList.getTopiaId()); @@ -983,8 +988,22 @@ personToListsUpdated.add(personToListLoaded); personToListLoaded.setWeight(personToList.getWeight()); + pollAccountLoaded.setVotingId(pollAccount.getVotingId()); + + boolean emailChanged = !Objects.equal(pollAccountLoaded.getEmail(), + pollAccount.getEmail()); + pollAccountLoaded.setEmail(pollAccount.getEmail()); - pollAccountLoaded.setVotingId(pollAccount.getVotingId()); + + // Send email to the person if service is defined and email has changed + if (emailService != null && emailChanged) { + emailService.onRestrictedPersonAdded(poll, pollAccountLoaded, voteURL); + } + + // Update the account (otherwise a saving error will occurs if + // already exists on delete cascade) + PollAccount pollAccountUpdated = update(pollAccountDAO, pollAccountLoaded); + personToListLoaded.setPollAccount(pollAccountUpdated); } result.setPollAccountPersonToList(personToListsUpdated); }