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: import java.text.ParseException;
029:
030: import javax.portlet.ActionRequest;
031: import javax.portlet.ActionResponse;
032: import javax.portlet.PortletException;
033:
034: import org.w3c.dom.Element;
035:
036: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
037: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
038: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
039: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
040: import com.nabhinc.util.db.DBConfigUtil;
041: import com.nabhinc.util.db.DBParamUtil;
042: import com.nabhinc.util.StringUtil;
043: import com.nabhinc.util.XMLUtil;
044: import com.nabhinc.util.db.DBUtil;
045:
046: /**
047: * Deletes a database record if an optional
048: * SQL condition is satisfied.<p>
049: * Required configuration parameters
050: * <ul>
051: * <li>sql - SQL used to delete desired record.</li>
052: * </ul>
053: * Optional configuration parameters
054: * <ul>
055: * <li>check-sql - If this sql returns a record then the delete
056: * sql will not be executed.</li>
057: * <li>params - Comma separated list of names of SQL parameters to be
058: * inserted. In general, these are the request parameter names. The
059: * following specialtokens are used to insert other values:
060: * <ul>
061: * <li>$userName - Current user name returned by request.getRemoteUser()</li>
062: * <li>$currentDate - Today's date</li>
063: * <li>$$currentTimestamp - Current time</li>
064: * </ul>
065: * </li>
066: * <li>param-types - Comma separated list of SQL parameter types. This
067: * parameter must be specified if "params" are specified. Possible parameter
068: * types are: VARCHAR, INTEGER, DECIMAL, BOOLEAN, SMALLINT, DATE,
069: * TIME, TIMESTAMP, FLOAT, DOUBLE, ARRAY, BIGINT, BINARY, BIT,
070: * BLOB, CHAR, CLOB, LONGVARBINARY, LONGVARCHAR, JAVA_OBJECT
071: * </li>
072: * </ul>
073: *
074: * @author Padmanabh Dabke
075: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
076: */
077: public class DeleteRecord extends BaseRequestProcessor implements
078: ActionProcessor {
079: private String irSQL = null;
080: private String[] irParams = null;
081: private int[] irParamTypes = null;
082: private String irCheckSQL = null;
083:
084: /**
085: * Initialization.
086: * @param config XML configuration element
087: * @throws PortletException Thrown if sql parameter is not specified,
088: * "params" parameter is specified but corresponding "param-types"
089: * are not specified.
090: */
091: public void init(Element config, ControllerPortletConfig cpConfig)
092: throws PortletException {
093: super .init(config, cpConfig);
094: irSQL = XMLUtil.getSubElementText(config, "sql");
095: if (irSQL == null) {
096: throw new PortletException(
097: "Missing required parameter: sql");
098: }
099:
100: irCheckSQL = XMLUtil.getSubElementText(config, "check-sql");
101: String params = XMLUtil.getSubElementText(config, "params");
102: if (params != null) {
103: irParams = StringUtil.split(params, ",");
104: String paramTypes = XMLUtil.getSubElementText(config,
105: "param-types");
106: if (paramTypes == null) {
107: throw new PortletException(
108: "You must specify param-types if you specify params.");
109: }
110: String[] typeArray = StringUtil.split(paramTypes, ",");
111: if (typeArray.length != irParams.length) {
112: throw new PortletException(
113: "Number of param-types must be equal to number of params.");
114: }
115: irParamTypes = new int[typeArray.length];
116: for (int i = 0; i < typeArray.length; i++) {
117: try {
118: irParamTypes[i] = DBConfigUtil
119: .getSQLType(typeArray[i]);
120: } catch (Exception ex) {
121: throw new PortletException(
122: "Failed to parse sql type.", ex);
123: }
124: }
125: }
126: }
127:
128: /**
129: * Deletes a database record if an optional
130: * SQL condition is satisfied.<p>
131: * @param request Action request
132: * @param response Action response
133: * @param config Action config
134: * @return "check_error" if check-sql returns a record, "success" otherwise.
135: * @throws PortletException
136: * @throws IOException
137: */
138: public String process(ActionRequest request,
139: ActionResponse response, ActionConfig config)
140: throws PortletException, IOException {
141:
142: Connection conn = null;
143: PreparedStatement st = null;
144: ResultSet results = null;
145: try {
146: conn = brpConfig.getDataSource().getConnection();
147: if (irCheckSQL != null) {
148: st = conn.prepareStatement(irCheckSQL);
149: if (irParams != null) {
150: for (int i = 0; i < irParams.length; i++) {
151: DBParamUtil.setSQLParam(st, i, irParams[i],
152: irParamTypes[i], request, -1);
153: }
154: }
155: results = st.executeQuery();
156: if (results.next()) {
157: return "check-error";
158: }
159: }
160: st = conn.prepareStatement(irSQL);
161: if (irParams != null) {
162: for (int i = 0; i < irParams.length; i++) {
163: DBParamUtil.setSQLParam(st, i, irParams[i],
164: irParamTypes[i], request, -1);
165: }
166: }
167: st.execute();
168: return "success";
169: } catch (SQLException sqe) {
170: throw new PortletException("Database exception.", sqe);
171: } catch (ParseException pex) {
172: throw new PortletException("Malformed request parameter.",
173: pex);
174: } finally {
175: DBUtil.close(results);
176: DBUtil.close(st);
177: DBUtil.close(conn);
178: }
179: }
180: }
|