myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r514167 - in /myfaces/fusion/trunk/examples/src/main: java/org/apache/myfaces/examples/ballot/backings/ java/org/apache/myfaces/examples/ballot/dao/ java/org/apache/myfaces/examples/ballot/model/ webapp/WEB-INF/ webapp/ballot/
Date Sat, 03 Mar 2007 15:13:22 GMT
Author: imario
Date: Sat Mar  3 07:13:21 2007
New Revision: 514167

URL: http://svn.apache.org/viewvc?view=rev&rev=514167
Log:
voting part

Modified:
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotHall.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotVoteTopic.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/ItemDao.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/TopicDao.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/VoteDao.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Item.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Topic.java
    myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Vote.java
    myfaces/fusion/trunk/examples/src/main/webapp/WEB-INF/faces-config.xml
    myfaces/fusion/trunk/examples/src/main/webapp/ballot/VoteTopic.jsp

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotHall.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotHall.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotHall.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotHall.java
Sat Mar  3 07:13:21 2007
@@ -45,9 +45,9 @@
 	public static class TopicInfo
 	{
 		private Topic topic;
-		private Integer votes;
+		private Long votes;
 
-		public TopicInfo(Topic topic, Integer votes)
+		public TopicInfo(Topic topic, Long votes)
 		{
 			this.topic = topic;
 			this.votes = votes;
@@ -58,7 +58,7 @@
 			return topic;
 		}
 
-		public Integer getVotes()
+		public Long getVotes()
 		{
 			return votes;
 		}
@@ -127,7 +127,7 @@
 		topicInfos = new ArrayList<TopicInfo>();
 
 		List<Topic> topics = getTopicDao().getTopics();
-		Map<Long, Integer> votes = getTopicDao().getTopicVotes();
+		Map<Long, Long> votes = getTopicDao().getTopicVotes();
 
 		for (Topic topic : topics)
 		{

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotVoteTopic.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotVoteTopic.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotVoteTopic.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/backings/BallotVoteTopic.java
Sat Mar  3 07:13:21 2007
@@ -26,43 +26,67 @@
 import org.apache.commons.lang.StringUtils;
 import org.apache.myfaces.examples.ballot.dao.TopicDao;
 import org.apache.myfaces.examples.ballot.dao.VoteDao;
+import org.apache.myfaces.examples.ballot.dao.VoterDao;
+import org.apache.myfaces.examples.ballot.dao.ItemDao;
 import org.apache.myfaces.examples.ballot.lib.BallotState;
 import org.apache.myfaces.examples.ballot.model.Item;
 import org.apache.myfaces.examples.ballot.model.Topic;
 import org.apache.myfaces.examples.ballot.model.Vote;
+import org.apache.myfaces.examples.ballot.model.Voter;
 import org.apache.myfaces.examples.lib.FacesConst;
 import org.apache.myfaces.examples.lib.FacesUtils;
 import org.apache.myfaces.fusion.conversation.Conversation;
+import org.apache.myfaces.fusion.conversation.ConversationUtils;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.faces.context.FacesContext;
+import javax.faces.model.SelectItem;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 public class BallotVoteTopic
 {
 	private TopicDao topicDao;
 	private VoteDao voteDao;
+	private VoterDao voterDao;
+	private ItemDao itemDao;
 
 	private BallotState ballotState;
 
 	private Topic topic;
 
 	private List<ItemInfo> itemInfos;
+	private List<SelectItem> choices;
 
 	public static class ItemInfo
 	{
 		private final Item item;
+		private final Vote vote;
+		private final Long currentVote;
 
-		protected ItemInfo(Item item)
+		protected ItemInfo(Item item, Vote vote, Long currentVote)
 		{
 			this.item = item;
+			this.vote = vote;
+			this.currentVote = currentVote;
 		}
 
 		public Item getItem()
 		{
 			return item;
 		}
+
+		public Vote getVote()
+		{
+			return vote;
+		}
+
+		public Long getCurrentVote()
+		{
+			return currentVote;
+		}
 	}
 
 	public BallotState getBallotState()
@@ -95,6 +119,26 @@
 		this.voteDao = voteDao;
 	}
 
+	public ItemDao getItemDao()
+	{
+		return itemDao;
+	}
+
+	public void setItemDao(ItemDao itemDao)
+	{
+		this.itemDao = itemDao;
+	}
+
+	public VoterDao getVoterDao()
+	{
+		return voterDao;
+	}
+
+	public void setVoterDao(VoterDao voterDao)
+	{
+		this.voterDao = voterDao;
+	}
+
 	public void initView()
 	{
 		if (!getBallotState().isAuthenticated())
@@ -148,10 +192,26 @@
 
 		itemInfos = new ArrayList<ItemInfo>();
 
-		List<Vote> votes = voteDao.getVotesForVoter(/* todo */ null); 
+		Voter voter = voterDao.getByKey(getBallotState().getVoterId());
+
+		Map<Item, Vote> voteMap = new HashMap<Item, Vote>();
+		for (Vote vote : voteDao.getVotesForVoter(voter))
+		{
+			voteMap.put(vote.getItem(), vote);
+		}
+
+		Map<Item, Long> itemVotes = itemDao.getItemVoteSumForTopic(getTopic());
+
 		for (Item item : getTopic().getItems())
 		{
-			itemInfos.add(new ItemInfo(item));
+			Vote vote = voteMap.get(item);
+			if (vote == null)
+			{
+				vote = new Vote();
+				vote.setItem(item);
+				vote.setVoter(voter);
+			}
+			itemInfos.add(new ItemInfo(item, vote, itemVotes.get(item)));
 		}
 
 		return itemInfos;
@@ -160,8 +220,30 @@
 	@Transactional
 	public String saveAction()
 	{
-		Conversation.getCurrentInstance().invalidate();
+		for (ItemInfo itemInfo : itemInfos)
+		{
+			voteDao.save(itemInfo.getVote());
+		}
+
+		BallotVoteTopic newBallotVoteTopic = (BallotVoteTopic) ConversationUtils.invalidateAndRestart(Conversation.getCurrentInstance());
+		newBallotVoteTopic.initTopic(getTopic().getId());
 
 		return FacesConst.SUCCESS;
+	}
+
+	public List<SelectItem> getChoices()
+	{
+		if (choices != null)
+		{
+			return choices;
+		}
+
+		choices = new ArrayList<SelectItem>();
+		for (int i = -2; i < 3; i++)
+		{
+			choices.add(new SelectItem(i, Integer.toString(i)));
+		}
+
+		return choices;
 	}
 }

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/ItemDao.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/ItemDao.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/ItemDao.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/ItemDao.java
Sat Mar  3 07:13:21 2007
@@ -24,9 +24,14 @@
 package org.apache.myfaces.examples.ballot.dao;
 
 import org.apache.myfaces.examples.ballot.model.Item;
+import org.apache.myfaces.examples.ballot.model.Topic;
 
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 public class ItemDao
 {
@@ -41,6 +46,30 @@
 	public void setEntityManager(EntityManager entityManager)
 	{
 		this.entityManager = entityManager;
+	}
+
+	public Map<Item, Long> getItemVoteSumForTopic(Topic topic)
+	{
+		Map<Item, Long> ret = new HashMap<Item, Long>();
+
+		Query query = entityManager.createQuery("select vote.item.id, sum(vote.choice) from Vote
vote where vote.item.topic = :topic group by vote.item.id");
+		query.setParameter("topic", topic);
+		
+		List<Object[]> itemsRet = query.getResultList();
+		for (Object[] itemRet : itemsRet)
+		{
+			Long itemId = (Long) itemRet[0];
+			Long count = (Long) itemRet[1];
+
+			ret.put(getByKey(itemId), count);
+		}
+
+		return ret;
+	}
+
+	private Item getByKey(Long id)
+	{
+		return entityManager.find(Item.class, id);
 	}
 
 	public void remove(Item item)

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/TopicDao.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/TopicDao.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/TopicDao.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/TopicDao.java
Sat Mar  3 07:13:21 2007
@@ -45,13 +45,13 @@
 
 	public List<Topic> getTopics()
 	{
-		Query query = entityManager.createQuery("select topic from Topic topic");
+		Query query = entityManager.createQuery("select topic from Topic topic order by topic.name");
 		return query.getResultList();
 	}
 
-	public Map<Long, Integer> getTopicVotes()
+	public Map<Long, Long> getTopicVotes()
 	{
-		Map<Long, Integer> ret = new TreeMap<Long, Integer>();
+		Map<Long, Long> ret = new TreeMap<Long, Long>();
 
 		Query query = entityManager.createNamedQuery("getVotes");
 		List votes = query.getResultList();
@@ -60,7 +60,7 @@
 			Object[] columns = (Object[]) vote;
 
 			Long id = (Long) columns[0];
-			Integer voteCount = (Integer) columns[1];
+			Long voteCount = (Long) columns[1];
 
 			ret.put(id, voteCount);
 		}

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/VoteDao.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/VoteDao.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/VoteDao.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/dao/VoteDao.java
Sat Mar  3 07:13:21 2007
@@ -52,4 +52,9 @@
 		query.setParameter("voter", voter);
 		return query.getResultList();
 	}
+
+	public void save(Vote vote)
+	{
+		entityManager.persist(vote);
+	}
 }

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Item.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Item.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Item.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Item.java
Sat Mar  3 07:13:21 2007
@@ -29,6 +29,9 @@
 import javax.persistence.Column;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
+import javax.persistence.OneToMany;
+import javax.persistence.CascadeType;
+import java.util.List;
 
 @Entity
 public class Item
@@ -46,6 +49,10 @@
 	@Version
 	private Long version;
 
+	@OneToMany(cascade = CascadeType.REMOVE, mappedBy = "item")
+	private List<Vote> votes;
+
+
 	public Long getId()
 	{
 		return id;
@@ -84,5 +91,15 @@
 	public void setVersion(Long version)
 	{
 		this.version = version;
+	}
+
+	public List<Vote> getVotes()
+	{
+		return votes;
+	}
+
+	public void setVotes(List<Vote> votes)
+	{
+		this.votes = votes;
 	}
 }

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Topic.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Topic.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Topic.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Topic.java
Sat Mar  3 07:13:21 2007
@@ -32,7 +32,7 @@
 
 @NamedQuery(
 	name="getVotes",
-	query="select topic.id, count(topic.id) from Topic topic join topic.votes vote group by
topic.id"
+	query="select topic.id, count(topic.id) from Topic topic join topic.items items join items.votes
vote group by topic.id"
 )
 @Entity
 public class Topic
@@ -50,9 +50,6 @@
 	@OneToMany(mappedBy="topic", cascade = CascadeType.ALL)
 	private List<Item> items;
 
-	@OneToMany(cascade = CascadeType.REMOVE, mappedBy = "topic")
-	private List<Vote> votes;
-
 	private boolean started;
 
 	@Version
@@ -129,6 +126,7 @@
 		this.started = started;
 	}
 
+	/*
 	public List<Vote> getVotes()
 	{
 		return votes;
@@ -138,4 +136,5 @@
 	{
 		this.votes = votes;
 	}
+	*/
 }

