Author: fdesbois Date: 2012-03-27 13:28:36 +0200 (Tue, 27 Mar 2012) New Revision: 3210 Url: http://chorem.org/repositories/revision/pollen/3210 Log: - Move isUpdateVoteAllowed to VoteService - Add getPollAccount method in PollService 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/VoteService.java 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-03-26 17:02:58 UTC (rev 3209) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-27 11:28:36 UTC (rev 3210) @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenConfiguration; @@ -381,24 +382,66 @@ } } + /** + * Retrieve a pollAccount with {@code accountId}. This pollAccount will be + * attached to the given {@code userAccount} (only if not already attached). + * If the {@code accountId} is undefined, a new instance of PollAccount will + * be retrieved, otherwise the existing PollAccount is used. No create or + * update is done here. + * + * @param accountId Id of the existing account (optional) + * @param userAccount UserAccount where account will be attached (optional) + * @return the existing PollAccount or a new instance + * @throws PollAccountNotFound if accountId is defined and doesn't match any + * existing PollAccount + */ + public PollAccount getPollAccount(String accountId, UserAccount userAccount) throws PollAccountNotFound { + PollAccount result; + if (StringUtils.isNotEmpty(accountId)) { + result = getPollAccountByAccountId(accountId); + + // REMARQUES : + // - pas le droit de modif si le pollAccount est rattaché a un userAccount et qu'on est pas loggé ?!? + // - Ce serait plus simple que l'Admin ne puisse jamais voter, il ne peut que modérer les votes + + // Don't remove or update userAccount link if already set + if (userAccount != null && result.getUserAccount() == null) { + result.setUserAccount(userAccount); + + if (log.isDebugEnabled()) { + log.debug(String.format("Attach User '%s' [%s] to the existing Account [%s]", + userAccount.getDisplayName(), + userAccount.getTopiaId(), + accountId + )); + } + } + + } else { + result = getNewPollAccount(userAccount); + } + return result; + } + public PollAccount getNewPollAccount(UserAccount userAccount) { PollAccountDAO dao = getDAO(PollAccount.class); PollAccount result = newInstance(dao); result.setAccountId(serviceContext.createPollenUrlId()); - if (userAccount != null) { - String displayName = userAccount.getDisplayName(); - result.setVotingId(displayName); - } else { - result.setVotingId(""); - } + String votingId = userAccount != null ? userAccount.getDisplayName() : ""; + result.setVotingId(votingId); result.setUserAccount(userAccount); return result; } - public PollAccount getPollAccountByAccountId(String accountId) { + public PollAccount getPollAccountByAccountId(String accountId) throws PollAccountNotFound { try { PollAccountDAO dao = getDAO(PollAccount.class); PollAccount result = dao.findByAccountId(accountId); + + if (result == null) { + throw new PollAccountNotFound(); + } + return result; } catch (TopiaException e) { throw new PollenTechnicalException("Could not botain account with this id", e); @@ -424,10 +467,6 @@ PollAccount account = getPollAccountByAccountId(accountId); - if (account == null) { - throw new PollAccountNotFound(); - } - if (!account.getAccountId().equals(poll.getCreator().getAccountId())) { throw new UnauthorizedPollAccessException(); } @@ -458,10 +497,6 @@ PollAccount account = getPollAccountByAccountId(accountId); - if (account == null) { - throw new PollAccountNotFound(); - } - if (!account.getAccountId().equals(poll.getCreator().getAccountId())) { throw new UnauthorizedPollAccessException(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-26 17:02:58 UTC (rev 3209) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-27 11:28:36 UTC (rev 3210) @@ -50,6 +50,7 @@ import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import java.net.URL; +import java.util.Date; import java.util.List; import java.util.Map; @@ -303,7 +304,34 @@ throw new PollenTechnicalException("Could not obtain votes", e); } } + + public boolean isUpdateAllowed(Poll poll, String voteId, String accountId, UserAccount userConnected) { + + Date now = serviceContext.getCurrentTime(); + + boolean result = false; + Vote vote = poll.getVoteByTopiaId(voteId); + + // can only modify a vote if poll is running. + if (vote != null && poll.isRunning(now)) { + PollAccount votePollAccount = vote.getPollAccount(); + + // si le votant du vote correspond au votant actuel (pollAccountId) + if (accountId != null + && accountId.equals(votePollAccount.getAccountId())) { + result = true; + } + + // si l'utilisateur du vote correspond à l'utilisateur actuel (user) + if (userConnected != null) { + UserAccount voteUserAccount = votePollAccount.getUserAccount(); + result = userConnected.equals(voteUserAccount); + } + } + return result; + } + public boolean hasAlreadyVoted(String votingId, Poll poll) { try { VoteDAO dao = getDAO(Vote.class);