
import at.clockwork.time.domain.*
import at.clockwork.time.timeType.domain.*
import at.clockwork.report.domain.*
import at.clockwork.time.enumeration.*
import at.clockwork.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.clockwork.system.service.DataSourceService.execute(
			connection,
			dataSourceFields,
			[
				mySQLStatement:"update cw_time_time_type set reduce_total = true where reduce_total is null",
				msSQLStatement:"update cw_time_time_type set reduce_total = 0 where reduce_total is null"
			]
		)

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

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

				long id = it.id
				int displayColumnBooking = it.display_column_booking
				TimeTimeType timeTimeType = TimeTimeType.get(id)

				if (timeTimeType && !timeTimeType.deleted) {

					timeTimeTypeDefinition.save()
                    new TimeTimeTypeDefinitionDetail(
                        timeTimeTypeDefinition:timeTimeTypeDefinition,
                        position:displayColumnBooking,
                        timeTimeType:timeTimeType
                    ).save()

				}
			}

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

		}

		true
	}
}
