package pt.utl.ist.scripts.runOnce.candidacy;

import net.sourceforge.fenixedu.domain.CandidacyPeriodInDegreeCurricularPlan;
import net.sourceforge.fenixedu.domain.ExecutionDegree;
import net.sourceforge.fenixedu.domain.ExecutionYear;
import net.sourceforge.fenixedu.domain.RegistrationPeriodInDegreeCurricularPlan;
import net.sourceforge.fenixedu.domain.degree.DegreeType;
import pt.utl.ist.scripts.commons.AtomicScript;

public class CreateDFACandidacyAndRegistrationPeriods extends AtomicScript {

    @Override
    protected void run() throws Exception {
        // if (true) {
        // throw new RuntimeException("DEFINED START DATE AND END DATE");
        // }
        final ExecutionYear executionYear = ExecutionYear.readCurrentExecutionYear();
        int totalCandidacyPeriods = 0;
        int totalRegistrationPeriods = 0;

        for (final ExecutionDegree executionDegree : executionYear
                .getExecutionDegreesByType(DegreeType.BOLONHA_ADVANCED_FORMATION_DIPLOMA)) {
            if (!executionDegree.getDegreeCurricularPlan().hasCandidacyPeriodFor(executionYear)) {
                new CandidacyPeriodInDegreeCurricularPlan(executionDegree.getDegreeCurricularPlan(), executionYear, executionYear
                        .getBeginDateYearMonthDay().toDateTimeAtMidnight(), executionYear.getBeginDateYearMonthDay().plusDays(10)
                        .toDateTimeAtMidnight());
                totalCandidacyPeriods++;
            }

            if (!executionDegree.getDegreeCurricularPlan().hasRegistrationPeriodFor(executionYear)) {
                new RegistrationPeriodInDegreeCurricularPlan(executionDegree.getDegreeCurricularPlan(), executionYear,
                        executionYear.getBeginDateYearMonthDay().toDateTimeAtMidnight(), executionYear.getBeginDateYearMonthDay()
                                .plusDays(10).toDateTimeAtMidnight());
                totalRegistrationPeriods++;
            }

        }

        logger.info("Total Candidacy Periods Created :" + totalCandidacyPeriods);
        logger.info("Total Registration Periods Created :" + totalRegistrationPeriods);

    }

    public static void main(String[] args) {
        processWriteTransaction(new CreateDFACandidacyAndRegistrationPeriods());
    }

}
