
import at.dockwork.time.domain.*
import at.dockwork.time.timeType.domain.*
import at.dockwork.security.domain.*
import at.dockwork.report.domain.*
import at.dockwork.time.enumeration.*




class UpdateScriptService {

	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:"PersonTime", description:"PersonTime")
		timeTimeTypeDefinition.save()
		if (timeTimeTypeDefinition) {

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

				long id = it.id
				int displayColumnReport = it.display_column_report
				TimeTimeType timeTimeType = TimeTimeType.get(id)

				if (timeTimeType && !timeTimeType.deleted) {

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

				}
			}

			ReportRole.findAll("from ReportRole where cwID in (:reportRoles)", [reportRoles:[ReportRole.PersonTime001, ReportRole.PersonTime001Landscape]]).each { reportRole ->
				reportRole.defaultTimeTimeTypeDefinition = timeTimeTypeDefinition
				reportRole.addToTimeTimeTypeDefinitions(timeTimeTypeDefinition)
				reportRole.save()
			}

		}

		true
	}
}
