
import at.dockwork.time.domain.*
import at.dockwork.time.timeType.domain.*
import at.dockwork.report.domain.*
import at.dockwork.time.enumeration.*
import at.dockwork.system.service.SystemConfigurationService

import grails.util.Holders




class UpdateScriptService {

	def systemConfigurationService = Holders.grailsApplication.mainContext.getBean 'systemConfigurationService'

	def run(def connection, def dataSourceFields) {

		def resultMap = at.dockwork.system.service.DataSourceService.execute(
			connection,
			dataSourceFields,
			[
				mySQLStatement:"update dw_time_time_type set reduce_total = true where reduce_total is null",
				msSQLStatement:"update dw_time_time_type set reduce_total = 0 where reduce_total is null"
			]
		)

		TimeTimeTypeDefinition timeTimeTypeDefinition = new TimeTimeTypeDefinition(name:"booking", description:"booking")
		timeTimeTypeDefinition.save()
		if (timeTimeTypeDefinition) {

			connection.eachRow('select id, display_column from dw_time_time_type where display_column is not null and display_column > 0 order by display_column') {

				long id = it.id
				int displayColumn = it.display_column
				TimeTimeType timeTimeType = TimeTimeType.get(id)

				if (timeTimeType && !timeTimeType.deleted) {

					timeTimeTypeDefinition.addToTimeTimeTypeDefinitionDetails(
						new TimeTimeTypeDefinitionDetail(
							position:displayColumn,
//							description:"",
							timeTimeType:timeTimeType
//							TimeFormat timeFormat
//							timeCalculation:TimeCalculation.DAY,
//							timeDurationInRelationTo:TimeDurationInRelationTo.DAY,
//							timeDuration:TimeDuration.TODAY
						)
					)
					timeTimeTypeDefinition.save()

				}
			}

			systemConfigurationService.updateByCwID((int) 62, (int) timeTimeTypeDefinition.id, (String) null, (Date) null, (boolean) true)

		}

		true
	}
}
