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

import java.util.Set;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

import org.joda.beans.impl.flexi.FlexiBean;

import com.opengamma.core.historicaltimeseries.HistoricalTimeSeriesSource;
import com.opengamma.core.security.SecuritySource;
import com.opengamma.master.position.ManageableTrade;
import com.opengamma.master.position.PositionMaster;
import com.opengamma.master.security.SecurityLoader;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.web.AbstractPerRequestWebResource;
import com.opengamma.web.WebHomeUris;
import com.opengamma.web.security.WebSecuritiesData;
import com.opengamma.web.security.WebSecuritiesUris;

/**
 * Abstract base class for RESTful position resources.
 */
public abstract class AbstractWebPositionResource extends AbstractPerRequestWebResource {
  /**
   * HTML ftl directory
   */
  protected static final String HTML_DIR = "positions/html/";
  /**
   * JSON ftl directory
   */
  protected static final String JSON_DIR = "positions/json/";
  /**
   * The backing bean.
   */
  private final WebPositionsData _data;

  /**
   * Creates the resource.
   * 
   * @param positionMaster  the position master, not null
   * @param securityLoader  the security loader, not null
   * @param securitySource  the security source, not null
   * @param htsSource  the historical time series source, not null
   */
  protected AbstractWebPositionResource(
      final PositionMaster positionMaster, final SecurityLoader securityLoader, final SecuritySource securitySource,
      final HistoricalTimeSeriesSource htsSource) {
    ArgumentChecker.notNull(positionMaster, "positionMaster");
    ArgumentChecker.notNull(securityLoader, "securityLoader");
    ArgumentChecker.notNull(securitySource, "securitySource");
    ArgumentChecker.notNull(htsSource, "htsSource");
    _data = new WebPositionsData();
    data().setPositionMaster(positionMaster);
    data().setSecurityLoader(securityLoader);
    data().setSecuritySource(securitySource);
    data().setHistoricalTimeSeriesSource(htsSource);
  }

  /**
   * Creates the resource.
   * @param parent  the parent resource, not null
   */
  protected AbstractWebPositionResource(final AbstractWebPositionResource parent) {
    super(parent);
    _data = parent._data;
  }

  /**
   * Setter used to inject the URIInfo.
   * This is a roundabout approach, because Spring and JSR-311 injection clash.
   * DO NOT CALL THIS METHOD DIRECTLY.
   * @param uriInfo  the URI info, not null
   */
  @Context
  public void setUriInfo(final UriInfo uriInfo) {
    data().setUriInfo(uriInfo);
  }

  //-------------------------------------------------------------------------
  /**
   * Creates the output root data.
   * @return the output root data, not null
   */
  protected FlexiBean createRootData() {
    FlexiBean out = getFreemarker().createRootData();
    out.put("homeUris", new WebHomeUris(data().getUriInfo()));
    out.put("uris", new WebPositionsUris(data()));
    WebSecuritiesData secData = new WebSecuritiesData(data().getUriInfo());
    out.put("securityUris", new WebSecuritiesUris(secData));
    return out;
  }

  //-------------------------------------------------------------------------
  /**
   * Gets the backing bean.
   * @return the backing bean, not null
   */
  protected WebPositionsData data() {
    return _data;
  }

  protected Set<ManageableTrade> parseTrades(String tradesJson) {
    return TradeJsonConverter.fromJson(tradesJson);
  }

}

@@ -421,32 +421,89 @@
 SecuritySource;%0A
+import com.opengamma.master.position.ManageablePosition;%0A
 import com.openg
@@ -589,24 +589,85 @@
 tionMaster;%0A
+import com.opengamma.master.security.ManageableSecurityLink;%0A
 import com.o
@@ -1106,24 +1106,131 @@
 rce %7B%0A  /**%0A
+   * Position XML parameter name%0A   */%0A  protected static final String POSITION_XML = %22positionXml%22;%0A  /**%0A
    * HTML ft
@@ -3865,12 +3865,604 @@
 on);%0A  %7D
+%0A  %0A  protected String getPositionXml(final ManageablePosition manageablePosition) %7B%0A    ManageablePosition position = manageablePosition.clone();%0A    ManageableSecurityLink securityLink = position.getSecurityLink();%0A    if (securityLink != null) %7B%0A      securityLink.setTarget(null);%0A    %7D%0A    for (ManageableTrade manageableTrade : position.getTrades()) %7B%0A      ManageableSecurityLink manageableSecurityLink = manageableTrade.getSecurityLink();%0A      if (manageableSecurityLink != null) %7B%0A        manageableSecurityLink.setTarget(null);%0A      %7D%0A    %7D%0A    return createBeanXML(position);%0A  %7D
 %0A%0A%7D%0A

Add method to create xml view of a managed position Reviewed-By:Stephen