#
# @since DBFlute-0.9.0
#
#set ($database = $schemaData.database)

## - - - - - - - - - - - -
## Initialize properties.
## - - - - - - - - - - - -
$database.initializeVersion(90)
$database.initializeClassificationDeployment()

## *****************************************************************************************************************
##                                                                                                   Global Paramter
##                                                                                                   ***************
## ===================================================================================
##                                                                            Language
##                                                                            ========
#set ($glTargetLanguage = ${database.TargetLanguage})

## ===================================================================================
##                                                                  Resource Directory
##                                                                  ==================
#set ($glResourceDirectory = ${database.resourceDirectory})

## ===================================================================================
##                                                                           Extension
##                                                                           =========
#set ($glTemplateFileExtension = ${database.templateFileExtension})
#set ($glClassFileExtension = ${database.classFileExtension})

## ===================================================================================
##                                                                              Prefix
##                                                                              ======
#set ($glProjectPrefix = ${database.projectPrefix})
#set ($glBasePrefix = ${database.basePrefix})
#set ($glBaseSuffixForEntity = ${database.baseSuffixForEntity})

## ===================================================================================
##                                                                           Container
##                                                                           =========
#set ($glDBFluteDiconNamespace = "${database.DBFluteDiconNamespace}")
#set ($glDBFluteDiconFileName = "${database.DBFluteDiconFileName}")
#set ($glCreatorDiconFileName = "${database.DBFluteCreatorDiconFileName}")
#set ($glCustomizerDiconFileName = "${database.DBFluteCustomizerDiconFileName}")
#set ($glJ2eeDiconResourceName = "${database.j2eeDiconResourceName}")
#set ($glDBFluteBeansFileName = "${database.DBFluteBeansFileName}")

## ===================================================================================
##                                                                          Class Name
##                                                                          ==========
## [Allcommon]
#set ($glAccessContext = "${glProjectPrefix}AccessContext")
#set ($glBuriDef = "${glProjectPrefix}BuriDef")
#set ($glCDef = "${glProjectPrefix}CDef")
#set ($glDBCurrent = "${glProjectPrefix}DBCurrent")
#set ($glDBFluteConfig = "${glProjectPrefix}DBFluteConfig")
#set ($glDBFluteInitializer = "${glProjectPrefix}DBFluteInitializer")
#set ($glEntityDefinedCommonColumn = "${glProjectPrefix}EntityDefinedCommonColumn")
#set ($glImplementedBehaviorSelector = "${glProjectPrefix}ImplementedBehaviorSelector")
#set ($glImplementedCommonColumnAutoSetupper = "${glProjectPrefix}ImplementedCommonColumnAutoSetupper")
#set ($glImplementedInvokerAssistant = "${glProjectPrefix}ImplementedInvokerAssistant")
#set ($glImplementedSqlClauseCreator = "${glProjectPrefix}ImplementedSqlClauseCreator")
#set ($glDBMetaInstanceHandler = "${glProjectPrefix}DBMetaInstanceHandler")

## ===================================================================================
##                                                                            Variable
##                                                                            ========
#set ($glConditionBeanInitialName = "CB")
#set ($glConditionQueryInitialName = "CQ")
#set ($glAttachedCBArgsVariableName = "cb")

## ===================================================================================
##                                                                             Package
##                                                                             =======
#set ($glPackageBaseCommon = "${database.baseCommonPackage}")
#set ($glPackageBaseCommonCBean = "${database.baseCommonPackage}.cbean")
#set ($glPackageBaseCommonCOption = "${database.baseCommonPackage}.cbean.coption")
#set ($glPackageBaseCommonCBeanOutsidesql = "${database.baseCommonPackage}.cbean.outsidesql")
#set ($glPackageBaseCommonDBMeta = "${database.baseCommonPackage}.dbmeta")
#set ($glPackageBaseCommonDBMetaInfo = "${database.baseCommonPackage}.dbmeta.info")
#set ($glPackageBaseCommonHelper = "${database.baseCommonPackage}.helper")
#set ($glPackageBaseCommonJavaLike = "${database.baseCommonPackage}.javalike")
#set ($glPackageBaseCommonJdbc = "${database.baseCommonPackage}.jdbc")
#set ($glPackageBaseCommonUtil = "${database.baseCommonPackage}.util")
#set ($glPackageBaseDao = "${database.baseDaoPackage}" )
#set ($glPackageBaseEntity = "${database.baseEntityPackage}")
#set ($glPackageExtendedDao = "${database.extendedDaoPackage}")
#set ($glPackageExtendedEntity = "${database.extendedEntityPackage}")
#set ($glPackageBaseCursor = "${database.sql2EntityBaseCursorPackage}" )
#set ($glPackageExtendedCursor = "${database.sql2EntityExtendedCursorPackage}")

