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

import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.Maps;
import com.opengamma.id.UniqueId;
import com.opengamma.master.position.Deal;
import com.opengamma.master.position.ManageablePosition;
import com.opengamma.master.position.ManageableTrade;
import com.opengamma.util.ArgumentChecker;

/**
 * Trade Attributes Model to help with freemarker rendering.
 */
public class TradeAttributesModel {

  private Map<UniqueId, Map<String, Map<String, String>>> _attrMap = Maps.newHashMap();
  
  public TradeAttributesModel(final ManageablePosition position) {
    ArgumentChecker.notNull(position, "position");
    initialize(position);
  }

  private void initialize(final ManageablePosition position) {
    for (ManageableTrade trade : position.getTrades()) {
      Map<String, Map<String, String>> tradeAttr = Maps.newHashMap();
      
      Map<String, String> dealAttr = Maps.newHashMap();
      Map<String, String> userAttr = Maps.newHashMap();
      tradeAttr.put("deal", dealAttr);
      tradeAttr.put("user", userAttr);
      
      _attrMap.put(trade.getUniqueId(), tradeAttr);
      
      for (Entry<String, String> entry : trade.getAttributes().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (key.startsWith(Deal.DEAL_PREFIX)) {
          dealAttr.put(key, value);
        } else {
          userAttr.put(key, value);
        }
      }
    }
  }

  /**
   * Gets the Deal attributes for a given trade uniqueId.
   * 
   * @param tradeId the tradeId
   * @return the deal attributes, not null
   */
  public Map<String, String> getDealAttributes(UniqueId tradeId) {
    Map<String, String> result = Maps.newHashMap();
    
    Map<String, Map<String, String>> attributes = _attrMap.get(tradeId);
    if (attributes != null) {
      result = attributes.get("deal");
    } 
    return result;
  }
  
  /**
   * Gets the User attributes for a given trade uniqueId.
   * 
   * @param tradeId the trade uniqueId
   * @return the user attributes, not null
   */
  public Map<String, String> getUserAttributes(UniqueId tradeId) {
    Map<String, String> result = Maps.newHashMap();
    Map<String, Map<String, String>> attributes = _attrMap.get(tradeId);
    if (attributes != null) {
      result = attributes.get("user");
    } 
    return result;
  }
}

@@ -169,16 +169,46 @@
 ition;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -248,16 +248,85 @@
 p.Entry;
+%0Aimport java.util.TreeMap;%0A%0Aimport org.joda.beans.PropertyDefinition;
 %0A%0Aimport
@@ -696,16 +696,38 @@
 odel %7B%0A%0A
+  @PropertyDefinition%0A
   privat
@@ -808,16 +808,105 @@
 ap();%0A  
+ %0A  /**%0A   * Creates an instance %0A   * %0A   * @param position the position, not null%0A   */
 %0A  publi
@@ -1169,35 +1169,30 @@
 ()) %7B%0A      
-Map%3CString,
+%0A     
  Map%3CString,
@@ -1203,294 +1203,116 @@
 ing%3E
-%3E tradeAttr = Maps.newHashMap();%0A      %0A      Map%3CString, String%3E dealAttr = Maps.newHashMap();%0A      Map%3CString, String%3E userAttr = Maps.newHashMap();%0A      tradeAttr.put(%22deal%22, dealAttr);%0A      tradeAttr.put(%22user%22, userAttr);%0A      %0A      _attrMap.put(trade.getUniqueId(), tradeAttr
+ dealAttr = new TreeMap%3CString, String%3E();%0A      Map%3CString, String%3E userAttr = new TreeMap%3CString, String%3E(
 );%0A 
@@ -1623,24 +1623,231 @@
   %7D%0A      %7D%0A
+      %0A      Map%3CString, Map%3CString, String%3E%3E tradeAttr = Maps.newHashMap();%0A      tradeAttr.put(%22deal%22, dealAttr);%0A      tradeAttr.put(%22user%22, userAttr);%0A      _attrMap.put(trade.getUniqueId(), tradeAttr);%0A
     %7D%0A  %7D%0A%0A 
@@ -2096,39 +2096,46 @@
 esult = 
-Maps.newHashMap();%0A    
+new TreeMap%3CString, String%3E();
 %0A    Map
@@ -2283,30 +2283,59 @@
 %0A    return 
+Collections.unmodifiableMap(
 result
+)
 ;%0A  %7D%0A  %0A  /
@@ -2589,31 +2589,43 @@
 esult = 
-Maps.newHashMap
+new TreeMap%3CString, String%3E
 ();%0A    
@@ -2784,18 +2784,50 @@
 urn 
+Collections.unmodifiableMap(
 result
+)
 ;%0A  %7D%0A
+  %0A
 %7D%0A

order trade attributes