/**
* Copyright © 2002 Instituto Superior Técnico
*
* This file is part of FenixEdu Academic.
*
* FenixEdu Academic is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FenixEdu Academic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FenixEdu Academic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Department.java
*
* Created on 6 de Novembro de 2002, 15:57
*/
/**
* @author Nuno Nunes & Joana Mota
*/
package org.fenixedu.academic.domain;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.lang.StringUtils;
import org.fenixedu.academic.domain.degree.DegreeType;
import org.fenixedu.academic.domain.degreeStructure.CycleType;
import org.fenixedu.academic.domain.exceptions.DomainException;
import org.fenixedu.academic.domain.messaging.DepartmentForum;
import org.fenixedu.academic.domain.organizationalStructure.CompetenceCourseGroupUnit;
import org.fenixedu.academic.domain.organizationalStructure.DepartmentUnit;
import org.fenixedu.academic.domain.organizationalStructure.ScientificAreaUnit;
import org.fenixedu.academic.domain.organizationalStructure.Unit;
import org.fenixedu.academic.domain.time.calendarStructure.AcademicInterval;
import org.fenixedu.academic.predicate.AccessControl;
import org.fenixedu.bennu.core.domain.Bennu;
import org.fenixedu.bennu.core.domain.User;
import org.fenixedu.bennu.core.groups.Group;
import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString;
public class Department extends Department_Base {
public static final Comparator<Department> COMPARATOR_BY_NAME = new ComparatorChain();
static {
((ComparatorChain) COMPARATOR_BY_NAME).addComparator(new BeanComparator("name", Collator.getInstance()));
((ComparatorChain) COMPARATOR_BY_NAME).addComparator(DomainObjectUtil.COMPARATOR_BY_ID);
}
public Department() {
super();
setRootDomainObject(Bennu.getInstance());
}
public List<Teacher> getAllCurrentTeachers() {
return getAllTeachers(ExecutionSemester.readActualExecutionSemester());
}
public List<Teacher> getAllTeachers(AcademicInterval interval) {
return getTeacherAuthorizationStream().filter(a -> a.getExecutionSemester().getAcademicInterval().overlaps(interval))
.map(a -> a.getTeacher()).collect(Collectors.toList());
}
public List<Teacher> getAllTeachers(ExecutionSemester semester) {
return getTeacherAuthorizationStream().filter(a -> a.getExecutionSemester().equals(semester)).map(a -> a.getTeacher())
.collect(Collectors.toList());
}
public List<Teacher> getAllTeachers(ExecutionYear executionYear) {
return getTeacherAuthorizationStream().filter(a -> a.getExecutionSemester().getExecutionYear().equals(executionYear))
.map(a -> a.getTeacher()).collect(Collectors.toList());
}
public List<Teacher> getAllTeachers() {
return getTeacherAuthorizationStream().map(a -> a.getTeacher()).collect(Collectors.toList());
}
public Stream<TeacherAuthorization> getRevokedTeacherAuthorizationStream() {
return getRevokedTeacherAuthorizationSet().stream();
}
public Stream<TeacherAuthorization> getTeacherAuthorizationStream() {
return getTeacherAuthorizationSet().stream();
}
public Set<DegreeType> getDegreeTypes() {
Set<DegreeType> degreeTypes = new TreeSet<DegreeType>();
for (Degree degree : getDegreesSet()) {
degreeTypes.add(degree.getDegreeType());
}
return degreeTypes;
}
public Set<CycleType> getCycleTypes() {
TreeSet<CycleType> cycles = new TreeSet<CycleType>();
for (DegreeType degreeType : getDegreeTypes()) {
cycles.addAll(degreeType.getCycleTypes());
}
return cycles;
}
@Deprecated
/**
*
* This direct association between CompetenceCourses and Departments should no longer be used.
* Instead, use the DepartmentUnit to get the CompetenceCourses.
*
*/
public List<CompetenceCourse> getCompetenceCoursesByExecutionYear(ExecutionYear executionYear) {
Collection<CompetenceCourse> competenceCourses = this.getCompetenceCoursesSet();
List<CompetenceCourse> competenceCoursesByExecutionYear = new ArrayList<CompetenceCourse>();
for (CompetenceCourse competenceCourse : competenceCourses) {
if (competenceCourse.hasActiveScopesInExecutionYear(executionYear)) {
competenceCoursesByExecutionYear.add(competenceCourse);
}
}
return competenceCoursesByExecutionYear;
}
@Deprecated
/**
*
* This direct association between CompetenceCourses and Departments should no longer be used.
* Instead, use addAllBolonhaCompetenceCourses()
*
*/
public void addAllCompetenceCoursesByExecutionPeriod(final Collection<CompetenceCourse> competenceCourses,
final ExecutionSemester executionSemester) {
for (CompetenceCourse competenceCourse : getCompetenceCoursesSet()) {
if (competenceCourse.hasActiveScopesInExecutionPeriod(executionSemester)) {
competenceCourses.add(competenceCourse);
}
}
}
@Deprecated
/**
*
* This direct association between CompetenceCourses and Departments should no longer be used.
* Instead, use the DepartmentUnit to get the CompetenceCourses.
*
*/
public Set<ExecutionCourse> getAllExecutionCoursesByExecutionPeriod(final ExecutionSemester executionSemester) {
Set<ExecutionCourse> executionCourses = new HashSet<ExecutionCourse>();
for (CompetenceCourse competenceCourse : getCompetenceCoursesSet()) {
competenceCourse.getExecutionCoursesByExecutionPeriod(executionSemester, executionCourses);
}
return executionCourses;
}
public String getAcronym() {
final int begin = this.getRealName().indexOf("(");
final int end = this.getRealName().indexOf(")");
return this.getRealName().substring(begin + 1, end);
}
public List<CompetenceCourse> getBolonhaCompetenceCourses() {
DepartmentUnit departmentUnit = this.getDepartmentUnit();
List<CompetenceCourse> courses = new ArrayList<CompetenceCourse>();
for (ScientificAreaUnit areaUnit : departmentUnit.getScientificAreaUnits()) {
for (CompetenceCourseGroupUnit competenceCourseGroupUnit : areaUnit.getCompetenceCourseGroupUnits()) {
courses.addAll(competenceCourseGroupUnit.getCompetenceCourses());
}
}
return courses;
}
public void addAllBolonhaCompetenceCourses(final Collection<CompetenceCourse> competenceCourses,
final ExecutionSemester period) {
for (CompetenceCourse course : getBolonhaCompetenceCourses()) {
if (!course.getCurricularCoursesWithActiveScopesInExecutionPeriod(period).isEmpty()) {
competenceCourses.add(course);
}
}
}
// -------------------------------------------------------------
// read static methods
// -------------------------------------------------------------
public static Department readByName(final String departmentName) {
for (final Department department : Bennu.getInstance().getDepartmentsSet()) {
if (department.getName().equals(departmentName)) {
return department;
}
}
return null;
}
public void delete() {
if (!getTeacherGroupSet().isEmpty()) {
throw new DomainException("error.department.cannotDeleteDepartmentUsedInAccessControl");
}
setDepartmentUnit(null);
setRootDomainObject(null);
deleteDomainObject();
}
/**
* Joins the portuguese and english version of the department's name in a
* MultiLanguageString for an easier handling of the name in a
* internacionalized context.
*
* @return a MultiLanguageString with the portuguese and english versions of
* the department's name
*/
public MultiLanguageString getNameI18n() {
return new MultiLanguageString().with(MultiLanguageString.pt, getRealName())
.with(MultiLanguageString.en, getRealNameEn());
}
public Integer getCompetenceCourseInformationChangeRequestsCount() {
int count = 0;
for (CompetenceCourse course : getDepartmentUnit().getCompetenceCourses()) {
count += course.getCompetenceCourseInformationChangeRequestsSet().size();
}
return count;
}
public Integer getDraftCompetenceCourseInformationChangeRequestsCount() {
int count = 0;
for (CompetenceCourse course : getDepartmentUnit().getCompetenceCourses()) {
count += course.getDraftCompetenceCourseInformationChangeRequestsCount();
}
return count;
}
public Group getCompetenceCourseMembersGroup() {
return getMembersGroup().toGroup();
}
public void setCompetenceCourseMembersGroup(Group group) {
setMembersGroup(group.toPersistentGroup());
}
public boolean isUserMemberOfCompetenceCourseMembersGroup(User user) {
return getCompetenceCourseMembersGroup().isMember(user);
}
public boolean isUserMemberOfCompetenceCourseMembersGroup(Person person) {
return getCompetenceCourseMembersGroup().isMember(person.getUser());
}
public boolean isCurrentUserMemberOfCompetenceCourseMembersGroup() {
return isUserMemberOfCompetenceCourseMembersGroup(AccessControl.getPerson());
}
public DepartmentForum getDepartmentForum() {
return getForum();
}
public static Department find(final String departmentCode) {
for (final Department department : Bennu.getInstance().getDepartmentsSet()) {
if (department.getAcronym().equals(departmentCode)) {
return department;
}
}
if (StringUtils.isNumeric(departmentCode)) {
final Unit unit = Unit.readByCostCenterCode(new Integer(departmentCode));
if (unit != null) {
final org.fenixedu.academic.domain.organizationalStructure.DepartmentUnit departmentUnit =
unit.getDepartmentUnit();
return departmentUnit == null ? null : departmentUnit.getDepartment();
}
}
return null;
}
public static List<Department> readActiveDepartments() {
final List<Department> departments = new ArrayList<Department>();
for (final Department department : Bennu.getInstance().getDepartmentsSet()) {
if (department.getActive()) {
departments.add(department);
}
}
Collections.sort(departments, Department.COMPARATOR_BY_NAME);
return departments;
}
}