001: /*
002: * Copyright 2006-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.chart.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.bo.PersistableBusinessObject;
020: import org.kuali.core.document.MaintenanceDocument;
021: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
022: import org.kuali.core.service.BusinessObjectService;
023: import org.kuali.core.util.ObjectUtils;
024: import org.kuali.kfs.KFSKeyConstants;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.module.chart.bo.ReportingCodes;
027:
028: /**
029: *
030: * This class implements the business rules specific to the {@link ReportingCodes} Maintenance Document.
031: */
032: public class ReportingCodesRule extends MaintenanceDocumentRuleBase {
033:
034: private ReportingCodes oldReportingCode;
035: private ReportingCodes newReportingCode;
036:
037: private BusinessObjectService businessObjectService;
038:
039: /**
040: *
041: * Constructs a ReportingCodesRule and pseudo-injects services
042: */
043: public ReportingCodesRule() {
044: super ();
045: setBusinessObjectService((BusinessObjectService) SpringContext
046: .getBean(BusinessObjectService.class));
047: }
048:
049: /**
050: * This performs rules checks on document route
051: * <ul>
052: * <li>{@link ProjectCodeRule#checkReportsToReportingCode()}</li>
053: * </ul>
054: * This rule fails on business rule failures
055: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
056: */
057: protected boolean processCustomRouteDocumentBusinessRules(
058: MaintenanceDocument document) {
059: boolean success = true;
060: setupConvenienceObjects(document);
061: success &= checkReportsToReportingCode();
062: return success;
063: }
064:
065: /**
066: * This performs rules checks on document save
067: * <ul>
068: * <li>{@link ProjectCodeRule#checkReportsToReportingCode()}</li>
069: * </ul>
070: * This rule does not fail on business rule failures
071: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
072: */
073: protected boolean processCustomSaveDocumentBusinessRules(
074: MaintenanceDocument document) {
075: boolean success = true;
076: setupConvenienceObjects(document);
077: checkReportsToReportingCode();
078: return success;
079: }
080:
081: /**
082: *
083: * This method sets the convenience objects like newReportingCode and oldReportingCode, so you have short and easy handles to the new and
084: * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
085: * all sub-objects from the DB by their primary keys, if available.
086: *
087: * @param document
088: */
089: private void setupConvenienceObjects(MaintenanceDocument document) {
090:
091: // setup oldAccount convenience objects, make sure all possible sub-objects are populated
092: oldReportingCode = (ReportingCodes) super .getOldBo();
093:
094: // setup newAccount convenience objects, make sure all possible sub-objects are populated
095: newReportingCode = (ReportingCodes) super .getNewBo();
096: }
097:
098: /**
099: *
100: * This checks to see if the user has entered in two different values for the reporting code and the
101: * reports to reporting code. If they are different then it makes sure that the reports to reporting code actually exists
102: * in the system.
103: * @return true if the reports to reporting code is filled and exists or true if it isn't filled in (doesn't need to be), false otherwise
104: */
105: private boolean checkReportsToReportingCode() {
106: boolean success = true;
107: boolean oneMissing = false;
108: boolean bothMissing = false;
109: boolean doExistenceTest = false;
110:
111: // if one of the codes is blank but the other isnt (ie, they are different), then
112: // do the existence test
113: if (StringUtils.isBlank(newReportingCode
114: .getFinancialReportingCode())
115: && StringUtils.isBlank(newReportingCode
116: .getFinancialReportsToReportingCode())) {
117: bothMissing = true;
118: } else if (StringUtils.isBlank(newReportingCode
119: .getFinancialReportingCode())
120: || StringUtils.isBlank(newReportingCode
121: .getFinancialReportsToReportingCode())) {
122: oneMissing = true;
123: }
124: if (oneMissing && !bothMissing) {
125: doExistenceTest = true;
126: }
127:
128: // if both codes are there, but they are different, then do the existence test
129: if (StringUtils.isNotBlank(newReportingCode
130: .getFinancialReportingCode())) {
131: if (!newReportingCode
132: .getFinancialReportingCode()
133: .equalsIgnoreCase(
134: newReportingCode
135: .getFinancialReportsToReportingCode())) {
136: doExistenceTest = true;
137: }
138: }
139:
140: // if these two aren't equal then we need to make sure that the object exists
141: if (doExistenceTest) {
142:
143: // attempt to retrieve the specified object from the db
144: PersistableBusinessObject referenceBo;
145: referenceBo = businessObjectService.getReferenceIfExists(
146: (PersistableBusinessObject) newReportingCode,
147: "reportingCodes");
148: if (!ObjectUtils.isNotNull(referenceBo)) {
149: putFieldError("financialReportsToReportingCode",
150: KFSKeyConstants.ERROR_EXISTENCE,
151: "Reports To Reporting Code");
152: success &= false;
153: }
154: }
155: return success;
156: }
157:
158: private void setBusinessObjectService(
159: BusinessObjectService boService) {
160: businessObjectService = boService;
161: }
162:
163: }
|