1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.vlh.adapter.jdbc.dynabean;
22
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import net.mlw.vlh.ValueListInfo;
30 import net.mlw.vlh.adapter.jdbc.AbstractDynaJdbcAdapter;
31 import net.mlw.vlh.adapter.jdbc.dynabean.fix.ResultSetDynaClass;
32
33 import org.apache.commons.beanutils.BasicDynaBean;
34 import org.apache.commons.beanutils.BasicDynaClass;
35 import org.apache.commons.beanutils.DynaBean;
36 import org.apache.commons.beanutils.DynaProperty;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /***
41 * This ValueListAdapter returns a ValueList of DynaBean(s).
42 * @see net.mlw.vlh.adapter.jdbc.AbstractDynaJdbcAdapter
43 * @see net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter
44 *
45 * @author Matthew L. Wilson, Andrej Zachar
46 * @version $Revision: 1.6 $ $Date: 2005/08/19 16:06:29 $
47 */
48 public class DefaultDynaBeanAdapter extends AbstractDynaJdbcAdapter
49 {
50
51 private static final Log LOGGER = LogFactory.getLog(DefaultDynaBeanAdapter.class);
52
53 public List processResultSet(String name, ResultSet result, int numberPerPage, ValueListInfo info) throws SQLException
54 {
55 List list = new ArrayList();
56
57 ResultSetDynaClass rsdc = new ResultSetDynaClass(result, false, isUseName());
58 BasicDynaClass bdc = new BasicDynaClass(name, BasicDynaBean.class, rsdc.getDynaProperties());
59
60 int rowIndex = 0;
61 for (Iterator rows = rsdc.iterator(); rows.hasNext() && rowIndex < numberPerPage; rowIndex++)
62 {
63 try
64 {
65 DynaBean oldRow = (DynaBean) rows.next();
66 DynaBean newRow = bdc.newInstance();
67
68 DynaProperty[] properties = oldRow.getDynaClass().getDynaProperties();
69 for (int i = 0, length = properties.length; i < length; i++)
70 {
71 String propertyName = properties[i].getName();
72 Object value = oldRow.get(propertyName);
73 newRow.set(propertyName, value);
74 }
75
76 list.add(newRow);
77 }
78 catch (Exception e)
79 {
80 LOGGER.error(e);
81 }
82 }
83
84 return list;
85 }
86 }