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




class UpdateScriptService {

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

			connection.eachRow('select id, display_column_report from cw_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.save()
                    new TimeTimeTypeDefinitionDetail(
                        timeTimeTypeDefinition:timeTimeTypeDefinition,
                        position:displayColumnReport,
//						description:"",
                        timeTimeType:timeTimeType
//						TimeFormat timeFormat
//						timeCalculation:TimeCalculation.DAY,
//						timeDurationInRelationTo:TimeDurationInRelationTo.DAY,
//						timeDuration:TimeDuration.TODAY
                    ).save()

				}
			}

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

		}

		true
	}
}
