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.populator;
022:
023: import java.sql.Connection;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.text.ParseException;
028: import java.util.Vector;
029:
030: import javax.portlet.PortletException;
031: import javax.portlet.PortletRequest;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
036: import com.nabhinc.util.StringUtil;
037: import com.nabhinc.util.XMLUtil;
038: import com.nabhinc.util.db.DBConfigUtil;
039: import com.nabhinc.util.db.DBParamUtil;
040: import com.nabhinc.util.db.DBUtil;
041:
042: /**
043: * Supplies an array of String values that retrieves from Database.
044: *
045: * @author Padmanabh Dabke
046: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
047: */
048: public class SelectString extends BasePopulator {
049: protected String sfSQL = null;
050: protected String[] sfParams = null;
051: protected int[] sfParamTypes = null;
052:
053: private String sfDefaultsSQL = null;
054: private String[] sfDefaultsParams = null;
055: private int[] sfDefaultsParamTypes = null;
056:
057: /* (non-Javadoc)
058: * @see com.nabhinc.portlet.mvcportlet.core.FormFieldPopulator#getValues(java.lang.String, javax.portlet.RenderRequest, com.nabhinc.portlet.mvcportlet.core.RenderConfig)
059: */
060: public Object getValue(PortletRequest req) throws PortletException {
061: Connection conn = null;
062: ResultSet results = null;
063: PreparedStatement st = null;
064:
065: try {
066: conn = bpConfig.getDataSource().getConnection();
067: st = conn.prepareStatement(sfSQL);
068: if (sfParams != null) {
069: for (int i = 0; i < sfParams.length; i++) {
070: DBParamUtil.setSQLParam(st, i, sfParams[i],
071: sfParamTypes[i], req, -1);
072: }
073: }
074: Vector fields = new Vector();
075: results = st.executeQuery();
076:
077: while (results.next()) {
078: fields.addElement(results.getString(1));
079: }
080: String[] values = new String[fields.size()];
081: fields.copyInto(values);
082: return values;
083: } catch (SQLException sqe) {
084: throw new PortletException("Database exception.", sqe);
085: } catch (ParseException pex) {
086: throw new PortletException("Malformed request parameter.",
087: pex);
088: } finally {
089: DBUtil.close(results);
090: DBUtil.close(st);
091: DBUtil.close(conn);
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see com.nabhinc.core.XMLInitable#init(org.w3c.dom.Element)
097: */
098: public void init(Element config, ControllerPortletConfig cpConfig)
099: throws PortletException {
100: super .init(config, cpConfig);
101: sfSQL = XMLUtil.getSubElementText(config, "sql");
102: if (sfSQL == null) {
103: throw new PortletException(
104: "Missing required parameter: sql");
105: }
106:
107: String params = XMLUtil.getSubElementText(config, "params");
108: if (params != null) {
109: sfParams = StringUtil.split(params, ",");
110: String paramTypes = XMLUtil.getSubElementText(config,
111: "param-types");
112: if (paramTypes == null) {
113: throw new PortletException(
114: "You must specify param-types if you specify params.");
115: } else {
116: String[] typeArray = StringUtil.split(paramTypes, ",");
117: if (typeArray.length != sfParams.length) {
118: throw new PortletException(
119: "Number of param-types must be equal to number of params.");
120: } else {
121: sfParamTypes = new int[typeArray.length];
122: for (int i = 0; i < typeArray.length; i++) {
123: try {
124: sfParamTypes[i] = DBConfigUtil
125: .getSQLType(typeArray[i]);
126: } catch (Exception ex) {
127: throw new PortletException(
128: "Failed to parse parameter type.",
129: ex);
130: }
131: }
132: }
133: }
134: }
135:
136: sfDefaultsSQL = XMLUtil.getSubElementText(config,
137: "defaults-sql");
138: params = XMLUtil.getSubElementText(config, "defaults-params");
139: if (params != null) {
140: sfDefaultsParams = StringUtil.split(params, ",");
141: String paramTypes = XMLUtil.getSubElementText(config,
142: "defaults-param-types");
143: if (paramTypes == null) {
144: throw new PortletException(
145: "You must specify defaults-param-types if you specify defaults-params.");
146: } else {
147: String[] typeArray = StringUtil.split(paramTypes, ",");
148: if (typeArray.length != sfParams.length) {
149: throw new PortletException(
150: "Number of defaults-param-types must be equal to number of defaults-params.");
151: } else {
152: sfDefaultsParamTypes = new int[typeArray.length];
153: for (int i = 0; i < typeArray.length; i++) {
154: try {
155: sfDefaultsParamTypes[i] = DBConfigUtil
156: .getSQLType(typeArray[i]);
157: } catch (Exception ex) {
158: throw new PortletException(
159: "Failed to parse parameter type.",
160: ex);
161: }
162: }
163: }
164: }
165: }
166: }
167:
168: public String[] getDefaultValues(PortletRequest req)
169: throws PortletException {
170: Connection conn = null;
171: ResultSet results = null;
172: PreparedStatement st = null;
173:
174: if (sfDefaultsSQL == null) {
175: return super .getDefaultValues(req);
176: }
177:
178: try {
179: conn = bpConfig.getDataSource().getConnection();
180: st = conn.prepareStatement(sfDefaultsSQL);
181: if (sfDefaultsParams != null) {
182: for (int i = 0; i < sfDefaultsParams.length; i++) {
183: DBParamUtil.setSQLParam(st, i, sfDefaultsParams[i],
184: sfDefaultsParamTypes[i], req, -1);
185: }
186: }
187: Vector fields = new Vector();
188: results = st.executeQuery();
189:
190: while (results.next()) {
191: fields.addElement(results.getString(1));
192: }
193: String[] values = new String[fields.size()];
194: fields.copyInto(values);
195: return values;
196: } catch (SQLException sqe) {
197: throw new PortletException("Database exception.", sqe);
198: } catch (ParseException pex) {
199: throw new PortletException("Malformed request parameter.",
200: pex);
201: } finally {
202: DBUtil.close(results);
203: DBUtil.close(st);
204: DBUtil.close(conn);
205: }
206: }
207:
208: }
|