/* * Created on 22/Dez/2003 * */ package net.sourceforge.fenixedu.presentationTier.Action.person; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.applicationTier.IUserView; import net.sourceforge.fenixedu.applicationTier.Servico.commons.degree.ReadAllDegreesByType; import net.sourceforge.fenixedu.applicationTier.Servico.department.ReadAllDepartments; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.applicationTier.Servico.person.SearchPerson; import net.sourceforge.fenixedu.applicationTier.Servico.person.SearchPerson.SearchParameters; import net.sourceforge.fenixedu.applicationTier.Servico.person.SearchPerson.SearchPersonPredicate; import net.sourceforge.fenixedu.dataTransferObject.InfoDegree; import net.sourceforge.fenixedu.dataTransferObject.InfoDepartment; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.framework.factory.ServiceManagerServiceFactory; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; 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.DynaActionForm; import pt.ist.fenixWebFramework.security.UserView; import pt.utl.ist.fenix.tools.util.CollectionPager; /** * @author Tânia Pousão * */ public class FindPersonAction extends FenixDispatchAction { public ActionForward prepareFindPerson(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("findPerson"); } public ActionForward preparePerson(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm findPersonForm = (DynaActionForm) actionForm; String roleType = null; String degreeType = null; if (request.getParameter("roleType") != null && request.getParameter("roleType").length() > 0) { roleType = (String) request.getParameter("roleType"); } else if (findPersonForm.get("roleType") != null) { roleType = (String) findPersonForm.get("roleType"); } if (request.getParameter("degreeType") != null && request.getParameter("degreeType").length() > 0) { degreeType = (String) request.getParameter("degreeType"); } else if (findPersonForm.get("degreeType") != null) { degreeType = (String) findPersonForm.get("degreeType"); } if (roleType != null && roleType.length() != 0) { if (roleType.equals(RoleType.EMPLOYEE.getName()) || roleType.equals(RoleType.TEACHER.getName())) { if (roleType.equals(RoleType.TEACHER.getName())) { List departments = (List) ReadAllDepartments.run(); request.setAttribute("departments", departments); } } if (roleType.equals(RoleType.STUDENT.getName())) { if (degreeType.length() != 0) { List nonMasterDegree = (List) ReadAllDegreesByType.run(degreeType); request.setAttribute("nonMasterDegree", nonMasterDegree); request.setAttribute("degreeType", true); } findPersonForm.set("degreeType", degreeType); request.setAttribute("degreeType", degreeType); } findPersonForm.set("roleType", roleType); request.setAttribute("roleType", roleType); } String name = null; if (request.getParameter("name") != null && request.getParameter("name").length() > 0) { name = request.getParameter("name"); } else if (findPersonForm.get("name") != null) { name = (String) findPersonForm.get("name"); } if (name != null && name.length() > 0) { findPersonForm.set("name", name); } Boolean viewPhoto = null; if (request.getParameter("viewPhoto") != null && request.getParameter("viewPhoto").length() > 0) { viewPhoto = getCheckBoxValue((String) request.getParameter("viewPhoto")); } else if (findPersonForm.get("viewPhoto") != null) { viewPhoto = getCheckBoxValue((String) findPersonForm.get("viewPhoto")); } findPersonForm.set("viewPhoto", viewPhoto.toString()); request.setAttribute("viewPhoto", viewPhoto); return mapping.findForward("findPerson"); } public ActionForward findPerson(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { IUserView userView = UserView.getUser(); DynaActionForm findPersonForm = (DynaActionForm) actionForm; String name = null; if (request.getParameter("name") != null && request.getParameter("name").length() > 0) { name = request.getParameter("name"); } else if (findPersonForm.get("name") != null) { name = (String) findPersonForm.get("name"); } String roleType = null; Integer departmentId = null; Integer degreeId = null; String degreeType = null; if (request.getParameter("roleType") != null && request.getParameter("roleType").length() > 0) { roleType = (String) request.getParameter("roleType"); } else if (findPersonForm.get("roleType") != null) { roleType = (String) findPersonForm.get("roleType"); } if (request.getParameter("degreeType") != null && request.getParameter("degreeType").length() > 0) { degreeType = (String) request.getParameter("degreeType"); } else if (findPersonForm.get("degreeType") != null) { degreeType = (String) findPersonForm.get("degreeType"); } if (degreeType.length() == 0 && roleType.length() == 0) { degreeType = null; } else if (roleType.equals(RoleType.STUDENT.getName())) { if (degreeType.length() != 0) { List nonMasterDegree = (List) ReadAllDegreesByType.run(degreeType); request.setAttribute("nonMasterDegree", nonMasterDegree); request.setAttribute("degreeType", degreeType); } else { request.setAttribute("degreeType", true); } } if (request.getParameter("departmentId") != null && request.getParameter("departmentId").length() > 0) { departmentId = Integer.valueOf(request.getParameter("departmentId")); } else if (findPersonForm.get("departmentId") != null) { departmentId = (Integer) findPersonForm.get("departmentId"); } if (roleType.equals(RoleType.TEACHER.getName())) { List departments = (List) ReadAllDepartments.run(); request.setAttribute("departments", departments); } if (request.getParameter("degreeId") != null && request.getParameter("degreeId").length() > 0) { degreeId = Integer.valueOf(request.getParameter("degreeId")); } else if (findPersonForm.get("degreeId") != null) { degreeId = (Integer) findPersonForm.get("degreeId"); } SearchParameters searchParameters = new SearchPerson.SearchParameters(name, null, null, null, null, roleType, degreeType, degreeId, departmentId, Boolean.TRUE, null, Boolean.FALSE); SearchPersonPredicate predicate = new SearchPerson.SearchPersonPredicate(searchParameters); Object[] args = { searchParameters, predicate }; CollectionPager result = null; try { result = (CollectionPager) ServiceManagerServiceFactory.executeService("SearchPerson", args); } catch (FenixServiceException e) { addErrorMessage(request, "impossibleFindPerson", e.getMessage()); return preparePerson(mapping, actionForm, request, response); } if (result == null) { addErrorMessage(request, "impossibleFindPerson", "error.manager.implossible.findPerson"); return preparePerson(mapping, actionForm, request, response); } if (result.getCollection().isEmpty()) { addErrorMessage(request, "impossibleFindPerson", "error.manager.implossible.findPerson"); return preparePerson(mapping, actionForm, request, response); } final String pageNumberString = request.getParameter("pageNumber"); final Integer pageNumber = !StringUtils.isEmpty(pageNumberString) ? Integer.valueOf(pageNumberString) : Integer .valueOf(1); request.setAttribute("pageNumber", pageNumber); request.setAttribute("numberOfPages", Integer.valueOf(result.getNumberOfPages())); request.setAttribute("personListFinded", result.getPage(pageNumber.intValue())); request.setAttribute("totalFindedPersons", result.getCollection().size()); request.setAttribute("name", name); request.setAttribute("roleType", roleType); request.setAttribute("degreeId", degreeId); request.setAttribute("departmentId", departmentId); findPersonForm.set("name", name); if (isEmployeeOrTeacher(userView)) { request.setAttribute("show", Boolean.TRUE); } else { request.setAttribute("show", Boolean.FALSE); } Boolean viewPhoto = null; if (request.getParameter("viewPhoto") != null && request.getParameter("viewPhoto").length() > 0) { viewPhoto = getCheckBoxValue((String) request.getParameter("viewPhoto")); } else if (findPersonForm.get("viewPhoto") != null) { viewPhoto = getCheckBoxValue((String) findPersonForm.get("viewPhoto")); } findPersonForm.set("viewPhoto", viewPhoto.toString()); request.setAttribute("viewPhoto", viewPhoto); return mapping.findForward("findPerson"); } private boolean isEmployeeOrTeacher(IUserView userView) { return userView.hasRoleType(RoleType.EMPLOYEE) || userView.hasRoleType(RoleType.TEACHER); } private Boolean getCheckBoxValue(String value) { if (value != null && (value.equals("true") || value.equals("yes") || value.equals("on"))) { return Boolean.TRUE; } return Boolean.FALSE; } }