
import at.clockwork.common.domain.*
import at.clockwork.security.domain.*
//import at.clockwork.security.operatorType.domain.*
//import at.clockwork.security.user.domain.*

import groovy.sql.Sql
import java.sql.Connection
import java.sql.DriverManager
import javax.sql.DataSource




class UpdateScriptService {

	def run(def connection, def dataSourceFields) {

		connection.eachRow("select id, operator_type_id, operator_role_id, general_validation_id from cw_operator_type_detail where class = 'at.clockwork.security.domain.OperatorTypeOperationDetail' and operator_type_id is not null") { operatorTypeDetail ->
			def operatorType2OperationRole = new OperatorType2OperationRole(
				operatorType:OperatorType.findWhere(id:(long)operatorTypeDetail?.operator_type_id ?: 0L),
				operatorRole:OperatorRole.findWhere(id:(long)operatorTypeDetail?.operator_role_id ?: 0L),
				generalValidation:GeneralValidation.findWhere(id:(long)operatorTypeDetail?.general_validation_id ?: 0L)
			)
			operatorType2OperationRole.save()
			if (operatorType2OperationRole.hasErrors())
				operatorType2OperationRole.errors.each { println it }

			connection.eachRow('select operator_type_detail_personal_pronouns_id, personal_pronoun_id from cw_operator_type_detail_cw_personal_pronoun where operator_type_detail_personal_pronouns_id = ' + operatorTypeDetail.id) { personalPronoun ->
				def operatorType2OperationRole2PersonalPronoun = new OperatorType2OperationRole2PersonalPronoun(
					operatorType2OperatorRole:operatorType2OperationRole,
					personalPronoun:PersonalPronoun.findWhere(id:(long)personalPronoun?.personal_pronoun_id ?: 0L)
				)
				operatorType2OperationRole2PersonalPronoun.save()
				if (operatorType2OperationRole2PersonalPronoun.hasErrors())
					operatorType2OperationRole2PersonalPronoun.errors.each { println it }

			}

		}


		true
	}
}
