package daruma.storage_manager.type_definition;

import daruma.sql.Separators;
import daruma.xml.SimpleXPath;
import daruma.xml.UniversalName;

import daruma.storage_manager.StorageManager;
import daruma.storage_manager.StorageException;


/* !!! [06/08/16 02:50 I.Noda] !!! 
 * TopLevelElementColumnNameFactory.java から移植。
 * より統一的な naming のために。
 */
public class ColumnNameFactory
{

    /**
     * column名の接続文字
     */
    public static final String ColNameSep = Separators.PATH_SEPARATOR ;

    /**
     * attribute の column名の接続文字
     */
//    public static final String ColNameAttributePrefix = "@";
    public static final String ColNameAttributePrefix = "_at_";

    /**
     * column名の拡張子接続文字
     */
    public static final String ColNameSuffixSep = "_" ;

    /**
     * toplevel の値、あるいは geometryやtimeの文字列としての値
     */
    public static final String ColNameForValue = "_value_" ;
    

    /**
     * constructor を隠す。
     */
	private	ColumnNameFactory()
	{
	}

	public	static	String	createColumnName
				    ( SimpleXPath  path ,
				      StorageManager  storage )
							throws StorageException
	{
		String	columnName;

		if ( path == null || path.getList().size() == 0 )
		{
			columnName = ColumnNameFactory
					.getTopLevelElementColumnName();

			if ( path != null && path.isAttribute() )
			{
				columnName += ColumnNameFactory.ColNameSep;
				columnName += ColumnNameFactory
						.ColNameAttributePrefix;
				columnName += storage.getUniversalNameID
						( path.getAttributeName() );
			}
		}
		else
		{
			SimpleXPath	newPath = new SimpleXPath( path );

			columnName = storage.getShortXPathStringForDB
								( newPath );
		}

		return( columnName );
	}


	public	static	String	createAttributeColumnName
				    ( SimpleXPath  path ,
				      UniversalName  attributeName ,
				      StorageManager  storage )
							throws StorageException
	{
		String	baseColumnName;

		if ( path == null )
		{
			baseColumnName = ColumnNameFactory
					.getTopLevelElementColumnName();
		}
		else
		{
			SimpleXPath	newPath = new SimpleXPath( path );

			baseColumnName = storage.getShortXPathStringForDB
								( newPath );
		}

		String	columnName = baseColumnName
				     + ColumnNameFactory.ColNameSep
				     + ColumnNameFactory.ColNameAttributePrefix
				     + storage.getUniversalNameID
							( attributeName );

		return( columnName );
	}

	public	static	String	createAttributeColumnName
				    ( SimpleXPath  path ,
				      ElementName  elementName ,
				      UniversalName  attributeName ,
				      StorageManager  storage )
							throws StorageException
	{
		String	baseColumnName;

		if ( path == null )
		{
			baseColumnName = ColumnNameFactory
					.getTopLevelElementColumnName();
		}
		else
		{
			SimpleXPath	newPath = new SimpleXPath
							( path , elementName );

			baseColumnName = storage.getShortXPathStringForDB
								( newPath );
		}

		String	columnName = baseColumnName
				     + ColumnNameFactory.ColNameSep
				     + ColumnNameFactory.ColNameAttributePrefix
				     + storage.getUniversalNameID
							( attributeName );

		return( columnName );
	}


    /**
     * toplevel のエレメントの値のためのカラム名
     */
	public	static	final	String	getTopLevelElementColumnName()
	{
	    /* !!! [06/08/16 02:50 I.Noda] !!! 
	     * 統一のために、文字列はできるだけ final 変数化
	     */
	    //return( "_value_" );
	    return( ColNameForValue ) ;
	}

    public	static	final	String	getTopLevelElementColumnName
							( String  suffix )
	{
	    return( ColNameForValue + ColNameSep + suffix ) ;
	}

    /* !!! [06/08/16 02:50 I.Noda] !!! 
     * suffix 付きカラム名
     */
    public static final	String	getColumnName(String shortName,
					      String suffix)
    {
	return( shortName + ColNameSep + suffix ) ;
    }

    public	static	final	String	getCoiceElementIndexColumnName
						     ( String  base )
	{
		return( base + ColNameSep
			+ "choice_element_index" );
	}

}
