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: * Create Survey Action processor
038: *
039: * @author Padmanabh Dabke
040: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class CreateSurveyActionProcessor 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 surveySQL = "INSERT INTO SB_SURVEYS (surveytitle) VALUES (?)";
053: String surveyIDSQL = "SELECT surveyid FROM SB_SURVEYS WHERE surveytitle = ?";
054: String surveyDataSQL = "INSERT INTO SB_SURVEY_DATA (surveyid, optionnum, optiontext) VALUES (?,?,?)";
055: String surveyTitle = request.getParameter("survey_title");
056: Connection conn = null;
057: PreparedStatement st = null;
058: ResultSet results = null;
059: try {
060: // Get database connection
061: conn = brpConfig.getDataSource().getConnection();
062: conn.setAutoCommit(false);
063:
064: // First create a survey record
065: st = conn.prepareStatement(surveySQL);
066: st.setString(1, surveyTitle);
067: st.execute();
068: st.close();
069: st = null;
070:
071: // Now get the survey ID
072: st = conn.prepareStatement(surveyIDSQL);
073: st.setString(1, surveyTitle);
074: results = st.executeQuery();
075: results.next();
076: int surveyID = results.getInt(1);
077: results.close();
078: results = null;
079: st.close();
080: st = null;
081:
082: // Insert survey options
083: st = conn.prepareStatement(surveyDataSQL);
084: st.setInt(1, surveyID);
085: for (int i = 0; i < 10; i++) {
086: String option = request.getParameter("survey_option_"
087: + i);
088: if (option != null && (!option.trim().equals(""))) {
089: option = option.trim();
090: st.setInt(2, i);
091: st.setString(3, option);
092: st.execute();
093: }
094: }
095:
096: conn.commit();
097: return "success";
098: } catch (SQLException sqex) {
099: try {
100: conn.rollback();
101: } catch (Exception ex) {
102: // Ignore
103: }
104: throw new PortletException("Database exception.", sqex);
105: } finally {
106: DBUtil.close(results);
107: DBUtil.close(st);
108: DBUtil.close(conn);
109: }
110:
111: }
112:
113: }
|