/**
 * 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 java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.fudgemsg.FudgeContext;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.engine.view.ViewProcessor;
import com.opengamma.id.UniqueId;
import com.opengamma.util.jms.JmsConnector;

/**
 * RESTful back-end to provide access to view processors
 */
@Path("/data/viewProcessors")
public class DataViewProcessorsResource {

  private final Map<UniqueId, DataViewProcessorResource> _viewProcessorResourceMap = new HashMap<UniqueId, DataViewProcessorResource>();

  public DataViewProcessorsResource() {
  }

  public DataViewProcessorsResource(final ViewProcessor viewProcessor, final JmsConnector jmsConnector, FudgeContext fudgeContext,
      ScheduledExecutorService scheduler) {
    addViewProcessor(viewProcessor, jmsConnector, fudgeContext, scheduler);
  }

  public DataViewProcessorsResource(final Collection<ViewProcessor> viewProcessors,
      final JmsConnector jmsConnector, FudgeContext fudgeContext, ScheduledExecutorService scheduler) {
    for (ViewProcessor viewProcessor : viewProcessors) {
      addViewProcessor(viewProcessor, jmsConnector, fudgeContext, scheduler);
    }
  }

  //-------------------------------------------------------------------------
  public void addViewProcessor(ViewProcessor viewProcessor, JmsConnector jmsConnector,
      FudgeContext fudgeContext, ScheduledExecutorService scheduler) {
    if (_viewProcessorResourceMap.get(viewProcessor.getUniqueId()) != null) {
      throw new OpenGammaRuntimeException("A view processor with the ID " + viewProcessor.getUniqueId() + " is already being managed");
    }
    _viewProcessorResourceMap.put(viewProcessor.getUniqueId(),
        new DataViewProcessorResource(viewProcessor, jmsConnector, fudgeContext, scheduler));
  }
  
  //-------------------------------------------------------------------------
  
  @Path("{viewProcessorId}")
  public DataViewProcessorResource findViewProcessor(@PathParam("viewProcessorId") String viewProcessorId) {
    return _viewProcessorResourceMap.get(UniqueId.parse(viewProcessorId));
  }
  
  @GET
  public Response get() {
    return Response.ok(_viewProcessorResourceMap.keySet()).build();
  }
  
}

@@ -200,24 +200,54 @@
 Collection;%0A
+import java.util.Collections;%0A
 import java.
@@ -885,107 +885,9 @@
 eMap
- = new HashMap%3CUniqueId, DataViewProcessorResource%3E();%0A%0A  public DataViewProcessorsResource() %7B%0A  %7D
+;
 %0A%0A  
@@ -980,32 +980,39 @@
 r jmsConnector, 
+%0A      
 FudgeContext fud
@@ -1021,22 +1021,16 @@
 Context,
-%0A     
  Schedul
@@ -1064,32 +1064,42 @@
 ) %7B%0A    
-addViewProcessor
+this(Collections.singleton
 (viewPro
@@ -1096,32 +1096,33 @@
 on(viewProcessor
+)
 , jmsConnector, 
@@ -1348,394 +1348,195 @@
     
-for (ViewProcessor viewProcessor : viewProcessors) %7B%0A      addViewProcessor(viewProcessor, jmsConnector, fudgeContext, scheduler);%0A    %7D%0A  %7D%0A%0A  //-------------------------------------------------------------------------%0A  public void addViewProcessor(ViewProcessor viewProcessor, JmsConnector jmsConnector,%0A      FudgeContext fudgeContext, ScheduledExecutorService scheduler) %7B%0A
+Map%3CUniqueId, DataViewProcessorResource%3E viewProcessorResourceMap = new HashMap%3CUniqueId, DataViewProcessorResource%3E();%0A    for (ViewProcessor viewProcessor : viewProcessors) %7B%0A  
     if (
 _vie
@@ -1531,17 +1531,16 @@
     if (
-_
 viewProc
@@ -1596,24 +1596,26 @@
  != null) %7B%0A
+  
       throw 
@@ -1738,16 +1738,18 @@
 aged%22);%0A
+  
     %7D%0A  
@@ -1750,17 +1750,18 @@
   %7D%0A    
-_
+  
 viewProc
@@ -1810,16 +1810,18 @@
 ueId(),%0A
+  
         
@@ -1910,95 +1910,105 @@
 ));%0A
+  
   %7D%0A  
-%0A
   
-//-------------------------------------------------------------------------
+_viewProcessorResourceMap = Collections.unmodifiableMap(viewProcessorResourceMap);
 %0A  
+%7D%0A
 %0A  @

Make this resource immutable