package gw.sampledata.small
uses gw.sampledata.AbstractSampleDataCollection
uses gw.api.databuilder.reinsurance.QuotaShareRITreatyBuilder
uses gw.api.databuilder.reinsurance.RIProgramBuilder
uses gw.api.databuilder.reinsurance.AgreementParticipantBuilder
uses gw.api.databuilder.reinsurance.SurplusRITreatyBuilder
uses gw.api.databuilder.reinsurance.NetExcessOfLossRITreatyBuilder
uses gw.api.databuilder.reinsurance.PerEventRITreatyBuilder
uses gw.api.databuilder.reinsurance.AnnualAggregateRITreatyBuilder
uses gw.api.builder.CompanyBuilder
uses gw.api.databuilder.reinsurance.FacProportionalRIAgreementBuilder
uses gw.api.databuilder.reinsurance.FacExcessOfLossRIAgreementBuilder
uses gw.api.databuilder.reinsurance.FacNetExcessOfLossRIAgreementBuilder
uses gw.transaction.Transaction
uses gw.api.databuilder.reinsurance.ExcessOfLossRITreatyBuilder
uses java.util.Date
uses gw.pl.persistence.core.Bundle
uses gw.api.database.Query
uses org.apache.xpath.operations.Equals

/**
 * This will load sample data in the Currency provided.  To load more or less the same data in a different Currency,
 * make a subclass of this class and override the CurrencyToLoad property
 */

@Export
class SmallSampleReinsuranceData extends AbstractSampleDataCollection{

  construct() {
  }

  static var policyYear : String = "PY" + Date.Today.YearOfDate 

  override property get CollectionName() : String {
    return "Small Reinsurance Programs & Agreements"
  }

  override property get AlreadyLoaded() : boolean {
    var treatyName = "Property QS " + policyYear
    var treatyQuery = Query.make(QuotaShareRITreaty)
        .compare("Name", Equals, treatyName)
        .compare("Currency", Equals, CurrencyToLoad)
    return treatyQuery.select().HasElements
  }

  override function load() {
    createRIData(CurrencyToLoad)
  }

  property get CurrencyToLoad() : Currency {
    return Currency.TC_USD
  }