## -----------------------------------------------------------------------------------------------------
##                                                                                Parse customize entity
##                                                                                ----------------------
## Setup output directory.
$database.enableSql2EntityOutputDirectory()

## for Sql2Entity
#set ($isSql2Entity = true)

#set ($glPackageBaseCustomizeEntity = "${database.Sql2EntityBaseEntityPackage}" )
#set ($glPackageCustomizeDBMeta = "${database.Sql2EntityDBMetaPackage}")
#set ($glPackageExtendedCustomizeEntity = "${database.Sql2EntityExtendedEntityPackage}" )

#set ($path = $database.getPackageAsPath(${glPackageBaseCustomizeEntity}))
$database.makeDirectory($path)

#set ($path = $database.getPackageAsPath(${glPackageCustomizeDBMeta}))
$database.makeDirectory($path)

#set ($path = $database.getPackageAsPath(${glPackageExtendedCustomizeEntity}))
$database.makeDirectory($path)

##
## Delete old customize classes.
##
$database.deleteOldCustomizeClass();

$database.debug("public void parseCustomizeEntity() {")
$database.debug("    final List<String> tableNameList = getTableList();")
$database.debug("    for (String tableName : tableNameList) {")
#foreach ($table in $database.tableList)
  #set ($myBaseObjectClassName = "${glProjectPrefix}${glBasePrefix}${table.javaName}")
  #set ($myBaseEntityPackageName = "${glPackageBaseCustomizeEntity}")
  #set ($myExtendedObjectClassName = "${glProjectPrefix}${table.javaName}")
  #set ($myExtendedEntityPackageName = "${glPackageExtendedCustomizeEntity}")
  #set ($myDBMetaClassName = "${glProjectPrefix}${table.javaName}Dbm")
  #set ($myDBMetaPackageName = "${glPackageCustomizeDBMeta}")

  #set ($myEntityVariableName = "${table.uncapitalisedJavaName}")
  #set ($myEntityListVariableName = "${table.uncapitalisedJavaName}List")
  #set ($myCBVariableName = "${table.uncapitalisedJavaName}")

  #set ($myBaseDaoCursorPackageName = "${glPackageBaseCursor}")
  #set ($myExtendedDaoCursorPackageName = "${glPackageExtendedCursor}")
  #set ($myBaseTypeSafeCursorClassName = "${glProjectPrefix}${glBasePrefix}${table.javaName}Cursor")
  #set ($myBaseTypeSafeCursorHandlerClassName = "${glProjectPrefix}${glBasePrefix}${table.javaName}CursorHandler")
  #set ($myExtendedTypeSafeCursorClassName = "${glProjectPrefix}${table.javaName}Cursor")
  #set ($myExtendedTypeSafeCursorHandlerClassName = "${glProjectPrefix}${table.javaName}CursorHandler")

  #set ($isExistDao = false)
  #set ($isExistConditionBean = false)

  #if ($table.isSql2EntityTypeSafeCursor())
    #set ($path = $database.getPackageAsPath(${myBaseDaoCursorPackageName}))
    $database.makeDirectory($path)

    $database.debug("        parse('BaseTypeSafeCursor')")
    #set ( $path = "${database.getPackageAsPath(${myBaseDaoCursorPackageName})}${myBaseTypeSafeCursorClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/bsbhv/cursor/BsTypeSafeCursor.${glTemplateFileExtension}",$path ,"" ,"")

    $database.debug("        parse('BaseTypeSafeCursorHandler')")
    #set ( $path = "${database.getPackageAsPath(${myBaseDaoCursorPackageName})}${myBaseTypeSafeCursorHandlerClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/bsbhv/cursor/BsTypeSafeCursorHandler.${glTemplateFileExtension}",$path ,"" ,"")

    #set ($path = $database.getPackageAsPath(${myExtendedDaoCursorPackageName}))
    $database.makeDirectory($path)

    $database.debug("        parse('ExtendedTypeSafeCursor')")
    #set ( $path = "${database.getPackageAsPath(${myExtendedDaoCursorPackageName})}${myExtendedTypeSafeCursorClassName}.${glClassFileExtension}" )
    #if (!$files.file(${generator.outputPath},$path).exists())
      $generator.parse("om/${glResourceDirectory}/exbhv/cursor/ExTypeSafeCursor.${glTemplateFileExtension}",$path ,"" ,"")
    #end
    $database.reflectAllExCopyright($path)

    $database.debug("        parse('ExtendedTypeSafeCursorHandler')")
    #set ( $path = "${database.getPackageAsPath(${myExtendedDaoCursorPackageName})}${myExtendedTypeSafeCursorHandlerClassName}.${glClassFileExtension}" )
    #if (!$files.file(${generator.outputPath},$path).exists())
      $generator.parse("om/${glResourceDirectory}/exbhv/cursor/ExTypeSafeCursorHandler.${glTemplateFileExtension}",$path ,"" ,"")
    #end
    $database.reflectAllExCopyright($path)
  #else
    $database.debug("        parseBaseCustomizeEntity(tableName);     // ${myBaseObjectClassName}")
    #set ( $path = "${database.getPackageAsPath(${myBaseEntityPackageName})}${myBaseObjectClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/bsentity/BaseEntity.${glTemplateFileExtension}",$path ,"" ,"")

    $database.debug("        parseCustomizeDBMeta(tableName);         // ${myDBMetaClassName}")
    #set ( $path = "${database.getPackageAsPath(${myDBMetaPackageName})}${myDBMetaClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/bsentity/dbmeta/DBMetaImpl.${glTemplateFileExtension}",$path,"", "")

    #set ( $path = "${database.getPackageAsPath(${myExtendedEntityPackageName})}${myExtendedObjectClassName}.${glClassFileExtension}" )
    #if (!$files.file(${generator.outputPath},$path).exists())
      $database.debug("        parseExtendedCustomizeEntity(tableName); // ${myExtendedObjectClassName}")
      $generator.parse("om/${glResourceDirectory}/exentity/ExtendedEntity.${glTemplateFileExtension}",$path ,"" ,"")
    #end
    $database.reflectAllExCopyright($path)
  #end
