package gw.rating.rtm.domain.migration
uses com.google.common.annotations.VisibleForTesting
uses com.guidewire.pc.util.PCDisplayKeys
uses gw.api.admin.PCImportDataInfo
uses gw.api.locale.DisplayKey
uses gw.api.system.PCLoggerCategory
uses gw.api.util.DisplayableException
uses gw.api.web.WebFile
uses PCDisplayKeys#Placeholder
uses PCDisplayKeys#matchesLocalizedString
/**
* Handle the import from XML process of a RateBook to the system.
* The RateBookImporter is wrapping PCImportDataInfo that is
* performing the actual parsing of the XML file, importing of the entities to the database
* and running validation.
* A RatingImportValidator is used to run rating specific validation on selected entities.
*/
class RateBookImporter {
var _importer : PCImportDataInfo
var _validator : RatingImportValidator
var _book : RateBook
/**
* Factory method to create a RateBookImporter
*/
static function create() : RateBookImporter {
var validator = new RatingImportValidator()
var importer = new PCImportDataInfo(validator, false)
return new RateBookImporter(importer, validator)
}
/**
* Construct a RateBookImporter
*
* @param importer the underlying PCImportDataInfo that handles XML parsing and entity creation
* @param validator performs rating specific validation on imported entities
*/
protected construct(importer : PCImportDataInfo, validator : RatingImportValidator) {
this._importer = importer
this._validator = validator
}
property get ImportedBook() : RateBook {
return _book
}
// Not checking platform level errors
function import(file : WebFile):RateBook{
_importer.File = file
return commit()
}
function import() {
_book = commit()
}
function isValidBeforeCommit(file : WebFile) : boolean {
_importer.File = file
return !_importer.hasErrors() or getPLLevelErrors().IsEmpty
}
function getWarnings() : List {
return _validator.ValidationWarnings.toList()
}
function getPLLevelErrors() : String[] {
return withoutErrorsToIgnore(_importer.ErrorLogs)
}
@VisibleForTesting
function withoutErrorsToIgnore(errors : String[]) : String[] {
var isNotDuplicatedEntityError = \errorString : String ->
!matchesLocalizedString(errorString, "ExportImport.Entity.Duplicated", {Placeholder, Placeholder})
var isNotInvalidBasedOnTableReferenceError = \errorString : String ->
!matchesLocalizedString(errorString, "ExportImport.Field.InvalidReference", {Placeholder, Placeholder,
"BasedOnTable", "RateTable", Placeholder})
return errors
.where(isNotDuplicatedEntityError)
.where(isNotInvalidBasedOnTableReferenceError)
}
private function commit() : RateBook {
_importer.commitChanges()
if (!_importer.Results.Success) {
handleError()
}
return _validator.ImportedBook
}
private function handleError() {
if (_validator.ValidationErrors.HasElements) {
throw new DisplayableException(_validator.ValidationErrors)
}
PCLoggerCategory.RATING.error("Failed to import rate book: ${_importer.Results.ErrorMessage}")
throw new DisplayableException(DisplayKey.get("Web.Rating.Errors.ImportRateBookFailed"))
}
}