From: <jbo...@li...> - 2005-11-16 23:02:42
|
Author: unibrew Date: 2005-11-16 18:02:33 -0500 (Wed, 16 Nov 2005) New Revision: 1582 Added: 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/PollsDescriptor.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 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/ProjectsHelper.java Log: [JBLAB-407] Core files for PollPortlet task. [JBLAB-263] Small comments update. Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java 2005-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Counter.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -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; Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java 2005-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCounterTools.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -161,7 +161,7 @@ * Method constructs path to the DownloadCounterPortlet JSP view file. * * @param portalName - * @return Path to the JSP DownloadCounterPortlet view file. + * @return Path to the DownloadCounterPortlet's JSP view file. */ public static String getJspCmPath() { return FORGE_FILE_ACCESS_DIR + File.separator + DOWNLOADCOUNTER_JSP; Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 2005-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersDescriptor.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -119,7 +119,7 @@ downloadCounters=getValuesFromNodes(nodes); // Getting Map containing pairs of ProjectId and shotoku Node - // which refers to project download counterdescriptor. + // which refers to projects' download counter descriptor. Map <String,org.jboss.shotoku.Node> descriptors = getDownloadDescriptors(portalName); // Synchronizing tracked links in downloadConters with links from @@ -142,7 +142,7 @@ /** * This method searches through projects direcories in order to find all download counter descriptors. - * Method returns Map containging pairs of String projectId and + * Method returns Map containing pairs of String projectId and * org.jboss.shotoku.Node download counter descriptor file node. * @param portalName * @return A Map<String,org.jboss.shotoku.Node> containing project ids with their download @@ -185,8 +185,8 @@ } /** - * Method removes links which was disabled from counting - * and adds those which was added in their project download counter descriptor. + * Method removes links which were disabled from counting + * and adds those which was added in their project's download counter descriptor. * * @param counters * This Map<Strin,org.jboss.shotoku.Node> contains project ids with their download @@ -233,7 +233,7 @@ } // Changing status to false becouse downloadCounters Map - // is now synchronized with download counters descriptors. + // is now synchronized with projects' download counter descriptors. changeStatus=false; } @@ -364,7 +364,7 @@ * @param link * Counter value for this <code>link</code> will be incremented. */ - synchronized public void increment (String link) { + synchronized public void increment (final String link) { if (downloadCounters.get(link)!=null) { // Status change to inform about counters modification. changeStatus=true; @@ -378,7 +378,7 @@ * If there is no link for this <projectId> tracked method returns false. * * @param projectId - * The id of a project for which occurence of the links will checked. + * The id of a project for which occurence of the links will bechecked. * @return Bool value whether there is a link to this project tracked or not. */ public synchronized boolean checkForProjectCounters(String projectId) { 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/DownloadCountersWatcher.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -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.Collection; @@ -35,7 +58,7 @@ /** * Method constructs the DownloadCountersDescriptor object registers it - * with the ResourceWatcher as well as all found DownloadCounterDescriptor's. + * with the ResourceWatcher as well as all found DownloadCounterDescriptors. * * @param portalName * @return DownloadCountersDescriptor object. Added: 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/Poll.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -0,0 +1,46 @@ +package org.jboss.forge.common.projects; + +public class Poll { + + String question; + long positive; + long negative; + + public Poll (long positive, long negative, String question) { + this.positive = positive; + this.negative = negative; + this.question = question; + } + + public Poll (String question) { + this.question = question; + } + + public void incrementPositive () { + positive++; + } + + public void incrementNegative () { + negative++; + } + + @Override + public boolean equals(Object obj) { + return ((Poll)obj).getQuestion().equals(question); + } + + public long getNegative() { + return negative; + } + + public long getPositive() { + return positive; + } + + public String getQuestion() { + return question; + } + + + +} Added: 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollDescriptor.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -0,0 +1,100 @@ + +/* + * 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.io.IOException; +import java.io.InputStream; +import java.util.LinkedList; +import java.util.List; + +import org.apache.xerces.parsers.DOMParser; +import org.jboss.forge.common.XmlTools; +import org.jboss.forge.common.projects.XmlInputFactory.XmlNotFoundException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class PollDescriptor extends AbstractDescriptor { + + /** + * This variable contains all polls containing questions from the xml descriptor. + */ + private List<Poll> polls; + + /** + * Name of tag in poll.xml containing poll's question. + */ + public static final String QUESTION = "question"; + + /** + * Name of tag in poll.xml containing poll. + */ + public static final String POLL = "poll"; + + + public PollDescriptor (org.jboss.shotoku.Node pollDescNode) + throws SAXException, IOException,XmlNotFoundException { + + // Opening the download project's poll descriptor. + DOMParser parser = new DOMParser(); + InputStream is = pollDescNode.getContentInputStream(); + parser.parse(new InputSource(is)); + Document doc = parser.getDocument(); + Node n=null,property=null; + NodeList nodes = doc.getDocumentElement().getChildNodes(); + + // Parsing and gettting polls' questions. + polls = new LinkedList<Poll>(); + for (int i = 0; i < nodes.getLength(); i++) { + n = nodes.item(i); + if (n.getNodeType() == Node.ELEMENT_NODE) { + if (n.getNodeName().equals(POLL)) { + NodeList counterProps = n.getChildNodes(); + for (int j=0;j< counterProps.getLength() ; j++) { + property = counterProps.item(j); + if (property.getNodeType()== Node.ELEMENT_NODE){ + if (property.getNodeName().equals(QUESTION) && XmlTools.unmarshallText(property) != null + && !XmlTools.unmarshallText(property).trim().equals("")) { + polls.add(new Poll(XmlTools.unmarshallText(property).trim())); + } + } + } + } + } + } + + } + + /** + * Method simply returns polls builded from xml descriptor. + * @return + * List<Poll> of polls achieved from the descriptor. + */ + public List<Poll> getPolls () { + return polls; + } + +} Added: 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollTools.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -0,0 +1,73 @@ + +/* + * 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.io.File; + +public class PollTools { + + /** + * Dir in portal where the JSP view file is. + */ + public static final String POLLS_DIR="polls"; + + /** + * Name of JSP view file. + */ + public static final String POLLS_JSP="normal.jsp"; + + /** + * Method constructs path to polls descriptor. + * + * @param portalName + * @return Path to polls descriptor. + */ + public static String getMainXmlPath (String portalName) { + return portalName + File.separator + ProjectsHelper.MEMBERS_DIR + File.separator + ProjectsHelper.POLLS_DESC; + } + + /** + * Method constructs path to project's poll descriptor. + * + * @param portalName + * @param projectId + * Project id name for which the poll descriptor path will be constructed. + * @return Path to project's poll descriptor. + */ + public static String getProjectXmlPath(String portalName,String projectId) { + return portalName+File.separator+ProjectsHelper.MEMBERS_DIR+File.separator+projectId+ + File.separator+ProjectsHelper.POLL_DESC; + } + + /** + * Method constructs path to the PollPortlet JSP view file. + * + * @param portalName + * @return Path to the PollPortlet's JSP view file. + */ + public static String getJspCmPath() { + return POLLS_DIR + File.separator + POLLS_JSP; + } + +} Added: 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsDescriptor.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -0,0 +1,490 @@ + +/* + * 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.io.File; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.xerces.parsers.DOMParser; +import org.jboss.forge.common.XmlTools; +import org.jboss.shotoku.ContentManager; +import org.jboss.shotoku.Directory; +import org.jboss.shotoku.exceptions.ResourceDoesNotExist; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +public class PollsDescriptor extends AbstractDescriptor { + + + /** + * Name of tag in polls descriptor containing poll's value of positive votes. + */ + public static final String POSITIVE_VOTES = "positive"; + + /** + * Name of tag in polls descriptor containing poll's value of negative votes. + */ + public static final String NEGATIVE_VOTES = "negative"; + + /** + * Name of tag in polls descriptor containing poll's question. + */ + public static final String QUESTION = "question"; + + /** + * Name of tag in polls descriptor containing project id name. + */ + public static final String PROJECT = "project"; + + /** + * Name of tag in polls descriptor containing project id name. + */ + public static final String PROJECT_ID = "id"; + + /** + * Name of tag in polls descriptor containing poll. + */ + public static final String POLL = "poll"; + + /** + * This Map contains all polls for the portal. + * Keys are project id names and values are Lists of polls defined for that project. + */ + private Map<String,List<Poll>> polls; + + /** + * ContentManager is used for getting to file resources. + */ + private ContentManager contentManager; + + /** + * This boolean variable turns to true if one of the polls + * has been incremented. If value is true the polls Map + * will be synchronized with a main xml polls descriptor. + */ + 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 + * from main xml polls descriptor. + * + * @param portalName + * @param contentManager + */ + public PollsDescriptor (final String portalName, ContentManager contentManager) { + + // Just initializing the changeStatus variable + changeStatus=false; + + // Getting the path to main polls descriptor. + String pathToPollsXml = File.separator + PollTools.getMainXmlPath(portalName); + + // Saving the ContentManager. + this.contentManager = contentManager; + + try { + + // Parsing main download counters descriptor. + DOMParser parser = new DOMParser(); + InputStream is = contentManager.getNode(pathToPollsXml).getContentInputStream(); + parser.parse(new InputSource(is)); + Document doc = parser.getDocument(); + NodeList nodes = doc.getDocumentElement().getChildNodes(); + polls=getValuesFromNodes(nodes); + + // Getting Map containing pairs of ProjectId and shotoku Node + // which refers to projects' poll descriptor. + Map <String,org.jboss.shotoku.Node> descriptors = getPollDescriptors(portalName); + + // Synchronizing polls Map with polls received from + // projects' poll descriptors. + synchronizePolls(descriptors); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** GOTOWA!!! + * Method only returns all polls. + * @return Returns a Map containing polls. + */ + public Map<String,List<Poll>> getPollsMap () { + return polls; + } + + + /** 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. + * @param portalName + * @return A Map<String,org.jboss.shotoku.Node> containing project ids with their poll's Node. + */ + public Map<String,org.jboss.shotoku.Node> getPollDescriptors (String portalName) { + Map<String,org.jboss.shotoku.Node> nodes = new Hashtable<String,org.jboss.shotoku.Node>(); + + List<Directory> membersProjectDirs=null; + try { + // Getting the members dir. + Directory membersDir = contentManager.getDirectory(portalName+File.separator+ProjectsHelper.MEMBERS_DIR); + + // Getting all projects dirs in members directory. + membersProjectDirs = membersDir.getDirectories(); + } catch (ResourceDoesNotExist exception) { + System.out.println ("[POLLSDESCRIPTOR] Cannot get members dir' project folders!"); + exception.printStackTrace(); + return nodes; + } + + for (Directory projectDir:membersProjectDirs) { + for (org.jboss.shotoku.Node n:projectDir.getNodes().toList()) { + // Checking if node is a node containing poll descriptor. + if (n.getName().compareTo(ProjectsHelper.POLL_DESC)==0) { + String projectId = projectDir.getName(); + try { + projectId = projectDir.getName(); + org.jboss.shotoku.Node poll = projectDir.getNode(ProjectsHelper.POLL_DESC); + nodes.put(projectId,poll); + break; + } catch (ResourceDoesNotExist e) { + System.out.println ("[POLLSDESCRIPTOR] Failed to get "+projectId+" poll descriptor!"); + } + } + } + + } + return nodes; + } + + /** GOTOWA!!! + * Method removes polls which were disabled from working + * and adds those which was added in their project's poll descriptor. + * + * @param projectPolls + * This Map<Strin,org.jboss.shotoku.Node> contains project ids with their download + * counter descriptor's Node. + */ + private void synchronizePolls (Map<String,org.jboss.shotoku.Node> projectPolls) { + + // Iterating through projects nodes containing poll descriptors. + for (String projectId:projectPolls.keySet()){ + try { + // Parsing project's poll descriptor. + PollDescriptor pollDesc = + new PollDescriptor(projectPolls.get(projectId)); + + // Getting list of polls from polls Map for a given projectId name. + List<Poll> xmlProjectPolls = getProjectPolls(projectId); + + // Getting the list of polls from project's poll descriptor. + List<Poll> descPolls = pollDesc.getPolls(); + + // This list will be used for collecting questions which must be + // removed from polls Map. + List<Poll> pollsToDelete = new LinkedList<Poll>(); + + // Searching for polls to remove and polls which are already added. + for (Poll poll:xmlProjectPolls) { + if (descPolls.contains(poll)) { + descPolls.remove(poll); + } else { + pollsToDelete.add(poll); + } + } + + removePollsFromVoting(pollsToDelete); + + addPollsForVoting (descPolls,projectId); + + } catch (Exception e) { + System.out.println ("[POLLSDESCRIPTOR] Problem with opening project "+ + projectId+" poll descriptor."); + e.printStackTrace(); + } + + } + + // Changing status to false becouse polls Map + // is now synchronized with projects' polls descriptors. + changeStatus=false; + } + + /** GOTOWA!!! + * Method removes polls specified in <code>pollsToDelete</code> + * from tracking their votes. + * + * @param pollsToDelete + * List<String> of polls which should be removed. + */ + private synchronized void removePollsFromVoting (List<Poll> pollsToDelete) { + // Removing polls which shouldn't be tracked any more. + for (Poll poll:pollsToDelete) { + polls.remove(poll); + } + } + + /** GOTOWA!!! + * Method adds polls which are wished to be tracked. + * + * @param questions + * List<Poll> of polls wished to be tracked + * @param projectId + * Project id name for which polls containing the questions are added. + */ + private synchronized void addPollsForVoting (List<Poll> newPolls,String projectId) { + // Adding new project id name with ArrayList<Poll> values. + if (polls.get(projectId)==null) { + polls.put(projectId,new ArrayList<Poll>()); + } + for (Poll poll:newPolls) { + if (!polls.get(projectId).contains(poll)){ + polls.get(projectId).add(poll); + } + } + } + + /** GOTOWA !!! + * This method returns a List<Poll> of Poll-s, which + * are now tracked, for project given by <projectId> parameter. + * + * @param projectId + * Id of a project for which polls will be searched. + * @return List<Poll> Containing tracked questions or null. + */ + private synchronized List<Poll> getProjectPolls (String projectId) { + return polls.get(projectId); + } + + /** GOTOWA!!! + * Method synchronizes the polls Map with a main polls descriptor file. + * + * @param portalName + */ + public void synchronizeWithFile(String portalName) { + + String pathToPollsXml = File.separator + PollTools.getMainXmlPath(portalName); + + try { + DOMParser parser = new DOMParser(); + + // Opening main polls descriptor file. + InputStream is = contentManager.getNode(pathToPollsXml).getContentInputStream(); + parser.parse(new InputSource(is)); + Document doc = parser.getDocument(); + Node mainTag = doc.createElement("polls"); + + // Replacing old main descriptor tag with new, empty. + doc.replaceChild(mainTag,doc.getDocumentElement()); + + // Filling the descriptor with new polls. + createDocument(doc); + + DomToXmlTransformer xht = new DomToXmlTransformer(); + + // Getting string containing whole xml. + String xmlString = xht.transformNode(doc.getDocumentElement()); + + // Saving the main polls xml descriptor with a new content. + org.jboss.shotoku.Node xmlFile = contentManager.getNode(pathToPollsXml); + xmlFile.setContent(xmlString); + xmlFile.save ("[Polls] Main xml descriptor file update."); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** GOTOWA!!! + * Method creates content of polls main descriptor by + * copying values from polls Map. + * + * @param doc + * This Document object points to polls main descriptor xml. + */ + private void createDocument (Document doc) { + Set<String> projects = polls.keySet(); + for (String projectId:projects) { + Node newProject = doc.createElement(PROJECT); + Node projectAttribute = doc.createAttribute(PROJECT_ID); + projectAttribute.appendChild(doc.createTextNode(projectId)); + newProject.appendChild(projectAttribute); + for (Poll poll:polls.get(projectId)) { + Node newPoll = doc.createElement(POLL); + + Node newQuestion = doc.createElement(QUESTION); + Node newQuestionText = doc.createTextNode(poll.getQuestion()); + newQuestion.appendChild(newQuestionText); + + Node newPositiveVotes = doc.createElement(POSITIVE_VOTES); + Node newPositiveVotesText = doc.createTextNode(Long.toString(poll.getPositive())); + newPositiveVotes.appendChild(newPositiveVotesText); + + Node newNegativeVotes = doc.createElement(NEGATIVE_VOTES); + Node newNegativeVotesText = doc.createTextNode(Long.toString(poll.getNegative())); + newNegativeVotes.appendChild(newNegativeVotesText); + + newPoll.appendChild(newQuestion); + newPoll.appendChild(newPositiveVotes); + newPoll.appendChild(newNegativeVotes); + + newProject.appendChild(newPoll); + } + + doc.getDocumentElement().appendChild(newProject); + } + } + + /** GOTOWA!!! + * Method simply just increments the positive counter value for + * given in parameters <code>projectId question</code>. + * + * @param question + * Poll's question. + * @param projectId + * Project id name for which is this question. + */ + synchronized public void incrementPositive (String question,String projectId) { + List<Poll> projectPolls = polls.get(projectId); + if (projectPolls!=null) { + int position = projectPolls.indexOf(new Poll(question)); + if (position!=-1) { + // Status change to inform about counters modification. + changeStatus=true; + projectPolls.get(position).incrementPositive(); + } + } + } + + /** GOTOWA!!! + * Method simply just increments the negative counter value for + * given in parameters <code>projectId question</code>. + * + * @param question + * Poll's question. + * @param projectId + * Project id name for which is this question. + */ + synchronized public void incrementNegative (String question,String projectId) { + List<Poll> projectPolls = polls.get(projectId); + if (projectPolls!=null) { + int position = projectPolls.indexOf(new Poll(question)); + if (position!=-1) { + // Status change to inform about counters modification. + changeStatus=true; + projectPolls.get(position).incrementNegative(); + } + } + } + + + + /** GOTOWA!!! + * This method returns true if there is at least one poll, + * connected with given in parameter <projectId>, defined. + * If not method returns false. + * + * @param projectId + * The id of a project for which occurence of the polls will be checked. + * @return boolean value whether there is a poll defined for this project or not. + */ + public synchronized boolean checkForProjectPolls(String projectId) { + return polls.containsKey(projectId); + } + + /** GOTOWA!!! + * Method returns true if there was change in polls Map. + * Othervise it returns false. + * + * @return Returns true if the status of polls has changed. + */ + public boolean hasChanged () { + return changeStatus; + } + + + /** GOTOWA!!! + * Method used for pulling the polls' values and projectIds out from the + * xml file nodes to the <String,List<Poll>> Map. + * @param nodes + * NodeList containing conent of xml descriptor + * @return + * Map<String,List<Poll>> containing polls read from the xml nodes. + */ + private static Map<String,List<Poll>> getValuesFromNodes (NodeList nodes) { + Map<String,List<Poll>> values = new Hashtable<String,List<Poll>>(); + + // Temporary variables used for parsing. + Node projectNode=null,pollNode=null,property=null; + + // Parsing main download counters descriptor. + for (int i = 0; i < nodes.getLength(); i++) { + projectNode = nodes.item(i); + if (projectNode.getNodeType() == Node.ELEMENT_NODE && projectNode.getNodeName().equals(PROJECT)) { + NodeList pollNodes = projectNode.getChildNodes(); + String tempProjectId = XmlTools.getAttributeValue(projectNode,PROJECT_ID); + String tempQuestion = null; + String tempPositiveVotes = null; + String tempNegativeVotes = null; + for (int j=0;j< pollNodes.getLength() ; j++) { + pollNode = pollNodes.item(j); + if (pollNode.getNodeType()== Node.ELEMENT_NODE && pollNode.getNodeName().equals(POLL)){ + NodeList properties = pollNode.getChildNodes(); + for (int k=0;k< properties.getLength() ; k++) { + property = properties.item(k); + if (property.getNodeType()== Node.ELEMENT_NODE){ + String nodeTextCnt = XmlTools.unmarshallText(property); + if (property.getNodeName().equals(QUESTION) && nodeTextCnt != null && !nodeTextCnt.trim().equals("")) { + tempQuestion = nodeTextCnt.trim(); + } else if (property.getNodeName().equals(POSITIVE_VOTES) && nodeTextCnt !=null && !nodeTextCnt.trim().equals("")) { + tempPositiveVotes = nodeTextCnt.trim(); + } else if (property.getNodeName().equals(NEGATIVE_VOTES) && nodeTextCnt !=null && !nodeTextCnt.trim().equals("")) { + tempNegativeVotes = nodeTextCnt.trim(); + } + } + } + } + } + if (tempProjectId!=null && tempQuestion!=null && tempPositiveVotes!=null && tempNegativeVotes!=null) { + if (values.get(tempProjectId)==null) { + values.put(tempProjectId,new ArrayList<Poll>()); + } + values.get(tempProjectId).add(new Poll(tempQuestion)); + } + } + } + return values; + } + +} Added: 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-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/PollsWatcher.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -0,0 +1,95 @@ + +/* + * 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.Collection; + +import org.jboss.forge.common.service.NodeWatcher; +import org.jboss.forge.common.service.ResourceWatcher; +import org.jboss.shotoku.ContentManager; + + +public class PollsWatcher implements NodeWatcher{ + + ContentManager contentManager; + ResourceWatcher rw; + + /** + * Simple constructor saving ContentManager given in parameter + * <code>conentManager</code> + * @param contentManager + */ + public PollsWatcher (ContentManager contentManager) { + this.contentManager = contentManager; + } + + + public Object init(String portalName) { + return getDescriptor(portalName); + } + + /** + * Method constructs the PollsDescriptor object registers it + * with the ResourceWatcher as well as all found PollDescriptors. + * + * @param portalName + * @return PollsDescriptor object. + */ + private Object getDescriptor (String portalName) { + PollsDescriptor descriptor = + new PollsDescriptor(portalName, contentManager); + rw = new ResourceWatcher(contentManager); + // Registering ResourceWatcher to watch for main polls descriptor file change. + rw.watchResource(DownloadCounterTools.getMainXmlPath(portalName)); + + // Getting the project ids names where are poll descriptors. + Collection<String> projects = descriptor.getPollDescriptors(portalName).keySet(); + + // Adding found project poll descriptors to the ResourceWatcher to watch + // for their changes. + for (String id:projects) { + rw.watchResource(PollTools.getProjectXmlPath(portalName,id)); + } + return descriptor; + } + + /** + * Method updates main polls descriptor if there is such a need. + * If the object is changed the method returns new object if not returns null. + */ + public Object nodeUpdate(String portalName, Object currentValue) { + if (currentValue==null || rw.checkResources()) { + return getDescriptor(portalName); + } else if (((PollsDescriptor)currentValue).hasChanged()){ + PollsDescriptor descriptor = + (PollsDescriptor)currentValue; + descriptor.synchronizeWithFile(portalName); + return null; + } + return null; + } + + + +} Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2005-11-16 22:44:49 UTC (rev 1581) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/projects/ProjectsHelper.java 2005-11-16 23:02:33 UTC (rev 1582) @@ -84,6 +84,8 @@ protected final static String DOWNLOADS_DESC = "downloads.xml"; protected final static String DOWNLOADCOUNTER_DESC = "counter.xml"; protected final static String DOWNLOADCOUNTERMAIN_DESC = "counters.xml"; + protected final static String POLLS_DESC = "polls.xml"; + protected final static String POLL_DESC = "poll.xml"; /** * <code>MEMBERS_DIRECTORY</code> - base repository directory in which |