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

import grails.util.Holders

class UpdateScriptService {

	def workCalculationTimeWriteService = Holders.grailsApplication.mainContext.getBean 'workCalculationTimeWriteService'
	def person2WorkCalculationTimeWriteService = Holders.grailsApplication.mainContext.getBean 'person2WorkCalculationTimeWriteService'

	def run(def connection, def dataSourceFields) {

        int counter = 0
        Person2WorkCalculation.findAllWhere(deleted:false).each { Person2WorkCalculation person2WorkCalculation ->

            def aFirstCWC = person2WorkCalculation.afterFirstComingWorkCalculation
            def aFurtherCWC = person2WorkCalculation.afterFurtherComingWorkCalculation

            def gormCriteria = WorkCalculationTime.createCriteria()
            def workCalculationTime = gormCriteria.get {

                and {
                    eq("afterFirstComingMinTime", person2WorkCalculation.afterFirstComingMinTime)

                    if (aFirstCWC?.workCalculationEnum == null)
                        isNull("afterFirstComingWorkCalculationEnum")
                    else
                        eq("afterFirstComingWorkCalculationEnum", aFirstCWC?.workCalculationEnum)

                    if (aFirstCWC?.project == null)
                        isNull("afterFirstComingProject")
                    else
                        eq("afterFirstComingProject", aFirstCWC?.project)

                    if (aFirstCWC?.order == null)
                        isNull("afterFirstComingOrder")
                    else
                        eq("afterFirstComingOrder", aFirstCWC?.order)

                    if (aFirstCWC?.orderItem == null)
                        isNull("afterFirstComingOrderItem")
                    else
                        eq("afterFirstComingOrderItem", aFirstCWC?.orderItem)

                    if (aFirstCWC?.costUnit == null)
                        isNull("afterFirstComingCostUnit")
                    else
                        eq("afterFirstComingCostUnit", aFirstCWC?.costUnit)

                    if (aFirstCWC?.machine == null)
                        isNull("afterFirstComingMachine")
                    else
                        eq("afterFirstComingMachine", aFirstCWC?.machine)

                    if (aFirstCWC?.workplace == null)
                        isNull("afterFirstComingWorkplace")
                    else
                        eq("afterFirstComingWorkplace", aFirstCWC?.workplace)

                    if (aFirstCWC?.workprocess == null)
                        isNull("afterFirstComingWorkprocess")
                    else
                        eq("afterFirstComingWorkprocess", aFirstCWC?.workprocess)

                    eq("afterFurtherComingMinTime", person2WorkCalculation.afterFurtherComingMinTime)

                    if (aFurtherCWC?.workCalculationEnum == null)
                        isNull("afterFurtherComingWorkCalculationEnum")
                    else
                        eq("afterFurtherComingWorkCalculationEnum", aFurtherCWC?.workCalculationEnum)

                    if (aFurtherCWC?.project == null)
                        isNull("afterFurtherComingProject")
                    else
                        eq("afterFurtherComingProject", aFurtherCWC?.project)

                    if (aFurtherCWC?.order == null)
                        isNull("afterFurtherComingOrder")
                    else
                        eq("afterFurtherComingOrder", aFurtherCWC?.order)

                    if (aFurtherCWC?.orderItem == null)
                        isNull("afterFurtherComingOrderItem")
                    else
                        eq("afterFurtherComingOrderItem", aFurtherCWC?.orderItem)

                    if (aFurtherCWC?.costUnit == null)
                        isNull("afterFurtherComingCostUnit")
                    else
                        eq("afterFurtherComingCostUnit", aFurtherCWC?.costUnit)

                    if (aFurtherCWC?.machine == null)
                        isNull("afterFurtherComingMachine")
                    else
                        eq("afterFurtherComingMachine", aFurtherCWC?.machine)

                    if (aFurtherCWC?.workplace == null)
                        isNull("afterFurtherComingWorkplace")
                    else
                        eq("afterFurtherComingWorkplace", aFurtherCWC?.workplace)

                    if (aFurtherCWC?.workprocess == null)
                        isNull("afterFurtherComingWorkprocess")
                    else
                        eq("afterFurtherComingWorkprocess", aFurtherCWC?.workprocess)

                    eq("deleted", false)
                }

            }

            if (!workCalculationTime) {
                counter ++
                workCalculationTime = new WorkCalculationTime(
                    name : "$counter",
                    description : "$counter",
                    afterFirstComingMinTime : person2WorkCalculation.afterFirstComingMinTime,
                    afterFirstComingWorkCalculationEnum : aFirstCWC?.workCalculationEnum,
                    afterFirstComingProject : aFirstCWC?.project,
                    afterFirstComingOrder : aFirstCWC?.order,
                    afterFirstComingOrderItem : aFirstCWC?.orderItem,
                    afterFirstComingCostUnit : aFirstCWC?.costUnit,
                    afterFirstComingMachine : aFirstCWC?.machine,
                    afterFirstComingWorkplace : aFirstCWC?.workplace,
                    afterFirstComingWorkprocess : aFirstCWC?.workprocess,
                    afterFurtherComingMinTime : person2WorkCalculation.afterFurtherComingMinTime,
                    afterFurtherComingWorkCalculationEnum : aFurtherCWC?.workCalculationEnum,
                    afterFurtherComingProject : aFurtherCWC?.project,
                    afterFurtherComingOrder : aFurtherCWC?.order,
                    afterFurtherComingOrderItem : aFurtherCWC?.orderItem,
                    afterFurtherComingCostUnit : aFurtherCWC?.costUnit,
                    afterFurtherComingMachine : aFurtherCWC?.machine,
                    afterFurtherComingWorkplace : aFurtherCWC?.workplace,
                    afterFurtherComingWorkprocess : aFurtherCWC?.workprocess
                    )
                workCalculationTimeWriteService.save(workCalculationTime)
            }

            Person2WorkCalculationTime person2WorkCalculationTime = new Person2WorkCalculationTime(
                person : person2WorkCalculation.person,
                workCalculationTime : workCalculationTime,
                startDate : person2WorkCalculation.startDate,
                endDate : person2WorkCalculation.endDate
            )
            person2WorkCalculationTimeWriteService.save(person2WorkCalculationTime)

        }

		true

	}
}