/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.fra;

import javax.time.calendar.ZonedDateTime;

import com.opengamma.financial.convention.ConventionBundle;
import com.opengamma.financial.convention.ConventionBundleSource;
import com.opengamma.financial.convention.InMemoryConventionBundleMaster;
import com.opengamma.financial.convention.businessday.BusinessDayConvention;
import com.opengamma.financial.convention.businessday.HolidaySourceCalendarAdapter;
import com.opengamma.financial.convention.calendar.Calendar;
import com.opengamma.financial.convention.daycount.DayCount;
import com.opengamma.financial.convention.daycount.DayCountFactory;
import com.opengamma.financial.interestrate.fra.definition.ForwardRateAgreement;
import com.opengamma.financial.security.fra.FRASecurity;
import com.opengamma.financial.world.holiday.HolidaySource;
import com.opengamma.id.Identifier;
/**
 * 
 */
public class FRASecuityToForwardRateAgreementConverter {
  
  private HolidaySource _holidaySource;
  private ConventionBundleSource _conventionSource;

  public FRASecuityToForwardRateAgreementConverter(HolidaySource holidaySource, ConventionBundleSource conventionSource) {
    _holidaySource = holidaySource;
    _conventionSource = conventionSource;
  }

  public ForwardRateAgreement getFRA(FRASecurity security, String fundingCurveName, String indexCurveName, double marketRate, ZonedDateTime now){
    String currency = security.getCurrency().getISOCode();
    ConventionBundle conventions = _conventionSource.getConventionBundle(Identifier.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, currency + "_FRA"));
    Calendar calendar = new HolidaySourceCalendarAdapter(_holidaySource, security.getCurrency()); // TODO: check we've got the right holiday calendar.
    
    BusinessDayConvention businessDayConvention = conventions.getBusinessDayConvention();
    ZonedDateTime fixingDate = businessDayConvention.adjustDate(calendar, security.getStartDate().toZonedDateTime()); // just in case
    
    ZonedDateTime settlementDate = businessDayConvention.adjustDate(calendar, security.getStartDate().toZonedDateTime().plusDays(conventions.getSettlementDays()));
    ZonedDateTime maturityDate = businessDayConvention.adjustDate(calendar, security.getEndDate().toZonedDateTime()); // just in case
    
    DayCount dayCount = conventions.getDayCount();
    
    // all times on discount/yield/forward curves are measured ACT/ACT
    double fixingTime = DayCountFactory.INSTANCE.getDayCount("Actual/Actual").getDayCountFraction(now, fixingDate);
    double settlementTime = DayCountFactory.INSTANCE.getDayCount("Actual/Actual").getDayCountFraction(now, settlementDate);
    double maturityTime = DayCountFactory.INSTANCE.getDayCount("Actual/Actual").getDayCountFraction(now, maturityDate);
    
    double forwardYearFraction = dayCount.getDayCountFraction(fixingDate, maturityDate);
    double discountingYearFraction = dayCount.getDayCountFraction(settlementDate, maturityDate);
    
    return new ForwardRateAgreement(settlementTime, maturityTime, fixingTime, forwardYearFraction, discountingYearFraction, marketRate, fundingCurveName, indexCurveName);
  }
  
}

@@ -991,32 +991,33 @@
 ic class FRASecu
+r
 ityToForwardRate
@@ -1037,18 +1037,16 @@
 erter %7B%0A
-  
 %0A  priva
@@ -1048,16 +1048,22 @@
 private 
+final 
 HolidayS
@@ -1094,16 +1094,22 @@
 private 
+final 
 Conventi
@@ -1159,16 +1159,17 @@
  FRASecu
+r
 ityToFor
@@ -1195,16 +1195,22 @@
 nverter(
+final 
 HolidayS
@@ -1229,16 +1229,22 @@
 ySource,
+ final
  Convent
@@ -1399,16 +1399,22 @@
  getFRA(
+final 
 FRASecur
@@ -1426,16 +1426,22 @@
 ecurity,
+ final
  String 
@@ -1457,16 +1457,22 @@
 rveName,
+ final
  String 
@@ -1486,16 +1486,22 @@
 rveName,
+ final
  double 
@@ -1511,16 +1511,22 @@
 ketRate,
+ final
  ZonedDa
@@ -1542,16 +1542,22 @@
 w)%7B%0A    
+final 
 String c
@@ -1607,16 +1607,22 @@
 ();%0A    
+final 
 Conventi
@@ -1772,16 +1772,22 @@
 ));%0A    
+final 
 Calendar
@@ -1925,25 +1925,27 @@
 lendar.%0A
-    
 %0A    
+final 
 Business
@@ -2022,24 +2022,30 @@
 tion();%0A    
+final 
 ZonedDateTim
@@ -2162,25 +2162,27 @@
 in case%0A
-    
 %0A    
+final 
 ZonedDat
@@ -2337,16 +2337,22 @@
 ));%0A    
+final 
 ZonedDat
@@ -2473,24 +2473,26 @@
 in case%0A
-    
 %0A   
+ final
  DayCoun
@@ -2531,20 +2531,16 @@
 ount();%0A
-    
 %0A    // 
@@ -2603,24 +2603,30 @@
 ACT/ACT%0A    
+final 
 double fixin
@@ -2725,24 +2725,30 @@
 gDate);%0A    
+final 
 double settl
@@ -2855,24 +2855,30 @@
 tDate);%0A    
+final 
 double matur
@@ -2981,24 +2981,26 @@
 yDate);%0A
-    
 %0A   
+ final
  double 
@@ -3081,16 +3081,22 @@
 e);%0A    
+final 
 double d
@@ -3180,20 +3180,16 @@
 yDate);%0A
-    
 %0A    ret
@@ -3356,13 +3356,11 @@
 e);%0A  %7D%0A
-  
 %0A%7D%0A

Corrected the spelling of the class