#end
$database.debug("    }")
$database.debug("}")

## -----------------------------------------------------------------------------------------------------
##                                                                                  Parse parameter bean
##                                                                                  --------------------
#if ($database.isExistPmbMetaData())
$database.debug("public void parseParameterBean() {")
$database.debug("    final List<DfParameterBeanMetaData> parameterBeanList = getParameterBeanList();")
$database.debug("    for (DfParameterBeanMetaData parameterBean : parameterBeanList) {")

#set ($glPackageBaseParameterBean = "${database.sql2EntityBaseParameterBeanPackage}" )
#set ($glPackageExtendedParameterBean = "${database.sql2EntityExtendedParameterBeanPackage}" )

  #set ($path = $database.getPackageAsPath(${glPackageBaseParameterBean}))
  $database.makeDirectory($path)
  #set ($path = $database.getPackageAsPath(${glPackageExtendedParameterBean}))
  $database.makeDirectory($path)

  #foreach ($pmbMetaData in $database.pmbMetaDataList)
    #set ($pmbClassName = "$pmbMetaData.className")
    #set ($myBaseParameterBeanClassName = "${glProjectPrefix}${glBasePrefix}${pmbClassName}")
    #set ($myExtendedParameterBeanClassName = "${glProjectPrefix}${pmbClassName}")

    $database.debug("        parseBaseParameterBean(parameterBean);     // ${myBaseParameterBeanClassName}")
    #set ( $path = "${database.getPackageAsPath(${glPackageBaseParameterBean})}${myBaseParameterBeanClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/bsbhv/pmbean/BsParameterBean.${glTemplateFileExtension}",$path ,"" ,"")

    #set ( $path = "${database.getPackageAsPath(${glPackageExtendedParameterBean})}${myExtendedParameterBeanClassName}.${glClassFileExtension}" )
    #if (!$files.file(${generator.outputPath},$path).exists())
      $database.debug("        parseExtendedParameterBean(parameterBean); // ${myExtendedParameterBeanClassName}")
      $generator.parse("om/${glResourceDirectory}/exbhv/pmbean/ExParameterBean.${glTemplateFileExtension}",$path ,"" ,"")
    #end
    $database.reflectAllExCopyright($path)
  #end
$database.debug("    }")
$database.debug("}")
#end

