package gw.exportimport uses gw.api.locale.DisplayKey uses com.guidewire.pl.system.bundle.NeverPersistedBundle uses gw.exportimport.validation.ImportValidationResult uses java.io.InputStream uses gw.api.tree.RowTreeRootNode uses gw.diff.tree.DiffTree uses gw.plugin.Plugins /** * An {@link ExcelImporter} imports data by given {@link InputStream}, generates a two-dimensional {@link CellData} * representation, and then converts the {@link CellData} to beans in a policy period. */ @ReadOnly class ExcelImporter { var _registry : EntityInfoRegistry construct() { this(EntityInfo.Registry) } internal construct(registry : EntityInfoRegistry) { _registry = registry } /** * Imports data from excel representation using {@link ExcelDataImporter}, * and then the result is transformed into beans using {@link CellDataTransformer}. * * @param inputStream the {@link InputStream} of the data to import * @param policyPeriod the data will be imported into this period * @return the {@link ImportValidationResult} which holds all validation errors that occurred during import */ function import(inputStream : InputStream, policyPeriod : PolicyPeriod) : ImportValidationResult { return import(inputStream, policyPeriod, null) } /** * Imports data from excel representation using {@link ExcelDataImporter}, * and then the result is transformed into beans using {@link CellDataTransformer}. * * @param inputStream the {@link InputStream} of the data to import * @param policyPeriod the data will be imported into this period * @param listener A RowProcessedListener used for callback status information * @return the {@link ImportValidationResult} which holds all validation errors that occurred during import */ function import(inputStream : InputStream, policyPeriod : PolicyPeriod, listener : RowProcessedListener) : ImportValidationResult { var importData = new ExcelDataImporter(_registry).import(inputStream, listener) if (not isValidPeriodToImport(importData, policyPeriod)) { importData.MetaData.ValidationResult.addError(DisplayKey.get("Import.Validation.FatalErrors.InvalidPeriod")) return importData.MetaData.ValidationResult } var cellDataImporter = new CellDataImporter(importData, listener) cellDataImporter.transformDataToBeans(policyPeriod) if (isImportDataFromOtherValidPeriod(importData, policyPeriod)) { importData.MetaData.ValidationResult.addWarning(DisplayKey.get("Web.ExcelImport.DifferentSourceImportWarning")) } ExportImportHandler.postImport(policyPeriod, importData) return importData.MetaData.ValidationResult } /** * Creates diff tree between the persisted policy period and imported policy period changes * @param policyPeriod the policy period with imported changes * @return the {@link RowTreeRootNode} which is the diff tree root node */ function createDiffTree(policyPeriod : PolicyPeriod) : RowTreeRootNode { var persistedPeriod = new NeverPersistedBundle().loadBean(policyPeriod.ID) as PolicyPeriod var rootNode = DiffTree.recalculateRootNode(persistedPeriod, policyPeriod, DiffReason.TC_IMPORT) return rootNode } /** * Checks whether to display confirmation message on import. * The conditions are: * 1) Not template * 2) Import into handled preemption * 3) Import from ancestor period * * @param inputStream the {@link InputStream} of the data to import * @param policyPeriod the data will be imported into this period * @return true if the above conditions are satisfied */ function showConfirmationMessageOnImport(inputStream : InputStream, policyPeriod : PolicyPeriod) : boolean { var importHeaderData = new ExcelDataImporter(_registry).importOnlyHeaderInfo(inputStream) return isImportDataFromOtherValidPeriod(importHeaderData, policyPeriod) } private function isImportDataFromOtherValidPeriod(importData : ImportData, policyPeriod : PolicyPeriod) : boolean { return not importData.MetaData.Template and areRelatedPeriods(policyPeriod, importData.MetaData.PeriodID) and importData.MetaData.PeriodID != policyPeriod.PublicID and (importData.MetaData.JobID == policyPeriod.Job.PublicID or isAncestorOfPeriod(importData, policyPeriod)) } private function isAncestorOfPeriod(importData : ImportData, policyPeriod : PolicyPeriod) : boolean { var period : PolicyPeriod = policyPeriod.BasedOn var ancestorPeriod = false while (period != null) { if (importData.MetaData.PeriodID == period.PublicID) { ancestorPeriod = true break } period = period.BasedOn } return ancestorPeriod } private function isValidPeriodToImport(importData : ImportData, policyPeriod : PolicyPeriod) : boolean { return importData.MetaData.Template or importData.MetaData.PeriodID == policyPeriod.PublicID or (importData.MetaData.JobID == policyPeriod.Job.PublicID and areRelatedPeriods(policyPeriod, importData.MetaData.PeriodID)) or isAncestorOfPeriod(importData, policyPeriod) } private function areRelatedPeriods(period : PolicyPeriod, importingPeriodID : String) : boolean { var basedOnPeriod : PolicyPeriod = period while (basedOnPeriod.BasedOn != null) { basedOnPeriod = basedOnPeriod.BasedOn } var importingPeriod : PolicyPeriod = null if (importingPeriodID != null) { importingPeriod = Policy.finder.findPolicyPeriodByPublicId(importingPeriodID) } while (importingPeriod.BasedOn != null) { importingPeriod = importingPeriod.BasedOn } return basedOnPeriod.PublicID == importingPeriod.PublicID } }