001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db;
028:
029: import org.databene.commons.ConversionException;
030: import org.databene.commons.Converter;
031: import org.databene.commons.converter.AnyConverter;
032: import org.databene.model.data.Entity;
033: import org.databene.model.data.EntityDescriptor;
034: import org.databene.platform.bean.BeanDescriptorProvider;
035:
036: import java.sql.ResultSet;
037: import java.sql.ResultSetMetaData;
038: import java.sql.SQLException;
039:
040: /**
041: * Converts a ResultSet's current cursor position to an Entity.<br/>
042: * <br/>
043: * Created: 23.08.2007 19:30:31
044: * @author Volker Bergmann
045: * @deprecated use ResultSetEntityIterator
046: */
047: public class ResultSet2EntityConverter implements
048: Converter<ResultSet, Entity> {
049:
050: // TODO v0.5 remove
051:
052: private BeanDescriptorProvider beanDescriptorProvider;
053:
054: private EntityDescriptor descriptor;
055:
056: public ResultSet2EntityConverter(EntityDescriptor descriptor) {
057: this .descriptor = descriptor;
058: this .beanDescriptorProvider = new BeanDescriptorProvider();
059: }
060:
061: public Class<Entity> getTargetType() {
062: return Entity.class;
063: }
064:
065: public Entity convert(ResultSet resultSet)
066: throws ConversionException {
067: try {
068: Entity entity = new Entity(descriptor);
069: ResultSetMetaData metaData = resultSet.getMetaData();
070: int columnCount = metaData.getColumnCount();
071: for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
072: String columnName = metaData.getColumnName(columnIndex);
073: String abstractType = descriptor
074: .getComponentDescriptor(columnName).getType();
075: Object javaValue = javaValue(resultSet, columnIndex,
076: abstractType);
077: entity.setComponent(columnName, javaValue);
078: }
079: return entity;
080: } catch (SQLException e) {
081: throw new RuntimeException(e);
082: }
083: }
084:
085: private Object javaValue(ResultSet resultSet, int columnIndex,
086: String abstractType) throws SQLException {
087: if ("date".equals(abstractType))
088: return resultSet.getDate(columnIndex);
089: else if ("timestamp".equals(abstractType))
090: return resultSet.getTimestamp(columnIndex);
091: else if ("string".equals(abstractType))
092: return resultSet.getString(columnIndex);
093: // try generic conversion
094: Object driverValue = resultSet.getObject(columnIndex);
095: Class<? extends Object> javaType = beanDescriptorProvider
096: .concreteType(abstractType);
097: Object javaValue = AnyConverter.convert(driverValue, javaType);
098: return javaValue;
099: }
100: }
|