001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library 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 GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone.statement;
021
022import java.sql.SQLException;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import net.sf.jkniv.reflect.Injectable;
028import net.sf.jkniv.reflect.InjectableFactory;
029import net.sf.jkniv.reflect.beans.ObjectProxy;
030import net.sf.jkniv.sqlegance.logger.DataMasking;
031import net.sf.jkniv.whinstone.JdbcColumn;
032import net.sf.jkniv.whinstone.types.Convertible;
033import net.sf.jkniv.whinstone.types.RegisterType;
034
035/**
036 * 
037 * @author Alisson Gomes
038 * @since 0.6.0
039 */
040public abstract class AbstractResultRow //implements ResultRow
041{
042    private final static Logger LOG = LoggerFactory.getLogger(AbstractResultRow.class);
043    private final Logger        sqlLog;
044    private final DataMasking   masking;
045    
046    public AbstractResultRow(final Logger log, final DataMasking masking)
047    {
048        this.sqlLog = log;
049        this.masking = masking;
050    }
051    
052    /**
053     * 
054     * @param <R> {@code ResultSet} implementation, that contains the rows of database
055     * @param column metadata
056     * @param rs {@code ResultSet} instance
057     * @return The value of column
058     * @throws SQLException if
059     */
060    public <R> Object getValueOf(JdbcColumn<R> column, R rs) throws SQLException
061    {
062        return column.isBinary() ? column.getBytes(rs): column.getValue(rs); 
063    }
064    
065    public <T,R> void setValueOf(final ObjectProxy<T> proxy, final JdbcColumn<R> column, R rs) throws SQLException
066    {
067        Injectable<?> reflect = InjectableFactory.of(proxy);
068        Object jdbcObject = getValueOf(column, rs);
069        if (sqlLog.isTraceEnabled())
070            sqlLog.trace("Mapping index [{}] column [{}] type of [{}] to value [{}]", column.getIndex(),
071                    column.getAttributeName(), (jdbcObject != null ? jdbcObject.getClass().getName() : "null"),
072                    masking.mask(column.getAttributeName(), jdbcObject));
073        
074        if (column.isNestedAttribute())
075        {
076            reflect.inject(column.getAttributeName(), jdbcObject);
077        }
078        else
079        {
080            String method = column.getMethodName();
081            if (proxy.hasMethod(method))
082            {
083                reflect.inject(method, jdbcObject);
084            }
085            else
086                LOG.warn("Method [{}] doesn't exists for [{}] to set value [{}]", method,
087                        proxy.getTargetClass().getName(), jdbcObject);
088        }
089    }
090}