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.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.ojb.broker.query.Criteria;
024: import org.apache.ojb.broker.query.QueryFactory;
025: import org.apache.ojb.broker.query.ReportQueryByCriteria;
026: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
027: import org.kuali.kfs.KFSConstants;
028: import org.kuali.kfs.KFSPropertyConstants;
029: import org.kuali.module.gl.util.OJBUtility;
030: import org.kuali.module.gl.web.Constant;
031: import org.kuali.module.labor.LaborPropertyConstants;
032: import org.kuali.module.labor.bo.AccountStatusBaseFunds;
033: import org.kuali.module.labor.dao.LaborBaseFundsDao;
034: import org.kuali.module.labor.util.ConsolidationUtil;
035: import org.kuali.module.labor.util.ObjectUtil;
036:
037: /**
038: * This is the data access object for account status base funds.
039: *
040: * @see org.kuali.module.labor.bo.AccountStatusBaseFunds
041: */
042: public class LaborBaseFundsDaoOjb extends PlatformAwareDaoBaseOjb
043: implements LaborBaseFundsDao {
044: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(LaborBaseFundsDaoOjb.class);
046:
047: /**
048: * @see org.kuali.module.labor.dao.LaborBaseFundsDao#findLaborBaseFunds(java.util.Map, boolean)
049: */
050: public List<AccountStatusBaseFunds> findLaborBaseFunds(
051: Map fieldValues, boolean isConsolidated) {
052: LOG.debug("Start findLaborBaseFunds()");
053:
054: Iterator<Object[]> queryResults = this
055: .findLaborBaseFundsRawData(fieldValues, isConsolidated);
056: List<AccountStatusBaseFunds> BaseFundsCollection = new ArrayList<AccountStatusBaseFunds>();
057: while (queryResults != null && queryResults.hasNext()) {
058: BaseFundsCollection
059: .add(this
060: .marshalAccountStatusBaseFunds(queryResults
061: .next()));
062: }
063: return BaseFundsCollection;
064: }
065:
066: // get the labor base funds according to the given criteria
067: private Iterator<Object[]> findLaborBaseFundsRawData(
068: Map fieldValues, boolean isConsolidated) {
069: Criteria criteria = OJBUtility.buildCriteriaFromMap(
070: fieldValues, new AccountStatusBaseFunds());
071: criteria.addEqualTo(
072: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
073: KFSConstants.BALANCE_TYPE_BASE_BUDGET);
074:
075: criteria.addEqualToField(LaborPropertyConstants.LABOR_OBJECT
076: + "." + KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
077: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
078: criteria.addEqualToField(LaborPropertyConstants.LABOR_OBJECT
079: + "." + KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
080: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
081: criteria.addEqualToField(LaborPropertyConstants.LABOR_OBJECT
082: + "." + KFSPropertyConstants.FINANCIAL_OBJECT_CODE,
083: KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
084:
085: // this statement is used to force OJB to join LABOR_OBJECT and GL_BALANCE tables
086: criteria
087: .addNotNull(LaborPropertyConstants.LABOR_OBJECT
088: + "."
089: + LaborPropertyConstants.FINANCIAL_OBJECT_FRINGE_OR_SALARY_CODE);
090:
091: ReportQueryByCriteria query = QueryFactory.newReportQuery(
092: AccountStatusBaseFunds.class, criteria);
093:
094: List<String> groupByList = getGroupByList(isConsolidated);
095: String[] groupBy = (String[]) groupByList
096: .toArray(new String[groupByList.size()]);
097: query.addGroupBy(groupBy);
098:
099: List<String> getAttributeList = getAttributeListForBaseFunds(
100: isConsolidated, false);
101: String[] attributes = (String[]) getAttributeList
102: .toArray(new String[getAttributeList.size()]);
103: query.setAttributes(attributes);
104:
105: return getPersistenceBrokerTemplate()
106: .getReportQueryIteratorByQuery(query);
107: }
108:
109: // marshal into AccountStatusBaseFunds from the query result
110: private AccountStatusBaseFunds marshalAccountStatusBaseFunds(
111: Object[] queryResult) {
112: AccountStatusBaseFunds baseFunds = new AccountStatusBaseFunds();
113: List<String> keyFields = this .getAttributeListForBaseFunds(
114: false, true);
115:
116: ObjectUtil.buildObject(baseFunds, queryResult, keyFields);
117: return baseFunds;
118: }
119:
120: // define a list of attributes that are used as the grouping criteria
121: private List<String> getGroupByList(boolean isConsolidated) {
122: List<String> groupByList = new ArrayList<String>();
123: groupByList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
124: groupByList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
125: groupByList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
126: groupByList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
127:
128: if (!isConsolidated) {
129: groupByList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
130: groupByList
131: .add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
132: }
133: return groupByList;
134: }
135:
136: // define the return attribute list
137: private List<String> getAttributeList(boolean isConsolidated) {
138: List<String> attributeList = getGroupByList(isConsolidated);
139:
140: if (isConsolidated) {
141: attributeList.add("'"
142: + Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER + "'");
143: attributeList.add("'"
144: + Constant.CONSOLIDATED_SUB_OBJECT_CODE + "'");
145: }
146: return attributeList;
147: }
148:
149: // define the return attribute list for base funds query
150: private List<String> getAttributeListForBaseFunds(
151: boolean isConsolidated, boolean isAttributeNameNeeded) {
152: List<String> attributeList = getAttributeList(isConsolidated);
153:
154: if (!isAttributeNameNeeded) {
155: attributeList
156: .add(ConsolidationUtil
157: .sum(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT));
158: attributeList
159: .add(ConsolidationUtil
160: .sum(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT));
161: attributeList
162: .add(ConsolidationUtil
163: .sum(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT));
164: } else {
165: attributeList
166: .add(KFSPropertyConstants.ACCOUNTING_LINE_ANNUAL_BALANCE_AMOUNT);
167: attributeList
168: .add(KFSPropertyConstants.FINANCIAL_BEGINNING_BALANCE_LINE_AMOUNT);
169: attributeList
170: .add(KFSPropertyConstants.CONTRACTS_GRANTS_BEGINNING_BALANCE_AMOUNT);
171: }
172: return attributeList;
173: }
174: }
|