package net.sourceforge.fenixedu.domain.accounting.events; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.accounting.AccountingTransaction; import net.sourceforge.fenixedu.domain.accounting.Event; import net.sourceforge.fenixedu.domain.accounting.EventState; import net.sourceforge.fenixedu.domain.accounting.EventType; import net.sourceforge.fenixedu.domain.accounting.PostingRule; import net.sourceforge.fenixedu.domain.accounting.ServiceAgreementTemplate; import net.sourceforge.fenixedu.domain.administrativeOffice.AdministrativeOffice; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import org.joda.time.DateTime; import org.joda.time.YearMonthDay; import pt.ist.fenixWebFramework.security.accessControl.Checked; public abstract class AnnualEvent extends AnnualEvent_Base { protected AnnualEvent() { super(); } protected void init(EventType eventType, Person person, ExecutionYear executionYear) { init(null, eventType, person, executionYear); } protected void init(AdministrativeOffice administrativeOffice, EventType eventType, Person person, ExecutionYear executionYear) { super.init(administrativeOffice, eventType, person); checkParameters(executionYear); super.setExecutionYear(executionYear); } private void checkParameters(ExecutionYear executionYear) { if (executionYear == null) { throw new DomainException("error.accounting.events.AnnualEvent.executionYear.cannot.be.null"); } } public DateTime getStartDate() { return getExecutionYear().getBeginDateYearMonthDay().toDateTimeAtMidnight(); } public DateTime getEndDate() { return getExecutionYear().getEndDateYearMonthDay().toDateTimeAtMidnight(); } @Override public void setExecutionYear(ExecutionYear executionYear) { throw new DomainException("error.accounting.events.AnnualEvent.cannot.modify.executionYear"); } @Override public PostingRule getPostingRule() { return getServiceAgreementTemplate().findPostingRuleBy(getEventType(), getStartDate(), getEndDate()); } public boolean isFor(final ExecutionYear executionYear) { return super.getExecutionYear() == executionYear; } @Checked("RolePredicates.MANAGER_PREDICATE") @Override protected void disconnect() { super.setExecutionYear(null); super.disconnect(); } @Override public boolean isAnnual() { return true; } private static List readBy(final ExecutionYear executionYear, final EventState eventState) { final List result = new ArrayList(); for (final Event event : executionYear.getAnnualEvents()) { if (event.isInState(eventState)) { result.add((AnnualEvent) event); } } return result; } public static List readNotPayedBy(final ExecutionYear executionYear) { return readBy(executionYear, EventState.OPEN); } static public Set readPaymentsFor(final Class eventClass, final YearMonthDay startDate, final YearMonthDay endDate) { final Set result = new HashSet(); for (final ExecutionYear executionYear : RootDomainObject.getInstance().getExecutionYears()) { for (final AnnualEvent each : executionYear.getAnnualEvents()) { if (eventClass.equals(each.getClass()) && !each.isCancelled()) { for (final AccountingTransaction transaction : each.getNonAdjustingTransactions()) { if (transaction.isInsidePeriod(startDate, endDate)) { result.add(transaction); } } } } } return result; } abstract protected ServiceAgreementTemplate getServiceAgreementTemplate(); }