import at.clockwork.time.domain.*
import at.clockwork.common.person.domain.*

import grails.util.Holders

class UpdateScriptService {

	def person2DaySettingWriteService = Holders.grailsApplication.mainContext.getBean 'person2DaySettingWriteService'
	def person2DaySetting2AdditionalDataWriteService = Holders.grailsApplication.mainContext.getBean 'person2DaySetting2AdditionalDataWriteService'

	def run(def connection, def dataSourceFields) {

        DaySetting2AdditionalData.findAll().each { daySetting2AdditionalData ->

            DaySetting2AdditionalData.withNewSession {

                if (daySetting2AdditionalData.person && daySetting2AdditionalData.date && daySetting2AdditionalData.daySettingAdditionalData) {

                    Person2DaySetting person2DaySetting = Person2DaySetting.findWhere(
                        person : daySetting2AdditionalData.person,
                        date : daySetting2AdditionalData.date )
                    if (!person2DaySetting)
                        person2DaySetting = new Person2DaySetting(
                            person : daySetting2AdditionalData.person,
                            date : daySetting2AdditionalData.date )

                    person2DaySettingWriteService.save ( person2DaySetting )


                    Person2DaySetting2AdditionalData person2DaySetting2AdditionalData = Person2DaySetting2AdditionalData.findWhere(
                        person2DaySetting : person2DaySetting,
                        daySettingAdditionalData : daySetting2AdditionalData.daySettingAdditionalData)
                    if ( !person2DaySetting2AdditionalData)
                        person2DaySetting2AdditionalData = new Person2DaySetting2AdditionalData(
                                        person2DaySetting : person2DaySetting,
                                        daySettingAdditionalData : daySetting2AdditionalData.daySettingAdditionalData )

                    person2DaySetting2AdditionalData.value = daySetting2AdditionalData.value
                    person2DaySetting2AdditionalData.info = daySetting2AdditionalData.info

                    person2DaySetting2AdditionalDataWriteService.save ( person2DaySetting2AdditionalData )

                }

            }

        }

		true
	}
}