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

import java.net.URI;

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

import com.opengamma.web.analytics.AnalyticsView;
import com.opengamma.web.analytics.GridStructure;
import com.opengamma.web.analytics.ViewportDefinition;

/**
 *
 */
public class MainGridResource extends AbstractGridResource implements DependencyGraphOwnerResource {

  public MainGridResource(AnalyticsView.GridType gridType, AnalyticsView view) {
    super(gridType, view);
  }

  @Override
  public GridStructure getGridStructure() {
    return _view.getGridStructure(_gridType);
  }

  @Override
  /* package */ void createViewport(int requestId, int viewportId, String callbackId, ViewportDefinition viewportDefinition) {
    _view.createViewport(requestId, _gridType, viewportId, callbackId, viewportDefinition);
  }

  @Override
  public AbstractViewportResource getViewport(int viewportId) {
    return new MainGridViewportResource(_gridType, _view, viewportId);
  }

  @Override
  public Response openDependencyGraph(UriInfo uriInfo, int requestId, int row, int col) {
    int graphId = s_nextId.getAndIncrement();
    String graphIdStr = Integer.toString(graphId);
    URI graphUri = uriInfo.getAbsolutePathBuilder().path(graphIdStr).build();
    String callbackId = graphUri.getPath();
    _view.openDependencyGraph(requestId, _gridType, graphId, callbackId, row, col);
    return Response.status(Response.Status.CREATED).build();
  }

  @Override
  public AbstractGridResource getDependencyGraph(int graphId) {
    return new DependencyGraphResource(_gridType, _view, graphId);
  }
}

@@ -213,330 +213,1005 @@
 vax.
-ws.rs.core.Response;%0Aimport javax.ws.rs.core.UriInfo;%0A%0Aimport com.opengamma.web.analytics.AnalyticsView;%0Aimport com.opengamma.web.analytics.GridStructure;%0Aimport com.opengamma.web.analytics.ViewportDefinition;%0A%0A/**%0A *%0A */%0Apublic class MainGridResource extends AbstractGridResource implements DependencyGraphOwnerResource %7B
+servlet.http.HttpServletResponse;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.core.Context;%0Aimport javax.ws.rs.core.Response;%0Aimport javax.ws.rs.core.UriInfo;%0A%0Aimport org.threeten.bp.Instant;%0Aimport org.threeten.bp.LocalDateTime;%0Aimport org.threeten.bp.format.DateTimeFormatter;%0A%0Aimport com.opengamma.util.ArgumentChecker;%0Aimport com.opengamma.util.OpenGammaClock;%0Aimport com.opengamma.util.rest.RestUtils;%0Aimport com.opengamma.web.analytics.AnalyticsView;%0Aimport com.opengamma.web.analytics.GridStructure;%0Aimport com.opengamma.web.analytics.ViewportDefinition;%0Aimport com.opengamma.web.analytics.ViewportResults;%0Aimport com.opengamma.web.analytics.formatting.TypeFormatter.Format;%0A%0A/**%0A *%0A */%0A@Path(%22views/%7BviewId%7D/%7BgridType%7D/%22)%0Apublic class MainGridResource extends AbstractGridResource implements DependencyGraphOwnerResource %7B%0A  %0A  private static final DateTimeFormatter CSV_TIME_FORMAT = DateTimeFormatter.ofPattern(%22yyyy_MM_dd_HH_mm_ss%22);
 %0A%0A  
@@ -2434,10 +2434,975 @@
 d);%0A  %7D%0A
+  %0A  /**%0A   * Produces view port results as CSV%0A   * %0A   * @param response the injected servlet response, not null.%0A   * @return The view port result as csv%0A   */%0A  @GET%0A  @Path(%22data%22)%0A  @Produces(RestUtils.TEXT_CSV)%0A  public ViewportResults getViewportResultAsCsv(@Context HttpServletResponse response) %7B%0A    ArgumentChecker.notNull(response, %22response%22);%0A    %0A    ViewportResults result = _view.getAllGridData(_gridType, Format.CELL);%0A    Instant valuationTime = result.getValuationTime() == null ? OpenGammaClock.getInstance().instant() : result.getValuationTime();%0A    LocalDateTime time = LocalDateTime.ofInstant(valuationTime, OpenGammaClock.getZone());%0A    %0A    String filename = String.format(%22%25s-%25s-%25s.csv%22, _view.getViewDefinitionId(), _gridType.name().toLowerCase(), time.toString(CSV_TIME_FORMAT));%0A    response.addHeader(%22content-disposition%22, %22attachment; filename=%5C%22%22 + filename + %22%5C%22%22);%0A    return _view.getAllGridData(_gridType, Format.CELL);%0A  %7D%0A
 %7D%0A

Add rest endpoint to save grid results as csv