Modified: myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Vote.java
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Vote.java?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Vote.java
(original)
+++ myfaces/fusion/trunk/examples/src/main/java/org/apache/myfaces/examples/ballot/model/Vote.java
Sat Mar  3 07:13:21 2007
@@ -44,7 +44,7 @@
 
 	@ManyToOne
 	@JoinColumn(nullable = false)
-	private Topic topic;
+	private Item item;
 
 	@ManyToOne
 	@JoinColumn(nullable = false)
@@ -63,14 +63,14 @@
 		this.id = id;
 	}
 
-	public Topic getTopic()
+	public Item getItem()
 	{
-		return topic;
+		return item;
 	}
 
-	public void setTopic(Topic topic)
+	public void setItem(Item item)
 	{
-		this.topic = topic;
+		this.item = item;
 	}
 
 	public Voter getVoter()

Modified: myfaces/fusion/trunk/examples/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/webapp/WEB-INF/faces-config.xml?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/fusion/trunk/examples/src/main/webapp/WEB-INF/faces-config.xml Sat Mar  3 07:13:21
2007
@@ -98,7 +98,7 @@
 	</navigation-rule>
 
 	<navigation-rule>
-		<description>vote on topic</description>
+		<description>save voter data</description>
 		<navigation-case>
 			<from-action>#{ballotVoter.saveAction}</from-action>
 			<from-outcome>success</from-outcome>

