package net.sourceforge.fenixedu.domain.organizationalStructure; import java.util.ArrayList; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.DomainObject; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.comparators.ComparatorChain; import org.apache.commons.collections.comparators.NullComparator; import org.joda.time.YearMonthDay; import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString; public class Function extends Function_Base { public static final Comparator COMPARATOR_BY_ORDER = new Comparator() { private ComparatorChain chain = null; public int compare(Function one, Function other) { if (this.chain == null) { chain = new ComparatorChain(); chain.addComparator(new BeanComparator("functionOrder", new NullComparator())); chain.addComparator(new BeanComparator("functionType", new NullComparator())); chain.addComparator(new BeanComparator("name")); chain.addComparator(DomainObject.COMPARATOR_BY_ID); } return chain.compare(one, other); } }; public Function(MultiLanguageString functionName, YearMonthDay beginDate, YearMonthDay endDate, FunctionType type, Unit unit) { super(); edit(functionName, beginDate, endDate, type); setUnit(unit); setType(AccountabilityTypeEnum.MANAGEMENT_FUNCTION); } public Function(MultiLanguageString functionName, YearMonthDay beginDate, YearMonthDay endDate, FunctionType type, Unit unit, AccountabilityTypeEnum accountabilityTypeEnum) { super(); edit(functionName, beginDate, endDate, type); setUnit(unit); setType(accountabilityTypeEnum); } public void edit(MultiLanguageString functionName, YearMonthDay beginDate, YearMonthDay endDate, FunctionType type) { setTypeName(functionName); setFunctionType(type); setBeginDateYearMonthDay(beginDate); setEndDateYearMonthDay(endDate); } @Override public void setUnit(Unit unit) { if (unit == null) { throw new DomainException("error.function.no.unit"); } super.setUnit(unit); } public boolean isActive(YearMonthDay currentDate) { return belongsToPeriod(currentDate, currentDate); } public boolean belongsToPeriod(YearMonthDay beginDate, YearMonthDay endDate) { return ((endDate == null || !getBeginDateYearMonthDay().isAfter(endDate)) && (getEndDateYearMonthDay() == null || !getEndDateYearMonthDay() .isBefore(beginDate))); } public void delete() { if (!canBeDeleted()) { throw new DomainException("error.delete.function"); } removeParentInherentFunction(); super.setUnit(null); removeRootDomainObject(); deleteDomainObject(); } private boolean canBeDeleted() { return !hasAnyAccountabilities() && !hasAnyInherentFunctions(); } public List getPersonFunctions() { List personFunctions = new ArrayList(); for (Accountability accountability : getAccountabilities()) { if (accountability.isPersonFunction()) { personFunctions.add((PersonFunction) accountability); } } return personFunctions; } public List getActivePersonFunctions() { List personFunctions = new ArrayList(); YearMonthDay currentDate = new YearMonthDay(); for (Accountability accountability : getAccountabilities()) { if (accountability.isPersonFunction() && accountability.isActive(currentDate)) { personFunctions.add((PersonFunction) accountability); } } return personFunctions; } public List getActivePersonFunctionsByPerson(final Person person) { List personFunctions = new ArrayList(); YearMonthDay currentDate = new YearMonthDay(); for (Accountability accountability : getAccountabilities()) { if (accountability.isPersonFunction() && accountability.isActive(currentDate)) { PersonFunction personFunction = (PersonFunction) accountability; if (personFunction.getPerson().equals(person)) { personFunctions.add(personFunction); } } } return personFunctions; } public boolean isInherentFunction() { return (this.getParentInherentFunction() != null); } public void addParentInherentFunction(Function parentInherentFunction) { if (parentInherentFunction.equals(this)) { throw new DomainException("error.function.parentInherentFunction.equals.function"); } removeParentInherentFunction(); setParentInherentFunction(parentInherentFunction); } @jvstm.cps.ConsistencyPredicate protected boolean checkDateInterval() { final YearMonthDay start = getBeginDateYearMonthDay(); final YearMonthDay end = getEndDateYearMonthDay(); return start != null && (end == null || !start.isAfter(end)); } public static Function createVirtualFunction(Unit unit, MultiLanguageString name) { return new Function(name, new YearMonthDay(), null, FunctionType.VIRTUAL, unit); } public boolean isVirtual() { FunctionType type = getFunctionType(); return type != null && type.equals(FunctionType.VIRTUAL); } @Override public boolean isFunction() { return true; } public static Set readAllActiveFunctionsByType(FunctionType functionType) { Set result = new HashSet(); YearMonthDay currentDate = new YearMonthDay(); List accountabilityTypes = RootDomainObject.getInstance().getAccountabilityTypes(); for (AccountabilityType accountabilityType : accountabilityTypes) { if (accountabilityType.isFunction() && ((Function) accountabilityType).getFunctionType() != null && ((Function) accountabilityType).getFunctionType().equals(functionType) && ((Function) accountabilityType).isActive(currentDate)) { result.add((Function) accountabilityType); } } return result; } public static Set readAllFunctionsByType(FunctionType functionType) { Set result = new HashSet(); List accountabilityTypes = RootDomainObject.getInstance().getAccountabilityTypes(); for (AccountabilityType accountabilityType : accountabilityTypes) { if (accountabilityType.isFunction() && ((Function) accountabilityType).getFunctionType() != null && ((Function) accountabilityType).getFunctionType().equals(functionType)) { result.add((Function) accountabilityType); } } return result; } public List getActivePersonFunctionsStartingIn(ExecutionYear executionYear) { List personFunctions = new ArrayList(); YearMonthDay currentDate = new YearMonthDay(); for (Accountability accountability : getAccountabilities()) { if (accountability.isPersonFunction() && accountability.isActive(currentDate) && executionYear.containsDate(accountability.getBeginDate().toDateTimeAtMidnight())) { personFunctions.add((PersonFunction) accountability); } } return personFunctions; } public boolean isOfFunctionType(FunctionType functionType) { return getFunctionType().equals(functionType); } }