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

import java.util.Map;

import org.fudgemsg.FudgeMsg;
import org.fudgemsg.MutableFudgeMsg;
import org.fudgemsg.mapping.FudgeBuilder;
import org.fudgemsg.mapping.FudgeBuilderFor;
import org.fudgemsg.mapping.FudgeDeserializer;
import org.fudgemsg.mapping.FudgeSerializer;

import com.opengamma.id.ExternalIdBundleFudgeBuilder;
import com.opengamma.id.UniqueIdFudgeBuilder;
import com.opengamma.util.fudgemsg.AbstractFudgeBuilder;

/**
 * A Fudge builder for {@code ManageableSecurity}.
 */
@FudgeBuilderFor(ManageableSecurity.class)
public class ManageableSecurityFudgeBuilder extends AbstractFudgeBuilder implements FudgeBuilder<ManageableSecurity> {

  /** Field name. */
  public static final String UNIQUE_ID_FIELD_NAME = "uniqueId";
  /** Field name. */
  public static final String NAME_FIELD_NAME = "name";
  /** Field name. */
  public static final String SECURITY_TYPE_FIELD_NAME = "securityType";
  /** Field name. */
  public static final String IDENTIFIERS_FIELD_NAME = "identifiers";
  /** Field name. */
  public static final String ATTRIBUTES_FIELD_NAME = "attributes";

  @Override
  public MutableFudgeMsg buildMessage(FudgeSerializer serializer, ManageableSecurity object) {
    final MutableFudgeMsg msg = serializer.newMessage();
    ManageableSecurityFudgeBuilder.toFudgeMsg(serializer, object, msg);
    return msg;
  }

  public static void toFudgeMsg(FudgeSerializer serializer, ManageableSecurity object, final MutableFudgeMsg msg) {
    addToMessage(msg, UNIQUE_ID_FIELD_NAME, UniqueIdFudgeBuilder.toFudgeMsg(serializer, object.getUniqueId()));
    addToMessage(msg, NAME_FIELD_NAME, object.getName());
    addToMessage(msg, SECURITY_TYPE_FIELD_NAME, object.getSecurityType());
    addToMessage(msg, IDENTIFIERS_FIELD_NAME, ExternalIdBundleFudgeBuilder.toFudgeMsg(serializer, object.getExternalIdBundle()));
    addToMessage(msg, ATTRIBUTES_FIELD_NAME, serializer.objectToFudgeMsg(object.getAttributes()));
  }

  @Override
  public ManageableSecurity buildObject(FudgeDeserializer deserializer, FudgeMsg msg) {
    ManageableSecurity object = new ManageableSecurity(msg.getString(SECURITY_TYPE_FIELD_NAME));
    ManageableSecurityFudgeBuilder.fromFudgeMsg(deserializer, msg, object);
    return object;
  }

  @SuppressWarnings("unchecked")
  public static void fromFudgeMsg(FudgeDeserializer deserializer, FudgeMsg msg, ManageableSecurity object) {
    object.setUniqueId(UniqueIdFudgeBuilder.fromFudgeMsg(deserializer, msg.getMessage(UNIQUE_ID_FIELD_NAME)));
    object.setName(msg.getString(NAME_FIELD_NAME));
    object.setExternalIdBundle(ExternalIdBundleFudgeBuilder.fromFudgeMsg(deserializer, msg.getMessage(IDENTIFIERS_FIELD_NAME)));
    object.setAttributes((Map<String, String>) deserializer.fieldValueToObject(msg.getByName(ATTRIBUTES_FIELD_NAME)));
  }

}

@@ -192,16 +192,38 @@
 til.Map;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1274,16 +1274,105 @@
 ibutes%22;
+%0A  /** Field name */%0A  public static final String PERMISSIONS_FIELD_NAME = %22permissions%22;
 %0A%0A  @Ove
@@ -1701,22 +1701,24 @@
 ecurity 
-object
+security
 , final 
@@ -1824,30 +1824,32 @@
 serializer, 
-object
+security
 .getUniqueId
@@ -1889,30 +1889,32 @@
 FIELD_NAME, 
-object
+security
 .getName());
@@ -1962,22 +1962,24 @@
 D_NAME, 
-object
+security
 .getSecu
@@ -2089,22 +2089,24 @@
 alizer, 
-object
+security
 .getExte
@@ -2196,22 +2196,24 @@
 udgeMsg(
-object
+security
 .getAttr
@@ -2220,24 +2220,183 @@
 ibutes()));%0A
+    if (!security.getPermissions().isEmpty()) %7B%0A      addToMessage(msg, PERMISSIONS_FIELD_NAME, serializer.objectToFudgeMsg(security.getPermissions()));%0A    %7D%0A
   %7D%0A%0A  @Over
@@ -2821,28 +2821,32 @@
 ity 
-object) %7B%0A    object
+security) %7B%0A    security
 .set
@@ -2942,30 +2942,32 @@
 AME)));%0A    
-object
+security
 .setName(msg
@@ -2996,30 +2996,32 @@
 NAME));%0A    
-object
+security
 .setExternal
@@ -3131,22 +3131,24 @@
 ));%0A    
-object
+security
 .setAttr
@@ -3248,15 +3248,186 @@
 AME)));%0A
+    if (msg.hasField(PERMISSIONS_FIELD_NAME)) %7B%0A      security.setPermissions((Set%3CString%3E) deserializer.fieldValueToObject(msg.getByName(PERMISSIONS_FIELD_NAME)));%0A    %7D%0A
   %7D%0A%0A%7D%0A

Add permissions field to manageable security fudge message Reviewed-By:Stephen