Package org.fenixedu.academic.ui.struts.action.manager.enrolments

Source Code of org.fenixedu.academic.ui.struts.action.manager.enrolments.CurriculumLineLogsDA$CurriculumLineLogStatisticsCalculator

/**
* Copyright © 2002 Instituto Superior Técnico
*
* This file is part of FenixEdu Academic.
*
* FenixEdu Academic is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FenixEdu Academic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FenixEdu Academic.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.fenixedu.academic.ui.struts.action.manager.enrolments;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.fenixedu.academic.domain.EnrolmentPeriod;
import org.fenixedu.academic.domain.EnrolmentPeriodInCurricularCourses;
import org.fenixedu.academic.domain.ExecutionSemester;
import org.fenixedu.academic.domain.degree.DegreeType;
import org.fenixedu.academic.domain.log.CurriculumLineLog;
import org.fenixedu.academic.domain.log.EnrolmentLog;
import org.fenixedu.academic.domain.student.Student;
import org.fenixedu.academic.dto.curriculumLineLog.SearchCurriculumLineLog;
import org.fenixedu.academic.ui.struts.action.base.FenixDispatchAction;
import org.fenixedu.academic.ui.struts.action.manager.ManagerApplications.ManagerStudentsApp;
import org.fenixedu.academic.util.EnrolmentAction;
import org.fenixedu.bennu.struts.annotations.Forward;
import org.fenixedu.bennu.struts.annotations.Forwards;
import org.fenixedu.bennu.struts.annotations.Mapping;
import org.fenixedu.bennu.struts.portal.EntryPoint;
import org.fenixedu.bennu.struts.portal.StrutsFunctionality;
import org.joda.time.DateTime;
import org.joda.time.Interval;

@StrutsFunctionality(app = ManagerStudentsApp.class, path = "curriculum-logs", titleKey = "title.curriculum.line.logs")
@Mapping(path = "/curriculumLineLogs", module = "manager")
@Forwards({ @Forward(name = "searchCurriculumLineLogs", path = "/manager/viewCurriculumLineLogs.jsp"),
        @Forward(name = "viewCurriculumLineLogStatistics", path = "/manager/viewCurriculumLineLogStatistics.jsp") })
public class CurriculumLineLogsDA extends FenixDispatchAction {

    @EntryPoint
    public ActionForward prepareViewCurriculumLineLogs(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        request.setAttribute("bean", new SearchCurriculumLineLog());
        return mapping.findForward("searchCurriculumLineLogs");
    }

    public ActionForward viewCurriculumLineLogs(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {

        final SearchCurriculumLineLog searchCurriculumLineLog = getRenderedObject();
        request.setAttribute("bean", searchCurriculumLineLog);
        Student student = Student.readStudentByNumber(searchCurriculumLineLog.getStudentNumber());

        if (student == null) {
            addActionMessage(request, "exception.student.does.not.exist");
            return mapping.findForward("searchCurriculumLineLogs");
        }

        request.setAttribute("curriculumLineLogs", student.getCurriculumLineLogs(searchCurriculumLineLog.getExecutionPeriod()));
        return mapping.findForward("searchCurriculumLineLogs");
    }

    public static class CurriculumLineLogStatisticsCalculator {

        private static final int INTERVAL_SIZE_IN_MILLIS = 300000;

        final Interval enrolmentPeriod;
        final int[] enrolments;
        final int[] unenrolments;

        public CurriculumLineLogStatisticsCalculator(final ExecutionSemester executionSemester) {
            enrolmentPeriod = findEnrolmentPeriod(executionSemester);
            final long start = enrolmentPeriod.getStart().getMillis();
            final long durationMillis = enrolmentPeriod.toDurationMillis();
            final int numberOfIntervals = (int) (durationMillis / INTERVAL_SIZE_IN_MILLIS) + 1;

            enrolments = new int[numberOfIntervals];
            unenrolments = new int[numberOfIntervals];

            for (final CurriculumLineLog curriculumLineLog : executionSemester.getCurriculumLineLogsSet()) {
                if (curriculumLineLog instanceof EnrolmentLog) {
                    final DateTime dateTime = curriculumLineLog.getDateDateTime();
                    if (enrolmentPeriod.contains(dateTime)) {
                        final long offset = dateTime.getMillis() - start;
                        final int i = (int) offset / INTERVAL_SIZE_IN_MILLIS;

                        if (curriculumLineLog.getAction() == EnrolmentAction.ENROL) {
                            enrolments[i]++;
                        } else {
                            unenrolments[i]++;
                        }
                    }
                }
            }
        }

        public Interval getEnrolmentPeriod() {
            return enrolmentPeriod;
        }

        public int[] getEnrolments() {
            return enrolments;
        }

        public int[] getUnenrolments() {
            return unenrolments;
        }

        private Interval findEnrolmentPeriod(final ExecutionSemester executionSemester) {
            DateTime start = null, end = null;
            for (final EnrolmentPeriod enrolmentPeriod : executionSemester.getEnrolmentPeriodSet()) {
                if (enrolmentPeriod instanceof EnrolmentPeriodInCurricularCourses) {
                    final DegreeType degreeType = enrolmentPeriod.getDegreeCurricularPlan().getDegreeType();
                    if (degreeType == DegreeType.BOLONHA_DEGREE || degreeType == DegreeType.BOLONHA_INTEGRATED_MASTER_DEGREE
                            || degreeType == DegreeType.BOLONHA_INTEGRATED_MASTER_DEGREE) {
                        if (start == null || start.isAfter(enrolmentPeriod.getStartDateDateTime())) {
                            start = enrolmentPeriod.getStartDateDateTime();
                        }
                        if (end == null || end.isBefore(enrolmentPeriod.getEndDateDateTime())) {
                            end = enrolmentPeriod.getEndDateDateTime();
                        }
                    }
                }
            }
            return start == null || end == null ? null : new Interval(start, end);
        }

    }

    public ActionForward viewCurriculumLineLogStatistics(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {

        final ExecutionSemester executionSemester = getDomainObject(request, "executionSemesterId");

        if (executionSemester != null) {
            request.setAttribute("executionSemester", executionSemester);

            final CurriculumLineLogStatisticsCalculator curriculumLineLogStatisticsCalculator =
                    new CurriculumLineLogStatisticsCalculator(executionSemester);
            request.setAttribute("curriculumLineLogStatisticsCalculator", curriculumLineLogStatisticsCalculator);
            return mapping.findForward("viewCurriculumLineLogStatistics");
        }

        return prepareViewCurriculumLineLogs(mapping, form, request, response);
    }

}
TOP

Related Classes of org.fenixedu.academic.ui.struts.action.manager.enrolments.CurriculumLineLogsDA$CurriculumLineLogStatisticsCalculator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.