
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, user_id, operator_type_id, person_category_id, general_validation_id from cw_user_operator_role where deleted = 0 and transfered_from_user_id is null') { userOperatorRole ->

			def user2OperatorType = new User2OperatorType(
				user:User.findWhere(id:(long) (userOperatorRole.user_id) ),
				operatorType:OperatorType.findWhere(id:(long) (userOperatorRole?.operator_type_id ?: 0L) ),
				personCategory:PersonCategory.findWhere(id:(long) (userOperatorRole?.person_category_id ?: 0L) ),
				generalValidation:GeneralValidation.findWhere(id:(long) (userOperatorRole?.general_validation_id ?: 0L) )
			)
			user2OperatorType.save()
			if (user2OperatorType.hasErrors())
				user2OperatorType.errors.each { println it }

			connection.eachRow('select person_category_id, user_operator_role_whitelist_person_categories_id from cw_user_operator_role_cw_person_category where user_operator_role_whitelist_person_categories_id = ' + userOperatorRole.id) { whitelist ->

				def user2OperatorType2PersonCategoryWhitelist = new User2OperatorType2PersonCategoryWhitelist(
					user2OperatorType:user2OperatorType,
					personCategory:PersonCategory.findWhere(id:(long) (whitelist?.person_category_id ?: 0L) )
				)
				user2OperatorType2PersonCategoryWhitelist.save()
				if (user2OperatorType2PersonCategoryWhitelist.hasErrors())
					user2OperatorType2PersonCategoryWhitelist.errors.each { println it }

			}
			connection.eachRow('select person_category_id, user_operator_role_blacklist_person_categories_id from cw_user_operator_role_cw_person_category where user_operator_role_blacklist_person_categories_id = ' + userOperatorRole.id) { blacklist ->

				def user2OperatorType2PersonCategoryBlacklist = new User2OperatorType2PersonCategoryBlacklist(
					user2OperatorType:user2OperatorType,
					personCategory:PersonCategory.findWhere(id:(long) (blacklist?.person_category_id ?: 0L) )
				)
				user2OperatorType2PersonCategoryBlacklist.save()
				if (user2OperatorType2PersonCategoryBlacklist.hasErrors())
					user2OperatorType2PersonCategoryBlacklist.errors.each { println it }

			}

			connection.eachRow('select person_id, user_operator_role_whitelist_persons_id from cw_user_operator_role_cw_person where user_operator_role_whitelist_persons_id = ' + userOperatorRole.id) { whitelist ->

				def user2OperatorType2PersonWhitelist = new User2OperatorType2PersonWhitelist(
					user2OperatorType:user2OperatorType,
					person:Person.findWhere(id:(long) (whitelist?.person_id ?: 0L) )
				)
				user2OperatorType2PersonWhitelist.save()
				if (user2OperatorType2PersonWhitelist.hasErrors())
					user2OperatorType2PersonWhitelist.errors.each { println it }

			}

			connection.eachRow('select person_id, user_operator_role_blacklist_persons_id from cw_user_operator_role_cw_person where user_operator_role_blacklist_persons_id = ' + userOperatorRole.id) { blacklist ->

				def user2OperatorType2PersonBlacklist = new User2OperatorType2PersonBlacklist(
					user2OperatorType:user2OperatorType,
					person:Person.findWhere(id:(long) (blacklist?.person_id ?: 0L) )
				)
				user2OperatorType2PersonBlacklist.save()
				if (user2OperatorType2PersonBlacklist.hasErrors())
					user2OperatorType2PersonBlacklist.errors.each { println it }

			}


		}

		true
	}
}
