import at.clockwork.security.domain.*

import at.clockwork.singleton.SystemConfigurations
import at.clockwork.transfer.gwtTransfer.client.enumeration.*
import at.clockwork.generated.service.*

import grails.util.Holders

class UpdateScriptService {

	def reportRoleWriteService = Holders.grailsApplication.mainContext.getBean 'reportRoleWriteService'
	def individualReportRoleWriteService = Holders.grailsApplication.mainContext.getBean 'individualReportRoleWriteService'

    SystemConfigurations systemConfigurations = SystemConfigurations.getInstance()

	def run(def connection, def dataSourceFields) {

        List occuringTimeTimeTypes001List = [
            ReportRole.SumOfTimeTimeTypeData001,
            ReportRole.SumNotZeroOfTimeTimeTypeData001,
            ReportRole.SumAndTotalOfTimeTimeTypeData001,
            ReportRole.TotalAndSumAndTotalOfTimeTimeTypeData001,
            ReportRole.SumOfTimeTimeTypeData101,
            ReportRole.SumNotZeroOfTimeTimeTypeData101,
            ReportRole.SumAndTotalOfTimeTimeTypeData101,
            ReportRole.TotalAndSumAndTotalOfTimeTimeTypeData101,
            ReportRole.SumOfTimeTimeTypeData201,
        ]

        List occuringTimeTimeTypes002List = [
            ReportRole.TimeTimeTypeOccurrence001,
            ReportRole.DataOfTimeTimeTypeData001,
            ReportRole.DataNotZeroOfTimeTimeTypeData001,
            ReportRole.TimeTimeTypeOccurrence101,
            ReportRole.DataOfTimeTimeTypeData101,
            ReportRole.DataNotZeroOfTimeTimeTypeData101,
        ]

        List  occuringTimeTimeTypes001LandscapeList = [
            ReportRole.SumOfTimeTimeTypeData001Landscape,
            ReportRole.SumNotZeroOfTimeTimeTypeData001Landscape,
            ReportRole.SumAndTotalOfTimeTimeTypeData001Landscape,
            ReportRole.TotalAndSumAndTotalOfTimeTimeTypeData001Landscape,
            ReportRole.SumOfTimeTimeTypeData101Landscape,
            ReportRole.SumNotZeroOfTimeTimeTypeData101Landscape,
            ReportRole.SumAndTotalOfTimeTimeTypeData101Landscape,
            ReportRole.TotalAndSumAndTotalOfTimeTimeTypeData101Landscape,
            ReportRole.SumOfTimeTimeTypeData201Landscape,
        ]

        List  occuringTimeTimeTypes002LandscapeList = [
            ReportRole.TimeTimeTypeOccurrence001Landscape,
            ReportRole.DataOfTimeTimeTypeData001Landscape,
            ReportRole.DataNotZeroOfTimeTimeTypeData001Landscape,
            ReportRole.TimeTimeTypeOccurrence101Landscape,
            ReportRole.DataOfTimeTimeTypeData101Landscape,
            ReportRole.DataNotZeroOfTimeTimeTypeData101Landscape,
        ]

        ReportRole.findAll().each { ReportRole itReportRole ->

            if ( occuringTimeTimeTypes001List.any{ it == itReportRole.cwID } ) {

                itReportRole.filename = "timeTimeTypeOccurrence001"
                reportRoleWriteService.save( itReportRole )

            } else if ( occuringTimeTimeTypes001LandscapeList.any{ it == itReportRole.cwID } ) {

                itReportRole.filename = "timeTimeTypeOccurrence001Landscape"
                reportRoleWriteService.save( itReportRole )

            } else if ( occuringTimeTimeTypes002List.any{ it == itReportRole.cwID } ) {

                itReportRole.filename = "timeTimeTypeOccurrence002"
                reportRoleWriteService.save( itReportRole )

            } else if ( occuringTimeTimeTypes002LandscapeList.any{ it == itReportRole.cwID } ) {

                itReportRole.filename = "timeTimeTypeOccurrence002Landscape"
                reportRoleWriteService.save( itReportRole )

            }

        }

		true

	}

}