/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package net.sf.joyaop.extension.hibernate;

import net.sf.joyaop.ObjectFactory;
import net.sf.joyaop.impl.WriteReplace;
import org.hibernate.CallbackException;
import org.hibernate.Interceptor;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Iterator;

/**
 * only for hibernate3
 *
 * @author Shen Li
 */
public class JoyInterceptor implements Interceptor {
    private final ObjectFactory objectFactory;
    private final Configuration configuration;

    public JoyInterceptor(ObjectFactory objectFactory, Configuration configuration) {
        this.objectFactory = objectFactory;
        this.configuration = configuration;
    }

	public boolean onLoad(Object entity, Serializable id, Object[] state,
                          String[] propertyNames, Type[] types) throws CallbackException {
        return false;
    }

	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
                                Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {
        return false;
    }

	public boolean onSave(Object entity, Serializable id, Object[] state,
                          String[] propertyNames, Type[] types) throws CallbackException {
        return false;
    }

	public void onDelete(Object entity, Serializable id, Object[] state,
                         String[] propertyNames, Type[] types) throws CallbackException {
    }

	public void preFlush(Iterator entities) throws CallbackException {

    }

	public void postFlush(Iterator entities) throws CallbackException {

    }

	public Boolean isUnsaved(Object entity) {
        return Boolean.FALSE;
    }

	public int[] findDirty(Object entity, Serializable id, Object[] currentState,
                           Object[] previousState, String[] propertyNames, Type[] types) {
        return null;
    }

	public Object instantiate(String entityName, Serializable id) throws CallbackException {
        // TODO
        Object entity = objectFactory.newInstance(entityName);
        PersistentClass persistentClass = configuration.getClassMapping(entityName);
        persistentClass.getIdentifierProperty().getSetter(persistentClass.getMappedClass()).set(entity, id);        
        return entity;
    }

	public String getEntityName(Object object) throws CallbackException {
        if (object instanceof WriteReplace) {
            return object.getClass().getSuperclass().getName();
        }
        return object.getClass().getName();
    }

	public Object getEntity(String entityName, Serializable id) throws CallbackException {
        return null;
    }
}
