/* This is a temp fix to allow an ibatis enhanced lazy loaded object to be Seralized.  This is needed
 * when a Profile is part of a search criteria that is passed to a message bean.
 * For use with eMits I made the following changes
 * 1) Made Serializable.
 * 2) ExtendedSqlMapClient field is not transient.
 * 3) Added a getter around the ExtendedSqlMapClient field.
 * 
 */

/*
 *  Copyright 2004 Clinton Begin
 *
 *  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 com.ibatis.sqlmap.engine.mapping.result.loader;

import emits.application.data.util.ibatis.AppSqlConfig;
import emits.domain.BaseDomain;
import emits.domain.EnhancedListCallbackFilter;
import emits.domain.IBaseDomain;
import emits.domain.ListEnhancable;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.NoOp;

import com.ibatis.common.beans.ClassInfo;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.type.DomTypeMarker;

/**
 * Class to lazily load results into objects (uses CGLib to improve performance)
 */
public class EnhancedLazyResultLoader  {

  private static final Class[] INTERFACES = new Class[]{List.class, Serializable.class};
  private Object loader;


  /**
   * Constructor for an enhanced lazy list loader
   *
   * @param client - the client that is creating the lazy list
   * @param statementName - the statement to be used to build the list
   * @param parameterObject - the parameter object to be used to build the list
   * @param targetType - the type we are putting data into
   */
  public EnhancedLazyResultLoader(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) {
    loader = new EnhancedLazyResultLoaderImpl(client, statementName, parameterObject, targetType);
  }

  /**
   * Loads the result
   *
   * @return the results - a list or object
   * 
   * @throws SQLException if there is a problem
   */
  public Object loadResult() throws SQLException {
    return ((EnhancedLazyResultLoaderImpl) loader).loadResult();
  }


  private static class EnhancedLazyResultLoaderImpl implements InvocationHandler, Serializable {


    protected transient ExtendedSqlMapClient client; // (PM) Can't serialize a jdbc connection.
    /* 
     * (PM) Added a getter method around the SqlMapClient field.  This is for when
     * any lazy loaded object is being serialized.
     */
    protected ExtendedSqlMapClient getClient() {
        if ( client == null) {
            client = (ExtendedSqlMapClient)AppSqlConfig.getSqlMapInstance();
        }
        return client;
    }
    
    protected String statementName;
    protected Object parameterObject;
    protected Class targetType;

    protected boolean loaded;
    protected Object resultObject;

    /**
     * Constructor for an enhanced lazy list loader implementation
     *
     * @param client - the client that is creating the lazy list
     * @param statementName - the statement to be used to build the list
     * @param parameterObject - the parameter object to be used to build the list
     * @param targetType - the type we are putting data into
     */
    public EnhancedLazyResultLoaderImpl(ExtendedSqlMapClient client, String statementName, Object parameterObject, Class targetType) {
      this.client = client;
      this.statementName = statementName;
      this.parameterObject = parameterObject;
      this.targetType = targetType;
    }

    /**
     * Loads the result
     *
     * @return the results - a list or object
     * 
     * @throws SQLException if there is a problem
     */
    public Object loadResult() throws SQLException {
      if (DomTypeMarker.class.isAssignableFrom(targetType)) {
        return ResultLoader.getResult(getClient(), statementName, parameterObject, targetType);
      } else if (Collection.class.isAssignableFrom(targetType)) {
        Enhancer enhancer = new Enhancer();
        enhancer.setInterfaces(INTERFACES);
        enhancer.setSuperclass(ListEnhancable.class);
        Callback[] callbacks = { NoOpCallback.INSTANCE, this};
        enhancer.setCallbackFilter(EnhancedListCallbackFilter.instance);
        enhancer.setCallbacks(callbacks);
        
        return enhancer.create();
      } else if (targetType.isArray() || ClassInfo.isKnownType(targetType)) {
        return ResultLoader.getResult(getClient(), statementName, parameterObject, targetType);
      } else if ( BaseDomain.class.isAssignableFrom(targetType)  && !Modifier.isAbstract(targetType.getModifiers())) {
          return  Enhancer.create(targetType, new Class[] { IBaseDomain.class } , this);
      } else {
	    loadObject();
		return resultObject;
      }
    }
    






    private interface NoOpCallback extends NoOp, Serializable {
        /**
         * A thread-safe singleton instance of the <code>NoOpCallback</code> callback.
         */
        public static final NoOpCallback INSTANCE = new NoOpCallback() {
        };
    }

    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
      if ("finalize".hashCode() == method.getName().hashCode()
          && "finalize".equals(method.getName())) {
        return null;
      }else {
        loadObject();
        if (resultObject != null) {
          try {
            return method.invoke(resultObject, objects);
          } catch (Throwable t) {
            throw ClassInfo.unwrapThrowable(t);
          }
        } else {
          return null;
        }
      }
    }

    private synchronized void loadObject() {
      if (!loaded) {
        try {
          loaded = true;
          resultObject = ResultLoader.getResult(getClient(), statementName, parameterObject, targetType);
        } catch (SQLException e) {
          throw new RuntimeException("Error lazy loading result. Cause: " + e, e);
        }
      }
    }
  }


}
