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.module.ld.businessobject;
017    
018    import java.util.HashSet;
019    import java.util.Set;
020    
021    import org.kuali.kfs.sys.businessobject.AccountingLine;
022    import org.kuali.kfs.sys.businessobject.AccountingLineOverride;
023    import org.kuali.kfs.sys.businessobject.AccountingLineOverride.COMPONENT;
024    
025    /**
026     * Labor business object for Labor Accounting Line Override
027     */
028    public class LaborAccountingLineOverride {
029    
030        /**
031         * On the given AccountingLine, converts override input checkboxes from a Struts Form into a persistable override code.
032         * 
033         * @param line
034         */
035        public static void populateFromInput(AccountingLine line) {
036            // todo: this logic won't work if a single account checkbox might also stands for NON_FRINGE_ACCOUNT_USED; needs thought
037    
038            Set<Integer> overrideInputComponents = new HashSet<Integer>();
039            if (line.getAccountExpiredOverride()) {
040                overrideInputComponents.add(COMPONENT.EXPIRED_ACCOUNT);
041            }
042            if (line.isObjectBudgetOverride()) {
043                overrideInputComponents.add(COMPONENT.NON_BUDGETED_OBJECT);
044            }
045            if (line.getNonFringeAccountOverride()) {
046                overrideInputComponents.add(COMPONENT.NON_FRINGE_ACCOUNT_USED);
047            }
048    
049            Integer[] inputComponentArray = overrideInputComponents.toArray(new Integer[overrideInputComponents.size()]);
050            line.setOverrideCode(AccountingLineOverride.valueOf(inputComponentArray).getCode());
051        }
052    
053        /**
054         * Prepares the given AccountingLine in a Struts Action for display by a JSP. This means converting the override code to
055         * checkboxes for display and input, as well as analyzing the accounting line and determining which override checkboxes are
056         * needed.
057         * 
058         * @param line
059         */
060        public static void processForOutput(AccountingLine line) {
061            AccountingLineOverride fromCurrentCode = AccountingLineOverride.valueOf(line.getOverrideCode());
062            AccountingLineOverride needed = determineNeededOverrides(line);
063            line.setAccountExpiredOverride(fromCurrentCode.hasComponent(COMPONENT.EXPIRED_ACCOUNT));
064            line.setAccountExpiredOverrideNeeded(needed.hasComponent(COMPONENT.EXPIRED_ACCOUNT));
065            line.setObjectBudgetOverride(fromCurrentCode.hasComponent(COMPONENT.NON_BUDGETED_OBJECT));
066            line.setObjectBudgetOverrideNeeded(needed.hasComponent(COMPONENT.NON_BUDGETED_OBJECT));
067            line.setNonFringeAccountOverride(fromCurrentCode.hasComponent(COMPONENT.NON_FRINGE_ACCOUNT_USED));
068            line.setNonFringeAccountOverrideNeeded(needed.hasComponent(COMPONENT.NON_FRINGE_ACCOUNT_USED));
069        }
070    
071        /**
072         * Determines what overrides the given line needs.
073         * 
074         * @param line
075         * @return what overrides the given line needs.
076         */
077        public static AccountingLineOverride determineNeededOverrides(AccountingLine line) {
078            Set<Integer> neededOverrideComponents = new HashSet<Integer>();
079            if (AccountingLineOverride.needsExpiredAccountOverride(line.getAccount())) {
080                neededOverrideComponents.add(COMPONENT.EXPIRED_ACCOUNT);
081            }
082            if (AccountingLineOverride.needsObjectBudgetOverride(line.getAccount(), line.getObjectCode())) {
083                neededOverrideComponents.add(COMPONENT.NON_BUDGETED_OBJECT);
084            }
085            if (AccountingLineOverride.needsNonFringAccountOverride(line.getAccount())) {
086                neededOverrideComponents.add(COMPONENT.NON_FRINGE_ACCOUNT_USED);
087            }
088            Integer[] inputComponentArray = neededOverrideComponents.toArray(new Integer[neededOverrideComponents.size()]);
089    
090            return AccountingLineOverride.valueOf(inputComponentArray);
091        }
092    }