Author: unibrew Date: 2005-12-20 12:54:10 -0500 (Tue, 20 Dec 2005) New Revision: 1893 Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Poll.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollDescriptor.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollTools.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollVotesDatabase.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsDescriptor.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java trunk/forge/portal-extensions/polls/src/java/org/jboss/forge/polls/PollsPortlet.java trunk/forge/portal-extensions/portal-default/src/web/WEB-INF/default-portal.xml Log: [JBLAB-407] Update of Polls and turning them off for release. Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -73,7 +73,7 @@ new DownloadCountersDescriptor(portalName, contentManager); rw = new ResourceWatcher(contentManager); // Registering ResourceWatcher to watch for main download counters xml file change. - rw.watchResource(DownloadCounterTools.getMainXmlPath(portalName)); + //rw.watchResource(DownloadCounterTools.getMainXmlPath(portalName)); // Getting the project ids names where are project download counter descriptors. counterDescriptors = descriptor.getDownloadDescriptors(portalName).keySet(); Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Poll.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Poll.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Poll.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -1,3 +1,26 @@ + +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + package org.jboss.forge.common.projects; import java.util.Collections; @@ -5,12 +28,39 @@ import java.util.Map; import java.util.Set; +/** + * Class which abstracts one Poll. It has all information about it and + * contains summary votes for answers. + * @author Ryszard Kozmik + * + */ public class Poll { + /** + * Contains poll's question. + */ private String question; + + /** + * Contains poll identifycation number. + */ private String pollId; + + /** + * This Map<String,Integer> contains pairs where the keys are + * answers and the values are summary numbers of votes. + */ private Map<String,Integer> votes; + /** + * Poll's constructor which initiates all variables with given parameters. + * @param answers + * Set<String> containing possible poll's answers for <code>question</code> + * @param question + * Poll's <code>question</code> + * @param pollId + * Poll identification number. + */ public Poll (Set<String>answers,String question, String pollId) { votes = Collections.synchronizedMap(new LinkedHashMap<String,Integer>(answers.size())); for (String answer : answers){ @@ -20,6 +70,15 @@ this.pollId = pollId; } + /** + * Poll's constructor which initiates all variables with given parameters. + * @param votes + * Map<String,Integer> containing pairs of answers and summary votes values for them. + * @param question + * Poll's question. + * @param pollId + * Poll's identification number. + */ public Poll (Map<String,Integer>votes,String question, String pollId) { this.votes = votes; if (votes==null) this.votes = Collections.synchronizedMap(new LinkedHashMap<String,Integer>()); @@ -27,17 +86,32 @@ this.pollId = pollId; } + /** + * Simplest constructor for Poll used only for temporary constructing of Poll + * used for comparing. + * @param question + * Poll's question. + */ public Poll (String question) { this.question = question; this.pollId= Integer.toString((int)(Math.random()*Integer.MAX_VALUE)); } + /** + * Method increments the number of votes for given in parameter <code>answer</code>. + * @param answer + * Answer on which number of votes must be incremented. + */ public void incrementAnswer (String answer) { if (votes.get(answer)!=null) { votes.put(answer,votes.get(answer)+1); } } + /** + * Overrided equals method is comparing two Polls checking if they have + * the same question and the same answers. + */ @Override public boolean equals(Object obj) { Poll poll = ((Poll)obj); @@ -49,35 +123,53 @@ return false; } + /** + * Method returns summary number of votes for specified <code>answer</code>. + * @param answer + * Answer for which number of votes is requested. + * @return + * Number of votes for given <code>answer</code> + */ public Integer getAnswerVotes (String answer) { return votes.get(answer); } + /** + * Method returns Poll's question. + * @return + * Poll's question. + */ public String getQuestion() { return question; } + /** + * Method returns Poll's identification number. + * @return + * Poll's identification number. + */ public String getPollId() { return pollId; } + /** + * Method returns Set<String> containing Poll's answers. + * @return + * Set<String> of Poll's answers. + */ public Set<String> getAnswers(){ return votes.keySet(); } + /** + * Method is used for voting on specified in parameter <code>answer</code>. + * @param answer + * The <code>answer</code> on which the vote is given. + */ public void vote (String answer) { if (votes.containsKey(answer)) { votes.put(answer,votes.get(answer)+1); } } - public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("POLLID: "+this.getPollId()); - sb.append("QUESTION: "+this.getQuestion()); - for (String answer :this.getAnswers()) { - sb.append("ANSWER "+answer); - } - return sb.toString(); - } } Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollDescriptor.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollDescriptor.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollDescriptor.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -40,6 +40,12 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; +/** + * This class abstracts poll.xml descriptor which is located in projects' dirs. + * It parses this xml and keeps all the data read from it. + * @author Ryszard Kozmik + * + */ public class PollDescriptor extends AbstractDescriptor { /** @@ -62,7 +68,18 @@ */ public static final String POLL = "poll"; - + /** + * Main class constructor which parses the descriptor and initiates + * all class variables. + * @param pollDescNode + * Shotoku Node which points to the poll.xml descriptor. + * @throws SAXException + * XML Parser exception. + * @throws IOException + * Exception which tell that there was problem with reading a file. + * @throws XmlNotFoundException + * This exception is thrown when under specified Node, there is no xml desciptor. + */ public PollDescriptor (org.jboss.shotoku.Node pollDescNode) throws SAXException, IOException,XmlNotFoundException { @@ -75,12 +92,24 @@ NodeList nodes = doc.getDocumentElement().getChildNodes(); // Parsing and gettting polls' questions. + /* + * XML schema: + * <polls>1..1 + * <poll>0..* + * <question />1..1 + * <answer />0..* + * </poll> + * </polls> + */ polls = new LinkedList<Poll>(); for (int i = 0; i < nodes.getLength(); i++) { n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { + + // temporary variables String tempQuestion = null; Set<String> tempAnswers = Collections.synchronizedSet(new LinkedHashSet<String>()); + System.out.println("[POLLSDESCRIPTOR] IN POLLS"); if (n.getNodeName().equals(POLL)) { System.out.println("[POLLSDESCRIPTOR] IN POLL"); @@ -102,6 +131,7 @@ } } } + // Checking if all needed information was collected and then creating Poll. if (tempQuestion!=null && !tempQuestion.equals("") && tempAnswers.size()>0) { System.out.println ("ADDING A POLL TO THE POLLS"); polls.add(new Poll(tempAnswers,tempQuestion,Integer.toString((int)(Math.random()*99999999)))); Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollTools.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollTools.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollTools.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -25,10 +25,7 @@ import java.io.File; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -36,7 +33,6 @@ import javax.portlet.PortletURL; import org.jboss.forge.common.ForgeHelper; -import org.jboss.portal.common.context.Context; import org.jboss.portal.common.context.DelegateContext; import org.jboss.portlet.JBossRenderResponse; import org.jboss.shotoku.ContentManager; @@ -55,6 +51,11 @@ public static final String VOTED_PARAMETER_NAME = "voted"; /** + * Name of the request parameter which contains error information which should be displayed. + */ + public static final String ERROR_PARAMETER_NAME = "error"; + + /** * Value of request parameter for false. */ public static final String FALSE = "false"; @@ -101,6 +102,7 @@ * Method constructs path to polls descriptor. * * @param portalName + * Name of the portal. * @return Path to polls descriptor. */ public static String getMainXmlPath (final String portalName) { @@ -111,6 +113,7 @@ * Method constructs path to project's poll descriptor. * * @param portalName + * Name of the portal. * @param projectId * Project id name for which the poll descriptor path will be constructed. * @return Path to project's poll descriptor. @@ -152,6 +155,7 @@ * information about voting results. * * @param portalName + * Name of the portal. * @return Path to the PollPortlet's JSP voting info view file. */ public static String getInfoJsp() { @@ -163,6 +167,7 @@ * detailed information about who and how has voted on specified poll. * * @param portalName + * Name of the portal. * @return Path to the PollPortlet's JSP voting detailed info view file. */ public static String getDetailsJsp() { @@ -174,6 +179,7 @@ * voting question. * * @param portalName + * Name of the portal. * @return Path to the PollPortlet's JSP voting view file. */ public static String getVotingJsp() { @@ -194,7 +200,7 @@ * @return * Content context for information JSP page. */ - public static Context getInfoContext (final String portalName,final String projectId, + public static DelegateContext getInfoContext (final String portalName,final String projectId, ContentManager cm,JBossRenderResponse response) { // If the projectId is null method returns empty DelegateContext object. @@ -224,14 +230,14 @@ // Filling the context for portlet. /** * Context structure: - * <polls>ONE - * <poll>MANY - * <question />ONE - * <renderUrl/>ONE - * <renderUrlDetailed />ONE - * <answers>MANY - * <votes />ONE - * <text />ONE + * <polls>1..1 + * <poll>0..* + * <question />1..1 + * <renderUrl/>1..1 + * <renderUrlDetailed />1..1 + * <answers>0..* + * <votes />1..1 + * <text />1..1 * </answers> * </poll> * </polls> @@ -273,8 +279,8 @@ * @return * Content context for detailed information JSP page. */ - public static Context getDetailsContext (final String portalName,final String projectId,final String pollId, - ContentManager cm,JBossRenderResponse response) { + public static DelegateContext getDetailsContext (final String portalName,final String projectId, + final String pollId, ContentManager cm,JBossRenderResponse response) { // If the projectId is null method returns empty DelegateContext object. if (projectId==null) { @@ -308,14 +314,14 @@ // Filling the context for portlet. /** * Context structure: - * <poll>ONE - * <question />ONE - * <renderUrlVoting />ONE - * <renderUrlInfo />ONE - * <answer>MANY - * <text />ONE - * <user>MANY - * <id />ONE + * <poll>1..1 + * <question />1..1 + * <renderUrlVoting />1..1 + * <renderUrlInfo />1..1 + * <answer>0..* + * <text />1..1 + * <user>1..* + * <id />1..1 * </user> * </answer> * </poll> @@ -359,10 +365,10 @@ * @return * Content context for detailed information JSP page. */ - public static Context getVotingContext (String portalName, String projectId, - ContentManager cm,JBossRenderResponse response) { + public static DelegateContext getVotingContext (String portalName, String projectId, + String error, ContentManager cm,JBossRenderResponse response) { - // If the projectId is null method returns empty DelegateContext object. + // If the projectId is null method returns empty DelegateContext object. if (projectId==null) { return new DelegateContext(); } @@ -382,33 +388,28 @@ if (!desc.checkForProjectPolls(projectId)) { return ctx; } - - /* - // Getting the polls for given projectId. - Map<String,String> values = desc.getPollsInfoForProject(projectId); - Collection<String> answersRedundant = values.values(); - - // Getting possible answers - Set<String> answers = new HashSet<String>(); - answers.addAll(answersRedundant);*/ - + List<Poll> pollDesc = desc.getPollsInfoForProject(projectId); // Filling the context for portlet. /** * Context structure: - * <polls>ONE - * <poll>MANY - * <question>ONE - * <actionUrl>ONE - * <pollId>ONE - * <answer>MANY - * <text />ONE + * <polls>1..1 + * <error>0..1 + * <poll>0..* + * <question>1..1 + * <actionUrl>1..1 + * <pollId>1..1 + * <answer>0..* + * <text />1..1 * </answer> * </poll> * </polls> */ DelegateContext polls = ctx.next("polls"); + if (error!=null && !error.equals("")) { + polls.put("error",error); + } for (Poll pollObj : pollDesc) { DelegateContext poll = polls.next("poll"); Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollVotesDatabase.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollVotesDatabase.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollVotesDatabase.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -1,3 +1,26 @@ + +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + package org.jboss.forge.common.projects; import java.util.Hashtable; @@ -13,19 +36,42 @@ import org.jboss.shotoku.exceptions.ResourceDoesNotExist; import org.jboss.shotoku.exceptions.SaveException; +/** + * This class is an interface for managing Polls' *database*. Information are stored in + * shotoku Node properties. Each file describes one Poll and keeps voting data for it. + * @author Ryszard Kozmik + * + */ public class PollVotesDatabase { + /** + * Location where the *database* is stored in content repository. + */ public static final String DATABASE_LOCATION = "default/polls/database/"; - Directory databaseDir; + /** + * Shotoku Directory object which points to the *database*. + */ + private Directory databaseDir; @Inject ContentManager contentManager; - + + /** + * Constructor just initiates the <code>databaseDir</code> variable + * to point to the *database* Directory in content repository. + * @throws ResourceDoesNotExist + * The exception is thrown if the specified *database*'s directory doesn't exist. + */ public PollVotesDatabase() throws ResourceDoesNotExist { databaseDir =contentManager.getDirectory(DATABASE_LOCATION); } + /** + * This method creates new file for keeping data for Poll. + * @param fileName + * The name of new Poll file. + */ public void createNewPollFile (String fileName) { try { System.out.println ("POLLVOTESDATABASE CREATENEWPOLL"); @@ -43,7 +89,13 @@ } } - + /** + * Method simply checks if file for given in parameter <code>pollId</pollId> Poll exists. + * @param pollId + * Id of a Poll to check. + * @return + * True if file exists, false othervise. + */ public boolean checkForPollFile(String pollId) { try { databaseDir.getNode(pollId); @@ -53,12 +105,28 @@ return true; } + /** + * This method saves vote on specified Poll <code>pollId</code> if user + * <code>userId</code> hasn't voted before. If he had already voted the vote + * will be rejected and method will return false. + * @param pollId + * Id name of the Poll on which user votes. + * @param userId + * User id login name. + * @param vote + * Text content of user's vote. + * @return + * True if vote was added successfully, false if user has already voted on that Poll. + */ public boolean votedOnPoll (String pollId, String userId, String vote) { System.out.println ("SETTING PROPERTY FOR FILE"); System.out.println ("POLLID: "+pollId+" USERID: "+userId+" VOTE: "+vote); Node pollFile=null; try { + // Getting the file for Poll pollFile = databaseDir.getNode(pollId); + + // Checking whether user has already voted before. String voteValue = pollFile.getProperty(userId); if (voteValue!= null && !voteValue.equals("")) return false; @@ -69,6 +137,7 @@ } catch (ResourceDoesNotExist e2) { System.out.println("[POLLPORTLET] Database file for poll didn't exist!"); e2.printStackTrace(); + // There was no file for Poll so it is created. try { databaseDir.newNode(pollId); } catch (ResourceAlreadyExists e3) { @@ -76,6 +145,7 @@ } } System.out.println("PRZED DODANIEM GŁOSU "+pollFile.getClass().getCanonicalName()); + // Saving vote pollFile.setProperty(userId,vote); System.out.println("PO DODANIU GŁOSU PRZED SAVEM"+pollFile.getClass().getCanonicalName()); try { @@ -88,13 +158,24 @@ return true; } + /** + * Method returns Map containing pairs of Strings where keys are users' login names + * and the values are their votes. + * @param pollId + * Poll id name for which votes must be returned. + * @return + * Map<String,String> containing users' votes. + */ public Map<String,String> getUserVotesForPollFile (String pollId) { System.out.println ("GETUSERVOTESFORPOLLFILE POLLID: "+pollId); Map<String,String> userVotes = null; + // if no poll id name was specified, method returns empty Map if (pollId==null) return new Hashtable<String,String>(0); try { + // Getting Node for Poll Node pollFile = databaseDir.getNode(pollId); System.out.println("PRZED GETPROPERTIES "+pollFile.getClass().getCanonicalName()); + //Collecting users' votes. userVotes = pollFile.getProperties(); System.out.println("PO GETPROPERTIES "+pollFile.getClass().getCanonicalName()); System.out.println ("I'M BEFORE DISPLAYING FILE PROPERTIES."); @@ -113,9 +194,16 @@ return userVotes; } + /** + * Method removes data file for Poll specified by id name in parameter <code>pollId</code>. + * @param pollId + * Poll id name which *database* file have to be deleted. + */ public void removePollDataFile (String pollId) { try { + // Getting Node for data file. Node pollFile = databaseDir.getNode(pollId); + // Removing file. pollFile.delete(); } catch (ResourceDoesNotExist e2) { System.out.println ("[POLLS] Poll's data file was already deleted."); Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsDescriptor.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsDescriptor.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsDescriptor.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -44,6 +44,12 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +/** + * Main class for Polls project. It is an image of main Polls xml file, polls.xml. + * Class serves methods for building Polls, synchronizing them and voting on them. + * @author Ryszard Kozmik + * + */ public class PollsDescriptor extends AbstractDescriptor { @@ -98,6 +104,9 @@ */ private ContentManager contentManager; + /** + * This variable references object used for accessing *database* of Polls' votes. + */ private PollVotesDatabase database; /** @@ -107,7 +116,7 @@ */ private boolean changeStatus; - /** GOTOWA!!! + /** * Constructor for PollsDescriptor. * While constructing object all information from projects' descriptors * is being collected and the polls Map is filled with values @@ -154,24 +163,27 @@ } } + /** + * This method returns List<Poll> which contains all Polls for specified in parameter + * <code>projectId> project id name. + * @param projectId + * @return + */ public synchronized List<Poll> getPollsInfoForProject(String projectId) { List<Poll> polls = getProjectPolls(projectId); List<Poll> pollsCopy = new ArrayList<Poll>(polls.size()); pollsCopy.addAll(polls); - /*Map<String,String> values = null; - if (polls!=null) { - values = new Hashtable<String,String>(polls.size()); - for (Poll poll:polls) { - values.put(poll.getPollId(),poll.getQuestion()); - } - - } else { - values = new Hashtable<String,String>(); - } - return values;*/ return pollsCopy; } + /** + * Method returns Map<String,Poll> which contains all the Polls for requested + * <code>projectId</code>. + * @param projectId + * Project id name for which the Polls are requested. + * @return + * Map<String,Polls> pairs of pollId:Poll. + */ public synchronized Map<String,Poll> getPollsStatsForProject(String projectId) { List<Poll> polls = getProjectPolls(projectId); Map<String,Poll> values = null; @@ -186,11 +198,19 @@ return values; } + /** + * Method returns Map<String,String> for specified poll id + * containing user:answer pairs which tells what answer user has given. + * @param pollId + * Poll's identification number. + * @return + * Map<String,String> - pairs of user:answer. + */ public Map<String,String> getDetailStatsForPoll (String pollId) { return database.getUserVotesForPollFile(pollId); } - /** GOTOWA!!! + /** * This method searches through projects direcories in order to find all poll descriptors. * Method returns Map containing pairs of String projectId and * org.jboss.shotoku.Node poll descriptor file node. @@ -213,6 +233,7 @@ return nodes; } + // going through projects' directories for (Directory projectDir:membersProjectDirs) { for (org.jboss.shotoku.Node n:projectDir.getNodes().toList()) { // Checking if node is a node containing poll descriptor. @@ -220,6 +241,7 @@ String projectId = projectDir.getName(); try { projectId = projectDir.getName(); + // getting Node for poll.xml project's descriptor org.jboss.shotoku.Node poll = projectDir.getNode(ProjectsHelper.POLL_DESC); nodes.put(projectId,poll); break; @@ -233,7 +255,7 @@ return nodes; } - /** GOTOWA!!! + /** * Method removes polls which were disabled from working * and adds those which was added in their project's poll descriptor. * @@ -294,10 +316,41 @@ changeStatus=false; } + /** + * Method remmoves all Polls which are not contained in + * specified in parameter <code>projectIds</code> String Set. + * @param projectIds + * Set<String> containing all project id names available. + */ private synchronized void removeNotTrackedProjects(Set<String> projectIds) { - polls.keySet().retainAll(projectIds); + //polls.keySet().retainAll(projectIds); + List<String> projectsToDelete = new LinkedList<String>(); + for (String projectId : polls.keySet()) { + if (!projectIds.contains(projectId)) { + projectsToDelete.add(projectId); + } + } + for (String projectId : projectsToDelete) { + List<Poll> pollObjects = polls.get(projectId); + if (pollObjects !=null && pollObjects.size()>0){ + for (Poll poll : pollObjects ){ + database.removePollDataFile(poll.getPollId()); + } + } + polls.remove(projectId); + } } + /** + * Method simply returns Poll object for specified <code>projectId</code> + * and <code>pollId</code> + * @param projectId + * Project id name. + * @param pollId + * Poll identification number. + * @return + * Poll object. + */ public synchronized Poll getPoll (String projectId,String pollId) { if (projectId!=null && pollId!=null && !projectId.equals("") && !pollId.equals("") && polls!=null) { @@ -312,7 +365,7 @@ return null; } - /** GOTOWA!!! + /** * Method removes polls specified in <code>pollsToDelete</code> * from tracking their votes. * @@ -327,7 +380,7 @@ } } - /** GOTOWA!!! + /** * Method adds polls which are wished to be tracked. * * @param questions @@ -348,7 +401,7 @@ } } - /** GOTOWA !!! + /** * This method returns a List<Poll> of Poll-s, which * are now tracked, for project given by <projectId> parameter. * @@ -360,7 +413,7 @@ return polls.get(projectId); } - /** GOTOWA!!! + /** * Method synchronizes the polls Map with a main polls descriptor file. * * @param portalName @@ -400,7 +453,7 @@ } } - /** GOTOWA!!! + /** * Method creates content of polls main descriptor by * copying values from polls Map. * @@ -413,7 +466,6 @@ Node newProject = doc.createElement(PROJECT); Node projectAttribute = doc.createAttribute(PROJECT_ID); projectAttribute.appendChild(doc.createTextNode(projectId)); - //newProject.appendChild(projectAttribute); newProject.getAttributes().setNamedItem(projectAttribute); for (Poll poll:polls.get(projectId)) { @@ -461,7 +513,7 @@ - /** GOTOWA!!! + /** * Method simply just increments the counter value for * <code>answer</code> for given in parameters <code>projectId</code> * and <code>pollId</code> @@ -500,7 +552,7 @@ } - /** GOTOWA!!! + /** * This method returns true if there is at least one poll, * connected with given in parameter <projectId>, defined. * If not method returns false. @@ -513,7 +565,7 @@ return polls.containsKey(projectId); } - /** GOTOWA!!! + /** * Method returns true if there was change in polls Map. * Othervise it returns false. * @@ -524,7 +576,7 @@ } - /** GOTOWA!!! + /** * Method used for pulling the polls' values and projectIds out from the * xml file nodes to the <String,List<Poll>> Map. * @param nodes @@ -539,6 +591,18 @@ Node projectNode=null,pollNode=null,property=null; // Parsing main download counters descriptor. + /* + * XML schema: + * <polls>1..1 + * <project>0..* attributes(id) + * <poll>1..* + * <question />1..1 + * <answer />0..* + * <id>1..1 + * </poll> + * </project> + * </polls> + */ for (int i = 0; i < nodes.getLength(); i++) { projectNode = nodes.item(i); if (projectNode.getNodeType() == Node.ELEMENT_NODE && projectNode.getNodeName().equals(PROJECT)) { @@ -546,9 +610,12 @@ String tempProjectId = XmlTools.getAttributeValue(projectNode,PROJECT_ID); for (int j=0;j< pollNodes.getLength() ; j++) { pollNode = pollNodes.item(j); + + // temporary variables String tempQuestion = null; Map<String,Integer> answerVotes = Collections.synchronizedMap(new LinkedHashMap<String,Integer>()); String tempPollId = null; + if (pollNode.getNodeType()== Node.ELEMENT_NODE && pollNode.getNodeName().equals(POLL)) { NodeList properties = pollNode.getChildNodes(); for (int k=0;k< properties.getLength() ; k++) { @@ -572,6 +639,7 @@ } } } + // Adding answer to the Map if it was correctly specified. if (answerVotesValue!=null && answerText!=null) { answerVotes.put(answerText,answerVotesValue); } @@ -581,12 +649,14 @@ } } } + // Checking if all information was correctly read and constructing a Poll. if (tempProjectId!=null && tempQuestion!=null && tempPollId!=null && answerVotes.size()>0 ) { if (values.get(tempProjectId)==null) { values.put(tempProjectId,new ArrayList<Poll>()); } values.get(tempProjectId).add(new Poll(answerVotes,tempQuestion,tempPollId)); System.out.println ("JESTEM PRZED CREATE"); + // Adding data file for saving votes if it isn't already created. if (!database.checkForPollFile(tempPollId)) { System.out.println ("JESTEM W CREATE"); database.createNewPollFile(tempPollId); Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -29,14 +29,20 @@ import org.jboss.forge.common.service.ResourceWatcher; import org.jboss.shotoku.ContentManager; - +/** + * This class which implements NodeWatcher interface is looking after changes + * in xml descriptors connected to Polls and executes apropriate methods according + * to the noticed change. + * @author Ryszard Kozmik + * + */ public class PollsWatcher implements NodeWatcher{ ContentManager contentManager; ResourceWatcher rw; /** - * This Collection contains names of projects which contain poll.xml descriptors. + * This Collection contains id names of projects which contain poll.xml descriptors. */ Set<String> pollDescriptors; @@ -65,8 +71,6 @@ PollsDescriptor descriptor = new PollsDescriptor(portalName, contentManager); rw = new ResourceWatcher(contentManager); - // Registering ResourceWatcher to watch for main polls descriptor file change. - //rw.watchResource(PollTools.getMainXmlPath(portalName)); // Getting the project ids names where are poll descriptors. pollDescriptors = descriptor.getPollDescriptors(portalName).keySet(); @@ -84,6 +88,7 @@ * If the object is changed the method returns new object if not returns null. */ public Object nodeUpdate(String portalName, Object currentValue) { + System.out.println ("IN UPDATE CHANGE STATUS: "+((PollsDescriptor)currentValue).hasChanged()); boolean resources = rw.checkResources(); boolean descriptors = false; @@ -96,8 +101,9 @@ return getDescriptor(portalName); } else if (((PollsDescriptor)currentValue).hasChanged()){ System.out.println ("I'M IN POLLWATCHER MAIN XML UPDATE."); - PollsDescriptor descriptor = - (PollsDescriptor)currentValue; + // There was some change on object connected to main polls xml descriptor + // so we must synchronize the object with the file. + PollsDescriptor descriptor = (PollsDescriptor)currentValue; descriptor.synchronizeWithFile(portalName); System.out.println ("I'M IN POLLWATCHER AFTER MAIN XML UPDATE."); return null; @@ -106,6 +112,15 @@ return null; } + /** + * This method checks whether someone hasn't added new poll.xml descriptor to his/her project dir. + * @param desc + * Main PollsDescriptor object. + * @param portalName + * Name of the portal. + * @return + * False if someone added poll.xml descriptor, true if not. + */ private boolean checkForNewResources (PollsDescriptor desc, String portalName) { return pollDescriptors.containsAll(desc.getPollDescriptors(portalName).keySet()); } Modified: trunk/forge/portal-extensions/polls/src/java/org/jboss/forge/polls/PollsPortlet.java =================================================================== --- trunk/forge/portal-extensions/polls/src/java/org/jboss/forge/polls/PollsPortlet.java 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/polls/src/java/org/jboss/forge/polls/PollsPortlet.java 2005-12-20 17:54:10 UTC (rev 1893) @@ -1,3 +1,26 @@ + +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + package org.jboss.forge.polls; import java.io.IOException; @@ -11,6 +34,7 @@ import org.jboss.forge.common.projects.PollTools; import org.jboss.forge.common.projects.ProjectsHelper; import org.jboss.portal.common.context.Context; +import org.jboss.portal.common.context.DelegateContext; import org.jboss.portal.core.model.User; import org.jboss.portal.core.servlet.jsp.PortalJsp; import org.jboss.portlet.JBossActionRequest; @@ -23,28 +47,37 @@ /** - * + * This class is Portlet for Polls. It manages three views - voting, info and detailed info. * @author Ryszard Kozmik * */ public class PollsPortlet extends JBossPortlet { - + /** + * Injected ContentManager object used for accessing content repository. + */ @Inject ContentManager contentManager; + /** + * Method displays one of three views - voting view or scores information view + * or voting detailed information. + */ public void doView(JBossRenderRequest request, JBossRenderResponse response) throws IOException, PortletException { + response.setContentType("text/html"); - + + // resolving name of a portal String portalName = ForgeHelper.getPortalName(request); // Getting name of the project on which the download counter is used. String projectId = ProjectsHelper.getSelectedProjectId(request); ProjectsHelper.prepareRequest(request); - PortletSession session = request.getPortletSession(false); + + // Getting User from request. User user = request.getUser(); if (user!=null) { String userName = user.getUserName(); @@ -52,74 +85,96 @@ } else { System.out.println ("USER IS NULL"); } + + // Checking whether user has given his vote. String voted = request.getParameter(PollTools.VOTED_PARAMETER_NAME); - Context pollContext=null; + + // Adding eventuall errors information to the context. + String errorReq = request.getParameter (PollTools.ERROR_PARAMETER_NAME); + + DelegateContext pollContext=null; PortletRequestDispatcher rd = null; + + // If user has voted information page with scores will be displayed. if (voted!=null && voted.compareTo(PollTools.TRUE)==0) { System.out.println ("USER VOTED"); - // Getting the poll context. + // Getting the poll context for scores information page. pollContext = PollTools.getInfoContext(portalName,projectId,contentManager,response); rd = getPortletContext().getRequestDispatcher( ForgeHelper.createRepoAccessPath(portalName, PollTools .getInfoJsp())); + // If user has requested detailed information about voting on specified poll. } else if (request.getParameter(PollTools.DETAILED_VIEW_REQUEST)!=null && request.getParameter(PollTools.DETAILED_VIEW_REQUEST).compareTo(PollTools.TRUE)==0 ) { System.out.println ("DETAILED VIEW"); + // Getting pollId about which user wants to see detailed information. String pollId = request.getParameter("pollId"); - // Getting the poll context. + + // Getting the poll context for detailed voting information on specified poll. pollContext = PollTools.getDetailsContext(portalName,projectId,pollId,contentManager,response); rd = getPortletContext().getRequestDispatcher( ForgeHelper.createRepoAccessPath(portalName, PollTools .getDetailsJsp())); + // If user hasn't done anything yet the voting page will be displayed. } else { System.out.println ("USER HAVEN'T VOTED"); - // Getting the poll context. - pollContext = PollTools.getVotingContext(portalName,projectId,contentManager,response); + + // Getting the poll voting context. + pollContext = PollTools.getVotingContext(portalName,projectId,errorReq,contentManager,response); rd = getPortletContext().getRequestDispatcher( ForgeHelper.createRepoAccessPath(portalName, PollTools .getVotingJsp())); } - // Displaying. + + // Adding content context to the request attributes. request.setAttribute(PortalJsp.CTX_REQUEST, pollContext); - rd.include(request, response); } + /** + * In this method PollsPortlet is procesing user's vote. + */ @Override public void processAction(JBossActionRequest request, JBossActionResponse response) { + + // Resolving portal name. String portalName = ForgeHelper.getPortalName(request); + + // Getting user's vote and pollId on which the vote was given. String vote=request.getParameter("vote"); String pollId = request.getParameter("pollId"); - PortletURL url=response.createRenderURL(); + + // Getting name of the project on which the PollsPortlet is used. + String projectId = request.getParameter(ProjectsHelper.PROJECT_URL_PARAM); + System.out.println ("HELLO I'M IN POLLPORTLET VOTE: "+vote); String userId = request.getUser()==null?"user"+Integer.toString((int)(Math.random()*100000)):request.getUser().getUserName(); - - // Getting name of the project on which the download counter is used. - String projectId = ProjectsHelper.getSelectedProjectId(request); + System.out.println("PROJECTID:"+projectId); - if (projectId==null){ - projectId = request.getParameter(ProjectsHelper.PROJECT_URL_PARAM); - System.out.println ("2 PROJECTID:"+projectId); - if (projectId==null) { - projectId="jbosswiki"; - } - } - + if (vote != null && !vote.equals("")) { - response.setRenderParameter(PollTools.VOTED_PARAMETER_NAME,PollTools.TRUE); + + System.out.println ("VOTING"); System.out.println ("VOTE: "+vote+"POLLID:"+pollId+" userId:"+userId+" PROJECTID: "+projectId); + + // Trying to vote. If it returns true it means that vote was given successfully + // if not it means that user has already voted on this poll. if (PollTools.getDesc(portalName,contentManager).vote(vote,pollId,userId,projectId)) { + // Adding parameter to url which tells doView to render scores information page. + response.setRenderParameter(PollTools.VOTED_PARAMETER_NAME,PollTools.TRUE); System.out.println ("ZAGŁOSOWANO"); } else { + response.setRenderParameter(PollTools.ERROR_PARAMETER_NAME,"You have already voted on that poll"); System.out.println ("GŁOS ODRZUCONO"); } } else { System.out.println ("USER ZONK"); + response.setRenderParameter(PollTools.ERROR_PARAMETER_NAME,"You haven't given your vote."); } } Modified: trunk/forge/portal-extensions/portal-default/src/web/WEB-INF/default-portal.xml =================================================================== --- trunk/forge/portal-extensions/portal-default/src/web/WEB-INF/default-portal.xml 2005-12-20 17:16:06 UTC (rev 1892) +++ trunk/forge/portal-extensions/portal-default/src/web/WEB-INF/default-portal.xml 2005-12-20 17:54:10 UTC (rev 1893) @@ -330,13 +330,13 @@ <height>1</height> <window-state>normal</window-state> </window> - <window> + <!-- <window> <window-name>PollsPortletWindowDefaultDownloads</window-name> <instance-ref>polls.PollsPortlet.PollsPortletInstance</instance-ref> <region>center</region> <height>2</height> <window-state>normal</window-state> - </window> + </window>--> </page> <page> |