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:
027: import javax.portlet.ActionRequest;
028: import javax.portlet.ActionResponse;
029: import javax.portlet.PortletException;
030:
031: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
032: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
033: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
034: import com.nabhinc.util.db.DBUtil;
035:
036: /**
037: *
038: *
039: * @author Padmanabh Dabke
040: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class UpdateSurveyActionProcessor extends BaseRequestProcessor
043: implements ActionProcessor {
044:
045: /* (non-Javadoc)
046: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
047: */
048: public String process(ActionRequest request,
049: ActionResponse response, ActionConfig actionConfig)
050: throws PortletException, IOException {
051:
052: String titleSQL = "UPDATE SB_SURVEYS SET surveytitle = ? WHERE surveyid = ?";
053: String optionSQL = "UPDATE SB_SURVEY_DATA SET optiontext = ? WHERE surveyid = ? AND optionnum = ?";
054: String newOptionsSQL = "INSERT INTO SB_SURVEY_DATA (surveyid,optionnum, optiontext) VALUES (?,?,?)";
055: String optionNumSQL = "SELECT COUNT(*) FROM SB_SURVEY_DATA WHERE surveyid = ?";
056: String surveyTitle = request.getParameter("survey_title");
057: int surveyID = Integer.parseInt(request
058: .getParameter("survey_id"));
059: Connection conn = null;
060: PreparedStatement st = null;
061: ResultSet results = null;
062: try {
063: // Get database connection
064: conn = brpConfig.getDataSource().getConnection();
065: conn.setAutoCommit(false);
066:
067: // First change survey title
068: if (surveyTitle != null && (!surveyTitle.trim().equals(""))) {
069: surveyTitle = surveyTitle.trim();
070: st = conn.prepareStatement(titleSQL);
071: st.setString(1, surveyTitle);
072: st.setInt(2, surveyID);
073: st.execute();
074: st.close();
075: st = null;
076: }
077:
078: // Get the number of existing options
079: st = conn.prepareStatement(optionNumSQL);
080: st.setInt(1, surveyID);
081: results = st.executeQuery();
082: results.next();
083: int optionCount = results.getInt(1);
084: results.close();
085: results = null;
086: st.close();
087: st = null;
088:
089: // Update existing survey options
090: st = conn.prepareStatement(optionSQL);
091: st.setInt(2, surveyID);
092: int numOptions = 0;
093: for (numOptions = 0; numOptions < optionCount; numOptions++) {
094: String option = request.getParameter("survey_option_"
095: + numOptions);
096: if (option != null && (!option.trim().equals(""))) {
097: option = option.trim();
098: st.setString(1, option);
099: st.setInt(3, numOptions);
100: st.execute();
101: }
102: }
103: st.close();
104: st = null;
105:
106: // Insert additional options if specified
107: st = conn.prepareStatement(newOptionsSQL);
108: st.setInt(1, surveyID);
109:
110: for (int i = numOptions; i < 10; i++) {
111: String option = request.getParameter("survey_option_"
112: + i);
113: if (option != null && (!option.trim().equals(""))) {
114: option = option.trim();
115: st.setInt(2, numOptions);
116: st.setString(3, option);
117: st.execute();
118: numOptions++;
119: }
120:
121: }
122: conn.commit();
123: return "success";
124: } catch (SQLException sqlex) {
125: try {
126: conn.rollback();
127: } catch (Exception ex) {
128: // Ignore
129: }
130: throw new PortletException("Database exception.", sqlex);
131: } catch (Exception ex) {
132: try {
133: conn.rollback();
134: } catch (Exception ex1) {
135: // Ignore
136: }
137: throw new PortletException(ex);
138: } finally {
139: DBUtil.close(results);
140: DBUtil.close(st);
141: DBUtil.close(conn);
142: }
143:
144: }
145:
146: }
|