Modified: myfaces/fusion/trunk/examples/src/main/webapp/ballot/VoteTopic.jsp
URL: http://svn.apache.org/viewvc/myfaces/fusion/trunk/examples/src/main/webapp/ballot/VoteTopic.jsp?view=diff&rev=514167&r1=514166&r2=514167
==============================================================================
--- myfaces/fusion/trunk/examples/src/main/webapp/ballot/VoteTopic.jsp (original)
+++ myfaces/fusion/trunk/examples/src/main/webapp/ballot/VoteTopic.jsp Sat Mar  3 07:13:21
2007
@@ -91,16 +91,22 @@
 					<h:outputText value="#{itemInfo.item.content}"/>
 				</t:column>
 
+				<t:column>
+					<f:facet name="header">
+						<h:outputText value="Current Vote"/>
+					</f:facet>
+					<h:outputText value="#{itemInfo.currentVote}"/>
+				</t:column>
+
 				<h:column>
 					<f:facet name="header">
 						<h:outputText value="Vote"/>
 					</f:facet>
 
-<%--
-					<h:selectOneRadio value="#{item.vote}">
-
+					<h:selectOneRadio value="#{itemInfo.vote.choice}">
+						<f:selectItems value="#{ballotVoteTopic.choices}" />
 					</h:selectOneRadio>
---%>					
+
 				</h:column>
 
 			</t:dataTable>



Mime
View raw message