/*
 * Copyright 2016 Bridje Framework.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bridje.orm.adpaters;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bridje.ioc.Component;
import org.bridje.orm.Column;
import org.bridje.orm.SQLAdapter;

/**
 * An SQLAdapter that will find and call the methods toSQL/fromSQL by
 * reflection in order to serialize/unserialize the values to/from the database.
 */
@Component
public class GenericSQLAdapter implements SQLAdapter
{
    private static final Logger LOG = Logger.getLogger(GenericSQLAdapter.class.getName());

    private final Map<Class, Method> fromSQLMap = new ConcurrentHashMap<>();
    
    private final Map<Class, Method> toSQLMap = new ConcurrentHashMap<>();
    
    @Override
    public Object serialize(Object value, Column column)
    {
        try
        {
            Method method = getToSQLMethod(column);
            return method.invoke(value);
        }
        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException ex)
        {
            LOG.log(Level.SEVERE, ex.getMessage(), ex);
        }
        return null;
    }

    @Override
    public Object unserialize(Object value, Column column)
    {
        try
        {
            Method method = getFromSQLMethod(column);
            method.setAccessible(true);
            return method.invoke(null, value);
        }
        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException ex)
        {
            LOG.log(Level.SEVERE, ex.getMessage(), ex);
        }
        return null;
    }


    private Method getToSQLMethod(Column column)
    {
        Method method = toSQLMap.get(column.getType());
        if(method != null)
        {
            return method;
        }
        else
        {
            method = findToSQLMethod(column);
            toSQLMap.put(column.getType(), method);
            return method;
        }
    }

    private Method getFromSQLMethod(Column column)
    {
        Method method = fromSQLMap.get(column.getType());
        if(method != null)
        {
            return method;
        }
        else
        {
            method = findFromSQLMethod(column);
            fromSQLMap.put(column.getType(), method);
            return method;
        }
    }
    
    private Method findToSQLMethod(Column column)
    {
        return findMethod(column.getType(), FromSQL.class);
    }

    private Method findFromSQLMethod(Column column)
    {
        return findMethod(column.getType(), ToSQL.class);
    }

    private Method findMethod(Class<?> type, Class<? extends Annotation> anot)
    {
        for(Method m : type.getDeclaredMethods())
        {
            if(m.getAnnotation(anot) != null)
            {
                m.setAccessible(true);
                return m;
            }
        }
        return null;
    }
}

@@ -1377,29 +1377,25 @@
 ashMap%3C%3E();%0A
-    
 %0A
+
     private 
@@ -1457,21 +1457,17 @@
 ap%3C%3E();%0A
-    
 %0A
+
     @Ove
@@ -1601,32 +1601,192 @@
 Method(column);%0A
+            if(method == null)%0A            %7B%0A                LOG.log(Level.WARNING, %22%7B0%7D does not have a valid ToSQL method.%22, column.getType());%0A            %7D%0A
             retu
@@ -1965,38 +1965,119 @@
   LOG.log(Level.
-SEVERE
+WARNING, %22%7B0%7D does not have a valid ToSQL method: %7B1%7D%22, new Object%5B%5D %7B column.getType()
 , ex.getMessage(
@@ -2069,36 +2069,34 @@
  ex.getMessage()
-, ex
+ %7D
 );%0A        %7D%0A   
@@ -2291,35 +2291,157 @@
     
+if(
 method
-.setAccessible(true);
+ == null)%0A            %7B%0A                LOG.log(Level.WARNING, %22%7B0%7D does not have a valid FromSQL method.%22, column.getType());%0A            %7D
 %0A   
@@ -2651,14 +2651,97 @@
 vel.
-SEVERE
+WARNING, %22%7B0%7D does not have a valid FromSQL method: %7B1%7D%22, new Object%5B%5D %7B column.getType()
 , ex
@@ -2753,20 +2753,18 @@
 essage()
-, ex
+ %7D
 );%0A     
@@ -2796,17 +2796,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -3507,13 +3507,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -3604,20 +3604,18 @@
 Type(), 
-From
+To
 SQL.clas
@@ -3719,34 +3719,36 @@
 lumn.getType(), 
-To
+From
 SQL.class);%0A    

Fix in the generic sql adapter.