/** * */ package net.sourceforge.fenixedu.presentationTier.Action.softplanning; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.ResourceBundle; import java.util.Set; 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.commons.CollectionUtils; import net.sourceforge.fenixedu.domain.Degree; import net.sourceforge.fenixedu.domain.DegreeCurricularPlan; import net.sourceforge.fenixedu.domain.ExecutionDegree; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.degree.DegreeType; import net.sourceforge.fenixedu.domain.degree.degreeCurricularPlan.DegreeCurricularPlanState; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.person.IDDocumentType; import net.sourceforge.fenixedu.domain.student.Registration; import net.sourceforge.fenixedu.domain.student.Student; import net.sourceforge.fenixedu.domain.teacher.Orientation; import net.sourceforge.fenixedu.framework.factory.ServiceManagerServiceFactory; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.presentationTier.Action.softplanning.utils.PlannedExecutionsAccessUtils; import org.apache.commons.lang.StringUtils; 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 (Paulo.Costa@iscte.pt) */ public class ManageOrientationsDispatchAction extends FenixDispatchAction { private static final ResourceBundle RESOURCE_BUNDLE = getResourceBundleByName("resources.SoftplanningResources"); public ActionForward prepareSearchStudent(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { setPageToSearchForStudents(request); return mapping.findForward("search-student"); } public ActionForward searchForStudent(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final DynaActionForm actionForm = (DynaActionForm) form; final String dcpId = (String) actionForm.get("degreeCurricularPlanId"); Boolean includeInactive = (Boolean) actionForm.get("includeInactive"); if (includeInactive == null) { includeInactive = Boolean.FALSE; } final String studentName = (String) actionForm.get("name"); final Integer studentNumber = (Integer) actionForm.get("number"); final String docIdNumber = (String) actionForm.get("documentIdNumber"); final Set totalRegistrations = new HashSet(); final Set matchRegistrations = new HashSet(); if (!dcpId.equals("-1")) { collectDcpRegistrations(dcpId, includeInactive, totalRegistrations); } if (studentNumber != 0) { collectStudentRegistrations(Student.readStudentByNumber(studentNumber), includeInactive, matchRegistrations); } if (!StringUtils.isEmpty(docIdNumber)) { final Collection persons = Person.readByDocumentIdNumber(docIdNumber); for (final Person person : persons) { if (person.hasStudent()) { collectStudentRegistrations(person.getStudent(), includeInactive, matchRegistrations); } } } if (!StringUtils.isEmpty(studentName)) { final Collection persons = Person.findPerson(studentName); for (final Person person : persons) { if (person.hasStudent()) { collectStudentRegistrations(person.getStudent(), includeInactive, matchRegistrations); } } } List data = new ArrayList(); if (matchRegistrations.isEmpty()) { for (final Registration dcpRegistration : totalRegistrations) { fillStudentData(data, dcpRegistration, getUserView(request).getPerson()); } } else if (totalRegistrations.isEmpty()) { for (final Registration registration : matchRegistrations) { fillStudentData(data, registration, getUserView(request).getPerson()); } } else { for (final Object registration : CollectionUtils.intersection(totalRegistrations, matchRegistrations)) { fillStudentData(data, (Registration) registration, getUserView(request).getPerson()); } } Collections.sort(data, STUDENT_DTO_BY_NUMBER); request.setAttribute("studentData", data); return mapping.findForward("student-data"); } private void collectStudentRegistrations(final Student student, final Boolean includeInactive, final Set match) { final List registrations = includeInactive ? student.getRegistrations() : student.getActiveRegistrations(); for (final Registration registration : registrations) { final DegreeCurricularPlan lastDcp = registration.getLastDegreeCurricularPlan(); if (lastDcp.canHaveOrientations()) { match.add(registration); } } } private void collectDcpRegistrations(final String dcpId, final Boolean inactive, final Set total) { final DegreeCurricularPlan dcp = rootDomainObject.readDegreeCurricularPlanByOID(Integer.valueOf(dcpId)); if (inactive) { total.addAll(dcp.getAllRegistrations()); } else { total.addAll(dcp.getRegistrations()); } } private void fillStudentData(final List data, final Registration registration, final Person responsible) { final Person person = registration.getPerson(); final Student student = person.getStudent(); final StudentData sd = new StudentData(); sd.setStudentNumber(student.getNumber()); sd.setRegistrationNumber(registration.getNumber()); sd.setName(person.getName()); sd.setDegree(registration.getDegreeType().getLocalizedName() + " " + registration.getDegree().getName()); sd.setYear(registration.getRegistrationYear().getName()); sd.setRegistrationId(registration.getIdInternal()); sd.setActive(registration.isActive() ? RESOURCE_BUNDLE.getString("label.active") : RESOURCE_BUNDLE .getString("label.inactive")); sd.setDocIdNumber(person.getDocumentIdNumber()); sd.setDocIdType(person.getIdDocumentType().getLocalizedName()); sd.setOrientations(Orientation.getOrientationsForRegistration(registration)); sd.setCanEdit(PlannedExecutionsAccessUtils.isPersonAllowedToEditDegree(responsible, registration.getDegree())); data.add(sd); } public ActionForward prepareCreate(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Integer registrationId = getRequestParameterAsInteger(request, "registrationId"); final Registration registration = rootDomainObject.readRegistrationByOID(registrationId); if (registration != null) { final DynaActionForm actionForm = (DynaActionForm) form; actionForm.set("degreeId", registration.getDegree().getIdInternal()); actionForm.set("studentId", registration.getStudent().getIdInternal()); actionForm.set("registrationId", registration.getIdInternal()); prepareCreateOrEditOrientation(request, registration, ((IUserView) UserView.getUser())); } else { setError(request, "registrationId", "error.no.student.defined"); } return mapping.findForward("createOrientation"); } public ActionForward finalizeCreation(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { DynaActionForm actionForm = (DynaActionForm) form; Registration registration = null; final Integer executionYearId = (Integer) actionForm.get("executionYearId"); if (executionYearId == null || executionYearId == -1) { setError(request, "executionYearId", "error.no.execution.year.defined"); registration = rootDomainObject.readRegistrationByOID((Integer) actionForm.get("registrationId")); } final Integer degreeId = (Integer) actionForm.get("degreeId"); if (degreeId == null || degreeId == -1) { setError(request, "degreeId", "error.no.execution.degree.defined"); } final Integer[] teacherIds = (Integer[]) actionForm.get("teacherIds"); if (teacherIds == null || teacherIds.length == 0) { setError(request, "teacherIds", "error.no.teacher.defined"); } final Integer studentId = (Integer) actionForm.get("studentId"); if (studentId == -1) { setError(request, "studentId", "error.no.student.defined"); } Integer executionDegreeId = null; if (degreeId != null && executionYearId != null && studentId != null) { final Degree degree = rootDomainObject.readDegreeByOID(degreeId); final ExecutionYear executionYear = rootDomainObject.readExecutionYearByOID(executionYearId); final Student student = rootDomainObject.readStudentByOID(studentId); if (degree != null && executionYear != null && student != null && student.getRegistrationFor(degree) != null) { for (final ExecutionDegree ed : degree.getExecutionDegreesForExecutionYear(executionYear)) { if (ed.equals(student.getRegistrationFor(degree).getExecutionDegreeByExecutionYear(executionYear))) { executionDegreeId = ed.getIdInternal(); registration = student.getRegistrationFor(degree); break; } } } } if (executionDegreeId == null || registration == null) { setError(request, "executionDegree", "error.no.execution.degree.for.execution.year"); } final IUserView userView = UserView.getUser(); if (!getErrors(request).isEmpty()) { prepareCreateOrEditOrientation(request, registration, userView); return mapping.findForward("createOrientation"); } final String description = (String) actionForm.get("description"); Object[] args = { executionDegreeId, teacherIds, studentId, description, userView.getUtilizador() }; try { Orientation orientation = (Orientation) ServiceManagerServiceFactory.executeService(userView, "CreateOrientation", args); request.setAttribute("successMessage", RESOURCE_BUNDLE.getString("label.createOrientation.success")); request.setAttribute("orientation", orientation); } catch (FenixFilterException e) { setError(request, "service", "error.creating.orientation"); prepareCreateOrEditOrientation(request, registration, userView); return mapping.findForward("createOrientation"); } catch (FenixServiceException e) { setError(request, "service", "error.creating.orientation"); prepareCreateOrEditOrientation(request, registration, userView); return mapping.findForward("createOrientation"); } catch (DomainException e) { setError(request, "service", e.getKey()); prepareCreateOrEditOrientation(request, registration, userView); return mapping.findForward("createOrientation"); } return mapping.findForward("createOrientationsSuccess"); } public ActionForward prepareEdit(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Orientation orientation = rootDomainObject.readOrientationByOID(getIdInternal(request, "orientationId")); if (orientation == null) { setError(request, "orientation", "error.orientation.not.found"); return mapping.findForward("editOrientation"); } // prepare form data Integer[] teacherIds = new Integer[orientation.getTeachersCount()]; final List teachers = orientation.getTeachers(); for (int counter = 0; counter < teacherIds.length; counter++) { teacherIds[counter] = teachers.get(counter).getIdInternal(); } // Fill form data final DynaActionForm actionForm = (DynaActionForm) form; actionForm.set("orientationId", orientation.getIdInternal()); actionForm.set("executionYearId", orientation.getExecutionDegree().getExecutionYear().getIdInternal()); actionForm.set("degreeId", orientation.getExecutionDegree().getDegree().getIdInternal()); actionForm.set("teacherIds", teacherIds); actionForm.set("studentId", orientation.getStudent().getIdInternal()); actionForm.set("description", orientation.getDescription()); final List degreeList = new ArrayList(); for (Registration reg : orientation.getStudent().getRegistrations()) { final Degree degree = reg.getDegree(); final String name = degree.getCode() + "-" + degree.getDegreeType().getLocalizedName() + degree.getName() + " (" + (reg.isActive() ? RESOURCE_BUNDLE.getString("label.active") : RESOURCE_BUNDLE.getString("label.inactive")) + ")"; degreeList.add(new SelectItem(degree.getIdInternal(), name)); } request.setAttribute("degreeList", degreeList); prepareCreateOrEditOrientation(request, orientation.getRegistration(), (IUserView) UserView.getUser()); return mapping.findForward("editOrientation"); } public ActionForward finalizeEdition(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { DynaActionForm actionForm = (DynaActionForm) form; final Orientation orientation = rootDomainObject.readOrientationByOID((Integer) actionForm.get("orientationId")); if (orientation == null) { setError(request, "orientationId", "error.orientation.not.found"); return mapping.findForward("editOrientation"); } final Integer executionYearId = (Integer) actionForm.get("executionYearId"); if (executionYearId == null || executionYearId == -1) { setError(request, "executionYearId", "error.no.execution.year.defined"); } final Integer degreeId = (Integer) actionForm.get("degreeId"); if (degreeId == null || degreeId == -1) { setError(request, "degreeId", "error.no.execution.degree.defined"); } final Integer[] teacherIds = (Integer[]) actionForm.get("teacherIds"); if (teacherIds == null || teacherIds.length == 0) { setError(request, "teacherIds", "error.no.teacher.defined"); } final Integer studentId = (Integer) actionForm.get("studentId"); if (studentId == -1) { setError(request, "studentId", "error.no.student.defined"); } final IUserView userView = UserView.getUser(); if (getErrors(request).isEmpty()) { // verify if student has registration for selected degree Integer executionDegreeId = null; final Degree degree = rootDomainObject.readDegreeByOID(degreeId); final ExecutionYear executionYear = rootDomainObject.readExecutionYearByOID(executionYearId); final Student student = rootDomainObject.readStudentByOID(studentId); if (degree != null && executionYear != null && student != null && student.getRegistrationFor(degree) != null) { for (final ExecutionDegree ed : degree.getExecutionDegreesForExecutionYear(executionYear)) { if (ed.equals(student.getRegistrationFor(degree).getExecutionDegreeByExecutionYear(executionYear))) { executionDegreeId = ed.getIdInternal(); break; } } } if (executionDegreeId != null) { final String description = (String) actionForm.get("description"); Object[] args = { orientation.getIdInternal(), executionDegreeId, teacherIds, description, userView.getUtilizador() }; try { Orientation result = (Orientation) ServiceManagerServiceFactory.executeService(userView, "EditOrientation", args); request.setAttribute("successMessage", RESOURCE_BUNDLE.getString("label.editOrientation.success")); request.setAttribute("orientation", result); } catch (FenixFilterException e) { setError(request, "service", "error.editing.orientation"); prepareCreateOrEditOrientation(request, orientation.getRegistration(), userView); return mapping.findForward("editOrientation"); } catch (FenixServiceException e) { setError(request, "service", "error.editing.orientation"); prepareCreateOrEditOrientation(request, orientation.getRegistration(), userView); return mapping.findForward("editOrientation"); } catch (DomainException e) { setError(request, "service", e.getKey()); e.printStackTrace(); prepareCreateOrEditOrientation(request, orientation.getRegistration(), userView); return mapping.findForward("editOrientation"); } } else { setError(request, "executionDegree", "error.no.execution.degree.for.execution.year"); prepareCreateOrEditOrientation(request, orientation.getRegistration(), userView); return mapping.findForward("editOrientation"); } } else { prepareCreateOrEditOrientation(request, orientation.getRegistration(), userView); return mapping.findForward("editOrientation"); } return mapping.findForward("editOrientationsSuccess"); } public ActionForward delete(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Orientation orientation = rootDomainObject.readOrientationByOID(getIdInternal(request, "orientationId")); if (orientation != null) { request.setAttribute("orientation", orientation); } else { setError(request, "orientation", "error.orientation.not.found"); } return mapping.findForward("confirmDelete"); } public ActionForward confirmDelete(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) { final Integer orientationId = getIdInternal(request, "idInternal"); if (orientationId != null) { // delete the orientation final IUserView userView = UserView.getUser(); final Object[] args = { orientationId }; try { if ((Boolean) ServiceManagerServiceFactory.executeService(userView, "DeleteOrientation", args)) { request.setAttribute("successMessage", RESOURCE_BUNDLE.getString("message.softplanning.deleteOrientation.success")); } else { setError(request, "orientation", "error.orientation.could.not.be.deleted"); } } catch (FenixFilterException e) { setError(request, "orientation", "error.orientation.could.not.be.deleted"); } catch (FenixServiceException e) { setError(request, "orientation", "error.orientation.could.not.be.deleted"); } } else { setError(request, "orientation", "error.orientation.could.not.be.deleted"); } return mapping.findForward("deleteSuccess"); } private void prepareCreateOrEditOrientation(HttpServletRequest request, Registration registration, IUserView userView) { final List executionYearList = new ArrayList(); executionYearList.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.select.one"))); ExecutionYear executionYear = registration.getRegistrationYear(); executionYearList.add(new SelectItem(executionYear.getIdInternal(), executionYear.getName())); List executionYears = new ArrayList(); executionYears.add(executionYear); while (executionYear.hasNextExecutionYear()) { final ExecutionYear nextExecutionYear = executionYear.getNextExecutionYear(); if (registration.getExecutionDegreeByExecutionYear(nextExecutionYear) != null) { executionYearList.add(new SelectItem(nextExecutionYear.getIdInternal(), nextExecutionYear.getName())); executionYears.add(nextExecutionYear); } executionYear = nextExecutionYear; } request.setAttribute("student", registration.getStudent()); request.setAttribute("degree", registration.getDegree()); request.setAttribute("executionYearList", executionYearList); request.setAttribute("teacherList", PlannedExecutionsAccessUtils.getMultipleTeacherListWithoutFutureTeachers(userView.getPerson(), executionYears)); } protected void setError(final HttpServletRequest request, final String property, final String key) { final ActionMessages errors = getMessages(request); errors.add(property, new ActionMessage(key)); saveErrors(request, errors); } private void setPageToSearchForStudents(final HttpServletRequest request) { setDocumentIdTypes(request); setDegreeCurricularPlans(request); } private void setDocumentIdTypes(final HttpServletRequest request) { final List docTypes = new ArrayList(); docTypes.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.select.one"))); for (final IDDocumentType idt : IDDocumentType.values()) { docTypes.add(new SelectItem(idt.getName(), idt.getLocalizedName())); } request.setAttribute("docIdTypeList", docTypes); } private void setDegreeCurricularPlans(final HttpServletRequest request) { final Set dcpSet = new HashSet(); for (final DegreeType degreeType : DegreeType.getBolonhaDegreeTypes()) { if (degreeType.isSecondCycle() || degreeType.isThirdCycle()) { dcpSet.addAll(DegreeCurricularPlan.readByDegreeTypeAndState(degreeType, DegreeCurricularPlanState.ACTIVE)); } } // Add Pré-Bolonha because some students are still finishing... dcpSet.addAll(DegreeCurricularPlan.readByDegreeTypeAndState(DegreeType.MASTER_DEGREE, DegreeCurricularPlanState.ACTIVE)); dcpSet.addAll(DegreeCurricularPlan.readByDegreeTypeAndState(DegreeType.MASTER_DEGREE, DegreeCurricularPlanState.PAST)); dcpSet.addAll(DegreeCurricularPlan.readByDegreeTypeAndState(DegreeType.PHD_DEGREE, DegreeCurricularPlanState.ACTIVE)); dcpSet.addAll(DegreeCurricularPlan.readByDegreeTypeAndState(DegreeType.PHD_DEGREE, DegreeCurricularPlanState.PAST)); final List allDcpList = new ArrayList(dcpSet); Collections.sort(allDcpList, DegreeCurricularPlan.DEGREE_CURRICULAR_PLAN_COMPARATOR_BY_DEGREE_TYPE_AND_EXECUTION_DEGREE_AND_DEGREE_CODE); final List dcpIds = new ArrayList(); dcpIds.add(new SelectItem(-1, RESOURCE_BUNDLE.getString("label.select.one"))); for (final DegreeCurricularPlan dcp : allDcpList) { dcpIds.add(new SelectItem(dcp.getIdInternal(), dcp.getDegreeType().getLocalizedName() + " - " + dcp.getName())); } request.setAttribute("dcpIdsList", dcpIds); } private static final Comparator STUDENT_DTO_BY_NUMBER = new Comparator() { public int compare(final StudentData dto1, final StudentData dto2) { return dto1.getStudentNumber().compareTo(dto2.getStudentNumber()); } }; public class StudentData { private Integer studentNumber; private Integer registrationNumber; private Integer registrationId; private String name; private String degree; private String year; private String docIdNumber; private String docIdType; private String active; private List orientations; private boolean canEdit; public Integer getStudentNumber() { return studentNumber; } public void setStudentNumber(final Integer studentNumber) { this.studentNumber = studentNumber; } public Integer getRegistrationNumber() { return registrationNumber; } public void setRegistrationNumber(final Integer registrationNumber) { this.registrationNumber = registrationNumber; } public String getName() { return name; } public void setName(final String name) { this.name = name; } public String getDegree() { return degree; } public void setDegree(final String degree) { this.degree = degree; } public String getYear() { return year; } public void setYear(final String year) { this.year = year; } public Integer getRegistrationId() { return registrationId; } public void setRegistrationId(final Integer registrationId) { this.registrationId = registrationId; } public void setActive(final String active) { this.active = active; } public String getActive() { return active; } public void setDocIdNumber(final String docIdNumber) { this.docIdNumber = docIdNumber; } public String getDocIdNumber() { return docIdNumber; } public void setDocIdType(final String docIdType) { this.docIdType = docIdType; } public String getDocIdType() { return docIdType; } public void setOrientations(final List orientations) { this.orientations = orientations; } public List getOrientations() { return orientations; } public void setCanEdit(final boolean canEdit) { this.canEdit = canEdit; } public boolean getCanEdit() { return canEdit; } } }