import at.clockwork.security.domain.*
import grails.util.Holders

class Update9008Service {

	def reportService = Holders.grailsApplication.mainContext.getBean 'reportService'
	def informationTransactionalService = Holders.grailsApplication.mainContext.getBean 'informationTransactionalService'

	def run(def connection, def dataSourceFields) {

        ReportRole.findAllWhere( deleted : false ).each { ReportRole itReportRole ->

            if (itReportRole.filename) {
                if (itReportRole.filename.indexOf("/") >= 0 || itReportRole.filename.indexOf("\\") >= 0) {

                    if (new File(itReportRole.filename).exists()) {

                        if (!reportService.updateJrxml(itReportRole.filename, true))
                            informationTransactionalService.add( "name=${itReportRole.filename}", new Date(), "reportService.updateJrxml()" )

                    } else if (new File(itReportRole.filename+".jrxml").exists()){

                        if (!reportService.updateJrxml(itReportRole.filename+".jrxml", true))
                            informationTransactionalService.add( "name=${itReportRole.filename+".jrxml"}", new Date(), "reportService.updateJrxml()" )

                    } else {

                        informationTransactionalService.add( "name=${itReportRole.filename}", new Date(), "reportService.updateJrxml()" )

                    }

                }
            }

            if (itReportRole.alternativeFilename) {
                if (itReportRole.alternativeFilename.indexOf("/") >= 0 || itReportRole.alternativeFilename.indexOf("\\") >= 0) {

                    if (new File(itReportRole.alternativeFilename).exists()) {

                        if (!reportService.updateJrxml(itReportRole.alternativeFilename, true))
                            informationTransactionalService.add( "name=${itReportRole.alternativeFilename}", new Date(), "reportService.updateJrxml()" )

                    } else if (new File(itReportRole.alternativeFilename+".jrxml").exists()) {

                        if (!reportService.updateJrxml(itReportRole.alternativeFilename+".jrxml", true))
                            informationTransactionalService.add( "name=${itReportRole.alternativeFilename+".jrxml"}", new Date(), "reportService.updateJrxml()" )

                    } else {

                        informationTransactionalService.add( "name=${itReportRole.alternativeFilename}", new Date(), "reportService.updateJrxml()" )

                    }

                }
            }

		}

		true

	}

}