001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (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: */
021: package com.nabhinc.portlet.mvcportlet.actionprocessor;
022:
023: import java.io.IOException;
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.portlet.PortletException;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
036: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
037: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
038: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
039: import com.nabhinc.util.XMLUtil;
040: import com.nabhinc.util.db.DBUtil;
041:
042: /**
043: * Deletes multiple database records corresponding to the checked
044: * items for deletion.
045: *
046: * @author Padmanabh Dabke
047: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
048: */
049: public class DeleteRecords extends BaseRequestProcessor implements
050: ActionProcessor {
051: private String irSQL = null;
052: private String irIdParam = "marked";
053:
054: /**
055: * Initialization.
056: * @param config XML configuration element
057: * @throws PortletException Thrown if sql parameter is not specified,
058: * "params" parameter is specified but corresponding "param-types"
059: * are not specified.
060: */
061: public void init(Element config, ControllerPortletConfig cpConfig)
062: throws PortletException {
063: super .init(config, cpConfig);
064: irSQL = XMLUtil.getSubElementText(config, "sql");
065: if (irSQL == null) {
066: throw new PortletException(
067: "Missing required parameter: sql");
068: }
069: String idParam = XMLUtil.getSubElementText(config, "id-param");
070: if (idParam != null)
071: irIdParam = idParam;
072: }
073:
074: /**
075: * Deletes a database record if an optional
076: * SQL condition is satisfied.<p>
077: * @param request Action request
078: * @param response Action response
079: * @param config Action config
080: * @return "check_error" if check-sql returns a record, "success" otherwise.
081: * @throws PortletException
082: * @throws IOException
083: */
084: public String process(ActionRequest request,
085: ActionResponse response, ActionConfig config)
086: throws PortletException, IOException {
087:
088: Connection conn = null;
089: PreparedStatement st = null;
090: ResultSet results = null;
091: try {
092: conn = brpConfig.getDataSource().getConnection();
093: st = conn.prepareStatement(irSQL);
094: String[] ids = request.getParameterValues(irIdParam);
095: if (ids != null) {
096: for (int i = 0; i < ids.length; i++) {
097: st.setString(1, ids[i]);
098: st.execute();
099: }
100: }
101: st.execute();
102: return "success";
103: } catch (SQLException sqe) {
104: throw new PortletException("Database exception.", sqe);
105: } finally {
106: DBUtil.close(results);
107: DBUtil.close(st);
108: DBUtil.close(conn);
109: }
110: }
111: }
|