  protected function createRIData(aCurrency : Currency) {
    Transaction.runWithNewBundle( \bundle ->  {
          // Reinsurance programs & agreements
      var contactRus = new CompanyBuilder()
                      .withCompanyName("Russian Re")
                      .create(bundle)
      var contactLon = new CompanyBuilder()
                      .withCompanyName("London Re")
                      .create(bundle)
      
      var treaty1 = new QuotaShareRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withNotificationThreshold(500000)
                      .withCountTowardTotalLimit(true)
                      .withName("Property QS " + policyYear)
                      .withCededShare(25)
                      .withCommission(13.8)
                      .withAttachmentPoint(0)
                      .withCoverageLimit(1000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty2 = new SurplusRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMaxRetention(1000000)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withNotificationThreshold(1000000)
                      .withName("Property Surplus 1 " + policyYear)
                      .withCommission(13.8)
                      .withAttachmentPoint(1000000)
                      .withCoverageLimit(4000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty3 = new SurplusRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMaxRetention(1000000)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withNotificationThreshold(4000000)
                      .withName("Property Surplus 2 " + policyYear)
                      .withCommission(13.8)
                      .withAttachmentPoint(4000000)
                      .withCoverageLimit(15000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty4 = new NetExcessOfLossRITreatyBuilder()
                     .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withNotificationThreshold(500000)
                      .withCountTowardTotalLimit(true)
                      .withAttachmentBasis(AttachmentBasisType.TC_LOSSDATEWRITTEN)
                      .withAttachmentIndexed(false)
                      .withLimitIndexed(false)
                      .withGNPSubtotal(GNPSubtotalType.TC_NETOFPROPORTIONAL)
                      .withName("Property NXOL " + policyYear)
                      .withCededShare(100)
                      .withCommission(13.8)
                      .withAttachmentPoint(500000)
                      .withCoverageLimit(750000)
                      .withPayableBasis(PayableBasisType.TC_ASWRITTEN)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty5 = new PerEventRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withAttachmentBasis(AttachmentBasisType.TC_LOSSDATEEARNED)
                      .withAttachmentIndexed(false)
                      .withLimitIndexed(false)
                      .withGNPSubtotal(GNPSubtotalType.TC_NETOFPROPORTIONAL)
                      .withName("Property Cat 1 " + policyYear)
                      .withCededShare(95)
                      .withCommission(13.8)
                      .withAttachmentPoint(2000000)
                      .withCoverageLimit(10000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty6 = new PerEventRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withAttachmentBasis(AttachmentBasisType.TC_LOSSDATEEARNED)
                      .withAttachmentIndexed(false)
                      .withLimitIndexed(false)
                      .withGNPSubtotal(GNPSubtotalType.TC_NETOFPROPORTIONAL)
                      .withName("Property Cat 2 " + policyYear)
                      .withCededShare(95)
                      .withCommission(13.8)
                      .withAttachmentPoint(10000000)
                      .withCoverageLimit(50000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty7 = new PerEventRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withAttachmentBasis(AttachmentBasisType.TC_LOSSDATEEARNED)
                      .withAttachmentIndexed(false)
                      .withLimitIndexed(false)
                      .withGNPSubtotal(GNPSubtotalType.TC_NETOFPROPORTIONAL)
                      .withName("Property Cat 3 " + policyYear)
                      .withCededShare(95)
                      .withCommission(13.8)
                      .withAttachmentPoint(50000000)
                      .withCoverageLimit(100000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
      var treaty8 = new AnnualAggregateRITreatyBuilder()
                      .withCurrency(aCurrency)
                      .withMinDepositPremium(0)
                      .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
                      .withAttachmentBasis(AttachmentBasisType.TC_LOSSDATEEARNED)
                      .withAttachmentIndexed(false)
                      .withLimitIndexed(false)
                      .withGNPSubtotal(GNPSubtotalType.TC_NETOFPROPORTIONAL)
                      .withName("Property SL " + policyYear)
                      .withCededShare(100)
                      .withCommission(13.8)
                      .withAttachmentPoint(50000000)
                      .withCoverageLimit(200000000)
                      .withPayableBasis(PayableBasisType.TC_ASEARNED)
                      .withoutParticipant()
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(60)
                        .withParticipant(contactRus)
                        .withCommissionRate(15))
                      .withParticipant(new AgreementParticipantBuilder()
                        .withRiskShare(40)
                        .withParticipant(contactLon)
                        .withCommissionRate(12))
    
      new RIProgramBuilder()
        .withCurrency(aCurrency)
        .withName("Property " + aCurrency.Code + " " + policyYear)
        .withTargetMaxRetention(500000)
        .withSingleRiskMaximum(15000000)
        .withStatus(TC_ACTIVE)
        .withTreaty(treaty1)
        .withTreaty(treaty2)
        .withTreaty(treaty3)
        .withTreaty(treaty4)
        .withTreaty(treaty5)
        .withTreaty(treaty6)
        .withTreaty(treaty7)
        .withTreaty(treaty8)
        .create(bundle)

      new FacProportionalRIAgreementBuilder()
        .withCurrency(aCurrency)
        .withCommission(12)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(12))
        .create(bundle)
      new FacExcessOfLossRIAgreementBuilder()
        .withCurrency(aCurrency)
        .withCededShare(100)
        .withAttachmentPoint(15000000)
        .withCoverageLimit(20000000)
        .withCommission(12)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(12))
        .create(bundle)
      new FacExcessOfLossRIAgreementBuilder()
        .withCurrency(aCurrency)
        .withCededShare(100)
        .withAttachmentPoint(20000000)
        .withCoverageLimit(25000000)
        .withCommission(12)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(12))
        .create(bundle)
      new FacNetExcessOfLossRIAgreementBuilder()
        .withCurrency(aCurrency)
        .withCededShare(100)
        .withAttachmentPoint(750000)
        .withCoverageLimit(1000000)
        .withCommission(12)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(12))
        .create(bundle)
      new FacNetExcessOfLossRIAgreementBuilder()
        .withCurrency(aCurrency)
        .withCededShare(100)
        .withAttachmentPoint(1000000)
        .withCoverageLimit(1500000)
        .withCommission(12)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(12))
        .create(bundle)
      // WC agreements & Programs
      var wcTreaty1 = new QuotaShareRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp QS " + policyYear)
        .withOnlyCoverageGroups({TC_WORKERSCOMP})
        .withCededShare(30)
        .withCoverageLimit(5000000)
        .withCommission(8)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(60)
          .withParticipant(contactRus)
          .withCommissionRate(15))
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(40)
          .withParticipant(contactLon)
          .withCommissionRate(12))
      var wcTreaty2 = new ExcessOfLossRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp XOL 5M " + policyYear)
        .withOnlyCoverageGroups({TC_WORKERSCOMP})
        .withAttachmentPoint(5000000)
        .withCoverageLimit(20000000)
        .withCedingRate(10)
        .withCommission(7)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactRus)
          .withCommissionRate(7))
      var wcTreaty3 = new ExcessOfLossRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp XOL 20M " + policyYear)
        .withOnlyCoverageGroups({TC_WORKERSCOMP})
        .withAttachmentPoint(20000000)
        .withCoverageLimit(50000000)
        .withCedingRate(10)
        .withCommission(11)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(11))
      var wcTreaty4 = new ExcessOfLossRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp Peril-specific XOL")
        .withOnlyCoverageGroups({TC_WORKERSCOMP})
        .withAttachmentPoint(20000000)
        .withCoverageLimit(100000000)
        .withCountTowardTotalLimit(false)
        .withCedingRate(10)
        .withCommission(5)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(5))
      var wcTreaty5 = new AnnualAggregateRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp AA " + policyYear + " 100M")
        .withOnlyCoverageGroups({TC_WORKERSCOMP})
        .withAttachmentPoint(100000000)
        .withCoverageLimit(500000000)
        .withCedingRate(10)
        .withCommission(5)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(100)
          .withParticipant(contactLon)
          .withCommissionRate(5))
      new RIProgramBuilder()
        .withCurrency(aCurrency)
        .withName("WorkersComp " + aCurrency.Code + " " + policyYear)
        .withStatus(TC_ACTIVE)
        .withTargetMaxRetention(3500000)
        .withSingleRiskMaximum(50000000)
        .withOnlyCoverageGroup(TC_WORKERSCOMP)
        .withTreaty(wcTreaty1)
        .withTreaty(wcTreaty2)
        .withTreaty(wcTreaty3)
        .withTreaty(wcTreaty4)
        .withTreaty(wcTreaty5)
        .create(bundle)

      //GL agreement and program
      createGenericProgram(RICoverageGroupType.TC_LIABILITY, contactRus, contactLon, aCurrency, bundle)
      createGenericProgram(RICoverageGroupType.TC_AUTOLIABILITY, contactRus, contactLon, aCurrency, bundle)
      createGenericProgram(RICoverageGroupType.TC_AUTOPD, contactRus, contactLon, aCurrency, bundle)
    })       
  }

  private function createGenericProgram(covGroup : RICoverageGroupType, company1 : Company, company2 : Company, aCurrency : Currency, bundle : Bundle) {
    var treaty1 = new QuotaShareRITreatyBuilder()
        .withCurrency(aCurrency)
        .withName(covGroup.Code + " QS " + policyYear)
        .withOnlyCoverageGroups({covGroup})
        .withCededShare(50)
        .withCoverageLimit(5000000)
        .withCommission(8)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(60)
          .withParticipant(company1)
          .withCommissionRate(15))
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(40)
          .withParticipant(company2)
          .withCommissionRate(12))
    var treaty2 = new SurplusRITreatyBuilder()
        .withCurrency(aCurrency)
        .withOnlyCoverageGroups({covGroup})
        .withMaxRetention(5000000)
        .withMinDepositPremium(0)
        .withDepositPaymentSchedule(DepPmntScheduleType.TC_QUARTERLYINADVANCE)
        .withNotificationThreshold(5000000)
        .withName(covGroup.Code + " Surplus 1 " + policyYear)
        .withCommission(13.8)
        .withAttachmentPoint(5000000)
        .withCoverageLimit(10000000)
        .withPayableBasis(PayableBasisType.TC_ASEARNED)
        .withoutParticipant()
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(60)
          .withParticipant(company1)
          .withCommissionRate(15))
        .withParticipant(new AgreementParticipantBuilder()
          .withRiskShare(40)
          .withParticipant(company2)
          .withCommissionRate(12))          
    new RIProgramBuilder()
        .withCurrency(aCurrency)
        .withName(covGroup.Code + " " + aCurrency.Code + " "+ policyYear)
        .withStatus(TC_ACTIVE)
        .withTargetMaxRetention(2500000)
        .withSingleRiskMaximum(10000000)
        .withOnlyCoverageGroup(covGroup)
        .withTreaty(treaty1)
        .withTreaty(treaty2)
        .create(bundle)
  }

}
