package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.organizationalStructure.AccountabilityTypeEnum; import net.sourceforge.fenixedu.domain.organizationalStructure.SchoolUnit; import net.sourceforge.fenixedu.domain.organizationalStructure.Unit; import net.sourceforge.fenixedu.domain.organizationalStructure.UnitUtils; import net.sourceforge.fenixedu.domain.organizationalStructure.UniversityUnit; import org.apache.commons.lang.StringUtils; import dml.runtime.RelationAdapter; public class ExternalCurricularCourse extends ExternalCurricularCourse_Base { static { ExternalCurricularCourseUnit.addListener(new RelationAdapter() { @Override public void beforeAdd(ExternalCurricularCourse externalCurricularCourse, Unit unit) { if (unit != null) { if (!unit.isUniversityUnit() && !unit.isSchoolUnit() && !unit.isDepartmentUnit()) { throw new DomainException("error.extraCurricularCourse.invalid.unit.type"); } } } }); } public ExternalCurricularCourse(final Unit unit, final String name, final String code) { super(); if (unit == null) { throw new DomainException("error.externalCurricularCourse.unit.cannot.be.null"); } if (StringUtils.isEmpty(name)) { throw new DomainException("error.externalCurricularCourse.name.cannot.be.empty"); } checkForExternalCurricularCourseWithSameNameAndCode(unit, name, code); setRootDomainObject(RootDomainObject.getInstance()); setUnit(unit); setName(name); setCode(code); } public void edit(final String name, final String code) { if (StringUtils.isEmpty(name)) { throw new DomainException("error.externalCurricularCourse.name.cannot.be.empty"); } checkForExternalCurricularCourseWithSameNameAndCode(getUnit(), name, code); setName(name); setCode(code); } private void checkForExternalCurricularCourseWithSameNameAndCode(final Unit unit, final String name, final String code) { final String nameToSearch = name.toLowerCase(); for (final ExternalCurricularCourse externalCurricularCourse : unit.getExternalCurricularCourses()) { if (externalCurricularCourse.getName().toLowerCase().equals(nameToSearch)) { if ((externalCurricularCourse.getCode() != null && externalCurricularCourse.getCode().equals(code)) || externalCurricularCourse.getCode() == null && code == null) { throw new DomainException( "error.externalCurricularCourse.parent.unit.already.has.externalCurricularCourse.with.same.type"); } } } } public void delete() { if (canBeDeleted()) { removeRootDomainObject(); removeUnit(); super.deleteDomainObject(); } else { throw new DomainException("error.external.enrolment.cannot.be.deleted"); } } private boolean canBeDeleted() { return !hasAnyExternalEnrolments(); } public String getFullPathName() { final List validAccountabilityTypes = Arrays.asList(new AccountabilityTypeEnum[] { AccountabilityTypeEnum.GEOGRAPHIC, AccountabilityTypeEnum.ORGANIZATIONAL_STRUCTURE, AccountabilityTypeEnum.ACADEMIC_STRUCTURE }); return UnitUtils.getUnitFullPathName(getUnit(), validAccountabilityTypes).toString() + " > " + getName(); } final public Unit getAcademicUnit() { final List validAccountabilityTypes = Arrays.asList(new AccountabilityTypeEnum[] { AccountabilityTypeEnum.GEOGRAPHIC, AccountabilityTypeEnum.ORGANIZATIONAL_STRUCTURE, AccountabilityTypeEnum.ACADEMIC_STRUCTURE }); UniversityUnit universityUnit = null; SchoolUnit schoolUnit = null; for (final Unit unit : UnitUtils.getUnitFullPath(getUnit(), validAccountabilityTypes)) { if (unit.isUniversityUnit()) { universityUnit = (UniversityUnit) unit; } else if (unit.isSchoolUnit()) { schoolUnit = (SchoolUnit) unit; } } return (schoolUnit != null) ? schoolUnit : (universityUnit != null) ? universityUnit : null; } static public ExternalCurricularCourse readExternalCurricularCourse(Unit unit, String name, String code) { for (final ExternalCurricularCourse externalCurricularCourse : unit.getExternalCurricularCourses()) { if (externalCurricularCourse.getCode().equals(code) && externalCurricularCourse.getName().equals(name)) { return externalCurricularCourse; } } return null; } static public ExternalCurricularCourse readExternalCurricularCourse(Unit unit, String code) { List result = new ArrayList(); for (final ExternalCurricularCourse externalCurricularCourse : unit.getExternalCurricularCourses()) { if (StringUtils.isEmpty(externalCurricularCourse.getCode()) && StringUtils.isEmpty(code) || externalCurricularCourse.getCode().equals(code)) { result.add(externalCurricularCourse); } } if (result.size() == 1) { return result.iterator().next(); } else if (result.size() == 0) { return null; } throw new DomainException("error.externalCurricularCourse.manyFoundWithSameCode"); } static public List readExternalCurricularCoursesByCode(String code) { List result = new ArrayList(); for (final ExternalCurricularCourse externalCurricularCourse : RootDomainObject.getInstance() .getExternalCurricularCourses()) { if (StringUtils.isEmpty(externalCurricularCourse.getCode()) && StringUtils.isEmpty(code) || externalCurricularCourse.getCode().equals(code)) { result.add(externalCurricularCourse); } } return result; } static public List readByName(final String name) { if (name == null) { return Collections.emptyList(); } final String nameToMatch = name.replaceAll("%", ".*").toLowerCase(); final List result = new ArrayList(); for (final ExternalCurricularCourse externalCurricularCourse : RootDomainObject.getInstance() .getExternalCurricularCoursesSet()) { if (externalCurricularCourse.getName().toLowerCase().matches(nameToMatch)) { result.add(externalCurricularCourse); } } return result; } }