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 under 5 * the terms of the GNU Lesser General Public License as published by the Free 6 * Software Foundation; either version 2.1 of the License, or (at your option) 7 * any later version. 8 * 9 * This library is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 12 * 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, Inc., 16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. > 17 * http://www.gnu.org/copyleft/lesser.html > 18 * http://www.opensource.org/licenses/lgpl-license.php 19 */ 20 package net.mlw.vlh.adapter.jdbc.objectWrapper; 21 22 import java.sql.ResultSet; 23 import java.sql.SQLException; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import net.mlw.vlh.ValueListInfo; 28 import net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter; 29 import net.mlw.vlh.adapter.util.ObjectWrapper; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 /*** 35 * DefaultIdWrappedAdapter wrap original record by calling interface 36 * <code>ObjectWrapper</code> method 37 * <code>getWrappedRecord(Object objectToBeWrapped)</code> and with this 38 * result populate the final valueList.. objectToBeWrapped could be whole 39 * resultSet or specific column from resultSet. 40 * 41 * @see net.mlw.vlh.adapter.util.ObjectWrapper 42 * @see net.mlw.vlh.adapter.util.ObjectValidator 43 * @see net.mlw.vlh.adapter.jdbc.objectWrapper.ResultSetDecorator 44 * @see net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter 45 * 46 * @author Andrej Zachar 47 * @version $Revision: 1.9 $ $Date: 2005/10/17 11:40:25 $ 48 */ 49 public class DefaultWrapperAdapter extends AbstractJdbcAdapter 50 { 51 /*** 52 * Logger for this class 53 */ 54 private static final Log LOGGER = LogFactory.getLog(DefaultWrapperAdapter.class); 55 56 private String _columnName = null; 57 58 private int _columnNumber = 1; 59 60 private ObjectWrapper _wrapper; 61 62 private boolean wrapResultSet = false; 63 64 /*** 65 * @param result 66 * @return 67 * @throws SQLException 68 */ 69 private Object getOrignalRecord(ResultSet result) throws SQLException 70 { 71 if (wrapResultSet) 72 { 73 return result; 74 } 75 else 76 { 77 if (_columnName != null && _columnName.length() > 0) 78 { 79 return result.getObject(_columnName); 80 } 81 else 82 { 83 return result.getObject(_columnNumber); 84 } 85 } 86 } 87 88 public List processResultSet(String name, ResultSet result, int numberPerPage, ValueListInfo info) throws SQLException 89 { 90 if (LOGGER.isDebugEnabled()) 91 { 92 LOGGER.debug("Start wrapping using column '" 93 + (_columnName != null && _columnName.length() > 0 ? _columnName : (_columnNumber + "")) + "'."); 94 } 95 96 List list = new ArrayList(); 97 if (_wrapper == null) 98 { 99 LOGGER.error("Required _wrapper is null!"); 100 } 101 else 102 { 103 _wrapper.setValueListInfo(info); 104 for (int i = 0; result.next() && i < numberPerPage; i++) 105 { 106 list.add(_wrapper.getWrappedRecord(getOrignalRecord(result))); 107 } 108 } 109 110 LOGGER.debug("End wrapping."); 111 return list; 112 } 113 114 /*** 115 * @return Returns the columnName. 116 */ 117 public String getColumnName() 118 { 119 return _columnName; 120 } 121 122 /*** 123 * Specify which column will be objectToBeWrapped. Default value is null. If 124 * is <b>null </b>, is used column <b>Number </b> instead. 125 * <h4>Example</h4> 126 * <ul> 127 * result.getObject(columnName); 128 * </ul> 129 * 130 * @param columnName The columnName to set. 131 */ 132 public void setColumnName(String columnName) 133 { 134 _columnName = columnName; 135 } 136 137 /*** 138 * @return Returns the columnNumber. 139 */ 140 public int getColumnNumber() 141 { 142 return _columnNumber; 143 } 144 145 /*** 146 * Specify which column will be objectToBeWrapped. Default is 1; 147 * <h4>Example</h4> 148 * <ul> 149 * result.getObject(columnNumber); 150 * </ul> 151 * 152 * @param columnNumber The columnNumber to set. 153 */ 154 public void setColumnNumber(int columnNumber) 155 { 156 _columnNumber = columnNumber; 157 } 158 159 /*** 160 * @return Returns the objectWrapper. 161 */ 162 public ObjectWrapper getWrapper() 163 { 164 return _wrapper; 165 } 166 167 /*** 168 * This param is required. 169 * 170 * @param objectWrapper The objectWrapper to set. 171 * @see net.mlw.vlh.adapter.util.ObjectWrapper 172 */ 173 public void setWrapper(ObjectWrapper objectWrapper) 174 { 175 this._wrapper = objectWrapper; 176 } 177 178 /*** 179 * @return Returns the wrapResultSet. 180 */ 181 public boolean isWrapResultSet() 182 { 183 return wrapResultSet; 184 } 185 186 /*** 187 * Determine object to be wrapped - resultSet or column from result set. * 188 * If true, wrapper will call getWrappedRecord with resultSet parameter as 189 * objectToBeWrapped. If false, wrapper will call getWrappedRecord with 190 * object from resultSet. Use false if you need to pass only ids. If you 191 * need to access more than one object, use true. Default is false; 192 * 193 * @param wrapResultSet 194 * @see ObjectWrapper#getWrappedRecord(Object) 195 */ 196 public void setWrapResultSet(boolean wrapResultSet) 197 { 198 this.wrapResultSet = wrapResultSet; 199 } 200 }