r3160 - in branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen: bean services/impl
Author: tchemit Date: 2012-03-06 00:25:37 +0100 (Tue, 06 Mar 2012) New Revision: 3160 Url: http://chorem.org/repositories/revision/pollen/3160 Log: ajout methode pour cr?\195?\169er un sondage + ajout bean pour choix d'une image Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java 2012-03-05 23:25:37 UTC (rev 3160) @@ -0,0 +1,47 @@ +/* + * #%L + * Pollen :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.bean; + +import org.chorem.pollen.business.persistence.ChoiceImpl; + +/** + * Extends the basic choice to keep location of uploaded image + * (during poll creation). + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollImageChoice extends ChoiceImpl { + private static final long serialVersionUID = 1L; + + protected String location; + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} Property changes on: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/bean/PollImageChoice.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native 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-02 18:52:12 UTC (rev 3159) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-05 23:25:37 UTC (rev 3160) @@ -25,23 +25,30 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenConfiguration; import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.bean.PollImageChoice; import org.chorem.pollen.business.persistence.Choice; import org.chorem.pollen.business.persistence.ChoiceDAO; import org.chorem.pollen.business.persistence.Comment; import org.chorem.pollen.business.persistence.PersonToList; +import org.chorem.pollen.business.persistence.PersonToListDAO; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.business.persistence.PollAccountDAO; import org.chorem.pollen.business.persistence.PollDAO; +import org.chorem.pollen.business.persistence.PreventRule; +import org.chorem.pollen.business.persistence.PreventRuleDAO; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.Vote; import org.chorem.pollen.business.persistence.VoteDAO; import org.chorem.pollen.business.persistence.VoteToChoice; import org.chorem.pollen.business.persistence.VotingList; +import org.chorem.pollen.business.persistence.VotingListDAO; +import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.PollChoiceNotFoundException; import org.chorem.pollen.services.exceptions.PollNotFoundException; @@ -57,7 +64,14 @@ import org.nuiton.topia.framework.TopiaQuery; import org.nuiton.web.struts2.FilterPagerUtil; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; import java.util.List; public class PollService extends PollenServiceSupport { @@ -86,10 +100,11 @@ // Initialize creator as an admin for the poll PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); PollAccount creator = newInstance(pollAccountDAO); + creator.setAccountId(serviceContext.createPollenUrlId()); // creator.setAdmin(true); if (user != null) { // Link the creator with the user -// creator.setName(user.getDisplayName()); + creator.setVotingId(user.getDisplayName()); creator.setEmail(user.getEmail()); creator.setUserAccount(user); } @@ -100,6 +115,129 @@ return result; } + public Poll createPoll(Poll poll) { + + PollDAO pollDAO = getDAO(Poll.class); + Poll result = create(pollDAO); + + result.setAnonymous(poll.getAnonymous()); + result.setAnonymousVoteAllowed(poll.getAnonymousVoteAllowed()); + result.setBeginChoiceDate(poll.getBeginChoiceDate()); + result.setBeginDate(poll.getBeginDate()); + result.setChoiceType(poll.getChoiceType()); + result.setContinuousResults(poll.getContinuousResults()); + result.setDescription(poll.getDescription()); + result.setEndDate(poll.getEndDate()); + result.setEndChoiceDate(poll.getEndChoiceDate()); + result.setPollId(poll.getPollId()); + result.setPollType(poll.getPollType()); + result.setMaxChoiceNb(poll.getMaxChoiceNb()); + result.setPublicResults(poll.getPublicResults()); + result.setTitle(poll.getTitle()); + result.setVoteCountingType(poll.getVoteCountingType()); + + // create creator + PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); + PollAccount creatorCreated = create(pollAccountDAO); + PollAccount creator = poll.getCreator(); + creatorCreated.setVotingId(creator.getVotingId()); + creatorCreated.setEmail(creator.getEmail()); + creatorCreated.setUserAccount(creator.getUserAccount()); + creatorCreated.setAccountId(creator.getAccountId()); + + result.setCreator(creatorCreated); + + if (!poll.isPreventRuleEmpty()) { + + // create prevent rules + + PreventRuleDAO preventRuleDAO = getDAO(PreventRule.class); + for (PreventRule preventRule : poll.getPreventRule()) { + + PreventRule preventRuleCreated = create(preventRuleDAO); + preventRuleCreated.setActive(preventRule.getActive()); + preventRuleCreated.setMethod(preventRule.getMethod()); + preventRuleCreated.setOneTime(preventRule.getOneTime()); + preventRuleCreated.setRepeated(preventRule.getRepeated()); + preventRuleCreated.setScope(preventRule.getScope()); + preventRuleCreated.setSensibility(preventRule.getSensibility()); + result.addPreventRule(preventRule); + } + } + + if (!poll.isVotingListEmpty()) { + + // create voting list + + VotingListDAO votingListDAO = getDAO(VotingList.class); + PersonToListDAO personToListDAO = getDAO(PersonToList.class); + + for (VotingList votingList : poll.getVotingList()) { + VotingList votingListCreated = create(votingListDAO); + + votingListCreated.setName(votingList.getName()); + votingListCreated.setWeight(votingList.getWeight()); + + if (!votingList.isPollAccountPersonToListEmpty()) { + + // create poll account of the voting list + + for (PersonToList personToList : votingList.getPollAccountPersonToList()) { + + PersonToList personToListCreated = create(personToListDAO); + personToListCreated.setHasVoted(personToList.getHasVoted()); + personToListCreated.setWeight(personToList.getWeight()); + votingListCreated.addPollAccountPersonToList(personToListCreated); + + PollAccount pollAccount = personToList.getPollAccount(); + PollAccount pollAccountCreated = create(pollAccountDAO); + pollAccountCreated.setAccountId(pollAccount.getAccountId()); + pollAccountCreated.setEmail(pollAccount.getEmail()); + pollAccountCreated.setUserAccount(pollAccount.getUserAccount()); + pollAccountCreated.setVotingId(pollAccount.getVotingId()); + + personToListCreated.setPollAccount(pollAccountCreated); + } + } + result.addVotingList(votingListCreated); + } + } + + if (!poll.isChoiceEmpty()) { + + // create choices + + ChoiceType choiceType = result.getChoiceType(); + + ChoiceDAO choiceDAO = getDAO(Choice.class); + + for (Choice choice : poll.getChoice()) { + Choice choiceCreated = create(choiceDAO); + choiceCreated.setDescription(choice.getDescription()); + choiceCreated.setName(choice.getName()); + choiceCreated.setValidate(choice.getValidate()); + result.addChoice(choiceCreated); + + if (choiceType == ChoiceType.IMAGE) { + + // copy image where it belong and generate the thumb + + PollImageChoice imageChoice = (PollImageChoice) choice; + + try { + saveImages(result, imageChoice); + } catch (IOException e) { + throw new PollenTechnicalException( + "Could not create image choice", e); + } + + + } + } + } + + return result; + } /* private void addFeedEntry() { PollAccountDTO creator = servicePollAccount.findPollAccountById(poll.getCreatorId()); @@ -271,660 +409,6 @@ } - /* - @Override - public List<PollDTO> findParticipatedPolls(String userId) { - TopiaContext transaction = null; - List<PollDTO> results = null; - UserAccountDAO userDAO = null; - try { - transaction = rootContext.beginTransaction(); - - userDAO = PollenModelDAOHelper.getUserAccountDAO(transaction); - UserAccount user = userDAO.findByTopiaId(userId); - - Set<Poll> pollEntities = new HashSet<Poll>(); - for (PollAccount pollAccount : user.getPollAccount()) { - for (Vote vote : pollAccount.getVote()) { - pollEntities.add(vote.getPoll()); - } - } - - converter.setTransaction(transaction); - results = converter - .createPollDTOs(new ArrayList<Poll>(pollEntities)); - - transaction.commitTransaction(); - - if (log.isDebugEnabled()) { - log.debug("Entities found: " - + ((results == null) ? "null" : results.size())); - } - - return results; - } catch (Exception e) { - ContextUtil.doCatch(e, transaction); - return null; - } finally { - ContextUtil.doFinally(transaction); - } - } - - @Override - public List<PollDTO> findInvitedPolls(String userId) { - TopiaContext transaction = null; - List<PollDTO> results = null; - UserAccountDAO userDAO = null; - try { - transaction = rootContext.beginTransaction(); - - userDAO = PollenModelDAOHelper.getUserAccountDAO(transaction); - UserAccount user = userDAO.findByTopiaId(userId); - - pollDAO = PollenModelDAOHelper.getPollDAO(transaction); - List<Poll> polls = pollDAO.findAll(); - - Set<Poll> pollEntities = new HashSet<Poll>(); - for (Poll poll : polls) { - for (VotingList votingList : poll.getVotingList()) { - for (PersonToList personToList : votingList - .getPollAccountPersonToList()) { - if (!personToList.getHasVoted()) { - PollAccount pollAccount = personToList - .getPollAccount(); - if (pollAccount != null - && pollAccount.getEmail() != null - && pollAccount.getEmail().equals( - user.getEmail())) { - pollEntities.add(poll); - } - } - } - } - } - - converter.setTransaction(transaction); - results = converter - .createPollDTOs(new ArrayList<Poll>(pollEntities)); - - transaction.commitTransaction(); - - if (log.isDebugEnabled()) { - log.debug("Entities found: " - + ((results == null) ? "null" : results.size())); - } - - return results; - } catch (Exception e) { - ContextUtil.doCatch(e, transaction); - return null; - } finally { - ContextUtil.doFinally(transaction); - } - } - */ - - -// -// /** -// * Build a new Poll instance based on existing {@code poll} (copy) -// * -// * @param poll Build a new Poll instance based on existing {@code poll} (copy) -// * @return Build a new Poll instance based on existing {@code poll} (copy) -// */ -// public Poll getNewPoll(Poll poll) { -// Poll result = new PollImpl(); -// TopiaEntityBinder<Poll> binder = PollenBinderHelper.getSimpleTopiaBinder(Poll.class); -// //TODO-fdesbois-2010-07-13 : add test and check if exluding only those properties is realistic -// binder.copyExcluding(poll, result, -// Poll.PROPERTY_POLL_ID, -// Poll.PROPERTY_CLOSED, -// Poll.PROPERTY_RESULT, -// Poll.PROPERTY_POLL_ACCOUNT -// ); -// // Copy of participants only for no free poll -// if (poll.getType().isRestrictedOrGroup()) { -// result.setPollAccount(poll.getPollAccount()); -// } -// return result; -// } -// -// public Poll createPoll(Poll poll, -// Collection<ParticipantList> lists) throws PollAlreadyExistException { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// if (dao.existByNaturalId(poll.getUid())) { -// throw new PollAlreadyExistException(); -// } -// -// // Create newPoll and copy simple properties from poll source -// Poll newPoll = createBasicPoll(poll); -// -// // Create accounts only for non free poll -// if (!newPoll.getType().isFree()) { -// // Create accounts depends on pollType and lists argument -// List<PollAccount> accounts = -// createPollAccounts(newPoll.getType(), lists); -// newPoll.setPollAccount(accounts); -// } -// -// // Create newPoll choices -// List<Choice> choices = createPollChoices(poll.getChoice()); -// -// newPoll.setChoice(choices); -// -// dao.update(newPoll); -// -// return newPoll; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Poll updatePoll(Poll poll) throws PollAlreadyClosedException { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// // Updating a Poll : -// // - TITLE -// // - DESCRIPTION -// // - BEGIN/END DATE -// // - CHOICE_ADD_ALLOWED -// // - BEGIN/END ADD CHOICE DATE -// // - MAX_CHOICE_NB -// // - CONTINUOUS_RESULTS -// // - PUBLIC_RESULTS -// // - ANONYMOUS_VOTE_ALLOWED -// -// // Anonymous change is not supported (need to manage accounts) -// // PollType change is not supported (need to manage accounts) -// // VoteCounting change is not supported (need to manage existing votes) -// -// // Comments, Results, Choices and Accounts will not be updated here -// -// // Update only for not started poll : -// // - VOTE_COUNTING -// // - CHOICE_TYPE -// // - CHOICES -// -// Poll pollToUpdate = dao.findByTopiaId(poll.getTopiaId()); -// -// // Can't update a closed poll -// if (pollToUpdate.isClosed()) { -// throw new PollAlreadyClosedException(); -// } -// -// TopiaEntityBinder<Poll> binder = -// PollenBinderHelper.getSimpleTopiaBinder(Poll.class); -// -// // Idea, if we can use Embeddable object for Hibernate, Options will be a good use case -// binder.copy(poll, pollToUpdate, -// Poll.PROPERTY_TITLE, -// Poll.PROPERTY_DESCRIPTION, -// Poll.PROPERTY_BEGIN_DATE, -// Poll.PROPERTY_END_DATE, -// Poll.PROPERTY_ANONYMOUS_VOTE_ALLOWED, -// Poll.PROPERTY_CHOICE_ADD_ALLOWED, -// Poll.PROPERTY_BEGIN_CHOICE_DATE, -// Poll.PROPERTY_END_CHOICE_DATE, -// Poll.PROPERTY_CONTINUOUS_RESULTS, -// Poll.PROPERTY_PUBLIC_RESULTS, -// Poll.PROPERTY_MAX_CHOICE_NB -// ); -// -// // if not started -// if (!pollToUpdate.isStarted()) { -// -// log.debug("Poll is not started, update voteCounting and choices"); -// -// // Update voteCounting is simple when poll have no vote -// pollToUpdate.setVoteCounting(poll.getVoteCounting()); -// -// // Override choices if type has changed, otherwise the choice entity will automatically be updated -// if (!pollToUpdate.getChoiceType().equals(poll.getChoiceType())) { -// pollToUpdate.setChoiceType(poll.getChoiceType()); -// -// ChoiceDAO choiceDAO = PollenDAOHelper.getChoiceDAO(getTransaction()); -// // Clear previous choices, will properly delete orphans -// pollToUpdate.getChoice().clear(); -// // Create new choices and add them to the poll -// for (Choice choice : poll.getChoice()) { -// Choice newChoice = pollToUpdate.addNewChoice(choice.getName(), choice.getDescription()); -// choiceDAO.create(newChoice); -// } -// } -// } -// -// // If continuous results is now activated, execute a voteCounting -// if (!pollToUpdate.isContinuousResults() && -// poll.isContinuousResults()) { -// // TODO-fdesbois-2011-07-13 : execute vote counting -// } -// -// Poll result = dao.update(pollToUpdate); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void deletePoll(Poll poll) { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// dao.delete(poll); -// //FIXME tchemit-20120124 Remove any other sotrages relatives to this poll (images choices for example). -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void deleteChoice(Choice choice) { -// try { -// ChoiceDAO dao = PollenDAOHelper.getChoiceDAO(getTransaction()); -// dao.delete(choice); -// //FIXME tchemit-20120124 Remove any other sotrages relatives to this poll (images choices for example). -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// //FIXME tchemit-20120123 Replace the filter by a simple PagerBean -// public List<UserPoll> getPolls(EntityFilter filter) { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// PollenQueryHelper.PollAccountProperty participantProperty = -// PollenQueryHelper.newPollAccountChildProperty(); -// -// // Filter can be apply on userAccount as reference -// TopiaQuery query = dao.createQueryFindAllByUser(filter); -// -// query.addSelect(participantProperty.$alias()); -// -// log.debug("Query : " + query); -// -// List<UserPoll> results = retrieveUserPolls(query); -// -// return results; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// //FIXME tchemit-20120123 Replace the filter by a simple PagerBean -// public List<Comment> getComments(EntityFilter filter) { -// try { -// CommentDAO dao = PollenDAOHelper.getCommentDAO(getTransaction()); -// -//// String pollAlias = PollenQueryHelper.ALIAS_POLL; -//// String pollCommentProperty = -//// TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_COMMENT); -//// String commentAlias = PollenQueryHelper.ALIAS_COMMENT; -// -// PollenQueryHelper.PollProperty pollProperty = -// PollenQueryHelper.newPollProperty(); -// PollenQueryHelper.CommentProperty commentProperty = -// PollenQueryHelper.newCommentProperty(); -// -// filter.checkReference(Poll.class, true); -// filter.setReferenceProperty(pollProperty.$alias()); -// -// TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). -// setSelect(commentProperty.$alias()). -// addJoin(pollProperty.comment(), commentProperty.$alias(), false). -// addFilter(filter); -// -// List<Comment> comments = dao.findAllByQuery(query); -// return comments; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public ParticipantList getNewPollList() { -// PollAccount result = new PollAccountImpl(); -// result.setList(true); -// result.setChild(new ArrayList<PollAccount>()); -// return result; -// } -// -// public Participant getNewPollParticipant() { -// PollAccount result = new PollAccountImpl(); -// result.setWeight(1.); -// return result; -// } -// -// //FIXME tchemit-20120123 Remove the fetch system we don't need it with our transaction management -// public Poll getPoll(String pollUid, String... properties) throws PollNotFoundException { -// try { -// // Property is used to control the alias and dependencies inside the query -// PollenQueryHelper.PollProperty pollProperty = -// PollenQueryHelper.newPollProperty(); -// -// // Need to control the alias for fetching, add it to each property -// String[] fetchProperties = new String[properties.length]; -// for (int i = 0; i < properties.length; i++) { -// // This will clean properties for fetching -// fetchProperties[i] = pollProperty.$property(properties[i]); -// } -// -// // Create the query using default alias for Poll -// TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). -// addFetch(fetchProperties). -// addEquals(pollProperty.uid(), pollUid); -// -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// Poll result = dao.findByQuery(query); -// if (result == null) { -// throw new PollNotFoundException(); -// } -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// //FIXME tchemit-20120123 Should check userAccount and pollUid exist ? -// public boolean canAdminPoll(String accountUid, String pollUid) { -// try { -// PollenQueryHelper.PollProperty pollProperty = -// PollenQueryHelper.newPollProperty(); -// -// TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). -// addEquals(pollProperty.uid(), pollUid). -// addEquals(pollProperty.creatorProperty().uid(), accountUid); -// -// -// // Note a boolean admin exist in PollAccount, maybe -// // find the account in lists -// -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// boolean result = dao.existByQuery(query); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public int getNbTotalPolls() { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// Long result = dao.count(); -// return result.intValue(); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// //FIXME tchemit-20120123 Remove this from dao, remove EntityFilter... -// public int getNbPollsByUser(UserAccount user) { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// EntityFilter filter = new TopiaFilter(); -// filter.setReference(user); -// TopiaQuery query = dao.createQueryFindAllByUser(filter); -// -// int result = dao.countByQuery(query); -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public List<Poll> getRunningPolls() { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// // isRunning : isStarted() && !isFinished() && !isClosed() -// -// Date now = serviceContext.getCurrentTime(); -// -// TopiaQuery query = dao.createQuery(). -// addWhere(Poll.PROPERTY_BEGIN_DATE, TopiaQuery.Op.LE, now). -// addWhere(Poll.PROPERTY_END_DATE, TopiaQuery.Op.GE, now). -// addEquals(Poll.PROPERTY_CLOSED, Boolean.FALSE). -// addOrderDesc(Poll.TOPIA_CREATE_DATE); -// -// List<Poll> result = dao.findAllByQuery(query); -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Choice createChoice(Poll poll, Choice choice) { -// try { -// ChoiceDAO dao = PollenDAOHelper.getChoiceDAO(getTransaction()); -// -// Choice choiceCreated = dao.create( -// Choice.PROPERTY_NAME, choice.getName(), -// Choice.PROPERTY_DESCRIPTION, choice.getDescription(), -// Choice.PROPERTY_TYPE, choice.getType() -// ); -// -// PollDAO pollDAO = PollenDAOHelper.getPollDAO(getTransaction()); -// -// Poll pollToUpdate = pollDAO.findByTopiaId(poll.getTopiaId()); -// -// pollToUpdate.addChoice(choiceCreated); -// -// pollDAO.update(pollToUpdate); -// -// return choiceCreated; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Choice getNewChoice(Poll poll) { -// Choice result = new ChoiceImpl(); -// result.setType(poll.getChoiceType()); -// return result; -// } -// -// public void setPollClosed(Poll poll, boolean closed) throws Exception { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// Poll pollToUpdate = dao.findByTopiaId(poll.getTopiaId()); -// pollToUpdate.setClosed(closed); -// -// dao.update(pollToUpdate); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Poll createBasicPoll(Poll source) -// throws TopiaException { -// try { -// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); -// -// PollAccountDAO accountDAO = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// // Create newPoll and copy simple properties from poll source -// Poll result = dao.create(source.getUid()); -// -// // Copy all properties instead of Uid (already set at creation), -// // Closed (not used in this case) and Creator (managed separately) -// PollenBinderHelper.getSimpleTopiaBinder(Poll.class). -// copyExcluding(source, result, -// Poll.PROPERTY_UID, -// Poll.PROPERTY_CLOSED, -// Poll.PROPERTY_CREATOR); -// -// // Create creator and set him to the poll -// PollAccount creator = accountDAO.create( -// serviceContext.createPollenUrlId()); -// -// PollenBinderHelper.getSimpleTopiaBinder(PollAccount.class). -// copy(source.getCreator(), creator, -// PollAccount.PROPERTY_NAME, -// PollAccount.PROPERTY_EMAIL, -//// PollAccount.WEIGHT, -// PollAccount.PROPERTY_ADMIN, -// PollAccount.PROPERTY_USER_ACCOUNT); -// accountDAO.update(creator); -// -// result.setCreator(creator); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void validatePoll(Poll poll) { -// -// Date currentTime = serviceContext.getCurrentTime(); -// -// if (poll.getBeginDate() == null) { -// poll.setBeginDate(currentTime); -// } -// -// if (poll.getEndDate() != null) { -// Preconditions.checkArgument(poll.getEndDate().before(poll.getBeginDate())); -// } -// -// // Add choices if allowed after creation -// if (poll.isChoiceAddAllowed()) { -// -// // Initialize beginChoiceDate if needed -// if (poll.getBeginChoiceDate() == null) { -// poll.setBeginChoiceDate(currentTime); -// } -// -// // Check beginChoiceDate -// if (poll.getEndDate() != null) { -// Preconditions.checkArgument(poll.getBeginChoiceDate().before(poll.getEndDate())); -// } -// -// // Check endChoiceDate -// if (poll.getEndChoiceDate() != null) { -// Preconditions.checkArgument(poll.getEndChoiceDate().after(poll.getBeginChoiceDate())); -// } -// } -// } -// -// protected List<PollAccount> createPollAccounts(PollType type, -// Collection<ParticipantList> lists) -// throws TopiaException, IllegalArgumentException { -// -// List<PollAccount> results = new ArrayList<PollAccount>(); -// -// PollAccountDAO accountDAO = -// PollenDAOHelper.getPollAccountDAO(getTransaction()); -// -// switch (type) { -// case RESTRICTED: -// if (CollectionUtils.isEmpty(lists)) { -// throw new IllegalArgumentException("lists can't be empty" + -// " for restricted poll"); -// } -// // Create accounts only on first participant list -// results = createPersonAccounts(accountDAO, -// lists.iterator().next()); -// break; -// case GROUP: -// if (CollectionUtils.isEmpty(lists)) { -// throw new IllegalArgumentException("lists can't be empty" + -// " for group poll"); -// } -// -// for (ParticipantList list : lists) { -// // Create the accountList -// PollAccount accountList = accountDAO.create( -// serviceContext.createPollenUrlId()); -// accountList.setList(true); -// accountList.setName(list.getName()); -// -// // Create account children -// List<PollAccount> accounts = -// createPersonAccounts(accountDAO, list); -// accountList.setChild(accounts); -// -// // Add the accountList to the results -// results.add(accountList); -// } -// } -// return results; -// } -// -// protected List<PollAccount> createPersonAccounts(PollAccountDAO dao, -// ParticipantList list) -// throws TopiaException { -// -// List<PollAccount> results = new ArrayList<PollAccount>(); -// -// for (Participant participant : list.getParticipants()) { -// String accountUid = serviceContext.createPollenUrlId(); -// PollAccount account = dao.create(accountUid); -// account.setName(participant.getName()); -// account.setEmail(participant.getEmail()); -// account.setWeight(participant.getWeight()); -// dao.update(account); -// results.add(account); -// } -// return results; -// } -// -// protected List<Choice> createPollChoices(List<Choice> source) -// throws TopiaException { -// -// ChoiceDAO dao = PollenDAOHelper.getChoiceDAO(getTransaction()); -// -// List<Choice> results = new ArrayList<Choice>(); -// -// for (Choice choice : source) { -// Choice newChoice = dao.create(); -// PollenBinderHelper.getSimpleTopiaBinder(Choice.class). -// copy(choice, newChoice); -// results.add(newChoice); -// } -// return results; -// } -// -// //FIXME tchemit-20120123 : help me to destroy this :( -// protected List<UserPoll> retrieveUserPolls(TopiaQuery query) -// throws TopiaException { -// -// List<UserPoll> results = new ArrayList<UserPoll>(); -// -// Map<String, UserPoll> pollsCache = -// new HashMap<String, UserPoll>(); -// -// -// List<Object[]> queryResults = getTransaction().findByQuery(query); -// for (Object[] row : queryResults) { -// Poll poll = (Poll) row[0]; -// PollAccount account = (PollAccount) row[1]; -// if (log.isDebugEnabled()) { -// log.debug("account : " + account.getUid() + " : " + -// account.getName() + -// " list(" + account.isList() + ")" + -// " admin(" + account.isAdmin() + ")" + -// " for poll " + poll.getTitle()); -// } -// -// UserPoll userPoll = pollsCache.get(poll.getTopiaId()); -// -// if (userPoll == null) { -// userPoll = new UserPoll(); -// userPoll.setPoll(poll); -// results.add(userPoll); -// pollsCache.put(poll.getTopiaId(), userPoll); -// } -// -// userPoll.addAccount(account); -// } -// -// return results; -// } - public Poll getPollByPollId(String pollId) { Preconditions.checkNotNull(pollId); @@ -1132,12 +616,65 @@ File imageDirectory = getConfiguration().getImageDirectory(); File pollDirectory = new File(imageDirectory, pollId); - if (thumb) { - choiceId = "thumb_" + choiceId; + choiceId = THUMB_PREFIX + choiceId; } File result = new File(pollDirectory, choiceId); return result; } + + public static final String THUMB_PREFIX = "thumb_"; + + + /** + * Sauvegarde des images d'un choix de type image. + * + * @param poll le sondage contenant le choix + * @param choice le choix à sauvegarder. + * @throws IOException si un problème IO lors de la copie ou génération de la miniature + */ + public void saveImages(Poll poll, PollImageChoice choice) throws IOException { + + String location = choice.getLocation(); + + File pollChoiceImage = getPollChoiceImageFile(poll.getPollId(), choice.getName(), false); + File pollChoiceImageThumb = getPollChoiceImageFile(poll.getPollId(), choice.getName(), true); + + FileUtils.copyFile(new File(location), pollChoiceImage); + createThumbnail(pollChoiceImage, pollChoiceImageThumb, 100); + } + + /** + * Création d'une miniature. + * + * @param img L'image à miniaturiser. + * @param thumbCopied le chemin complet vers la miniature. + * @param width La largeur de la miniature. + * @throws IOException if could not create thumb image + */ + protected static void createThumbnail(File img, + File thumbCopied, + int width) throws IOException { + + ImageIcon ii = new ImageIcon(img.getAbsolutePath()); + Image image = ii.getImage(); + double imageRatio = (double) image.getHeight(null) + / (double) image.getWidth(null); + int height = (int) (width * imageRatio); + + BufferedImage thumbImage = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + Graphics2D graphics2D = thumbImage.createGraphics(); + graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics2D.drawImage(image, 0, 0, width, height, null); + + ImageIO.write(thumbImage, "jpg", thumbCopied); + + if (log.isDebugEnabled()) { + log.debug("Thumbnail created: " + thumbCopied.getName() + " (size=" + + thumbCopied.length() + ")"); + } + } }
participants (1)
-
tchemit@users.chorem.org