package net.sourceforge.fenixedu.presentationTier.Action.softplanning; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ResourceBundle; import javax.faces.model.SelectItem; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.applicationTier.IUserView; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.teacher.TeacherObservation; import net.sourceforge.fenixedu.domain.teacher.TeacherStatistics; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.utils.ServiceUtils; import net.sourceforge.fenixedu.presentationTier.Action.softplanning.utils.PlannedExecutionsAccessUtils; import net.sourceforge.fenixedu.util.report.Spreadsheet; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.DynaActionForm; import pt.ist.fenixWebFramework.security.UserView; /** * @author - Nuno Alves (nmsa@iscte.pt) * @author - Paulo Costa (pramc@iscte.pt) */ public class VisualizeStatisticsDispatchAction extends FenixDispatchAction { private static final ResourceBundle RESOURCE_BUNDLE = getResourceBundleByName("resources.SoftplanningResources"); public ActionForward prepareSearch(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Person loggedPerson = getUserView(request).getPerson(); request.setAttribute("departmentList", PlannedExecutionsAccessUtils.getDepartmentListFor(loggedPerson)); final List executionYearsList = new ArrayList(); executionYearsList.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.select.one"))); final List executionYears = ExecutionYear.readNotClosedExecutionYears(); Collections.sort(executionYears, ExecutionYear.REVERSE_COMPARATOR_BY_YEAR); for (final ExecutionYear executionYear : executionYears) { executionYearsList.add(new SelectItem(executionYear.getIdInternal(), executionYear.getName())); } request.setAttribute("executionYearList", executionYearsList); if (loggedPerson.isFromScientificCouncil()) { request.setAttribute("authorized", true); } return mapping.findForward("first-page"); } /** * Search for statistical data */ public ActionForward search(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final DynaActionForm actionForm = (DynaActionForm) form; final ActionMessages errors = new ActionMessages(); final Integer departmentId = (Integer) actionForm.get("departmentId"); if (departmentId == null || (departmentId != null && departmentId == -1)) { errors.add("departmentId", new ActionMessage("error.department.required")); } final Integer executionYearId = (Integer) actionForm.get("beginExecutionYearId"); if (executionYearId == null || (executionYearId != null && executionYearId == -1)) { errors.add("beginExecutionYearId", new ActionMessage("error.execution.year.required")); } if (errors.isEmpty()) {// continue final IUserView userView = UserView.getUser(); final Object[] args = new Object[] { departmentId, executionYearId, userView.getUtilizador() }; try { final List result = (List) ServiceUtils.executeService( "SearchTeacherStatisticsData", args); request.setAttribute("statisticData", result); if (getUserView(request).getPerson().isFromScientificCouncil()) { request.setAttribute("authorized", true); } return mapping.findForward("search-results"); } catch (final FenixFilterException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareSearch(mapping, actionForm, request, response); } catch (final FenixServiceException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareSearch(mapping, actionForm, request, response); } catch (final DomainException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareSearch(mapping, actionForm, request, response); } } else { saveErrors(request, errors); return prepareSearch(mapping, actionForm, request, response); } } /** * goto page to create a new statistical data information */ public ActionForward prepareCreate(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { prepareAttributesToEditOrCreate(request); return mapping.findForward("create-new"); } /** * finalize create a new statistical data information */ public ActionForward create(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final DynaActionForm actionForm = (DynaActionForm) form; final ActionMessages errors = new ActionMessages(); final IUserView userView = UserView.getUser(); final Object[] args = getStatisticalData(actionForm, errors, request, userView, null); if (errors.isEmpty()) { // continue try { ServiceUtils.executeService( "CreateTeacherStatisticsData", args); request.setAttribute("success", "success.create.teacherStatistics"); return prepareSearch(mapping, actionForm, request, response); } catch (FenixFilterException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareCreate(mapping, actionForm, request, response); } catch (FenixServiceException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareCreate(mapping, actionForm, request, response); } catch (DomainException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return prepareCreate(mapping, actionForm, request, response); } } else { saveErrors(request, errors); return prepareCreate(mapping, actionForm, request, response); } } /** * goto page to edit statistical data information */ public ActionForward prepareEdit(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { getStatistic(request); return mapping.findForward("edit"); } /** * goto page to delete statistical data information */ public ActionForward prepareDelete(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { getStatistic(request); return mapping.findForward("delete"); } private void getStatistic(final HttpServletRequest request) { final Integer statisticId = getIntegerFromRequest(request, "idInternal"); request.setAttribute("idInternal", statisticId); request.setAttribute("statisticData", rootDomainObject.readTeacherStatisticsByOID(statisticId)); prepareAttributesToEditOrCreate(request); } /** * finalize edit statistical data information */ public ActionForward edit(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Integer statisticId = getIntegerFromRequest(request, "idInternal"); request.setAttribute("idInternal", statisticId); final DynaActionForm actionForm = (DynaActionForm) form; final ActionMessages errors = new ActionMessages(); final IUserView userView = UserView.getUser(); final Object[] args = getStatisticalData(actionForm, errors, request, userView, statisticId); if (errors.isEmpty()) { try { ServiceUtils.executeService( "EditTeacherStatisticsData", args); request.setAttribute("success", "success.edit.teacherStatistics"); return prepareSearch(mapping, actionForm, request, response); } catch (FenixFilterException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, actionForm, request, response); } catch (FenixServiceException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, actionForm, request, response); } catch (DomainException e) { errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, actionForm, request, response); } } else { saveErrors(request, errors); return search(mapping, actionForm, request, response); } } /** * confirm delete statistical data information */ public ActionForward delete(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Integer statisticId = getIntegerFromRequest(request, "idInternal"); request.setAttribute("idInternal", statisticId); final ActionMessages errors = new ActionMessages(); final IUserView userView = UserView.getUser(); try { ServiceUtils.executeService( "DeleteTeacherStatisticsData", statisticId); request.setAttribute("success", "success.delete.teacherStatistics"); return prepareSearch(mapping, form, request, response); } catch (final FenixFilterException e) { e.printStackTrace(); errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, form, request, response); } catch (final FenixServiceException e) { e.printStackTrace(); errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, form, request, response); } catch (final DomainException e) { e.printStackTrace(); errors.add("teacherStatistics", new ActionMessage(e.getMessage())); saveErrors(request, errors); return search(mapping, form, request, response); } } private Object[] getStatisticalData(final DynaActionForm actionForm, final ActionMessages errors, final HttpServletRequest request, final IUserView userView, Integer statisticId) { final Integer teacherId = (Integer) actionForm.get("teacherId"); if (teacherId == null || (teacherId != null && teacherId == -1)) { errors.add("teacherId", new ActionMessage("error.teacher.required")); } final Double percentage = (Double) actionForm.get("percentage"); final String observation_s = (String) actionForm.get("observation"); TeacherObservation observation = null; if (observation_s == null || (observation_s != null && observation_s.equals("-1"))) { errors.add("observation", new ActionMessage("error.observation.required")); } else { observation = TeacherObservation.valueOf(observation_s); } if (observation == null) { errors.add("observation", new ActionMessage("error.observation.required")); } final Boolean isSpecialOrientation = (Boolean) actionForm.get("isSpecialOrientation"); final Boolean isSpecialPosition = (Boolean) actionForm.get("isSpecialPosition"); final Integer beginExecutionYearId = (Integer) actionForm.get("beginExecutionYearId"); if (beginExecutionYearId == null || (beginExecutionYearId != null && beginExecutionYearId == -1)) { errors.add("beginExecutionYearId", new ActionMessage("error.begin.execution.year.required")); } final Integer endExecutionYearId = (Integer) actionForm.get("endExecutionYearId"); if (statisticId != null) { return new Object[] { statisticId, teacherId, percentage, observation, isSpecialOrientation, isSpecialPosition, beginExecutionYearId, endExecutionYearId, userView.getUtilizador() }; } else { return new Object[] { teacherId, percentage, observation, isSpecialOrientation, isSpecialPosition, beginExecutionYearId, endExecutionYearId, userView.getUtilizador() }; } } private void prepareAttributesToEditOrCreate(final HttpServletRequest request) { final Person loggedPerson = getUserView(request).getPerson(); request.setAttribute("teacherList", PlannedExecutionsAccessUtils.getTeacherListWithoutFutureTeachers(loggedPerson)); final List observationTypes = new ArrayList(); observationTypes.add(new SelectItem("-1", RESOURCE_BUNDLE.getString("label.select.one"))); for (final TeacherObservation observations : TeacherObservation.values()) { observationTypes.add(new SelectItem(observations.name(), observations.getLocalizedName())); } request.setAttribute("observationList", observationTypes); final List beginYearList = new ArrayList(); final List endYearList = new ArrayList(); beginYearList.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.select.one"))); endYearList.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.open"))); final List executionsYears = ExecutionYear.readNotClosedExecutionYears(); Collections.sort(executionsYears, ExecutionYear.REVERSE_COMPARATOR_BY_YEAR); for (final ExecutionYear executionYear : executionsYears) { beginYearList.add(new SelectItem(executionYear.getIdInternal(), executionYear.getName())); endYearList.add(new SelectItem(executionYear.getIdInternal(), executionYear.getName())); } request.setAttribute("beginExecutionYearList", beginYearList); request.setAttribute("endExecutionYearList", endYearList); } //-------------------------------------------------------------------------------------------------------- public ActionForward downloadPlanningData(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) throws Exception { response.setContentType("text/plain"); response.setHeader("Content-disposition", "attachment; filename=planning-overview.xls"); final OutputStream outputStream = response.getOutputStream(); final Spreadsheet spreadsheet = new Spreadsheet("planning-overview"); //TODO check /scripts/src/pt/iscte/ci/scripts/reports/softplanning/SoftPlanningByTeacherReport.java //the script is complex enough to justify a simpler approach here spreadsheet.exportToXLSSheet(outputStream); outputStream.close(); return null; } }