001/* 
002 * JKNIV, SQLegance keeping queries maintainable.
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.classification;
021
022import java.util.Map;
023
024import net.sf.jkniv.reflect.Injectable;
025import net.sf.jkniv.reflect.InjectableFactory;
026import net.sf.jkniv.reflect.beans.CapitalNameFactory;
027import net.sf.jkniv.reflect.beans.Capitalize;
028import net.sf.jkniv.reflect.beans.ObjectProxy;
029import net.sf.jkniv.reflect.beans.ObjectProxyFactory;
030
031@SuppressWarnings("unchecked")
032public class MapTransform implements Transformable<Map<String, Object>>
033{
034    private static final Capitalize CAPITAL_SETTER = CapitalNameFactory.getInstanceOfSetter();
035
036    /* (non-Javadoc)
037     * @see net.sf.jkniv.sqlegance.classifier.Transformable#transform(java.lang.Object, java.lang.Class)
038     */
039    @Override
040    public <T> T transform(Map<String, Object> row, Class<T> type)
041    {
042        ObjectProxy<?> proxy = ObjectProxyFactory.of(type);
043        Injectable<?> inject = InjectableFactory.of(proxy);
044        for(String key : row.keySet())
045        {
046            Object v = row.get(key);
047            inject.inject(CAPITAL_SETTER.does(key), v);
048        }
049        return (T)proxy.getInstance();
050    }
051    
052    /* (non-Javadoc)
053     * @see net.sf.jkniv.sqlegance.classifier.Transformable#transform(java.lang.Object, T)
054     */
055    @Override
056    public void transform(Map<String, Object> row, Object instance)
057    {
058        ObjectProxy<?> proxy = ObjectProxyFactory.of(instance);
059        Injectable<?> inject = InjectableFactory.of(proxy);
060        for(String key : row.keySet())
061        {
062            Object v = row.get(key);
063            inject.inject(CAPITAL_SETTER.does(key), v);
064        }
065    }
066}