package gw.beantransfer.impl uses gw.api.beantransfer.BeanTransferException uses gw.api.importing.ImportingUtil uses com.guidewire.pl.system.internal.InternalMethods uses gw.api.importing.graph.Element uses gw.api.profiler.PCProfilerTag uses gw.beantransfer.BeanImportHandler uses gw.entity.IEntityPropertyInfo uses gw.entity.IEntityType uses gw.entity.ILinkPropertyInfo uses gw.pl.persistence.core.Key uses gw.util.Pair uses com.guidewire.pl.system.database.Query uses com.guidewire.pl.domain.archiving.DomainGraphSupport /** * Bean Transfer import helper class. * This class defines callbacks called by {@link gw.api.importing.ImportingUtil}. */ class BeanImportGraphSupport extends DomainGraphSupport { static final var BRANCH_VALUE_ATTRIB = "BranchValue" var _graph : BeanImportGraph var _currentElement : Element var _currentEntityType : IEntityType var _branchByDatabaseID : Map = {} var _beanHandlers : List construct(graph : BeanImportGraph, beanHandlers : List) { _graph = graph _beanHandlers = beanHandlers } override function duplicateIsOkay(bean : KeyableBean) : boolean { return true } override function overrideDatabase(bean : KeyableBean) : boolean { // If the bean is retrieved from the database, we don't want to overwrite its property values. // This return value has no effect on beans that were newly created. return false } override property get EffDatedBranchContext() : EffDatedBranch { // _currentElement and _currentEntityType are set by #findEntityById(IEntityType, String), which is invoked before this callback is invoked. if (InternalMethods.asIEntityTypeInternal(_currentEntityType).EffDated) { // Try to get the branch from _currentElement. // This is possible if an element was succesfully found in #findEntityById(IEntityType, String) by calling BeanImportGraph#getElementByPublicId(PublicID). if (_currentElement != null) { // To find the EffDatedBranch for the current EffDated bean, we first get the branch element. Then, the database // ID of the EffDatedBranch from the element. Using the DB ID, we can get the actual EffDatedBranch bean instance. var branchID = _currentElement.getAttribute(BRANCH_VALUE_ATTRIB) var branchElement = _graph.getElement(branchID) var branchDatabaseID = branchElement.getAttribute(ImportingUtil.DATABASE_ID_ATTRIB) var branch = _branchByDatabaseID[branchDatabaseID] if (branch == null) { throw new BeanTransferException(String.format( "Branch not found for the following bean (EntityType, Database ID): (%s, %s)", {_currentEntityType.Name, branchDatabaseID})) } return branch } else { // _currentElement could not be populated, so look through the _branchByDatabaseID map for a singular branch. var effDatedBranches = _branchByDatabaseID.Values.toSet() if (effDatedBranches.Count == 0) { throw new BeanTransferException(String.format( "No branches were found for bean of type: %s", {_currentEntityType.Name})) } else if (effDatedBranches.Count > 1) { throw new BeanTransferException(String.format( "Multiple possible branches were found for bean of type: %s", {_currentEntityType.Name})) } else { return effDatedBranches.single() } } } return null } override property get LinkFromRootInfoToRoot() : ILinkPropertyInfo { return null } override function updateRootInfoObjectOnArchive(rootInfo : T) {} override property get QueryOnRootInfoForArchivingProcess() : Query { return null } override function getInformationalString(rootInfo : T) : String { return null } override property get DomainGraphName(): String { return null } override property get RootInfoType(): IEntityType { return null } override property get PublicIDOfRootProperty(): IEntityPropertyInfo { return null } override property get RootType(): IEntityType { return null } protected override function getRootInfoIdsToPurge(rootInfo : T): List>> { return null } protected override property get KnownLinksFromOutsideOfDomainPointingToDomain(): Set { return null } protected override function filterExceptionsToUnreachableTables(tablesNotVisited: Set) {} override function goToPage(rootInfo : T) {} override function findEntityById(entityType : IEntityType, publicId : String) : Optional { using (PCProfilerTag.BEAN_TRANSFER_FIND_ENTITY) { _currentEntityType = entityType _currentElement = _graph.getElementByPublicId(entityType, publicId) var result : KeyableBean for (var handler in _beanHandlers) { var bean = handler.findEntityById(entityType, publicId) if (bean != null and result != null) { if (bean.ID != result.ID) { throw new BeanTransferException("More than one handler returned a different result.") } } else if (result == null) { result = bean } } if (result == null) { // Use the reference specified in XML return null } // Use the reference specified by one of the BeanTransferHandler's return Optional.of(result) } } /** * Callback used to set IDs of a bean based on the database ID. * * @param bean the bean * @param id the database ID of the bean */ override function setIdOnBean(bean : KeyableBean, id : String) { using (PCProfilerTag.BEAN_TRANSFER_SET_ID) { if (bean typeis EffDatedBranch) { _branchByDatabaseID[id] = bean } _beanHandlers.each(\handler -> handler.setIdOnBean(bean, id)) } } /** * Callback for bean import. This method is called after all the beans have been created and added to the bundle. * The EffDated beans in the bundle are unsliced. It is used to perform any post processing of the imported beans. * * @param infos Set of RootInfos that were imported. */ function handlePostImport(infos : Set) { using (PCProfilerTag.BEAN_TRANSFER_POST_IMPORT) { _beanHandlers.each(\handler -> handler.handlePostImport(infos)) } } }