/*
 * DeleteHandler.java
 *
 * Created on 11 de Outubro de 2004, 16:04
 */

package siprp.data;

import com.evolute.utils.jdo.*;

/**
 *
 * @author  fpalma
 */
public class DisableDeleteHandler implements JDOOperationHandlerInterface
{
	public static final DisableDeleteHandler INSTANCE = new DisableDeleteHandler();
	
	/** Creates a new instance of DeleteHandler */
	public DisableDeleteHandler()
	{
	}
	
	public boolean handle( JDOObject object, int operation, int moment )
		throws Exception
	{
		if( !( object instanceof DisableDeleteable ) )
		{
			return false;
		}
		switch( operation )
		{
			case OP_SAVE:
				return save( object, moment );
				
			case OP_DELETE:
				return delete( object, moment );
				
		}
		return false;
	}
	
	protected boolean save( JDOObject object, int moment )
		throws Exception
	{
		if( moment != MOMENT_BEFORE )
		{
			return false;
		}
		String old = (String) object.get( DisableDeleteable.INACTIVO );
		if( old == null )
		{
			object.set( DisableDeleteable.INACTIVO, "n" );
		}
		return true;
	}
	
	protected boolean delete( JDOObject object, int moment )
		throws Exception
	{
		if( moment != MOMENT_INSTEAD )
		{
			return false;
		}
		object.set( DisableDeleteable.INACTIVO, "y" );
		object.save();
		return true;
	}
	
}
