/** * Fully resolves the portfolio structure for a view. A fully resolved structure has resolved * {@link Security} objects for each {@link Position} within the portfolio. Note however that any * underlying or related data referenced by a security will not be resolved at this stage. * * @param compilationContext the compilation context containing the view being compiled, not null * @param versionCorrection the version-correction at which the portfolio is required, not null */ private static Portfolio getPortfolio( ViewCompilationContext compilationContext, VersionCorrection versionCorrection) { ObjectId portfolioOid = compilationContext.getViewDefinition().getPortfolioOid(); if (portfolioOid == null) { throw new OpenGammaRuntimeException( "The view definition '" + compilationContext.getViewDefinition().getName() + "' contains required portfolio outputs, but it does not reference a portfolio."); } PositionSource positionSource = compilationContext.getServices().getPositionSource(); if (positionSource == null) { throw new OpenGammaRuntimeException( "The view definition '" + compilationContext.getViewDefinition().getName() + "' contains required portfolio outputs, but the compiler does not have access to a position source."); } Portfolio portfolio = positionSource.getPortfolio(portfolioOid, versionCorrection); if (portfolio == null) { throw new OpenGammaRuntimeException( "Unable to resolve portfolio '" + portfolioOid + "' in position source '" + positionSource + "' used by view definition '" + compilationContext.getViewDefinition().getName() + "'"); } Portfolio cloned = new SimplePortfolio(portfolio); return resolveSecurities(compilationContext, cloned, versionCorrection); }
/** * Adds portfolio targets to the dependency graphs as required, and fully resolves the portfolio * structure. * * @param compilationContext the context of the view definition compilation * @param versionCorrection the version-correction at which to operate, not null * @param forcePortfolioResolution {@code true} if there are external portfolio targets, false * otherwise * @return the fully-resolved portfolio structure if any portfolio targets were required, null * otherwise. */ protected static Portfolio execute( ViewCompilationContext compilationContext, VersionCorrection versionCorrection, boolean forcePortfolioResolution) { // Everything we do here is geared towards the avoidance of resolution (of portfolios, // positions, securities) // wherever possible, to prevent needless dependencies (on a position master, security master) // when a view never // really has them. if (!isPortfolioOutputEnabled(compilationContext.getViewDefinition())) { // Doesn't even matter if the portfolio can't be resolved - we're not outputting anything at // the portfolio level // (which might be because the user knows the portfolio can't be resolved right now) so there // are no portfolio // targets to add to the dependency graph. return null; } Portfolio portfolio = forcePortfolioResolution ? getPortfolio(compilationContext, versionCorrection) : null; for (ViewCalculationConfiguration calcConfig : compilationContext.getViewDefinition().getAllCalculationConfigurations()) { if (calcConfig.getAllPortfolioRequirements().size() == 0) { // No portfolio requirements for this calculation configuration - avoid further processing. continue; } // Actually need the portfolio now if (portfolio == null) { portfolio = getPortfolio(compilationContext, versionCorrection); } DependencyGraphBuilder builder = compilationContext.getBuilders().get(calcConfig.getName()); // Cache PortfolioNode, Trade and Position entities CachingComputationTargetResolver resolver = compilationContext.getServices().getComputationTargetResolver(); resolver.cachePortfolioNodeHierarchy(portfolio.getRootNode()); cacheTradesPositionsAndSecurities(resolver, portfolio.getRootNode()); // Add portfolio requirements to the dependency graph PortfolioCompilerTraversalCallback traversalCallback = new PortfolioCompilerTraversalCallback(builder, calcConfig); PortfolioNodeTraverser.depthFirst(traversalCallback).traverse(portfolio.getRootNode()); } return portfolio; }