private void migrateCourseAgent(
      CourseAgent caSrc,
      ExpeditedAdverseEventReport aeReportDest,
      DomainObjectImportOutcome<ExpeditedAdverseEventReport> outcome) {

    Study study = aeReportDest.getStudy();

    StudyAgent studyAgent = caSrc.getStudyAgent();
    if (studyAgent == null
        || ((studyAgent.getAgent() == null
                || StringUtils.isEmpty(studyAgent.getAgent().getNscNumber()))
            && StringUtils.isEmpty(studyAgent.getOtherAgent()))) {
      outcome.addWarning("ER-CA-1", "Study Agent is missing in the source");
      return;
    }

    StudyAgent realStudyAgent =
        studyAgent.getAgent() == null
            ? study.findStudyAgentByNscOrName(studyAgent.getOtherAgent())
            : study.findStudyAgentByNscOrName(studyAgent.getAgent().getNscNumber());
    if (realStudyAgent == null) {
      outcome.addWarning("ER-CA-2", "Given Agent is no longer associated to the study");
      return;
    }

    CourseAgent caDest = new CourseAgent();
    aeReportDest.getTreatmentInformation().addCourseAgent(caDest);

    // set the study agent
    caDest.setStudyAgent(realStudyAgent);

    caDest.setDose(caSrc.getDose());
    caDest.setAdministrationDelay(caSrc.getAdministrationDelay());
    caDest.setAdministrationDelayAmount(caSrc.getAdministrationDelayAmount());
    caDest.setAdministrationDelayUnits(caSrc.getAdministrationDelayUnits());
    caDest.setComments(caSrc.getComments());
    caDest.setAgentAdjustment(caSrc.getAgentAdjustment());
    caDest.setModifiedDose(caSrc.getModifiedDose());
    caDest.setLastAdministeredDate(caSrc.getLastAdministeredDate());
    caDest.setDurationAndSchedule(caSrc.getDurationAndSchedule());
    caDest.setFirstAdministeredDate(caSrc.getFirstAdministeredDate());
    caDest.setTotalDoseAdministeredThisCourse(caSrc.getTotalDoseAdministeredThisCourse());
    caDest.setFormulation(caSrc.getFormulation());
    caDest.setLotNumber(caSrc.getLotNumber());
  }
  public void testSynchronizeStudyWithAgent() {
    {
      StudyAgent studyAgent = studyAgentDao.getById(-1);
      Study s = studyDao.getById(-2);
      System.out.println(s.getExpectedAECtcTerms().size());
      service.synchronizeStudyWithAgent(studyAgent, AgentSpecificTerm.EXPTECTED_AE_ADDED);
      studyDao.save(studyAgent.getStudy());
    }

    interruptSession();
    {
      Study s = studyDao.getById(-2);
      assertEquals(3, s.getExpectedAECtcTerms().size());
      assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
      assertExpectednessSingleTAAE(
          s.getTreatmentAssignments()
              .get(0)
              .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));
    }

    interruptSession();
    {
      StudyAgent studyAgent = studyAgentDao.getById(-2);
      service.synchronizeStudyWithAgent(studyAgent, AgentSpecificTerm.EXPTECTED_AE_ADDED);
      studyDao.save(studyAgent.getStudy());
    }
    interruptSession();
    {
      Study s = studyDao.getById(-2);
      assertEquals(6, s.getExpectedAECtcTerms().size());
      assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          4, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
      assertEquals(
          4, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
      assertExpectednessMultipleTAAE(
          s.getTreatmentAssignments()
              .get(0)
              .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));

      StudyAgent studyAgent = studyAgentDao.getById(-2);
      service.synchronizeStudyWithAgent(studyAgent, AgentSpecificTerm.EXPTECTED_AE_ADDED);
      studyDao.save(studyAgent.getStudy());
    }

    interruptSession();

    {
      Study s = studyDao.getById(-2);
      assertEquals(6, s.getExpectedAECtcTerms().size());
      assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          4, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
      assertEquals(
          4, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
      assertExpectednessMultipleTAAE(
          s.getTreatmentAssignments()
              .get(0)
              .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));
    }

    interruptSession();
    {
      Study s = studyDao.getById(-2);
      s.getExpectedAECtcTerms().size();
      s.getExpectedAEMeddraLowLevelTerms().size();
      for (TreatmentAssignment ta : s.getTreatmentAssignments()) {
        ta.getTreatmentAssignmentStudyInterventions().size();
        for (AbstractStudyInterventionExpectedAE as :
            ta.getAbstractStudyInterventionExpectedAEs()) {
          as.getTreatmentAssignmentAgents().size();
        }
      }
      for (StudyAgent sa : s.getStudyAgents()) {
        if (sa.getAgent() != null) {
          sa.getAgent().getAgentSpecificTerms().size();
          sa.getTreatmentAssignmentAgents().size();
        }
      }
    }

    interruptSession();
    {
      Study s = studyDao.getById(-2);
      StudyAgent studyAgent = s.getStudyAgents().get(0);
      studyAgent.retire();
      service.synchronizeStudyWithAgent(studyAgent, AgentSpecificTerm.EXPTECTED_AE_DELETED);
      studyAgent.removeTreatmentAssignmentAgents();
      studyDao.save(studyAgent.getStudy());

      interruptSession();

      s = studyDao.getById(-2);
      for (ExpectedAECtcTerm ex_ae : s.getExpectedAECtcTerms()) {
        System.out.println(ex_ae.getCtcTerm().getCtepTerm());
      }

      assertEquals(4, s.getExpectedAECtcTerms().size());
      assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
      assertEquals(
          1, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          1, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
      assertEquals(
          2, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
      assertExpectednessSingleTAAE(
          s.getTreatmentAssignments()
              .get(0)
              .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));
    }
  }
  public void testSynchronizeStudyWithAgentTerm() {
    StudyAgent studyAgent = studyAgentDao.getById(-1);
    AgentSpecificTerm ast = agentSpecificTermDao.getById(-1);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    ast = agentSpecificTermDao.getById(-2);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    studyDao.save(studyAgent.getStudy());
    interruptSession();
    Study s = studyDao.getById(-2);
    assertEquals(3, s.getExpectedAECtcTerms().size());
    assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
    assertExpectednessSingleTAAE(
        s.getTreatmentAssignments()
            .get(0)
            .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));
    interruptSession();

    studyAgent = studyAgentDao.getById(-2);
    ast = agentSpecificTermDao.getById(-3);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    ast = agentSpecificTermDao.getById(-4);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    ast = agentSpecificTermDao.getById(-5);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    studyDao.save(studyAgent.getStudy());
    interruptSession();
    s = studyDao.getById(-2);
    assertEquals(6, s.getExpectedAECtcTerms().size());
    assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
    assertExpectednessMultipleTAAE(
        s.getTreatmentAssignments()
            .get(0)
            .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));

    studyAgent = studyAgentDao.getById(-2);
    ast = agentSpecificTermDao.getById(-3);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_ADDED);
    studyDao.save(studyAgent.getStudy());
    interruptSession();
    s = studyDao.getById(-2);
    assertEquals(6, s.getExpectedAECtcTerms().size());
    assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
    assertExpectednessMultipleTAAE(
        s.getTreatmentAssignments()
            .get(0)
            .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));

    studyAgent = studyAgentDao.getById(-2);
    ast = agentSpecificTermDao.getById(-3);
    ast.setGrade1Frequency(20.00);
    ast.setGrade2Frequency(10.00);
    ast.setGrade3Frequency(4.34);
    ast.setGrade4Frequency(6.23);
    ast.setGrade5Frequency(44.00);
    ast.setExpectednessFrequency(60.00);
    ast.setExpected(false);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_UPDATED);
    studyDao.save(studyAgent.getStudy());
    interruptSession();
    s = studyDao.getById(-2);
    assertEquals(6, s.getExpectedAECtcTerms().size());
    assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
    assertEquals(
        4, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
    assertExpectednessUpdatedTAAE(
        s.getTreatmentAssignments()
            .get(0)
            .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));

    studyAgent = studyAgentDao.getById(-2);
    assertTrue(studyAgent.equals(studyAgent.getStudy().getStudyAgents().get(0)));
    ast = agentSpecificTermDao.getById(-3);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_DELETED);
    ast = agentSpecificTermDao.getById(-4);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_DELETED);
    ast = agentSpecificTermDao.getById(-5);
    service.synchronizeStudyWithAgentTerm(studyAgent, ast, AgentSpecificTerm.EXPTECTED_AE_DELETED);
    studyDao.save(studyAgent.getStudy());
    interruptSession();
    s = studyDao.getById(-2);
    assertEquals(4, s.getExpectedAECtcTerms().size());
    assertEquals(1, s.getExpectedAEMeddraLowLevelTerms().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getTreatmentAssignmentStudyInterventions().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(0).getAbstractStudyInterventionExpectedAEs().size());
    assertEquals(
        2, s.getTreatmentAssignments().get(1).getAbstractStudyInterventionExpectedAEs().size());
    assertExpectednessSingleTAAE(
        s.getTreatmentAssignments()
            .get(0)
            .getExistingTerm(agentSpecificTermDao.getById(-1).getTerm()));
  }