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 org.w3c.dom.Element;
033:
034: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
035: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
036: import com.nabhinc.portlet.mvcportlet.core.RenderConfig;
037: import com.nabhinc.portlet.mvcportlet.core.RenderProcessor;
038: import com.nabhinc.util.XMLUtil;
039: import com.nabhinc.util.db.DBUtil;
040:
041: /**
042: *
043: *
044: * @author Padmanabh Dabke
045: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
046: */
047: public class ViewIndexRenderProcessor extends BaseRequestProcessor
048: implements RenderProcessor {
049: private String virpCurrentSurveySQL = "SELECT TOP 1 surveyid, surveytitle FROM SB_SURVEYS ORDER BY stime DESC";
050: private String virpVotesSQL = "SELECT SUM(optionvotes) FROM SB_SURVEY_DATA WHERE surveyid = ?";
051: private String virpOptionsSQL = "SELECT optionnum, optiontext FROM SB_SURVEY_DATA WHERE surveyid = ?";
052: private String virpSurveySQL = "SELECT surveyid, surveytitle FROM SB_SURVEYS WHERE surveyid = ?";
053:
054: /**
055: * Looks for config elements named: survey-sql, votes-sql, options-sql
056: */
057: public void init(Element arg0, ControllerPortletConfig cpConfig)
058: throws PortletException {
059: super .init(arg0, cpConfig);
060: String temp = XMLUtil.getSubElementText(arg0,
061: "current-survey-sql");
062: if (temp != null)
063: virpCurrentSurveySQL = temp;
064:
065: temp = XMLUtil.getSubElementText(arg0, "survey-sql");
066: if (temp != null)
067: virpSurveySQL = temp;
068:
069: temp = XMLUtil.getSubElementText(arg0, "votes-sql");
070: if (temp != null)
071: virpVotesSQL = temp;
072:
073: temp = XMLUtil.getSubElementText(arg0, "options-sql");
074: if (temp != null)
075: virpOptionsSQL = temp;
076:
077: }
078:
079: /* (non-Javadoc)
080: * @see com.nabhinc.portlet.mvcportlet.core.RenderProcessor#process(javax.portlet.RenderRequest, javax.portlet.RenderResponse, com.nabhinc.portlet.mvcportlet.core.RenderConfig)
081: */
082: public String process(RenderRequest request,
083: RenderResponse response, RenderConfig renderConfig)
084: throws PortletException, IOException {
085:
086: Connection conn = null;
087: ResultSet resultSet = null;
088: PreparedStatement st = null;
089: int surveyID = -1;
090: boolean surveyIDSpecified = false;
091: // Check if the survey ID is specified
092: String surveyIDStr = request.getParameter("survey_id");
093: String surveySQL = virpCurrentSurveySQL;
094: if (surveyIDStr != null) {
095: surveyID = Integer.parseInt(surveyIDStr);
096: surveyIDSpecified = true;
097: surveySQL = virpSurveySQL;
098: }
099:
100: try {
101: conn = brpConfig.getDataSource().getConnection();
102:
103: // First get the parameters for the survey.
104: st = conn.prepareStatement(surveySQL);
105: if (surveyIDSpecified) {
106: st.setInt(1, surveyID);
107: }
108: resultSet = st.executeQuery();
109: if (!resultSet.next()) {
110: if (surveyIDSpecified) {
111: // This may mean that the survey the user was trying to
112: // access was deleted, so let's show the default survey
113: resultSet.close();
114: st.close();
115: st = conn.prepareStatement(virpCurrentSurveySQL);
116: resultSet = st.executeQuery();
117: if (!resultSet.next())
118: return null;
119: } else {
120: return null;
121: }
122: }
123: surveyID = resultSet.getInt(1);
124: request.setAttribute("survey.survey_id", Integer
125: .toString(surveyID));
126: request
127: .setAttribute("survey.title", resultSet
128: .getString(2));
129:
130: resultSet.close();
131: resultSet = null;
132: st.close();
133: st = null;
134:
135: // Get total votes for the survey
136: st = conn.prepareStatement(virpVotesSQL);
137: st.setInt(1, surveyID);
138: resultSet = st.executeQuery();
139: if (resultSet.next()) {
140: request.setAttribute("survey.votes", resultSet
141: .getString(1));
142: } else {
143: request.setAttribute("survey.votes", "0");
144: }
145:
146: // Now get options for the latest survey
147: Vector options = new Vector(10);
148: st = conn.prepareStatement(virpOptionsSQL);
149: st.setInt(1, surveyID);
150: resultSet = st.executeQuery();
151: while (resultSet.next()) {
152: String[] record = new String[2];
153: record[0] = resultSet.getString(1);
154: record[1] = resultSet.getString(2);
155: options.addElement(record);
156: }
157: request.setAttribute("survey.options", options);
158: } catch (SQLException ex) {
159: throw new PortletException("Database exception.", ex);
160: } finally {
161: DBUtil.close(resultSet);
162: DBUtil.close(st);
163: DBUtil.close(conn);
164: }
165:
166: return null;
167: }
168:
169: }
|