import at.clockwork.system.domain.*

import at.clockwork.transfer.gwtTransfer.client.enumeration.JobStartTypeEnum
import at.clockwork.transfer.gwtTransfer.client.enumeration.TriggerTypeEnum

import grails.util.Holders

class UpdateScriptService {

	def triggerWriteService = Holders.grailsApplication.mainContext.getBean 'triggerWriteService'

	def run(def connection, def dataSourceFields) {

        Trigger deleteLogCommunicationTrigger = Trigger.findWhere(triggerTypeEnum:TriggerTypeEnum.DELETE_LOG_COMMUNICATION, deleted:false)
        if (!deleteLogCommunicationTrigger) {
            deleteLogCommunicationTrigger = new Trigger(
                name : "DeleteLogCommunication",
                description : "Delete Log Communication",
                triggerTypeEnum : TriggerTypeEnum.DELETE_LOG_COMMUNICATION,
                jobStartTypeEnum : JobStartTypeEnum.AUTOMATIC,
                cronDefinition : "0 0 2 * * ?",
                detail1 : "14"
            )
            triggerWriteService.save(deleteLogCommunicationTrigger)
        }

        Trigger deleteLogTriggerTrigger = Trigger.findWhere(triggerTypeEnum:TriggerTypeEnum.DELETE_LOG_TRIGGER, deleted:false)
        if (!deleteLogTriggerTrigger) {
            deleteLogTriggerTrigger = new Trigger(
                name : "DeleteLogTrigger",
                description : "Delete Log Trigger",
                triggerTypeEnum : TriggerTypeEnum.DELETE_LOG_TRIGGER,
                jobStartTypeEnum : JobStartTypeEnum.AUTOMATIC,
                cronDefinition : "0 5 2 * * ?",
                detail1 : "14"
            )
            triggerWriteService.save(deleteLogTriggerTrigger)
        }

        Trigger deleteTerminalCommunicationTrigger = Trigger.findWhere(triggerTypeEnum:TriggerTypeEnum.DELETE_TERMINAL_COMMUNICATION, deleted:false)
        if (!deleteTerminalCommunicationTrigger) {
            deleteTerminalCommunicationTrigger = new Trigger(
                name : "DeleteTerminalCommunication",
                description : "Delete Terminal Communication",
                triggerTypeEnum : TriggerTypeEnum.DELETE_TERMINAL_COMMUNICATION,
                jobStartTypeEnum : JobStartTypeEnum.AUTOMATIC,
                cronDefinition : "0 10 2 * * ?",
                detail1 : "14"
            )
            triggerWriteService.save(deleteTerminalCommunicationTrigger)
        }

		true

	}
}