package gw.api.rating uses gw.api.locale.DisplayKey uses gw.api.system.PCLoggerCategory uses gw.api.system.PLDependenciesGateway uses gw.pl.persistence.core.Key uses gw.plugin.Plugins uses gw.plugin.rateflow.IImpactTestingPlugin uses entity.PolicyLine /** * This internal thread class holds a PolicyLine that gets initialized from the PolicyLine that is passed * into its constructor. */ internal class DefaultParallelRatingThread extends Thread implements ParallelRatingThread { var _policyLine : PolicyLine var _parentThreadTokenUserID : Key as readonly ParentThreadTokenUserID var _alternateRateBooks : List var _originalToken : com.guidewire.pl.system.service.context.ServiceToken var _newToken : com.guidewire.pl.system.service.context.ServiceToken /** * Construct a new ParallelRatingThread. Takes a number of parameters that allow the runnable associated with * the thread to be set, along with the thread group and the name of the thread. In addition, the policy line * that this thread is associated with is passed in and the token user. * * @param runnable the runnable associated with this thread. It will be invoked when the thread is * started * @param policyLine the policy line in the main thread. The beans in this policy line's bundle will be * copied to * the bundle associated with this thread during construction * @param group the thread group which this thread should be associated with * @param name the name of the thread * @param parentThreadTokenUser the parent thread's user token. Shares the permission of the main thread with this * child thread */ construct(runnable : Runnable, policyLine : PolicyLine, group : ThreadGroup, name : String, parentThreadTokenUserID : Key) { super(group, runnable, name) _parentThreadTokenUserID = parentThreadTokenUserID _policyLine = copyBundleAndBeansForParallelRating(policyLine) // Determines if impact testing is in progress and the alternate rate books map is set. If so, copies the // alternate rate books into the child thread bundle. if (Plugins.isEnabled(IImpactTestingPlugin)) { _alternateRateBooks = Plugins.get(IImpactTestingPlugin).AlternateRatebooks?.map(\rateBook -> ChildThreadPolicyLine.Bundle.add(rateBook)) } } override property get Bundle() : com.guidewire.pl.domain.persistence.core.impl.BundleInternal { return com.guidewire.pl.system.internal.InternalMethods.asBundleInternal(ChildThreadPolicyLine.Bundle) } /** * Copy the bean and all the Inserted, Updated and Read beans from the passed-in bean's bundle into a new bundle. * Typically the passed in bean will be a PolicyLine. * Returns the copy of the passed-in bean that was created in new bundle. * * @param bean the bean which you want to copy along with all the Inserted, Updated and Read beans * * @return the copy of the bean that is now associated with new bundle */ internal reified function copyBundleAndBeansForParallelRating(bean : T) : T { var startTime = System.currentTimeMillis() // Use the internal bundle method to copy the beans from the passed-in bean's bundle to a new bundle. var parallelRatingBundle = new ParallelRatingBundle() var beanBundle = bean.Bundle parallelRatingBundle.copy(beanBundle) // Find the passed-in bean in the new bundle. It should have been copied in there. var childThreadBean = ParallelRatingHelper.getBeanInBundle(bean, parallelRatingBundle) if (childThreadBean == null) { throw new IllegalStateException(DisplayKey.get("BundleState.Error.EffDatedBeanNotFound")) } PCLoggerCategory.PARALLELIZATION.info( String.format("Bean Copy Elapsed Time: %,dms; InsertedBeans: %,d, UpdatedBeans: %,d, RemovedBeans: %,d, ReadBeans: %,d", { (System.currentTimeMillis() - startTime), beanBundle.InsertedBeans.Count, beanBundle.UpdatedBeans.Count, beanBundle.RemovedBeans.Count, com.guidewire.pl.system.internal.InternalMethods.asBundleInternal(beanBundle).ReadBeans.Count})) return childThreadBean } /** * Initializes additional values after construction of thread and clean up has occurred. * We set the authenticated token and set the bundle in the thread local bundle provider. */ override function run() { try { setNewToken() PLDependenciesGateway.ThreadLocalBundleProvider = ChildThreadPolicyLine.Bundle maybeSetAlternateRatebooks() super.run() } finally { PLDependenciesGateway.clearThreadLocalBundleProvider() restoreToken() } } private function maybeSetAlternateRatebooks() { if (_alternateRateBooks != null) { Plugins.get(IImpactTestingPlugin).setAlternateRatebooks(_alternateRateBooks) } } private function setNewToken() { _originalToken = PLDependenciesGateway.CommonDependencies.ServiceToken _newToken = PLDependenciesGateway.ServiceTokenManager.createAuthenticatedToken(ParentThreadTokenUserID) PLDependenciesGateway.CommonDependencies.ServiceToken = _newToken } internal function restoreToken() { PLDependenciesGateway.CommonDependencies.setServiceToken(_originalToken) PLDependenciesGateway.ServiceTokenManager.removeToken(_newToken) } override property get ChildThreadPolicyLine() : PolicyLine { return _policyLine } }