@Atomic public static void run(Department department, String[] add, String[] remove) { check(RolePredicates.DEPARTMENT_ADMINISTRATIVE_OFFICE_PREDICATE); List<Person> toAdd = materializePersons(add); List<Person> toRemove = materializePersons(remove); List<Person> finalList = new ArrayList<Person>(); Role bolonhaRole = Role.getRoleByRoleType(RoleType.BOLONHA_MANAGER); Group group = department.getCompetenceCourseMembersGroup(); if (group == null) { group = new FixedSetGroup(); } for (Person person : group.getElements()) { if (!toRemove.contains(person)) { finalList.add(person); addBolonhaRole(person, bolonhaRole); } else if (person.hasRole(RoleType.BOLONHA_MANAGER) && !belongsToOtherGroupsWithSameRole(department, person)) { person.removeRoleByType(RoleType.BOLONHA_MANAGER); } } for (Person person : toAdd) { if (!finalList.contains(person)) { finalList.add(person); addBolonhaRole(person, bolonhaRole); } } department.setCompetenceCourseMembersGroup(new FixedSetGroup(finalList)); }
private static boolean belongsToOtherGroupsWithSameRole( Department departmentWhoAsks, Person person) { Collection<Department> departments = Bennu.getInstance().getDepartmentsSet(); for (Department department : departments) { if (department != departmentWhoAsks) { Group group = department.getCompetenceCourseMembersGroup(); if (group != null && group.isMember(person)) { return true; } } } for (Degree degree : Degree.readBolonhaDegrees()) { for (DegreeCurricularPlan dcp : degree.getDegreeCurricularPlans()) { Group group = dcp.getCurricularPlanMembersGroup(); if (group != null && group.isMember(person)) { return true; } } } return false; }