package net.sourceforge.fenixedu.presentationTier.Action.departmentAdmOffice; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.dataTransferObject.InfoTeacher; import net.sourceforge.fenixedu.dataTransferObject.person.ChoosePersonBean; import net.sourceforge.fenixedu.dataTransferObject.person.PersonBean; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.utils.ServiceUtils; import net.sourceforge.fenixedu.util.StringUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.joda.time.YearMonthDay; import pt.ist.fenixWebFramework.renderers.utils.RenderUtils; public class TeachersManagementDA extends FenixDispatchAction { protected static final Comparator NAME_COMPARATOR = new Comparator() { public int compare(final InfoTeacher i1, final InfoTeacher i2) { return StringUtils.normalize(i1.getTeacherName()).compareTo(StringUtils.normalize(i2.getTeacherName())); }; }; private Department getDepartment(final HttpServletRequest request) { return getUserView(request).getPerson().getEmployee().getCurrentDepartmentWorkingPlace(); } public ActionForward prepareCategories(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { final Department department = getDepartment(request); List infoTeachers = null; try { infoTeachers = (List) executeService("ReadDepartmentTeachersByDepartmentID", new Object[] { department .getIdInternal() }); } catch (DomainException e) { addActionMessage(request, e.getMessage()); } Collections.sort(infoTeachers, NAME_COMPARATOR); request.setAttribute("infoTeachers", infoTeachers); return mapping.findForward("showTeachersAndCategories"); } public ActionForward editCategory(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Teacher teacher = Teacher.readByNumber(Integer.parseInt(request.getParameter("teacher"))); request.setAttribute("teacher", teacher); return mapping.findForward("editTeacherCategory"); } public ActionForward prepareCreate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute("choosePersonBean", new ChoosePersonBean()); return mapping.findForward("prepareCreate"); } public ActionForward choosePerson(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PersonBean personBean = null; Person person = null; if (RenderUtils.getViewState("person") != null) { // Postback request.setAttribute("personBean", RenderUtils.getViewState("person").getMetaObject().getObject()); return mapping.findForward("fillNewPersonData"); } ChoosePersonBean choosePersonBean = (ChoosePersonBean) RenderUtils.getViewState("choosePerson").getMetaObject() .getObject(); final String identificationNumber = choosePersonBean.getIdentificationNumber(); final YearMonthDay dateOfBirth = choosePersonBean.getDateOfBirth(); if (choosePersonBean.getPerson() == null) { Collection persons = Person.findPersonByDocumentID(identificationNumber); if (choosePersonBean.isFirstTimeSearch()) { choosePersonBean.setFirstTimeSearch(false); if (!persons.isEmpty() || !Person.findByDateOfBirth(dateOfBirth, Person.findInternalPersonMatchingFirstAndLastName(choosePersonBean.getName())).isEmpty()) { // show similar persons RenderUtils.invalidateViewState(); request.setAttribute("choosePersonBean", choosePersonBean); return mapping.findForward("prepareCreate"); } } } else { person = choosePersonBean.getPerson(); } if (person != null) { personBean = new PersonBean(person); } else { Collection persons = Person.findPersonByDocumentID(identificationNumber); if (!persons.isEmpty()) { addActionMessage(request, "error.person.existent.docIdAndType"); RenderUtils.invalidateViewState(); request.setAttribute("choosePersonBean", choosePersonBean); return mapping.findForward("prepareCreate"); } personBean = new PersonBean(choosePersonBean.getName(), identificationNumber, choosePersonBean.getDocumentType(), dateOfBirth); } request.setAttribute("personBean", personBean); return mapping.findForward("fillNewPersonData"); } public ActionForward prepareCreateContract(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PersonBean personBean = (PersonBean) RenderUtils.getViewState("person").getMetaObject().getObject(); request.setAttribute("personBean", personBean); final WorkingContractBean workingContractBean = new WorkingContractBean(personBean.getPerson()); request.setAttribute("workingContractBean", workingContractBean); return mapping.findForward("fillNewPersonData"); } public ActionForward prepareShowCreateTeacherConfirmation(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PersonBean personBean = (PersonBean) RenderUtils.getViewState("person").getMetaObject().getObject(); request.setAttribute("personBean", personBean); return mapping.findForward("showCreateTeacherConfirmation"); } public ActionForward createTeacher(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws FenixFilterException, FenixServiceException { PersonBean personBean = (PersonBean) RenderUtils.getViewState("person").getMetaObject().getObject(); final WorkingContractBean workingContractBean = (WorkingContractBean) RenderUtils.getViewState("workingContractBean") .getMetaObject().getObject(); Object[] args = { personBean, workingContractBean }; try { Teacher teacher = (Teacher) ServiceUtils.executeService("CreateExternalTeacher", args); request.setAttribute("teacher", teacher); } catch (DomainException e) { addActionMessage(request, e.getKey(), e.getArgs()); return prepareShowCreateTeacherConfirmation(mapping, actionForm, request, response); } return mapping.findForward("createTeacherSuccess"); } }