package net.sourceforge.fenixedu.domain.serviceRequests.documentRequests; import java.util.Set; import com.linkare.commons.metainfo.Linkare; import net.sourceforge.fenixedu.dataTransferObject.serviceRequests.DocumentRequestCreateBean; import net.sourceforge.fenixedu.domain.ExecutionYear; import net.sourceforge.fenixedu.domain.accounting.EventType; import net.sourceforge.fenixedu.domain.exceptions.DomainException; public class SchoolRegistrationDeclarationRequest extends SchoolRegistrationDeclarationRequest_Base { private static final int MAX_FREE_DECLARATIONS_PER_EXECUTION_YEAR = 4; protected SchoolRegistrationDeclarationRequest() { super(); } public SchoolRegistrationDeclarationRequest(final DocumentRequestCreateBean bean) { this(); checkRulesToCreate(bean); bean.setExecutionYear(getRequestedExecutionYear(bean)); super.init(bean); } private ExecutionYear getRequestedExecutionYear(DocumentRequestCreateBean bean) { return bean.getRegistration().hasIndividualCandidacyFor(bean.getExecutionYear().getNextExecutionYear()) ? bean .getExecutionYear().getNextExecutionYear() : bean.getExecutionYear(); } private void checkRulesToCreate(final DocumentRequestCreateBean bean) { final ExecutionYear executionYear = bean.getExecutionYear(); if (!bean.getRegistration().isRegistered(executionYear) && !bean.getRegistration().hasIndividualCandidacyFor(executionYear.getNextExecutionYear())) { throw new DomainException( "SchoolRegistrationDeclarationRequest.registration.not.in.registered.state.in.current.executionYear"); } } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final DocumentRequestType getDocumentRequestType() { return DocumentRequestType.SCHOOL_REGISTRATION_DECLARATION; } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final String getDocumentTemplateKey() { return getClass().getName(); } @Linkare(author = "Paulo Zenida", comments = "Changed the modifiers order according to the JLS suggestions") @Override public final EventType getEventType() { return EventType.SCHOOL_REGISTRATION_DECLARATION_REQUEST; } @Override public boolean hasPersonalInfo() { return true; } @Override protected boolean hasFreeDeclarationRequests() { final ExecutionYear currentExecutionYear = ExecutionYear.readCurrentExecutionYear(); final Set schoolRegistrationDeclarations = getRegistration().getSucessfullyFinishedDocumentRequestsBy( currentExecutionYear, DocumentRequestType.SCHOOL_REGISTRATION_DECLARATION, false); final Set enrolmentDeclarations = getRegistration().getSucessfullyFinishedDocumentRequestsBy( currentExecutionYear, DocumentRequestType.ENROLMENT_DECLARATION, false); return ((schoolRegistrationDeclarations.size() + enrolmentDeclarations.size()) < MAX_FREE_DECLARATIONS_PER_EXECUTION_YEAR); } }