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.kuali.core.document.MaintenanceDocument;
019: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
020: import org.kuali.kfs.KFSKeyConstants;
021: import org.kuali.kfs.context.SpringContext;
022: import org.kuali.module.chart.bo.ObjLevel;
023: import org.kuali.module.chart.bo.ObjectCode;
024: import org.kuali.module.chart.bo.ObjectCons;
025: import org.kuali.module.chart.service.ChartService;
026: import org.kuali.module.chart.service.ObjectCodeService;
027: import org.kuali.module.chart.service.ObjectLevelService;
028:
029: /**
030: *
031: * This class implements the business rules for {@link ObjectCons}
032: */
033: public class ObjectConsRule extends MaintenanceDocumentRuleBase {
034:
035: private static ChartService chartService;
036: private static ObjectLevelService objectLevelService;
037: private static ObjectCodeService objectCodeService;
038:
039: /**
040: *
041: * Constructs a {@link ObjectConsRule}
042: * Pseudo-injects some services
043: */
044: public ObjectConsRule() {
045: if (chartService == null) {
046: objectLevelService = SpringContext
047: .getBean(ObjectLevelService.class);
048: objectCodeService = SpringContext
049: .getBean(ObjectCodeService.class);
050: chartService = SpringContext.getBean(ChartService.class);
051: }
052: }
053:
054: /**
055: * This performs rules checks on document save
056: * <ul>
057: * <li>{@link ObjectConsRule#checkObjLevelCode(ObjectCons)}</li>
058: * <li>{@link ObjectConsRule#checkEliminationCode(ObjectCons)}</li>
059: * </ul>
060: * This rule does not fail on business rule failures
061: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
062: */
063: @Override
064: protected boolean processCustomSaveDocumentBusinessRules(
065: MaintenanceDocument document) {
066: ObjectCons objConsolidation = (ObjectCons) getNewBo();
067:
068: checkObjLevelCode(objConsolidation);
069: checkEliminationCode(objConsolidation);
070: return true;
071: }
072:
073: /**
074: * This performs rules checks on document route
075: * <ul>
076: * <li>{@link ObjectConsRule#checkObjLevelCode(ObjectCons)}</li>
077: * <li>{@link ObjectConsRule#checkEliminationCode(ObjectCons)}</li>
078: * </ul>
079: * This rule fails on business rule failures
080: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
081: */
082: @Override
083: protected boolean processCustomRouteDocumentBusinessRules(
084: MaintenanceDocument document) {
085: boolean success = true;
086: ObjectCons objConsolidation = (ObjectCons) getNewBo();
087:
088: success &= checkObjLevelCode(objConsolidation);
089: success &= checkEliminationCode(objConsolidation);
090: return success;
091: }
092:
093: /**
094: * This method checks to see if the Object Consolidation code matches a pre-existing Object Level code that is already entered.
095: * If it does it returns false with an error
096: *
097: * @param document
098: * @return false if Object Level Code already exists
099: */
100: private boolean checkObjLevelCode(ObjectCons objConsolidation) {
101: boolean success = true;
102:
103: ObjLevel objLevel = objectLevelService.getByPrimaryId(
104: objConsolidation.getChartOfAccountsCode(),
105: objConsolidation.getFinConsolidationObjectCode());
106: if (objLevel != null) {
107: success = false;
108: putFieldError(
109: "finConsolidationObjectCode",
110: KFSKeyConstants.ERROR_DOCUMENT_OBJCONSMAINT_ALREADY_EXISTS_AS_OBJLEVEL);
111: }
112: return success;
113: }
114:
115: /**
116: * This method checks that the eliminations object code is really a valid current object code.
117: *
118: * @return true if eliminations object code is a valid object code currently, false if otherwise
119: */
120: private boolean checkEliminationCode(ObjectCons objConsolidation) {
121: boolean success = true;
122:
123: ObjectCode elimCode = objectCodeService
124: .getByPrimaryIdForCurrentYear(objConsolidation
125: .getChartOfAccountsCode(), objConsolidation
126: .getFinancialEliminationsObjectCode());
127: if (elimCode == null) {
128: // KULRNE-61 - otherwise, allow the invalid value if the object is at the top of the hieratchy and the eliminiation
129: // object code
130: // is itself
131: if (!objConsolidation
132: .getFinConsolidationObjectCode()
133: .equals(
134: objConsolidation
135: .getFinancialEliminationsObjectCode())
136: || !chartService.getReportsToHierarchy().get(
137: objConsolidation.getChartOfAccountsCode())
138: .equals(
139: objConsolidation
140: .getChartOfAccountsCode())) {
141: success = false;
142: putFieldError(
143: "financialEliminationsObjectCode",
144: KFSKeyConstants.ERROR_DOCUMENT_OBJCONSMAINT_INVALID_ELIM_OBJCODE,
145: new String[] { objConsolidation
146: .getFinancialEliminationsObjectCode() });
147: }
148: }
149: return success;
150: }
151: }
|