package daruma.wfs.transaction;

import daruma.storage_manager.StorageManager;
import daruma.storage_manager.StorageException;
import daruma.storage_manager.type_definition.ElementName;
import daruma.storage_manager.type_definition.TypeName;
import daruma.storage_manager.type_definition.TypeDefinition;
import daruma.storage_manager.type_definition.TypedInstance;
import daruma.storage_manager.type_definition.TypeException;

import daruma.wfs.TransactionHandler;
/* !!! [06/08/22 18:56 I.Noda] !!! 
 * TransactionResultInfo を導入。
 */
import daruma.wfs.TransactionResultInfo ;

import daruma.xml.SimpleXPath;
import daruma.xml.handler.XSAXDOMCreateHandler;
import daruma.xml.util.ElementUtil;
import daruma.util.Pair;
import daruma.util.LogWriter;

import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.Element;

import java.io.OutputStream;
import java.util.List;

// XXX: only for debugging
import daruma.xml.util.XMLFormatConverter;
import javax.xml.transform.TransformerException;


public class InsertAnElementHandler extends XSAXDOMCreateHandler
{
	private	StorageManager		storage;
	private	TransactionHandler	errorReceiver;

        /* !!! [06/08/22 18:56 I.Noda] !!! 
         * TransactionResultInfo を導入。
         */
        private TransactionResultInfo resultInfo ;

	public	InsertAnElementHandler
			( OutputStream  out ,
			  XMLReader  parser ,
			  boolean  isTopLevelHandler ,
			  StorageManager  storage ,
			  TransactionHandler  errorReceiver,
			  /* !!! [06/08/22 18:56 I.Noda] !!! 
			   * TransactionResultInfo を導入。
			   */
			  TransactionResultInfo resultInfo)
	{
		super( out , parser , isTopLevelHandler );
		super.setInhibitEndPrefixBeforeEndDocument( true );

		this.storage       = storage;
		this.errorReceiver = errorReceiver;

		/* !!! [06/08/22 18:56 I.Noda] !!! 
		 * TransactionResultInfo を導入。
		 */
		this.resultInfo  = resultInfo;
	}

	public  void  xEndDocument() throws SAXException
	{
		Element	element = null;

		element = super.getDocumentElement();

		/* !!! [06/08/17 15:14 I.Noda] !!! 
		 * エラーメッセージを整理
		 */
		LogWriter.qwrite("DEBUG", "data = [", element, "]") ;

		ElementName	elementName = new ElementName
						  ( element.getNamespaceURI() ,
						    element.getLocalName() );

		TypeName	typeName = null;

		try
		{
			typeName = storage.getElementTypeName( elementName );
		}
		catch( StorageException  e )
		{
			this.throwError( new SAXParseException
					 ( e.getMessage() ,
					   super.getLocator() ,
					   e ) );
		}

		LogWriter.qwrite("DEBUG",  "typeName = [", typeName, "]" );

		if ( typeName == null )
		{
			this.throwError( new SAXParseException
					 ( "type of element "
					   + elementName.getLocalName()
					   + " in namespace "
					   + elementName.getNamespace()
					   + " not found" ,
					   super.getLocator() ) );
		}

		TypeDefinition	type = null;

		try
		{
			type = storage.getTypeDefinition( typeName );
		}
		catch( StorageException  e )
		{
			this.throwError( new SAXParseException
					     ( e.getMessage() ,
					       super.getLocator() ,
					       e ) );
		}

		if ( type == null )
		{
			this.throwError( new SAXParseException
					 ( "type definition of "
					   + elementName.getLocalName()
					   + " in namespace "
					   + elementName.getNamespace()
					   + " not found" ,
					   super.getLocator() ) );
		}


		TypedInstance	obj = null;

		try
		{
			Pair<TypedInstance, Integer>
				r = type.createInstance( element ,
							 elementName ,
							 (SimpleXPath)null ,
							 this.storage ,
							 0 );
			obj = r.getFirst();

			if ( ElementUtil.hasExtraChildElements
			     ( element , r.getSecond() ) )
			{
				List<Element> ex = ElementUtil
						   .getExtraChildElements
						    ( element ,
						      r.getSecond() );

				for( Element e : ex )
				{
					ElementUtil.debugPrint( e ,
								System.err );
				}

				throw new TypeException
					  ( "too many child elements of "
					    + new ElementName( element )
								.toString() );
			}
		}
		catch( TypeException  e )
		{
			this.throwError( new SAXParseException
					 ( "invalid element " + elementName
					   + ": " + e.getMessage() ,
					   super.getLocator() ) );
		}


		// XXX
		if ( obj == null )
		{
			this.throwError( new SAXParseException
					 ( "invalid element "
					   + elementName.getLocalName()
					   + " in namespace "
					   + elementName.getNamespace() ,
					   super.getLocator() ) );
		}


		try
		{
			this.storage.insertElement( elementName , obj,
						    super.getTransactionSN());
			/* !!! [06/08/22 18:56 I.Noda] !!! 
			 */
			this.resultInfo.countInsert++ ;
		}
		catch( StorageException  e )
		{
			this.throwError( new SAXParseException
					     ( e.getMessage() ,
					       super.getLocator() ,
					       e ) );
		}
	}


	private	void	throwError( SAXParseException  e ) throws SAXException
	{
		this.errorReceiver.notifyError( e );

		throw e;
	}
}