## ===================================================================================
##                                                                          Simple Dto
##                                                                          ==========
#if ($database.hasSimpleDtoDefinition() && $database.isTargetLanguageJava())
  $database.debug("parseSimpleDto() {// mapper=$database.isSimpleDtoUseDtoMapper()")
  #set ($myDomainEntityPackageName = "${glPackageExtendedEntity}.customize")
  #set ($myBaseEntityPackageName = "${database.simpleDtoBaseDtoPackage}.customize")
  #set ($myExtendedEntityPackageName = "${database.simpleDtoExtendedDtoPackage}.customize")
  #set ($myDtoMapperPackageName = "${database.simpleDtoDtoMapperPackage}.customize")

  #set ($path = $database.getPackageAsPath(${myBaseEntityPackageName}))
  $database.makeDirectory($path)

  #set ($path = $database.getPackageAsPath(${myExtendedEntityPackageName}))
  $database.makeDirectory($path)

  #if ($database.isSimpleDtoUseDtoMapper())
    #set ($path = $database.getPackageAsPath(${myDtoMapperPackageName}))
    $database.makeDirectory($path)
  #end

  #foreach ($table in $database.tables)
    #if (!$table.isSql2EntityTypeSafeCursor())
    #set ($myBaseObjectClassName = "${database.simpleDtoBaseDtoPrefix}${table.javaName}${database.simpleDtoBaseDtoSuffix}")
    #set ($myExtendedObjectClassName = "${database.simpleDtoExtendedDtoPrefix}${table.javaName}${database.simpleDtoExtendedDtoSuffix}")
    #set ($myDtoMapperClassName = "${myExtendedObjectClassName}Mapper")

    $database.debug("    parse('${myExtendedObjectClassName}');")

    #set ( $path = "${database.getPackageAsPath(${myBaseEntityPackageName})}${myBaseObjectClassName}.${glClassFileExtension}" )
    $generator.parse("om/${glResourceDirectory}/plugin/simpledto/SimpleDtoBaseEntity.${glTemplateFileExtension}", $path, "table", $table)

    #if (!$database.isStopGenerateExtendedEntity())
      #set ( $path = "${database.getPackageAsPath(${myExtendedEntityPackageName})}${myExtendedObjectClassName}.${glClassFileExtension}" )
      #if (!$files.file(${generator.outputPath},$path).exists())
        $generator.parse("om/${glResourceDirectory}/plugin/simpledto/SimpleDtoExtendedEntity.${glTemplateFileExtension}", $path, "table", $table)
      #end
      $database.reflectAllExCopyright($path)
    #end

    #if ($database.isSimpleDtoUseDtoMapper())
      #set ( $path = "${database.getPackageAsPath(${myDtoMapperPackageName})}${myDtoMapperClassName}.${glClassFileExtension}" )
      $generator.parse("om/${glResourceDirectory}/plugin/simpledto/mapper/SimpleDtoMapper.${glTemplateFileExtension}", $path, "table", $table)
    #end
    #end
  #end
  $database.debug("}")
#end

## ===================================================================================
##                                                                            Flex Dto
##                                                                            ========
#if ($database.hasFlexDtoDefinition() && $database.isTargetLanguageJava())
  $database.enableFlexDtoOutputDirectory()
  $database.debug("parseFlexDto() {")
  #set ($myBaseEntityPackageName = "${database.flexDtoBaseDtoPackage}.customize")
  #set ($myExtendedEntityPackageName = "${database.flexDtoExtendedDtoPackage}.customize")

  #set ($path = $database.getPackageAsPath(${myBaseEntityPackageName}))
  $database.makeDirectory($path)

  #set ($path = $database.getPackageAsPath(${myExtendedEntityPackageName}))
  $database.makeDirectory($path)

  #foreach ($table in $database.tables)
    #if (!$table.isSql2EntityTypeSafeCursor())
    #set ($myBaseObjectClassName = "${database.flexDtoBaseDtoPrefix}${table.javaName}${database.flexDtoBaseDtoSuffix}")
    #set ($myExtendedObjectClassName = "${database.flexDtoExtendedDtoPrefix}${table.javaName}${database.flexDtoExtendedDtoSuffix}")
    #set ($myRemoteSimpleDtoClassName = "${database.simpleDtoExtendedDtoPackage}.customize.${myExtendedObjectClassName}")

    $database.debug("    parse('${myExtendedObjectClassName}');")

    #set ( $path = "${database.getPackageAsPath(${myBaseEntityPackageName})}${myBaseObjectClassName}.as" )
    $generator.parse("om/${glResourceDirectory}/plugin/flexdto/FlexDtoBaseEntity.${glTemplateFileExtension}", $path, "table", $table)

    #if (!$database.isStopGenerateExtendedEntity())
      #set ( $path = "${database.getPackageAsPath(${myExtendedEntityPackageName})}${myExtendedObjectClassName}.as" )
      #if ($database.isFlexDtoOverrideExtended() || !$files.file(${generator.outputPath},$path).exists())
        $generator.parse("om/${glResourceDirectory}/plugin/flexdto/FlexDtoExtendedEntity.${glTemplateFileExtension}", $path, "table", $table)
      #end
      $database.reflectAllExCopyright($path)
    #end
    #end
  #end
  $database.debug("}")
  $database.enableSql2EntityOutputDirectory()
#end
