r3180 - in branches/pollen-1.2.6-struts2/pollen-persistence/src/main: java/org/chorem/pollen/business/persistence java/org/chorem/pollen/entities/migration xmi
Author: tchemit Date: 2012-03-14 15:39:03 +0100 (Wed, 14 Mar 2012) New Revision: 3180 Url: http://chorem.org/repositories/revision/pollen/3180 Log: move all query from services to persistence layer add foreign key indexes Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PersonListDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/UserAccountDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.properties branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -0,0 +1,67 @@ +/* + * #%L + * Pollen :: Persistence + * + * $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.business.persistence; + +import com.google.common.base.Preconditions; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; + +import java.util.List; + +public class CommentDAOImpl<E extends Comment> extends CommentDAOAbstract<E> { + + public List<E> getComments(TopiaFilterPagerUtil.FilterPagerBean pager, + String pollId) throws TopiaException { + + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(pollId); + + TopiaQuery countQuery = createQuery("e"). + addWhere("e." + Comment.PROPERTY_POLL + ".pollId", + TopiaQuery.Op.EQ, pollId); + long records = countByQuery(countQuery); + pager.setRecords((int) records); + + TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, + pager); + List<E> result = findAllByQuery(query); + return result; + + } + + public List<E> getAllComments(String pollId) throws TopiaException { + + Preconditions.checkNotNull(pollId); + + TopiaQuery query = createQuery("e"). + addWhere("e." + Comment.PROPERTY_POLL + ".pollId", + TopiaQuery.Op.EQ, pollId); + query.addOrderDesc(Comment.PROPERTY_POST_DATE); + List<E> result = findAllByQuery(query); + return result; + + } + +} //CommentDAOImpl<E extends Comment> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/CommentDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PersonListDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PersonListDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PersonListDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -0,0 +1,84 @@ +/* + * #%L + * Pollen :: Persistence + * + * $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.business.persistence; + +import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; + +import java.util.List; + +public class PersonListDAOImpl<E extends PersonList> extends PersonListDAOAbstract<E> { + + public List<E> getFavoriteLists(UserAccount user, + TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { + + Preconditions.checkNotNull(user); + + TopiaQuery countQuery = createQuery("e"); + countQuery.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); + long records = countByQuery(countQuery); + + pager.setRecords((int) records); + + TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); + + List<E> result = findAllByQuery(query); + return result; + } + + public List<E> getFavoriteLists(UserAccount user) throws TopiaException { + + Preconditions.checkNotNull(user); + + TopiaQuery query = createQuery("e"); + query.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); + List<E> result = findAllByQuery(query); + return result; + + } + + public boolean isPersonListExist(UserAccount user, String name) throws TopiaException { + + Preconditions.checkNotNull(user); + Preconditions.checkNotNull(name); + + // can't accept favorite list without name + Preconditions.checkArgument(StringUtils.isNotEmpty(name)); + + + // check list does not already exists + + TopiaQuery query = createQuery("e"); + + query.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); + query.addWhere("e." + PersonList.PROPERTY_NAME, TopiaQuery.Op.EQ, name); + + boolean exist = existByQuery(query); + return exist; + } + +} //PersonListDAOImpl<E extends PersonList> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PersonListDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java 2012-03-14 09:42:22 UTC (rev 3179) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollAccountDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -23,13 +23,17 @@ */ package org.chorem.pollen.business.persistence; +import com.google.common.base.Preconditions; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; +import java.util.List; + public class PollAccountDAOImpl<E extends PollAccount> extends PollAccountDAOAbstract<E> { - public PollAccount getRestrictedPollAccount(String pollId, - String accountId) throws TopiaException { + public E getRestrictedPollAccount(String pollId, + String accountId) throws TopiaException { // List<PersonToList> tmp = transaction.find( // "FROM " + PersonToList.class.getName() + @@ -38,11 +42,60 @@ // "accountUId", accountId, // "pollUId", pollId); - TopiaQuery query = new TopiaQuery(PersonToList.class,"p"). - setSelect("p."+PersonToList.PROPERTY_POLL_ACCOUNT). - addWhere("p."+PersonToList.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_ACCOUNT_ID, TopiaQuery.Op.EQ, accountId). - addWhere("p."+PersonToList.PROPERTY_VOTING_LIST + "." + VotingList.PROPERTY_POLL + "." + Poll.PROPERTY_POLL_ID, TopiaQuery.Op.EQ, pollId); - PollAccount result = findByQuery(query); + TopiaQuery query = new TopiaQuery(PersonToList.class, "p"). + setSelect("p." + PersonToList.PROPERTY_POLL_ACCOUNT). + addWhere("p." + PersonToList.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_ACCOUNT_ID, TopiaQuery.Op.EQ, accountId). + addWhere("p." + PersonToList.PROPERTY_VOTING_LIST + "." + VotingList.PROPERTY_POLL + "." + Poll.PROPERTY_POLL_ID, TopiaQuery.Op.EQ, pollId); + E result = findByQuery(query); return result; } + + public List<E> getFavoriteListUsers(PersonList favoriteList, + TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { + + Preconditions.checkNotNull(favoriteList); + Preconditions.checkNotNull(pager); + + + int records = favoriteList.sizePollAccount(); + pager.setRecords(records); + + TopiaQuery query = createQuery("e"); + TopiaFilterPagerUtil.addPagerToQuery(query, pager); + query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, favoriteList); + + List<E> result = findAllByQuery(query); + return result; + } + + public boolean isPollAccountExist(PersonList personListToUpdate, + PollAccount pollAccount) throws TopiaException { + + Preconditions.checkNotNull(personListToUpdate); + Preconditions.checkNotNull(pollAccount); + + // check there is other poll account in this list with same id + TopiaQuery query = createQuery("e"); + query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, personListToUpdate); + query.addWhere("e." + PollAccount.PROPERTY_VOTING_ID, TopiaQuery.Op.EQ, pollAccount.getVotingId()); + boolean pollAccountExists = existByQuery(query); + return pollAccountExists; + } + + public boolean isPollAccountAlreadyExist(PersonList personListToUpdate, + PollAccount pollAccount) throws TopiaException { + + Preconditions.checkNotNull(personListToUpdate); + Preconditions.checkNotNull(pollAccount); + + // check there is another poll account in this list with same id + TopiaQuery query = createQuery("e"); + query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, personListToUpdate); + query.addWhere("e." + PollAccount.PROPERTY_VOTING_ID, TopiaQuery.Op.EQ, pollAccount.getVotingId()); + query.addWhere("e." + PollAccount.TOPIA_ID, TopiaQuery.Op.NEQ, pollAccount.getTopiaId()); + boolean pollAccountExists = existByQuery(query); + return pollAccountExists; + + } + } //PollAccountDAOImpl<E extends PollAccount> Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -0,0 +1,188 @@ +/* + * #%L + * Pollen :: Persistence + * + * $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.business.persistence; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.entities.PollenDAOHelper; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; + +import java.util.List; + +public class PollDAOImpl<E extends Poll> extends PollDAOAbstract<E> { + + /** Logger. */ + private static final Log log = LogFactory.getLog(PollDAOImpl.class); + + public List<E> getPolls(TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { + + Preconditions.checkNotNull(pager); + + long records = count(); + pager.setRecords((int) records); + + TopiaQuery query = createQuery("e"); + TopiaFilterPagerUtil.addPagerToQuery(query, pager); + + List<E> result = findAllByQuery(query); + return result; + } + + + public List<E> getCreatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount user) throws TopiaException { + + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(user); + + TopiaQuery countQuery = createQuery("e"); + countQuery.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); + long records = countByQuery(countQuery); + pager.setRecords((int) records); + + TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); + + List<E> result = findAllByQuery(query); + return result; + } + + public List<E> getInvitedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount userToUse) throws TopiaException { + + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(userToUse); + + //FIXME Use a query to do this to avoid in memory pagination + String email = userToUse.getEmail(); + + List<E> polls = findAll(); + List<E> allPolls = Lists.newLinkedList(); + for (E poll : polls) { + for (VotingList votingList : poll.getVotingList()) { + for (PersonToList personToList : votingList + .getPollAccountPersonToList()) { + if (!personToList.isHasVoted()) { + PollAccount pollAccount = personToList + .getPollAccount(); + + if (pollAccount != null + && pollAccount.getEmail() != null + && pollAccount.getEmail().equals( + email)) { + allPolls.add(poll); + } + } + } + } + } + + int records = allPolls.size(); + pager.setRecords(records); + + List<E> result = TopiaFilterPagerUtil.getPageFromList(allPolls, pager); + return result; + +// try { +// PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); +// TopiaQuery countQuery = dao.createQuery("e"); +// countQuery.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); +// long records = dao.countByQuery(countQuery); +// pager.setRecords((int) records); +// +// PagerUtil.computeRecordIndexesAndPagesNumber(pager); +// TopiaQuery query = dao.createQuery("e"); +// TopiaFilterPagerUtil.addPagerToQuery(pager, query); +// query.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); +// +// List<Poll> result = dao.findAllByQuery(query); +// return result; +// } catch (TopiaException e) { +// throw new PollenTechnicalException(e); +// } + } + + public List<E> getParticipatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, + UserAccount userToUse) throws TopiaException { + + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(userToUse); + + VoteDAO voteDao = PollenDAOHelper.getVoteDAO(context); + + TopiaQuery countQuery = voteDao.createQuery("e"). + addDistinct(). + setSelect("e." + Vote.PROPERTY_POLL). + addWhere("e." + Vote.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, userToUse); + + long records = countByQuery(countQuery); + pager.setRecords((int) records); + + TopiaQuery query = TopiaFilterPagerUtil.addPagerToQuery(countQuery, pager); + + List<E> result = findAllByQuery(query); + return result; + + } + + public List<E> getRunningPolls(boolean withEndDate) throws TopiaException { + + List<E> results; + TopiaQuery query; + + if (withEndDate) { + + query = createQuery("poll"). + addWhere("poll.endDate is not null and poll.endDate > current_timestamp()"). + addWhere("poll.beginDate is null or poll.beginDate < current_timestamp()"); + +// results = transaction +// .find("from " +// + Poll.class.getName() +// + " as poll where (poll.endDate is not null and poll.endDate > current_timestamp())" +// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); + } else { + query = createQuery("poll"). + addWhere("poll.endDate is null or poll.endDate > current_timestamp()"). + addWhere("poll.beginDate is null or poll.beginDate < current_timestamp()"); + +// results = transaction +// .find("from " +// + Poll.class.getName() +// + " as poll where (poll.endDate is null or poll.endDate > current_timestamp())" +// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); + } + + results = findAllByQuery(query); + + if (log.isDebugEnabled()) { + log.debug("Entities found: " + + ((results == null) ? "null" : results.size())); + } + return results; + } +} //PollDAOImpl<E extends Poll> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-03-14 09:42:22 UTC (rev 3179) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -42,4 +42,10 @@ } return resultVote; } + + @Override + public String getAdminId() { + String accountId = getCreator().getAccountId(); + return getPollId() + ":" + accountId; + } } //PollImpl Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/UserAccountDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/UserAccountDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/UserAccountDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -0,0 +1,67 @@ +/* + * #%L + * Pollen :: Persistence + * + * $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.business.persistence; + +import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; + +import java.util.List; + +public class UserAccountDAOImpl<E extends UserAccount> extends UserAccountDAOAbstract<E> { + + public boolean isUserExist(UserAccount user) throws TopiaException { + + Preconditions.checkNotNull(user); + + // In case of email change, check if an other user has not already + // the new email + // FIXME-fdesbois-20100510 : replace by using id directly + TopiaQuery query = createQuery(). + addWhere(UserAccount.PROPERTY_LOGIN, + TopiaQuery.Op.NEQ, user.getLogin()); + query.addEquals(UserAccount.PROPERTY_EMAIL, + StringUtils.lowerCase(user.getEmail())); + + // existing user found + boolean result = existByQuery(query); + return result; + } + + public List<E> getUsers(TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { + + Preconditions.checkNotNull(pager); + + long records = count(); + pager.setRecords((int) records); + + TopiaQuery query = createQuery("e"); + TopiaFilterPagerUtil.addPagerToQuery(query, pager); + + List<E> result = findAllByQuery(query); + return result; + } +} //UserAccountDAOImpl<E extends UserAccount> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/UserAccountDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -0,0 +1,115 @@ +/* + * #%L + * Pollen :: Persistence + * + * $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.business.persistence; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; + +import java.util.List; + +public class VoteDAOImpl<E extends Vote> extends VoteDAOAbstract<E> { + + /** Logger. */ + private static final Log log = LogFactory.getLog(VoteDAOImpl.class); + + public List<E> getVotesByPoll(Poll poll, + TopiaFilterPagerUtil.FilterPagerBean pager) throws TopiaException { + + Preconditions.checkNotNull(poll); + Preconditions.checkNotNull(pager); + + List<E> results; + + // No need to load votes for an anonymous poll + if (poll.isAnonymous()) { + + results = Lists.newArrayList(); + } else { + + // FIXME-FD20100309 : change model to suppress link from poll to vote + // It's not necessary to have votes when retrieving the poll, this + // method do this job only when it's needed + +// if (log.isDebugEnabled()) { +// log.debug("Load votes for poll with uid = " + poll.getPollId()); +// log.debug("LIMIT : startIndex = " + startIndex + " _ " + +// "endIndex = " + endIndex); +// } + + // FIXME : refactor this to use TopiaQuery from ToPIA 2.3 + // Order the results by creation date (the last vote done will be + // at the end of the list) + TopiaQuery query = createQuery("e"). + addWhere("e.poll", TopiaQuery.Op.EQ, poll). + addOrder("topiaCreateDate"); + TopiaFilterPagerUtil.addPagerToQuery(query, pager); + results = findAllByQuery(query); + +// +// List<E> votes = find( +// "FROM " + Vote.class.getName() + +// " WHERE poll.pollId = :pollUId" + +// " ORDER BY topiaCreateDate", +// startIndex, endIndex, +// new Object[]{"pollUId", poll.getPollId()}); + +// if (log.isDebugEnabled()) { +// log.debug("Nb votes found : " + votes.size()); +// } +// +// results = Lists.newArrayList(results); + } + return results; + } + + public boolean hasAlreadyVoted(String votingId, Poll poll) throws TopiaException { + + // Test using a count(*) on votes + TopiaQuery query = createQuery("e"). + addWhere("e.poll", TopiaQuery.Op.EQ, poll). + addWhere("e.pollAccount.votingId", TopiaQuery.Op.EQ, votingId). + addOrder("topiaCreateDate"); + boolean result = existByQuery(query); + return result; +// List<Long> tmp = transaction.find( +// "SELECT COUNT(*)" + +// " FROM " + Vote.class.getName() + +// " WHERE poll.pollId = :pollUId" + +// " AND pollAccount.votingId = :votingId", +// "pollUId", poll.getPollId(), +// "votingId", votingId); +// +// int count = tmp.get(0).intValue(); +// +// // If the count is greater than 0, there is an existing votingId +// // who has already voted for the poll +// result = count > 0; + + } +} //VoteDAOImpl<E extends Vote> Property changes on: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/VoteDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java 2012-03-14 09:42:22 UTC (rev 3179) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/java/org/chorem/pollen/entities/migration/PollenMigrationCallbackV1_2_6.java 2012-03-14 14:39:03 UTC (rev 3180) @@ -78,12 +78,30 @@ migrateVoteCountingTypes(tx, queries); // change all Boolean properties to boolean - migrateToPrimitiveTypes(tx, queries); + migrateToPrimitiveTypes(queries); + + // add foreign key indexes + addForeignKeyIndexes(queries); } - private void migrateToPrimitiveTypes(TopiaContextImplementor tx, - List<String> queries) throws TopiaException { + private void addForeignKeyIndexes(List<String> queries) { + queries.add("CREATE INDEX idx_PollAccount_pollsCreated ON poll(creator);"); + queries.add("CREATE INDEX idx_PollAccount_comment ON comment(pollAccount);"); + queries.add("CREATE INDEX idx_PollAccount_vote ON vote(pollAccount);"); + queries.add("CREATE INDEX idx_Poll_vote ON vote(poll);"); + queries.add("CREATE INDEX idx_Poll_choice ON choice(poll);"); + queries.add("CREATE INDEX idx_Poll_result ON result(poll);"); + queries.add("CREATE INDEX idx_Poll_comment ON comment(poll);"); + queries.add("CREATE INDEX idx_Poll_preventRule ON preventRule(poll);"); + queries.add("CREATE INDEX idx_Poll_votingList ON votingList(poll);"); + queries.add("CREATE INDEX idx_UserAccount_favoriteList ON personList(owner);"); + queries.add("CREATE INDEX idx_UserAccount_pollAccount ON pollAccount(userAccount);"); + queries.add("CREATE INDEX idx_PersonList_pollAccount ON pollAccount(personList);"); + } + + private void migrateToPrimitiveTypes(List<String> queries) throws TopiaException { + migrateBoolean(queries, "useraccount", "administrator"); migrateBoolean(queries, "choice", "validate"); migrateBoolean(queries, "personToList", "hasVoted"); Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.properties =================================================================== --- branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.properties 2012-03-14 09:42:22 UTC (rev 3179) +++ branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.properties 2012-03-14 14:39:03 UTC (rev 3180) @@ -27,3 +27,4 @@ model.tagvalue.java.lang.String=text model.tagvalue.version=1.2.6 model.tagvalue.doNotGenerateBooleanGetMethods=true +model.tagvalue.indexForeignKeys=true Modified: branches/pollen-1.2.6-struts2/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ)
participants (1)
-
tchemit@users.chorem.org