package net.sourceforge.fenixedu.presentationTier.backBeans.softplanning; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.ResourceBundle; import java.util.Set; import javax.faces.component.UIParameter; import javax.faces.component.UISelectItems; import javax.faces.event.ActionEvent; import javax.faces.model.SelectItem; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.FutureTeacher; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.utils.ServiceUtils; import net.sourceforge.fenixedu.presentationTier.backBeans.base.FenixBackingBean; import org.apache.axis.utils.StringUtils; /** * * @author - Paulo Costa (paulo.costa@iscte.pt) * */ public class FutureTeacherManager extends FenixBackingBean { private final ResourceBundle resourceBundle = getResourceBundle("resources/SoftplanningResources"); private static final String OUTCOME_SUCCESS = "success"; private static final String OUTCOME_CREATE = "create"; private static final String OUTCOME_DELETE_OK = "delete"; private static final String OUTCOME_EDIT = "edit"; private static final String OUTCOME_SEARCH = "search"; private static final String OUTCOME_ERROR = "error"; // To be used on Page private Integer teacherId; private String errorMessage; private String successMessage; private List teachers; private List departmentList; private UISelectItems teachersToAssociate; private String teacherNumber; private String teacherNumberToSearch; private String teacherName; private Integer teacherDepartmentId; private Integer associatedTeacherId; private String associatedTeacherName; private Boolean isEdit; private Boolean selectAllTeachers; public FutureTeacherManager() { super(); resetFields(); } // Action to open the dialog to create a new teacher public String createFutureTeacher() { return OUTCOME_CREATE; } // Action that is deployed when user clicks on edit link public String editFutureTeacher() { setIsEdit(true); return OUTCOME_EDIT; } // Action when cancel is hit on create/edit Future Teacher public String searchFutureTeacher() { resetFields(); return OUTCOME_SEARCH; } //Action that is deployed when the user clicks on the find future teachers button on the search page public String listAllFutureTeachers() { resetFields(); selectAllTeachers = true; return listFutureTeachers(); } //Action that is deployed when the user clicks on the find future teachers button on the search page public String listFutureTeachers() { if ((getSelectAllTeachers() != null && getSelectAllTeachers()) || (StringUtils.isEmpty(getTeacherNumberToSearch()) && StringUtils.isEmpty(getTeacherName()) && (getTeacherDepartmentId() == null || teacherDepartmentId == -1))) { teachers = FutureTeacher.getAllTeachers(); return OUTCOME_SUCCESS; } teachers = new ArrayList(); Integer number = null; if (!StringUtils.isEmpty(teacherNumberToSearch)) { try { number = new Integer(teacherNumberToSearch); } catch (Exception e) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.number.incorrect")); } } // try to find by number if (number != null) { final FutureTeacher ft = FutureTeacher.getByNumber(number); if (ft != null) { teachers.add(ft); } } if (!StringUtils.isEmpty(teacherName)) { if (teacherDepartmentId != null && teacherDepartmentId != -1) { //if (PlannedExecutionsAccessUtils.isPersonFromDepartment(AccessControl.getPerson(), RootDomainObject.getInstance().readDepartmentByOID(teacherDepartmentId)) addToTeacherList(FutureTeacher.getByNameAndDepartment(teacherName, teacherDepartmentId)); } else { addToTeacherList(FutureTeacher.getByName(teacherName)); } } else if (teacherDepartmentId != null && teacherDepartmentId != -1) { addToTeacherList(FutureTeacher.getByDepartment(teacherDepartmentId)); } return OUTCOME_SUCCESS; } // Action that is deployed when the user clicks OK on the deleteFutureTeacher page public String deleteFutureTeacher() { try { ServiceUtils.executeService( "DeleteFutureTeachers", teacherId); successMessage = resourceBundle.getString("message.futureTeacher.delete.success"); return OUTCOME_DELETE_OK; } catch (DomainException e) { return prepareErrorPageForDeletion(resourceBundle.getString(e.getKey())); } catch (Exception e) { return prepareErrorPageForDeletion(resourceBundle.getString("error.futureTeacher.deleting")); } } //Action to submit the creation or edition of a new Future Teacher public String submitFutureTeacher() { if (StringUtils.isEmpty(teacherNumber)) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.number.not.set")); } if (StringUtils.isEmpty(teacherName)) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.name.not.set")); } if (teacherDepartmentId == null || teacherDepartmentId == -1) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.department.not.set")); } // see if teacherNumber is correct Integer number = null; try { number = new Integer(teacherNumber); } catch (Exception e) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.number.incorrect")); } // edit / create new Future Teacher final Object[] args = { number, teacherName, teacherDepartmentId, null }; if (getIsEdit()) { successMessage = resourceBundle.getString("message.futureTeacher.modifying.success"); args[3] = teacherId; } else { if (FutureTeacher.existsTeacherWithNumber(number)) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.already.exists")); } successMessage = resourceBundle.getString("message.futureTeacher.creating.success"); args[3] = userView.getPerson().getUsername(); } try { ServiceUtils.executeService( "ManageFutureTeachers", args); } catch (Exception e) { successMessage = null; return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.creating")); } return OUTCOME_SUCCESS; } // Action that is deployed when the user clicks OK on the associateTeacher page public String submitAssociatedTeacher() { if (associatedTeacherId == -1) { associatedTeacherId = null; } Object args[] = { associatedTeacherId, teacherId, userView.getPerson().getName() }; try { ServiceUtils.executeService( "AssociateTeacherToFutureTeacher", args); if (associatedTeacherId != null) { successMessage = resourceBundle.getString("message.futureTeacher.associate.success"); } else { successMessage = resourceBundle.getString("message.futureTeacher.desassociate.success"); } return OUTCOME_SUCCESS; } catch (Exception e) { return prepareErrorPageForEdition(resourceBundle.getString("error.futureTeacher.associating")); } } //Action that is deployed when user clicks on a link public void prepareFutureTeacher(final ActionEvent event) { teacherId = getIntegerParameterByName(event, "teacherID"); final FutureTeacher ft = rootDomainObject.readFutureTeacherByOID(teacherId); teacherName = ft.getName(); teacherDepartmentId = ft.getDepartment().getIdInternal(); teacherNumber = ft.getTeacherNumber().toString(); } //Action that is deployed when user clicks on associate link public void prepareFutureTeacherForAssociation(final ActionEvent event) { prepareFutureTeacher(event); getAssociatedTeacherId(); getTeachersFromDepartment(); } //------------------------------------------------------------------------------------------ private void addToTeacherList(final List teachersList) { if (teachersList != null) { for (final FutureTeacher t : teachersList) { if (!teachers.contains(t)) { teachers.add(t); } } } } private Integer getIntegerParameterByName(final ActionEvent event, final String tag) { final UIParameter parameter = (UIParameter) event.getComponent().findComponent(tag); return Integer.parseInt(parameter.getValue().toString()); } private String prepareErrorPageForEdition(final String error) { errorMessage = error; getAllDepartments(); return OUTCOME_ERROR; } private String prepareErrorPageForDeletion(final String error) { errorMessage = error; return OUTCOME_ERROR; } private void getAllDepartments() { departmentList = new ArrayList(); departmentList.add(new SelectItem(-1, resourceBundle.getString("label.select.one"))); for (final Department department : rootDomainObject.getDepartments()) { departmentList.add(new SelectItem(department.getIdInternal(), department.getRealName() + " (" + department.getAcronym() + ")")); } } private void getTeachersFromDepartment() { final List items = new ArrayList(); items.add(new SelectItem(-1, resourceBundle.getString("label.none"))); if (teacherDepartmentId != null) { final Department department = rootDomainObject.readDepartmentByOID(teacherDepartmentId); final Set auxSet = new HashSet(); auxSet.addAll(Teacher.readExternalTeachersByDepartment(department, true)); auxSet.addAll(department.getAllCurrentTeachers()); final List teachers = new ArrayList(auxSet); Collections.sort(teachers, Teacher.TEACHER_COMPARATOR_BY_NAME); for (final Teacher teacher : teachers) { items.add(new SelectItem(teacher.getIdInternal(), teacher.getTeacherNumber() + " - " + teacher.getPerson().getName())); } } teachersToAssociate = new UISelectItems(); teachersToAssociate.setValue(items); } private void resetFields() { teacherId = null; errorMessage = null; successMessage = null; teachers = null; departmentList = null; teacherNumber = null; teacherName = null; teacherDepartmentId = null; associatedTeacherId = null; associatedTeacherName = null; teachersToAssociate = null; isEdit = null; selectAllTeachers = null; } //------------------------------------------------------------------------------------------ public List getTeachers() { if (teachers == null) { listFutureTeachers(); } return teachers; } public void setTeachers(final List teachers) { this.teachers = teachers; } @Override public String getErrorMessage() { return errorMessage; } @Override public void setErrorMessage(final String errorMessage) { this.errorMessage = errorMessage; } public List getDepartmentList() { if (departmentList == null) { getAllDepartments(); } return departmentList; } public void setDepartmentList(List departmentList) { this.departmentList = departmentList; } public void setTeachersToAssociate(UISelectItems teachersToAssociate) { this.teachersToAssociate = teachersToAssociate; } public UISelectItems getTeachersToAssociate() { if (teachersToAssociate == null) { getTeachersFromDepartment(); } return teachersToAssociate; } public String getTeacherName() { if (teacherName == null && teacherId != null) { teacherName = rootDomainObject.readFutureTeacherByOID(teacherId) != null ? rootDomainObject.readFutureTeacherByOID( teacherId).getName() : null; } else if (teacherName == null) { teacherName = getAndHoldStringParameter("futureTeacherList:teacherName"); } return teacherName; } public void setTeacherName(final String teacherName) { this.teacherName = teacherName; } public Integer getTeacherDepartmentId() { if (teacherDepartmentId == null) { teacherDepartmentId = getAndHoldIntegerParameter("futureTeacherList:teacherDepartmentId"); } return teacherDepartmentId; } public void setTeacherDepartmentId(final Integer teacherDepartment) { this.teacherDepartmentId = teacherDepartment; } public Integer getTeacherId() { return teacherId; } public void setTeacherId(final Integer teacherId) { this.teacherId = teacherId; } public String getTeacherNumber() { if (teacherNumber == null && teacherId != null) { teacherNumber = rootDomainObject.readFutureTeacherByOID(teacherId) != null ? rootDomainObject.readFutureTeacherByOID( teacherId).getTeacherNumber().toString() : null; } else if (teacherNumber == null) { teacherNumber = getAndHoldStringParameter("futureTeacherList:teacherNumber"); if(teacherNumber == null){ teacherNumber = String.valueOf(FutureTeacher.getNextTeacherNumber()); } } return teacherNumber; } public void setTeacherNumber(final String teacherNumber) { this.teacherNumber = teacherNumber; } public String getTeacherNumberToSearch() { return teacherNumberToSearch; } public void setTeacherNumberToSearch(String teacherNumberToSearch) { this.teacherNumberToSearch = teacherNumberToSearch; } public String getSuccessMessage() { return successMessage; } public void setSuccessMessage(final String successMessage) { this.successMessage = successMessage; } public Integer getAssociatedTeacherId() { if (associatedTeacherId == null && getTeacherId() != null) { final FutureTeacher ft = rootDomainObject.readFutureTeacherByOID(teacherId); if (ft.hasTeacher()) { associatedTeacherId = ft.getTeacher().getIdInternal(); } } return associatedTeacherId; } public void setAssociatedTeacherId(final Integer associatedTeacherId) { this.associatedTeacherId = associatedTeacherId; } public String getAssociatedTeacherName() { if (associatedTeacherName == null && associatedTeacherId != null) { associatedTeacherName = rootDomainObject.readTeacherByOID(associatedTeacherId).getPerson().getName(); } else if (associatedTeacherName == null && teacherId != null) { associatedTeacherName = rootDomainObject.readFutureTeacherByOID(teacherId).getTeacher() != null ? rootDomainObject .readFutureTeacherByOID(teacherId).getTeacher().getPerson().getName() : null; } return associatedTeacherName; } public void setAssociatedTeacherName(final String associatedTeacherName) { this.associatedTeacherName = associatedTeacherName; } public Boolean getIsEdit() { if (isEdit == null) { isEdit = false; } return isEdit; } public void setIsEdit(final Boolean bool) { isEdit = bool; } public String getDepartmentName() { if (teacherDepartmentId != null) { final Department department = rootDomainObject.readDepartmentByOID(teacherDepartmentId); return department.getRealName() + " (" + department.getAcronym() + ")"; } return ""; } public Boolean getSelectAllTeachers() { if (selectAllTeachers == null) { selectAllTeachers = getAndHoldBooleanParameter("futureTeacherList:selectAllTeachers"); } return selectAllTeachers; } public void setSelectAllTeachers(final Boolean selectAllTeachers) { this.selectAllTeachers = selectAllTeachers; } }