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

import java.util.Map;
import java.util.Set;

import com.google.common.collect.Maps;
import com.opengamma.DataNotFoundException;
import com.opengamma.core.change.ChangeManager;
import com.opengamma.core.security.Security;
import com.opengamma.core.security.SecuritySource;
import com.opengamma.id.ExternalIdBundle;
import com.opengamma.id.ObjectId;
import com.opengamma.id.UniqueId;
import com.opengamma.id.VersionCorrection;
import com.opengamma.util.ArgumentChecker;

/**
 * A {@link ObjectResolver} built on a {@link SecuritySource}.
 */
public class SecuritySourceResolver implements Resolver<Security> {

  private final SecuritySource _underlying;

  public SecuritySourceResolver(final SecuritySource underlying) {
    ArgumentChecker.notNull(underlying, "underlying");
    _underlying = underlying;
  }

  protected SecuritySource getUnderlying() {
    return _underlying;
  }

  // ObjectResolver

  @Override
  public Security resolveObject(final UniqueId uniqueId, final VersionCorrection versionCorrection) {
    try {
      return getUnderlying().get(uniqueId);
    } catch (DataNotFoundException e) {
      return null;
    }
  }

  @Override
  public ChangeManager changeManager() {
    return getUnderlying().changeManager();
  }

  // IdentifierResolver

  @Override
  public UniqueId resolveExternalId(final ExternalIdBundle identifiers, final VersionCorrection versionCorrection) {
    final Security security = getUnderlying().getSingle(identifiers, versionCorrection);
    if (security == null) {
      return null;
    } else {
      return security.getUniqueId();
    }
  }

  @Override
  public Map<ExternalIdBundle, UniqueId> resolveExternalIds(final Set<ExternalIdBundle> identifiers, final VersionCorrection versionCorrection) {
    final Map<ExternalIdBundle, UniqueId> result = Maps.newHashMapWithExpectedSize(identifiers.size());
    for (ExternalIdBundle identifier : identifiers) {
      final UniqueId uid = resolveExternalId(identifier, versionCorrection);
      if (uid != null) {
        result.put(identifier, uid);
      }
    }
    return result;
  }

  @Override
  public UniqueId resolveObjectId(final ObjectId identifier, final VersionCorrection versionCorrection) {
    try {
      return getUnderlying().get(identifier, versionCorrection).getUniqueId();
    } catch (DataNotFoundException e) {
      return null;
    }
  }

  @Override
  public Map<ObjectId, UniqueId> resolveObjectIds(final Set<ObjectId> identifiers, final VersionCorrection versionCorrection) {
    final Map<ObjectId, UniqueId> result = Maps.newHashMapWithExpectedSize(identifiers.size());
    for (ObjectId identifier : identifiers) {
      final UniqueId uid = resolveObjectId(identifier, versionCorrection);
      if (uid != null) {
        result.put(identifier, uid);
      }
    }
    return result;
  }

}

@@ -179,16 +179,67 @@
 olver;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -252,16 +252,16 @@
 il.Map;%0A
-
 import j
@@ -406,16 +406,62 @@
 anager;%0A
+import com.opengamma.core.id.ExternalSchemes;%0A
 import c
@@ -548,16 +548,52 @@
 Source;%0A
+import com.opengamma.id.ExternalId;%0A
 import c
@@ -1193,24 +1193,982 @@
 lying;%0A  %7D%0A%0A
+  protected ExternalIdBundle replaceWeakTickers(final ExternalIdBundle identifiers) %7B%0A    final String bbgWeakTicker = identifiers.getValue(ExternalSchemes.BLOOMBERG_TICKER_WEAK);%0A    final String bbgWeakBUID = identifiers.getValue(ExternalSchemes.BLOOMBERG_BUID_WEAK);%0A    if ((bbgWeakTicker != null) %7C%7C (bbgWeakBUID != null)) %7B%0A      final List%3CExternalId%3E ids = new ArrayList%3CExternalId%3E();%0A      for (final ExternalId identifier : identifiers) %7B%0A        if (ExternalSchemes.BLOOMBERG_TICKER_WEAK.equals(identifier.getScheme())) %7B%0A          ids.add(ExternalId.of(ExternalSchemes.BLOOMBERG_TICKER, identifier.getValue()));%0A        %7D else if (ExternalSchemes.BLOOMBERG_BUID_WEAK.equals(identifier.getScheme())) %7B%0A          ids.add(ExternalId.of(ExternalSchemes.BLOOMBERG_BUID, identifier.getValue()));%0A        %7D else %7B%0A          ids.add(identifier);%0A        %7D%0A      %7D%0A      return ExternalIdBundle.of(ids);%0A    %7D else %7B%0A      return identifiers;%0A    %7D%0A  %7D%0A%0A
   // ObjectR
@@ -2349,32 +2349,38 @@
 );%0A    %7D catch (
+final 
 DataNotFoundExce
@@ -2732,16 +2732,35 @@
 tSingle(
+replaceWeakTickers(
 identifi
@@ -2762,16 +2762,17 @@
 ntifiers
+)
 , versio
@@ -3164,16 +3164,22 @@
    for (
+final 
 External
@@ -3612,16 +3612,22 @@
  catch (
+final 
 DataNotF
@@ -3903,32 +3903,32 @@
 ifiers.size());%0A
-
     for (ObjectI
@@ -3920,16 +3920,22 @@
    for (
+final 
 ObjectId

Allow SecuritySourceResolver to handle weak tickers.