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.renderprocessor;
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.ResultSetMetaData;
028: import java.sql.SQLException;
029: import java.sql.Types;
030: import java.text.ParseException;
031: import java.util.Vector;
032:
033: import javax.portlet.PortletException;
034: import javax.portlet.RenderRequest;
035: import javax.portlet.RenderResponse;
036:
037: import org.w3c.dom.Element;
038:
039: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
040: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
041: import com.nabhinc.portlet.mvcportlet.core.RenderConfig;
042: import com.nabhinc.portlet.mvcportlet.core.RenderProcessor;
043: import com.nabhinc.util.StringUtil;
044: import com.nabhinc.util.XMLUtil;
045: import com.nabhinc.util.db.DBConfigUtil;
046: import com.nabhinc.util.db.DBParamUtil;
047: import com.nabhinc.util.db.DBUtil;
048:
049: /**
050: * Retrieves database records and sets request attribute
051: * "mvcportlet.records" to a vector of object arrays corresponding to
052: * the records.
053: * <ul>
054: * <li>sql - SQL used to update or insert a record.</li>
055: * </ul>
056: * Optional configuration parameters
057: * <ul>
058: * <li>params - Comma separated list of names of SQL parameters to be
059: * inserted. In general, these are the request parameter names. The
060: * following specialtokens are used to insert other values:
061: * <ul>
062: * <li>$userName - Current user name returned by request.getRemoteUser()</li>
063: * <li>$currentDate - Today's date</li>
064: * <li>$currentTimestamp - Current time</li>
065: * </ul>
066: * </li>
067: * <li>param-types - Comma separated list of SQL parameter types. This
068: * parameter must be specified if "params" are specified. Possible parameter
069: * types are: VARCHAR, INTEGER, DECIMAL, BOOLEAN, SMALLINT, DATE,
070: * TIME, TIMESTAMP, FLOAT, DOUBLE, ARRAY, BIGINT, BINARY, BIT,
071: * BLOB, CHAR, CLOB, LONGVARBINARY, LONGVARCHAR, JAVA_OBJECT
072: * </li>
073: * </ul>
074: *
075: * @author Padmanabh Dabke
076: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
077: */
078: public class SelectRecords extends BaseRequestProcessor implements
079: RenderProcessor {
080:
081: private String srSQL = null;
082: private String[] srParams = null;
083: private int[] srParamTypes = null;
084: private String srAttributeName = "mvcportlet.records";
085:
086: public void init(Element config, ControllerPortletConfig cpConfig)
087: throws PortletException {
088: super .init(config, cpConfig);
089: srSQL = XMLUtil.getSubElementText(config, "sql");
090: if (srSQL == null) {
091: throw new PortletException(
092: "Missing required parameter: sql");
093: }
094:
095: String params = XMLUtil.getSubElementText(config, "params");
096: if (params != null) {
097: srParams = StringUtil.split(params, ",");
098: String paramTypes = XMLUtil.getSubElementText(config,
099: "param-types");
100: if (paramTypes == null) {
101: throw new PortletException(
102: "You must specify param-types if you specify params.");
103: } else {
104: String[] typeArray = StringUtil.split(paramTypes, ",");
105: if (typeArray.length != srParams.length) {
106: throw new PortletException(
107: "Number of param-types must be equal to number of params.");
108: } else {
109: srParamTypes = new int[typeArray.length];
110: for (int i = 0; i < typeArray.length; i++) {
111: try {
112: srParamTypes[i] = DBConfigUtil
113: .getSQLType(typeArray[i]);
114: } catch (Exception ex) {
115: throw new PortletException(
116: "Failed to parse parameter type.",
117: ex);
118: }
119: }
120: }
121: }
122: }
123:
124: // Check if a custom attribute name for record vector
125: String attribName = XMLUtil.getSubElementText(config,
126: "attribute-name");
127: if (attribName != null)
128: srAttributeName = attribName;
129: }
130:
131: /**
132: * Retrieves database records. If one or more records are found, it sets request attribute
133: * "mvcportlet.records" to a vector of object arrays corresponding to
134: * the records. If no records are found, it returns "no-such-record".
135: */
136: public String process(RenderRequest request,
137: RenderResponse response, RenderConfig config)
138: throws PortletException, IOException {
139:
140: Connection conn = null;
141: ResultSet results = null;
142: PreparedStatement st = null;
143:
144: try {
145: conn = brpConfig.getDataSource().getConnection();
146: st = conn.prepareStatement(srSQL);
147: if (srParams != null) {
148: for (int i = 0; i < srParams.length; i++) {
149: DBParamUtil.setSQLParam(st, i, srParams[i],
150: srParamTypes[i], request, -1);
151: }
152: }
153: Vector records = new Vector();
154: results = st.executeQuery();
155: ResultSetMetaData metaData = results.getMetaData();
156: int numColumns = metaData.getColumnCount();
157: while (results.next()) {
158: Object[] record = new Object[numColumns];
159: for (int i = 0; i < numColumns; i++) {
160: int colIndex = i + 1;
161: int colType = metaData.getColumnType(colIndex);
162: switch (colType) {
163: case Types.BLOB:
164: case Types.LONGVARBINARY:
165: record[i] = results.getBytes(colIndex);
166: break;
167: case Types.CLOB:
168: case Types.LONGVARCHAR:
169: record[i] = results.getString(colIndex);
170: break;
171: default:
172: record[i] = results.getObject(colIndex);
173:
174: }
175: }
176: records.addElement(record);
177: }
178: if (records.size() == 0) {
179: return "no-such-record";
180: } else {
181: request.setAttribute(srAttributeName, records);
182: }
183: return "success";
184: } catch (SQLException sqe) {
185: throw new PortletException("Database exception.", sqe);
186: } catch (ParseException pex) {
187: throw new PortletException("Malformed request parameter.",
188: pex);
189: } finally {
190: DBUtil.close(results);
191: DBUtil.close(st);
192: DBUtil.close(conn);
193: }
194: }
195:
196: }
|