package gw.rating.rtm uses gw.api.system.PCLoggerCategory uses gw.api.util.LocaleUtil uses gw.pl.persistence.core.Bundle uses gw.processes.WorkQueueBase uses gw.rating.rtm.domain.migration.RateBookXMLExportVisitor uses gw.rating.rtm.domain.migration.RatingXmlWriter uses gw.rating.rtm.domain.table.RateBookModel uses gw.rating.rtm.excel.ExcelExport uses org.apache.commons.lang.exception.ExceptionUtils uses java.io.ByteArrayOutputStream uses java.io.OutputStream uses java.util.zip.GZIPOutputStream class RateBookExportWorkQueue extends WorkQueueBase { // sort of like a virtual "interface" // both workbook and xmlExportVisitor comply because they have the write(stream) method structure ExportWriter { function write(stream : OutputStream) } construct () { super(BatchProcessType.TC_RATEBOOKEXPORT, RateBookExportWorkItem, RateBook) } static function initializeRateBookExportResultAndCreateWorkItem(rateBook: RateBook, exportType : RateBookExportType, b : Bundle) : RateBookExportWorkItem { //Lock the rateBook and create/reset export result before starting excel/xml creation rateBook.ExportLock = true var rateBookExportResult = rateBook.getExportResult(exportType) ?: rateBook.createExportResult(exportType) //Reset following fields in case we have an existing export result for the rate book rateBookExportResult.ExportFailed = false rateBookExportResult.Data = null rateBookExportResult.CompletionPercentage = 0 var workItem = new RateBookExportWorkItem(b) workItem.RateBook = rateBook workItem.RateBookExportType = exportType workItem.LocaleType = LocaleUtil.CurrentLocaleType workItem.LanguageType = LocaleUtil.CurrentLanguageType workItem.initialize() workItem.Bundle.commit() return workItem } override function processWorkItem(workItem : RateBookExportWorkItem) { var locale = LocaleUtil.toLocale(workItem.LocaleType) var language = LocaleUtil.toLanguage(workItem.LanguageType) LocaleUtil.runAsCurrentLocaleAndLanguage(locale, language, \-> { var rateBookToExport = workItem.RateBook var rateBookExportResult: RateBookExportResult var exportWriter: ExportWriter var exportType = workItem.RateBookExportType PCLoggerCategory.EXPORT.info("Rate book " + rateBookToExport.DisplayName + " export started by work item " + workItem.getDisplayName()) rateBookToExport.ExportLock = false if (exportType == RateBookExportType.TC_EXCEL) { rateBookExportResult = rateBookToExport.ExcelExportResult var workbook = new ExcelExport().createRateBookWorkbook(rateBookToExport) exportWriter = workbook } else { var exportVisitor = new RateBookXMLExportVisitor(new RatingXmlWriter()) rateBookExportResult = rateBookToExport.XMLExportResult exportWriter = exportVisitor new RateBookModel(rateBookToExport, true).accept(exportVisitor) } using ( var byteArrayOutputStream = new ByteArrayOutputStream(1024 * 1024), var compressedStream = new GZIPOutputStream(byteArrayOutputStream)) { exportWriter.write(compressedStream) rateBookExportResult.Data = new Blob(byteArrayOutputStream.toByteArray()) } rateBookToExport.Bundle.commit() PCLoggerCategory.EXPORT.info("Rate book " + rateBookToExport.DisplayName + " export is completed and the data is persisted by work item " + workItem.getDisplayName()) }) } override function logStackTraceForWorkItem(workItem : RateBookExportWorkItem, throwable: Throwable) { PCLoggerCategory.EXPORT.error(ExceptionUtils.getFullStackTrace(throwable)) } override function workItemFailed(workItem : RateBookExportWorkItem) { PCLoggerCategory.EXPORT.info("Rate book export failed and the export lock is reset by work item " + workItem.getDisplayName()) var rateBookToExport = workItem.RateBook rateBookToExport.ExportLock = false var exportType = workItem.RateBookExportType var rateBookExportResult = rateBookToExport.getExportResult(exportType) ?: rateBookToExport.createExportResult(exportType) rateBookExportResult.ExportFailed = true rateBookExportResult.Data = null //Set this to 100 since in terms of exporting, it's completed rateBookExportResult.CompletionPercentage = 100 } }