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.labor.util;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.apache.ojb.broker.query.ReportQueryByCriteria;
024: import org.kuali.kfs.KFSPropertyConstants;
025: import org.kuali.module.labor.bo.LedgerBalance;
026:
027: /**
028: * Utility class for helping DAOs deal with building queries for the consolidation option
029: */
030: public class ConsolidationUtil {
031: private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
032: .getLog(ConsolidationUtil.class);
033:
034: /**
035: * wrap the given field name with SQL function "sum"
036: *
037: * @param fieldName the given field name
038: * @return the wrapped field name with SQL function "sum"
039: */
040: public static final String sum(String fieldName) {
041: return "sum(" + fieldName + ")";
042: }
043:
044: /**
045: * This method builds the atrribute list used by balance searching
046: *
047: * @param extendedFields extra fields
048: * @return List an attribute list
049: */
050: public static Collection<String> buildAttributeCollection(
051: String... extendedFields) {
052: return buildAttributeCollection(Arrays.asList(extendedFields));
053: }
054:
055: /**
056: * This method builds the atrribute list used by balance searching
057: *
058: * @param extendedFields extra fields
059: * @return Collection an attribute list
060: */
061: public static Collection<String> buildAttributeCollection(
062: Collection<String> extendedFields) {
063: Collection<String> attributeList = buildGroupByCollection();
064:
065: attributeList
066: .add(sum(KFSPropertyConstants.ACCOUNT_LINE_ANNUAL_BALANCE_AMOUNT));
067: attributeList
068: .add(sum(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT));
069: attributeList
070: .add(sum(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT));
071:
072: // add the entended elements into the list
073: attributeList.addAll(extendedFields);
074: return attributeList;
075: }
076:
077: /**
078: * Utility class for helping DAOs deal with building queries for the consolidation option
079: *
080: * @param query Query to make consolidated
081: * @param extraFields fields included in the query
082: * @param ignoredFields to omit from the query
083: */
084: public static void buildConsolidatedQuery(
085: ReportQueryByCriteria query, String... extraFields) {
086: Collection<String> attributeList = buildAttributeCollection(extraFields);
087: Collection<String> groupByList = buildGroupByCollection();
088:
089: attributeList.remove(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
090: groupByList.remove(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
091: attributeList
092: .remove(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
093: groupByList
094: .remove(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
095: attributeList
096: .remove(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
097: groupByList
098: .remove(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
099:
100: // set the selection attributes
101: String[] attributes = (String[]) attributeList
102: .toArray(new String[attributeList.size()]);
103: query.setAttributes(attributes);
104: LOG.debug("Built Attributes for Query: "
105: + attributeList.toString());
106:
107: // add the group criteria into the selection statement
108: String[] groupBy = (String[]) groupByList
109: .toArray(new String[attributeList.size()]);
110: query.addGroupBy(groupBy);
111: LOG.debug("Built GroupBy for Query: " + groupByList.toString());
112: }
113:
114: /**
115: * This method builds group by attribute list used by balance searching
116: *
117: * @return extraFields
118: * @return Collection an group by attribute list
119: */
120: public static Collection<String> buildGroupByCollection(
121: Collection<String> extraFields) {
122: Collection<String> retval = new ArrayList();
123: retval.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
124: retval.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
125: retval.add(KFSPropertyConstants.ACCOUNT_NUMBER);
126: retval.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
127: retval.add(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
128: retval.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
129: retval.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
130: retval.add(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
131: retval.add(KFSPropertyConstants.EMPLID);
132: retval.add(KFSPropertyConstants.POSITION_NUMBER);
133: retval.addAll(extraFields);
134: return retval;
135: }
136:
137: /**
138: * This method builds group by attribute list used by balance searching
139: *
140: * @return extraFields
141: * @return Collection an group by attribute list
142: */
143: public static Collection<String> buildGroupByCollection(
144: String... extraFields) {
145: return buildGroupByCollection(Arrays.asList(extraFields));
146: }
147:
148: /**
149: * Consolidates a collection of actual balances with a collection of A2 balances. The A2 balances are changed to AC, then
150: * matched by balance key with balances from the actual collection.
151: *
152: * @param actualBalances - collection of actual balances (consolidatedBalanceTypeCode)
153: * @param effortBalances - collection of effort balances ('A2')
154: * @param consolidatedBalanceTypeCode - balance type to change A2 records to
155: * @return Collection<LedgerBalance> - collection with consolidated balance records
156: */
157: public static Collection<LedgerBalance> consolidateA2Balances(
158: Collection<LedgerBalance> actualBalances,
159: Collection<LedgerBalance> effortBalances,
160: String consolidatedBalanceTypeCode) {
161: Collection<LedgerBalance> consolidatedBalances = new ArrayList<LedgerBalance>();
162:
163: // first change a2 balance to given balance type code and add to consolidated list
164: for (LedgerBalance balance : effortBalances) {
165: balance.setBalanceTypeCode(consolidatedBalanceTypeCode);
166: consolidatedBalances.add(balance);
167: }
168:
169: // look for a matching a2 balance for each ac, if found add the amount of the ac to the a2 record, if not add the full ac
170: // record
171: for (LedgerBalance actualBalance : actualBalances) {
172: boolean matchFound = false;
173: actualBalance
174: .setBalanceTypeCode(consolidatedBalanceTypeCode);
175: String actualKey = ObjectUtil.buildPropertyMap(
176: actualBalance, actualBalance.getPrimaryKeyList())
177: .toString();
178:
179: for (LedgerBalance effortBalance : consolidatedBalances) {
180: String effortKey = ObjectUtil.buildPropertyMap(
181: effortBalance,
182: effortBalance.getPrimaryKeyList()).toString();
183:
184: // if found matching effort record, sum the amount fields of the actual and effort
185: if (StringUtils.equals(effortKey, actualKey)) {
186: sumLedgerBalances(effortBalance, actualBalance);
187: matchFound = true;
188: }
189: }
190:
191: if (!matchFound) {
192: consolidatedBalances.add(actualBalance);
193: }
194: }
195:
196: return consolidatedBalances;
197: }
198:
199: /**
200: * Adds the amounts fields of the second balance record to the first.
201: *
202: * @param balance1 - LedgerBalance
203: * @param balance2 - LedgerBalance
204: */
205: public static void sumLedgerBalances(LedgerBalance balance1,
206: LedgerBalance balance2) {
207: balance1.setAccountLineAnnualBalanceAmount(balance1
208: .getAccountLineAnnualBalanceAmount().add(
209: balance2.getAccountLineAnnualBalanceAmount()));
210: balance1.setBeginningBalanceLineAmount(balance1
211: .getBeginningBalanceLineAmount().add(
212: balance2.getBeginningBalanceLineAmount()));
213: balance1
214: .setContractsGrantsBeginningBalanceAmount(balance1
215: .getContractsGrantsBeginningBalanceAmount()
216: .add(
217: balance2
218: .getContractsGrantsBeginningBalanceAmount()));
219: balance1.setMonth1Amount(balance1.getMonth1Amount().add(
220: balance2.getMonth1Amount()));
221: balance1.setMonth2Amount(balance1.getMonth2Amount().add(
222: balance2.getMonth2Amount()));
223: balance1.setMonth3Amount(balance1.getMonth3Amount().add(
224: balance2.getMonth3Amount()));
225: balance1.setMonth4Amount(balance1.getMonth4Amount().add(
226: balance2.getMonth4Amount()));
227: balance1.setMonth5Amount(balance1.getMonth5Amount().add(
228: balance2.getMonth5Amount()));
229: balance1.setMonth6Amount(balance1.getMonth6Amount().add(
230: balance2.getMonth6Amount()));
231: balance1.setMonth7Amount(balance1.getMonth7Amount().add(
232: balance2.getMonth7Amount()));
233: balance1.setMonth8Amount(balance1.getMonth8Amount().add(
234: balance2.getMonth8Amount()));
235: balance1.setMonth9Amount(balance1.getMonth9Amount().add(
236: balance2.getMonth9Amount()));
237: balance1.setMonth10Amount(balance1.getMonth10Amount().add(
238: balance2.getMonth10Amount()));
239: balance1.setMonth11Amount(balance1.getMonth11Amount().add(
240: balance2.getMonth11Amount()));
241: balance1.setMonth12Amount(balance1.getMonth12Amount().add(
242: balance2.getMonth12Amount()));
243: balance1.setMonth13Amount(balance1.getMonth13Amount().add(
244: balance2.getMonth13Amount()));
245: }
246:
247: /**
248: * wrap the attribute name based on the given flag: isAttributeNameNeeded
249: *
250: * @param attributeName the given attribute name
251: * @param isAttributeNameNeeded the flag that indicates if the attribute name needs to be wrapped with consolidation
252: * @return the attribute name as it is if isAttributeNameNeeded is true; otherwise, the attribute name wrapped with
253: * consolidation string
254: */
255: public static String wrapAttributeName(String attributeName,
256: boolean isAttributeNameNeeded) {
257: return isAttributeNameNeeded ? attributeName
258: : ConsolidationUtil.sum(attributeName);
259: }
260: }
|