001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2003 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import java.util.*;
023: import java.io.*;
024: import com.ecyrd.jspwiki.*;
025: import com.ecyrd.jspwiki.attachment.AttachmentManager;
026: import com.ecyrd.jspwiki.attachment.Attachment;
027: import com.ecyrd.jspwiki.providers.ProviderException;
028:
029: import org.apache.log4j.Logger;
030:
031: /**
032: * Implements a simple voting system. WARNING: The storage method is
033: * still experimental; I will probably change it at some point.
034: */
035:
036: public class VotePlugin implements WikiPlugin {
037: static Logger log = Logger.getLogger(VotePlugin.class);
038:
039: public static final String ATTACHMENT_NAME = "VotePlugin.properties";
040:
041: public static final String VAR_VOTES = "VotePlugin.votes";
042:
043: /**
044: * +1 for yes, -1 for no.
045: *
046: * @return number of votes, or -1 if an error occurred.
047: */
048: public int vote(WikiContext context, int vote) {
049: getVotes(context);
050:
051: if (vote > 0) {
052: int nVotes = getYesVotes(context);
053:
054: putVotes(context, "yes", ++nVotes);
055:
056: return nVotes;
057: } else if (vote < 0) {
058: int nVotes = getNoVotes(context);
059:
060: putVotes(context, "no", ++nVotes);
061:
062: return nVotes;
063: }
064:
065: return -1; // Error
066: }
067:
068: private void putVotes(WikiContext context, String yesno, int nVotes) {
069: WikiPage page = context.getPage();
070:
071: Properties props = getVotes(context);
072:
073: props.setProperty(yesno, Integer.toString(nVotes));
074:
075: page.setAttribute(VAR_VOTES, props);
076:
077: storeAttachment(context, props);
078: }
079:
080: private void storeAttachment(WikiContext context, Properties props) {
081: try {
082: ByteArrayOutputStream out = new ByteArrayOutputStream();
083: props
084: .store(out,
085: "JSPWiki Votes plugin stores its votes here. Don't modify!");
086:
087: out.close();
088:
089: AttachmentManager attmgr = context.getEngine()
090: .getAttachmentManager();
091:
092: Attachment att = findAttachment(context);
093:
094: InputStream in = new ByteArrayInputStream(out.toByteArray());
095:
096: attmgr.storeAttachment(att, in);
097:
098: in.close();
099: } catch (Exception ex) {
100: log.error("Unable to write properties", ex);
101: }
102: }
103:
104: private Attachment findAttachment(WikiContext context)
105: throws ProviderException {
106: Attachment att = context.getEngine().getAttachmentManager()
107: .getAttachmentInfo(context, ATTACHMENT_NAME);
108:
109: if (att == null) {
110: att = new Attachment(context.getEngine(), context.getPage()
111: .getName(), ATTACHMENT_NAME);
112: }
113:
114: return att;
115: }
116:
117: private Properties getVotes(WikiContext context) {
118: WikiPage page = context.getPage();
119:
120: Properties props = (Properties) page.getAttribute(VAR_VOTES);
121:
122: //
123: // Not loaded yet
124: //
125: if (props == null) {
126: props = new Properties();
127:
128: AttachmentManager attmgr = context.getEngine()
129: .getAttachmentManager();
130:
131: try {
132: Attachment att = attmgr.getAttachmentInfo(context,
133: ATTACHMENT_NAME);
134:
135: if (att != null) {
136: props.load(attmgr.getAttachmentStream(att));
137: }
138: } catch (Exception e) {
139: log.error("Unable to load attachment ", e);
140: }
141: }
142:
143: return props;
144: }
145:
146: private int getYesVotes(WikiContext context) {
147: Properties props = getVotes(context);
148:
149: return TextUtil.getIntegerProperty(props, "yes", 0);
150: }
151:
152: private int getNoVotes(WikiContext context) {
153: Properties props = getVotes(context);
154:
155: return TextUtil.getIntegerProperty(props, "no", 0);
156: }
157:
158: public String execute(WikiContext context, Map params)
159: throws PluginException {
160: String posneg = (String) params.get("value");
161:
162: if (TextUtil.isPositive(posneg)) {
163: return Integer.toString(getYesVotes(context));
164: }
165:
166: return Integer.toString(getNoVotes(context));
167: }
168: }
|