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