001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.cg.rules;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.log4j.Logger;
023: import org.kuali.core.document.MaintenanceDocument;
024: import org.kuali.core.service.DataDictionaryService;
025: import org.kuali.core.util.ObjectUtils;
026: import org.kuali.kfs.KFSKeyConstants;
027: import org.kuali.kfs.KFSPropertyConstants;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.cg.bo.Award;
030: import org.kuali.module.cg.bo.AwardAccount;
031: import org.kuali.module.cg.bo.AwardOrganization;
032: import org.kuali.module.cg.bo.AwardProjectDirector;
033:
034: /**
035: * Rules for the Award maintenance document.
036: */
037: public class AwardRule extends CGMaintenanceDocumentRuleBase {
038: protected static Logger LOG = org.apache.log4j.Logger
039: .getLogger(AwardRule.class);
040:
041: private Award newAwardCopy;
042:
043: private static final String GRANT_DESCRIPTION_NPT = "NPT";
044: private static final String GRANT_DESCRIPTION_OPT = "OPT";
045: private static final String[] NON_FED_GRANT_DESCS = new String[] {
046: GRANT_DESCRIPTION_NPT, GRANT_DESCRIPTION_OPT };
047:
048: @Override
049: protected boolean processCustomSaveDocumentBusinessRules(
050: MaintenanceDocument document) {
051: LOG
052: .info("Entering AwardRule.processCustomSaveDocumentBusinessRules");
053: processCustomRouteDocumentBusinessRules(document);
054: LOG
055: .info("Leaving AwardRule.processCustomSaveDocumentBusinessRules");
056: return true; // save despite error messages
057: }
058:
059: @Override
060: protected boolean processCustomRouteDocumentBusinessRules(
061: MaintenanceDocument document) {
062: LOG
063: .info("Entering AwardRule.processCustomRouteDocumentBusinessRules");
064: boolean success = true;
065: success &= checkProposal();
066: success &= checkEndAfterBegin(newAwardCopy
067: .getAwardBeginningDate(), newAwardCopy
068: .getAwardEndingDate(),
069: KFSPropertyConstants.AWARD_ENDING_DATE);
070: success &= checkPrimary(newAwardCopy.getAwardOrganizations(),
071: AwardOrganization.class,
072: KFSPropertyConstants.AWARD_ORGRANIZATIONS, Award.class);
073: success &= checkPrimary(
074: newAwardCopy.getAwardProjectDirectors(),
075: AwardProjectDirector.class,
076: KFSPropertyConstants.AWARD_PROJECT_DIRECTORS,
077: Award.class);
078: success &= checkAccounts();
079: success &= checkProjectDirectorsExist(newAwardCopy
080: .getAwardProjectDirectors(),
081: AwardProjectDirector.class,
082: KFSPropertyConstants.AWARD_PROJECT_DIRECTORS);
083: success &= checkProjectDirectorsExist(newAwardCopy
084: .getAwardAccounts(), AwardAccount.class,
085: KFSPropertyConstants.AWARD_ACCOUNTS);
086: success &= checkProjectDirectorsStatuses(newAwardCopy
087: .getAwardProjectDirectors(),
088: AwardProjectDirector.class,
089: KFSPropertyConstants.AWARD_PROJECT_DIRECTORS);
090: success &= checkFederalPassThrough();
091: success &= checkAgencyNotEqualToFederalPassThroughAgency(
092: newAwardCopy.getAgency(), newAwardCopy
093: .getFederalPassThroughAgency(),
094: KFSPropertyConstants.AGENCY_NUMBER,
095: KFSPropertyConstants.FEDERAL_PASS_THROUGH_AGENCY_NUMBER);
096: LOG
097: .info("Leaving AwardRule.processCustomRouteDocumentBusinessRules");
098: return success;
099: }
100:
101: /**
102: * checks to see if at least 1 award account exists
103: *
104: * @return true if the award contains at least 1 {@link AwardAccount}, false otherwise
105: */
106: private boolean checkAccounts() {
107: boolean success = true;
108: Collection<AwardAccount> awardAccounts = newAwardCopy
109: .getAwardAccounts();
110:
111: if (ObjectUtils.isNull(awardAccounts)
112: || awardAccounts.isEmpty()) {
113: String elementLabel = SpringContext.getBean(
114: DataDictionaryService.class)
115: .getCollectionElementLabel(Award.class.getName(),
116: KFSPropertyConstants.AWARD_ACCOUNTS,
117: AwardAccount.class);
118: putFieldError(KFSPropertyConstants.AWARD_ACCOUNTS,
119: KFSKeyConstants.ERROR_ONE_REQUIRED, elementLabel);
120: success = false;
121: }
122:
123: return success;
124: }
125:
126: /**
127: * checks to see if:
128: * <ol>
129: * <li> a proposal has already been awarded
130: * <li> a proposal is inactive
131: * </ol>
132: *
133: * @return
134: */
135: private boolean checkProposal() {
136: boolean success = true;
137: if (AwardRuleUtil.isProposalAwarded(newAwardCopy)) {
138: putFieldError(KFSPropertyConstants.PROPOSAL_NUMBER,
139: KFSKeyConstants.ERROR_AWARD_PROPOSAL_AWARDED,
140: newAwardCopy.getProposalNumber().toString());
141: success = false;
142: }
143: // SEE KULCG-315 for details on why this code is commented out.
144: // else if (AwardRuleUtil.isProposalInactive(newAwardCopy)) {
145: // putFieldError(KFSPropertyConstants.PROPOSAL_NUMBER, KFSKeyConstants.ERROR_AWARD_PROPOSAL_INACTIVE,
146: // newAwardCopy.getProposalNumber().toString());
147: // }
148:
149: return success;
150: }
151:
152: /**
153: * checks if the required federal pass through fields are filled in if the federal pass through indicator is yes
154: *
155: * @return
156: */
157: private boolean checkFederalPassThrough() {
158: boolean success = true;
159: success = super .checkFederalPassThrough(newAwardCopy
160: .getFederalPassThroughIndicator(), newAwardCopy
161: .getAgency(), newAwardCopy
162: .getFederalPassThroughAgencyNumber(), Award.class,
163: KFSPropertyConstants.FEDERAL_PASS_THROUGH_INDICATOR);
164:
165: if (newAwardCopy.getFederalPassThroughIndicator()) {
166:
167: String indicatorLabel = SpringContext
168: .getBean(DataDictionaryService.class)
169: .getAttributeErrorLabel(
170: Award.class,
171: KFSPropertyConstants.FEDERAL_PASS_THROUGH_INDICATOR);
172: if (null == newAwardCopy
173: .getFederalPassThroughFundedAmount()) {
174: String amountLabel = SpringContext
175: .getBean(DataDictionaryService.class)
176: .getAttributeErrorLabel(
177: Award.class,
178: KFSPropertyConstants.FEDERAL_PASS_THROUGH_FUNDED_AMOUNT);
179: putFieldError(
180: KFSPropertyConstants.FEDERAL_PASS_THROUGH_FUNDED_AMOUNT,
181: KFSKeyConstants.ERROR_FPT_AGENCY_NUMBER_REQUIRED,
182: new String[] { amountLabel, indicatorLabel });
183: success = false;
184: }
185: String grantDescCode = newAwardCopy.getGrantDescription()
186: .getGrantDescriptionCode();
187: if (StringUtils.isBlank(grantDescCode)
188: || !Arrays.asList(NON_FED_GRANT_DESCS).contains(
189: grantDescCode)) {
190: String grantDescLabel = SpringContext
191: .getBean(DataDictionaryService.class)
192: .getAttributeErrorLabel(
193: Award.class,
194: KFSPropertyConstants.GRANT_DESCRIPTION_CODE);
195: putFieldError(
196: KFSPropertyConstants.GRANT_DESCRIPTION_CODE,
197: KFSKeyConstants.ERROR_GRANT_DESCRIPTION_INVALID_WITH_FED_PASS_THROUGH_AGENCY_INDICATOR_SELECTED);
198: success = false;
199: }
200: }
201: return success;
202: }
203:
204: /**
205: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
206: */
207: @Override
208: public void setupConvenienceObjects() {
209: // oldAward = (Award) super.getOldBo();
210: newAwardCopy = (Award) super.getNewBo();
211: }
212:
213: }
|