001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.fp.document.validation.impl;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.kuali.kfs.fp.document.BudgetAdjustmentDocument;
022 import org.kuali.kfs.fp.document.service.BudgetAdjustmentLaborBenefitsService;
023 import org.kuali.kfs.fp.document.web.struts.BudgetAdjustmentForm;
024 import org.kuali.kfs.sys.KFSConstants;
025 import org.kuali.kfs.sys.KFSKeyConstants;
026 import org.kuali.kfs.sys.businessobject.AccountingLine;
027 import org.kuali.kfs.sys.context.SpringContext;
028 import org.kuali.rice.kns.document.Document;
029 import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
030 import org.kuali.rice.kns.service.KualiConfigurationService;
031 import org.kuali.rice.kns.util.KNSConstants;
032 import org.kuali.rice.kns.util.ObjectUtils;
033 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
034
035 /**
036 * Checks warnings and prompt conditions for ba document.
037 */
038 public class BudgetAdjustmentDocumentPreRules extends PromptBeforeValidationBase {
039 protected KualiConfigurationService kualiConfiguration;
040
041
042 /**
043 * Execute pre-rules for BudgetAdjustmentDocument
044 *
045 * @document document with pre-rules being applied
046 * @return true if pre-rules fire without problem
047 * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.kns.document.MaintenanceDocument)
048 */
049 @Override
050 public boolean doPrompts(Document document) {
051 boolean preRulesOK = true;
052
053 BudgetAdjustmentDocument budgetDocument = (BudgetAdjustmentDocument) document;
054 preRulesOK = askLaborBenefitsGeneration(budgetDocument);
055
056 return preRulesOK;
057 }
058
059
060 /**
061 * Calls service to determine if any labor object codes are present on the ba document. If so, asks the user if they want the
062 * system to automatically generate the benefit lines. If Yes, calls service to generate the accounting lines.
063 *
064 * @param budgetDocument submitted budget document
065 * @return true if labor benefits generation question is NOT asked
066 */
067 protected boolean askLaborBenefitsGeneration(BudgetAdjustmentDocument budgetDocument) {
068 // before prompting, check the document contains one or more labor object codes
069 final boolean hasLaborObjectCodes = SpringContext.getBean(BudgetAdjustmentLaborBenefitsService.class).hasLaborObjectCodes(budgetDocument);
070 final boolean canEdit = ((BudgetAdjustmentForm)form).getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT);
071 final boolean canGenerateLaborBenefitsByRouteStatusResult = canGenerateLaborBenefitsByRouteStatus(budgetDocument);
072 if (canEdit && hasLaborObjectCodes && canGenerateLaborBenefitsByRouteStatusResult) {
073 final String questionText = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSKeyConstants.QUESTION_GENERATE_LABOR_BENEFIT_LINES);
074 final boolean generateBenefits = super.askOrAnalyzeYesNoQuestion(KFSConstants.BudgetAdjustmentDocumentConstants.GENERATE_BENEFITS_QUESTION_ID, questionText);
075 if (generateBenefits) {
076 SpringContext.getBean(BudgetAdjustmentLaborBenefitsService.class).generateLaborBenefitsAccountingLines(budgetDocument);
077 // return to document after lines are generated
078 super.event.setActionForwardName(KFSConstants.MAPPING_BASIC);
079 return false;
080 }
081 }
082
083 return true;
084 }
085
086 /**
087 * TODO: remove this method once baseline accounting lines has been removed
088 */
089 protected List deepCopyAccountingLinesList(List originals) {
090 if (originals == null) {
091 return null;
092 }
093 List copiedLines = new ArrayList();
094 for (int i = 0; i < originals.size(); i++) {
095 copiedLines.add(ObjectUtils.deepCopy((AccountingLine) originals.get(i)));
096 }
097 return copiedLines;
098 }
099
100 /**
101 * Based on the routing status of the document, determines if labor benefits can be generated on the document
102 * @param budgetAdjustmentDocument the budget adjustment document that labor benefits would be generated on
103 * @return true if labor benefits can be generated, false otherwise
104 */
105 protected boolean canGenerateLaborBenefitsByRouteStatus(BudgetAdjustmentDocument budgetAdjustmentDocument) {
106 final KualiWorkflowDocument workflowDocument = budgetAdjustmentDocument.getDocumentHeader().getWorkflowDocument();
107 if (workflowDocument.stateIsInitiated() || workflowDocument.stateIsSaved()) return true; // we're pre-route; we can add labor benefits
108 if (workflowDocument.stateIsEnroute() && workflowDocument.getCurrentRouteNodeNames().contains(KFSConstants.RouteLevelNames.ACCOUNT)) return true; // we're fiscal officers approving; we can add labor benefits
109 return false; // we're someone else and we're plum out of luck
110 }
111 }
112