/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.view.rest;

import javax.time.InstantProvider;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeMsg;
import org.fudgemsg.mapping.FudgeDeserializer;

import com.opengamma.engine.view.helper.AvailableOutputs;
import com.opengamma.id.UniqueId;
import com.opengamma.transport.jaxrs.RestClient;
import com.opengamma.transport.jaxrs.RestTarget;
import com.opengamma.util.ArgumentChecker;

/**
 * Provides access to a remote representation of {@link AvailableOutputs} data.
 */
public class RemoteAvailableOutputs {

  private final FudgeContext _fudgeContext;
  private final RestTarget _serviceTarget;

  public RemoteAvailableOutputs(final FudgeContext fudgeContext, final RestTarget serviceTarget) {
    ArgumentChecker.notNull(fudgeContext, "fudgeContext");
    ArgumentChecker.notNull(serviceTarget, "serviceTarget");
    _fudgeContext = fudgeContext;
    _serviceTarget = serviceTarget;
  }

  public FudgeContext getFudgeContext() {
    return _fudgeContext;
  }

  protected RestTarget getServiceTarget() {
    return _serviceTarget;
  }

  private AvailableOutputs getPortfolioOutputs(final UniqueId portfolioId, final String timestamp) {
    final RestClient client = RestClient.getInstance(getFudgeContext(), null);
    final FudgeMsg msg = client.getMsg(getServiceTarget().resolveBase("portfolio").resolveBase(timestamp).resolve(portfolioId.toString()));
    if (msg == null) {
      return null;
    }
    final FudgeDeserializer fd = new FudgeDeserializer(getFudgeContext());
    return fd.fudgeMsgToObject(AvailableOutputs.class, msg);
  }

  public AvailableOutputs getPortfolioOutputs(final UniqueId portfolioId) {
    return getPortfolioOutputs(portfolioId, "now");
  }

  public AvailableOutputs getPortfolioOutputs(final UniqueId portfolioId, final InstantProvider compilationInstant) {
    return getPortfolioOutputs(portfolioId, Long.toString(compilationInstant.toInstant().getEpochSeconds()));
  }

}

@@ -773,16 +773,81 @@
 arget;%0A%0A
+  private int _maxNodes = -1;%0A  private int _maxPositions = -1;%0A%0A
   public
@@ -1287,243 +1287,1168 @@
 %0A%0A  
-private AvailableOutputs getPortfolioOutputs(final UniqueId portfolioId, final String timestamp) %7B%0A    final RestClient client = RestClient.getInstance(getFudgeContext(), null);%0A    final FudgeMsg msg = client.getMsg(getServiceTarget()
+/**%0A   * Truncates the portfolio to the given number of nodes at each level.%0A   * %0A   * @param maxNodes maximum number of nodes, or %7B@code -1%7D to not truncate%0A   */%0A  public void setMaxNodes(final int maxNodes) %7B%0A    _maxNodes = maxNodes;%0A  %7D%0A%0A  public int getMaxNodes() %7B%0A    return _maxNodes;%0A  %7D%0A%0A  /**%0A   * Truncates the portfolio to the given number of positions at each node.%0A   * %0A   * @param maxPositions maximum number of positions, or %7B@code -1%7D to not truncate%0A   */%0A  public void setMaxPositions(final int maxPositions) %7B%0A    _maxPositions = maxPositions;%0A  %7D%0A%0A  public int getMaxPositions() %7B%0A    return _maxPositions;%0A  %7D%0A%0A  private AvailableOutputs getPortfolioOutputs(final UniqueId portfolioId, final String timestamp) %7B%0A    final RestClient client = RestClient.getInstance(getFudgeContext(), null);%0A    RestTarget target = getServiceTarget().resolveBase(%22portfolio%22).resolveBase(timestamp);%0A    final int maxNodes = getMaxNodes();%0A    if (maxNodes %3E 0) %7B%0A      target = target.resolveBase(%22nodes%22).resolveBase(Integer.toString(maxNodes));%0A    %7D%0A    final int maxPositions = getMaxPositions();%0A    if (maxPositions %3E 0) %7B%0A      target = target
 .res
@@ -2451,39 +2451,39 @@
 .resolveBase(%22po
-rtfolio
+sitions
 %22).resolveBase(t
@@ -2481,26 +2481,100 @@
 lveBase(
-timestamp)
+Integer.toString(maxPositions));%0A    %7D%0A    final FudgeMsg msg = client.getMsg(target
 .resolve

Truncate the portfolio to avoid the delay from resolving the full portfolio (forgot in previous commit)