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

import java.net.URI;

import com.opengamma.core.position.Portfolio;
import com.opengamma.core.position.PortfolioNode;
import com.opengamma.core.position.Position;
import com.opengamma.core.position.PositionSource;
import com.opengamma.core.position.Trade;
import com.opengamma.id.UniqueIdentifier;
import com.opengamma.transport.jaxrs.FudgeRest;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.rest.FudgeRestClient;
import com.sun.jersey.api.client.WebResource.Builder;

/**
 * Provides access to a remote {@link PositionSource}.
 */
public class RemotePositionSource implements PositionSource {

  /**
   * The base URI to call.
   */
  private final URI _baseUri;
  /**
   * The client API.
   */
  private final FudgeRestClient _client;

  /**
   * Creates an instance.
   * 
   * @param baseUri  the base target URI for all RESTful web services, not null
   */
  public RemotePositionSource(final URI baseUri) {
    _baseUri = baseUri;
    _client = FudgeRestClient.create();
  }

  //-------------------------------------------------------------------------
  @Override
  public Portfolio getPortfolio(UniqueIdentifier uid) {
    ArgumentChecker.notNull(uid, "uid");
    
    URI uri = DataPositionSourceResource.uriPortfolio(_baseUri, uid);
    return accessRemote(uri).get(Portfolio.class);
  }

  @Override
  public PortfolioNode getPortfolioNode(UniqueIdentifier uid) {
    ArgumentChecker.notNull(uid, "uid");
    
    URI uri = DataPositionSourceResource.uriNode(_baseUri, uid);
    return accessRemote(uri).get(PortfolioNode.class);
  }

  @Override
  public Position getPosition(UniqueIdentifier uid) {
    ArgumentChecker.notNull(uid, "uid");
    
    URI uri = DataPositionSourceResource.uriPosition(_baseUri, uid);
    return accessRemote(uri).get(Position.class);
  }

  @Override
  public Trade getTrade(UniqueIdentifier uid) {
    ArgumentChecker.notNull(uid, "uid");
    
    URI uri = DataPositionSourceResource.uriTrade(_baseUri, uid);
    return accessRemote(uri).get(Trade.class);
  }

  //-------------------------------------------------------------------------
  /**
   * Accesses the remote object.
   * 
   * @param uri  the URI to call, not null
   * @return the resource, suitable for calling get/post/put/delete on, not null
   */
  protected Builder accessRemote(URI uri) {
    // TODO: Better solution to this limitation in JAX-RS (we shouldn't have "data" in URI)
    // this code removes a second duplicate "data"
    String uriStr = uri.toString();
    int pos = uriStr.indexOf("/jax/data/");
    if (pos > 0) {
      pos = uriStr.indexOf("/data/", pos + 10);
      if (pos > 0) {
        uriStr = uriStr.substring(0, pos) + uriStr.substring(pos + 5);
      }
    }
    uri = URI.create(uriStr);
    return _client.access(uri).type(FudgeRest.MEDIA_TYPE).accept(FudgeRest.MEDIA_TYPE);
  }

  //-------------------------------------------------------------------------
  /**
   * Returns a string summary of this object.
   * 
   * @return the string summary, not null
   */
  @Override
  public String toString() {
    return getClass().getSimpleName() + "[" + _baseUri + "]";
  }

}

@@ -86,17 +86,18 @@
 anies%0A *
+ 
 %0A
-
  * Pleas
@@ -523,16 +523,65 @@
 geRest;%0A
+import com.opengamma.transport.jaxrs.RestTarget;%0A
 import c
@@ -1224,36 +1224,151 @@
 .create();%0A  %7D%0A%0A
+  public RemotePositionSource(final RestTarget restTarget) %7B%0A    this(restTarget.getURI().resolve(%22../..%22));%0A  %7D%0A%0A
   //
+ 
 ----------------
@@ -1526,37 +1526,33 @@
 ll(uid, %22uid%22);%0A
-    
 %0A
+
     URI uri = Da
@@ -1770,37 +1770,33 @@
 ll(uid, %22uid%22);%0A
-    
 %0A
+
     URI uri = Da
@@ -2003,37 +2003,33 @@
 ll(uid, %22uid%22);%0A
-    
 %0A
+
     URI uri = Da
@@ -2241,13 +2241,9 @@
 %22);%0A
-    
 %0A
+
     
@@ -2352,32 +2352,33 @@
 lass);%0A  %7D%0A%0A  //
+ 
 ----------------
@@ -3173,20 +3173,21 @@
 );%0A  %7D%0A%0A
-
   //
+ 
 --------

Allow RemotePositionSource to accept a RestTarget in the constructor for use at a remote calc node.