001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.survey;
020:
021: import java.io.IOException;
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.util.Vector;
027:
028: import javax.portlet.PortletException;
029: import javax.portlet.RenderRequest;
030: import javax.portlet.RenderResponse;
031:
032: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
033: import com.nabhinc.portlet.mvcportlet.core.RenderConfig;
034: import com.nabhinc.portlet.mvcportlet.core.RenderProcessor;
035: import com.nabhinc.util.db.DBUtil;
036:
037: /**
038: * Survey Results render processor.
039: *
040: * @author Padmanabh Dabke
041: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
042: */
043: public class SurveyResultsRenderProcessor extends BaseRequestProcessor
044: implements RenderProcessor {
045:
046: /* (non-Javadoc)
047: * @see com.nabhinc.portlet.mvcportlet.core.RenderProcessor#process(javax.portlet.RenderRequest, javax.portlet.RenderResponse, com.nabhinc.portlet.mvcportlet.core.RenderConfig)
048: */
049: public String process(RenderRequest request,
050: RenderResponse response, RenderConfig renderConfig)
051: throws PortletException, IOException {
052:
053: String surveyIDStr = request.getParameter("survey_id");
054: String surveySQL = "SELECT surveyid, surveytitle FROM SB_SURVEYS WHERE surveyid = ?";
055: String votesSQL = "SELECT SUM(optionvotes) FROM SB_SURVEY_DATA WHERE surveyid = ?";
056: String optionsSQL = "SELECT optionvotes, optiontext FROM SB_SURVEY_DATA WHERE surveyid = ? ORDER BY optionvotes DESC";
057: String commentsSQL = "SELECT ctime, subject, comments, username FROM SB_SURVEY_COMMENTS WHERE surveyid = ?";
058:
059: Connection conn = null;
060: ResultSet resultSet = null;
061: PreparedStatement st = null;
062:
063: if (surveyIDStr == null) {
064: throw new PortletException(
065: "Missing required request parameter: survey_id.");
066: }
067:
068: try {
069: conn = brpConfig.getDataSource().getConnection();
070:
071: // First get the parameters for the survey.
072: st = conn.prepareStatement(surveySQL);
073: int surveyID = Integer.parseInt(surveyIDStr);
074: st.setInt(1, surveyID);
075: resultSet = st.executeQuery();
076: if (!resultSet.next())
077: return "no-such-survey";
078: request
079: .setAttribute("survey.title", resultSet
080: .getString(2));
081:
082: st.close();
083: st = null;
084: resultSet.close();
085: resultSet = null;
086:
087: // Get total votes for the survey
088: st = conn.prepareStatement(votesSQL);
089: st.setInt(1, surveyID);
090: resultSet = st.executeQuery();
091: int numVotes = 0;
092: if (resultSet.next()) {
093: numVotes = resultSet.getInt(1);
094: request.setAttribute("survey.votes", Integer
095: .toString(numVotes));
096: } else {
097: request.setAttribute("survey.votes", "0");
098: numVotes = 0;
099: }
100:
101: if (numVotes == 0)
102: return "success";
103:
104: // Get options for the selected survey
105: Vector results = new Vector(10);
106: st = conn.prepareStatement(optionsSQL);
107: st.setInt(1, surveyID);
108: resultSet = st.executeQuery();
109: while (resultSet.next()) {
110: int optionVotes = resultSet.getInt(1);
111: SurveyResultDataPoint dataPoint = new SurveyResultDataPoint(
112: resultSet.getString(2), optionVotes,
113: ((float) optionVotes) / numVotes);
114: results.addElement(dataPoint);
115: }
116: request.setAttribute("survey.results", results);
117:
118: st.close();
119: st = null;
120: resultSet.close();
121: resultSet = null;
122:
123: // Get comments associated with this survey
124: Vector comments = new Vector(10);
125: st = conn.prepareStatement(commentsSQL);
126: st.setInt(1, surveyID);
127: resultSet = st.executeQuery();
128: while (resultSet.next()) {
129: Object[] record = new Object[4];
130: record[0] = resultSet.getTimestamp(1);
131: record[1] = resultSet.getString(2);
132: record[2] = resultSet.getString(3);
133: record[3] = resultSet.getString(4);
134: comments.addElement(record);
135: }
136: request.setAttribute("survey.comments", comments);
137:
138: } catch (SQLException ex) {
139: throw new PortletException("Database exception.", ex);
140: } finally {
141: DBUtil.close(resultSet);
142: DBUtil.close(st);
143: DBUtil.close(conn);
144: }
145:
146: return null;
147: }
148:
149: }
|