package net.sourceforge.fenixedu.presentationTier.backBeans.departmentAdmOffice; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import javax.faces.event.ActionEvent; import javax.faces.model.SelectItem; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.departmentAdmOffice.UpdateDepartmentsCompetenceCourseManagementGroup; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.domain.Department; import net.sourceforge.fenixedu.domain.Employee; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.accessControl.Group; import net.sourceforge.fenixedu.presentationTier.backBeans.base.FenixBackingBean; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.comparators.ComparatorChain; import com.linkare.commons.metainfo.Linkare; public class ManagementGroupsBackingBean extends FenixBackingBean { private List employees = getEmployees(); private Integer[] selectedPersonsIDsToAdd; private Integer[] selectedPersonsIDsToRemove; public Department getDepartment() { return (getUserView().getPerson().getEmployee() != null) ? getUserView().getPerson().getEmployee() .getCurrentDepartmentWorkingPlace() : null; } public List getEmployees() { List result = (getDepartment() != null) ? new ArrayList(getDepartment() .getAllCurrentActiveWorkingEmployees()) : null; if (result != null) { ComparatorChain chainComparator = new ComparatorChain(); chainComparator.addComparator(new BeanComparator("person.name"), false); chainComparator.addComparator(new BeanComparator("employeeNumber"), false); Collections.sort(result, chainComparator); } return result; } public List getDepartmentEmployeesSelectItems() { Group competenceCoursesManagementGroup = getDepartment().getCompetenceCourseMembersGroup(); List result = new ArrayList(employees.size()); for (Employee departmentEmployee : employees) { Person person = departmentEmployee.getPerson(); if (competenceCoursesManagementGroup == null || !competenceCoursesManagementGroup.isMember(person)) { result.add(new SelectItem(person.getIdInternal(), person.getName() + " (" + person.getUsername() + ")")); } } return result; } public int getDepartmentEmployeesSize() { return employees.size(); } public List getSelectedDepartmentEmployeesSelectItems() throws FenixFilterException, FenixServiceException { List result = new ArrayList(); Group competenceCoursesManagementGroup = getDepartment().getCompetenceCourseMembersGroup(); if (competenceCoursesManagementGroup != null) { for (Person person : competenceCoursesManagementGroup.getElements()) { result.add(new SelectItem(person.getIdInternal(), person.getName() + " (" + person.getUsername() + ")")); } } return result; } @Linkare(author = "Paulo Zenida", comments = "Implemented defensive copy to prevent the violation of a best practice") public void setSelectedPersonsIDsToAdd(final Integer[] selectedPersonsIDs) { this.selectedPersonsIDsToAdd = selectedPersonsIDs == null ? null : Arrays.copyOf(selectedPersonsIDs, selectedPersonsIDs.length); } public Integer[] getSelectedPersonsIDsToAdd() { return selectedPersonsIDsToAdd; } @Linkare(author = "Paulo Zenida", comments = "Implemented defensive copy to prevent the violation of a best practice") public void setSelectedPersonsIDsToRemove(final Integer[] selectedPersonsIDsToRemove) { this.selectedPersonsIDsToRemove = selectedPersonsIDsToRemove == null ? null : Arrays.copyOf(selectedPersonsIDsToRemove, selectedPersonsIDsToRemove.length); } public Integer[] getSelectedPersonsIDsToRemove() { return selectedPersonsIDsToRemove; } public void addMembers(ActionEvent event) throws FenixFilterException, FenixServiceException { if (selectedPersonsIDsToAdd != null) { UpdateDepartmentsCompetenceCourseManagementGroup.run(getDepartment(), selectedPersonsIDsToAdd, null); } // avoid preset check-boxes after action selectedPersonsIDsToAdd = null; selectedPersonsIDsToRemove = null; } public void removeMembers(ActionEvent event) throws FenixFilterException, FenixServiceException { if (selectedPersonsIDsToRemove != null) { UpdateDepartmentsCompetenceCourseManagementGroup.run(getDepartment(), null, selectedPersonsIDsToRemove); } // avoid preset check-boxes after action selectedPersonsIDsToAdd = null; selectedPersonsIDsToRemove = null; } }