/*
   * DOMAIN SERVICES
   */
  public static YearDelegateElection createDelegateElectionWithCandidacyPeriod(
      Degree degree,
      ExecutionYear executionYear,
      YearMonthDay candidacyPeriodStartDate,
      YearMonthDay candidacyPeriodEndDate,
      CurricularYear curricularYear) {

    DelegateElectionCandidacyPeriod period =
        new DelegateElectionCandidacyPeriod(candidacyPeriodStartDate, candidacyPeriodEndDate);
    checkNewElectionCandidacyPeriod(degree, executionYear, curricularYear, period);

    /* Checks if there is a previous delegate election and its state */
    checkPreviousDelegateElectionExistence(degree, curricularYear, executionYear);

    YearDelegateElection election =
        new YearDelegateElection(executionYear, degree, curricularYear, period);

    /* Add degree students to election students list */
    for (DegreeCurricularPlan dcp : degree.getActiveDegreeCurricularPlans()) {
      for (StudentCurricularPlan scp : dcp.getActiveStudentCurricularPlans()) {
        if (scp.getRegistration().getCurricularYear(executionYear) == curricularYear.getYear()) {
          if (!hasDelegateElection(election, scp)) {
            election.addStudents(scp.getRegistration().getStudent());
          }
        }
      }
    }

    return election;
  }
 private static boolean hasDelegateElection(
     YearDelegateElection election, StudentCurricularPlan scp) {
   for (DelegateElection delegateElection :
       scp.getRegistration().getStudent().getDelegateElections()) {
     if (delegateElection instanceof YearDelegateElection) {
       if (delegateElection.getDegree().equals(election.getDegree())
           && delegateElection.getExecutionYear().equals(election.getExecutionYear())
           && (delegateElection.hasLastVotingPeriod()
               && !delegateElection.getLastVotingPeriod().isPastPeriod())) {
         return true;
       }
     }
   }
   return false;
 }