01: /*
02: * (C) Copyright 2004 Nabh Information Systems, Inc.
03: *
04: * All copyright notices regarding Nabh's products MUST remain
05: * intact in the scripts and in the outputted HTML.
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package com.nabhinc.portlet.mvcportlet.populator;
22:
23: import java.sql.Connection;
24: import java.sql.PreparedStatement;
25: import java.sql.ResultSet;
26: import java.sql.SQLException;
27: import java.text.ParseException;
28: import java.util.Vector;
29:
30: import javax.portlet.PortletException;
31: import javax.portlet.PortletRequest;
32:
33: import com.nabhinc.portlet.mvcportlet.core.ValueAndLabel;
34: import com.nabhinc.util.db.DBParamUtil;
35: import com.nabhinc.util.db.DBUtil;
36:
37: /**
38: * Supplies an array of ValueAndLabel object that retrieves from database.
39: *
40: * @author Padmanabh Dabke
41: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
42: */
43: public class SelectValueAndLabel extends SelectString {
44:
45: /* (non-Javadoc)
46: * @see com.nabhinc.portlet.mvcportlet.core.FormFieldPopulator#getValues(java.lang.String, javax.portlet.RenderRequest, com.nabhinc.portlet.mvcportlet.core.RenderConfig)
47: */
48: public Object getValue(PortletRequest req) throws PortletException {
49: Connection conn = null;
50: ResultSet results = null;
51: PreparedStatement st = null;
52:
53: try {
54: conn = bpConfig.getDataSource().getConnection();
55: st = conn.prepareStatement(sfSQL);
56: if (sfParams != null) {
57: for (int i = 0; i < sfParams.length; i++) {
58: DBParamUtil.setSQLParam(st, i, sfParams[i],
59: sfParamTypes[i], req, -1);
60: }
61: }
62: Vector fields = new Vector();
63: results = st.executeQuery();
64:
65: while (results.next()) {
66: fields.addElement(new ValueAndLabel(results
67: .getString(1), results.getString(2)));
68: }
69: ValueAndLabel[] values = new ValueAndLabel[fields.size()];
70: fields.copyInto(values);
71: return values;
72: } catch (SQLException sqe) {
73: throw new PortletException("Database exception.", sqe);
74: } catch (ParseException pex) {
75: throw new PortletException("Malformed request parameter.",
76: pex);
77: } finally {
78: DBUtil.close(results);
79: DBUtil.close(st);
80: DBUtil.close(conn);
81: }
82: }
83:
84: }
|