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.List;
020:
021: import org.kuali.core.util.KualiDecimal;
022: import org.kuali.kfs.KFSConstants;
023: import org.kuali.kfs.KFSPropertyConstants;
024: import org.kuali.module.labor.LaborConstants;
025: import org.kuali.module.labor.bo.LaborOriginEntry;
026:
027: /**
028: * This class is a working unit of labor origin entry. It is formed by a group of slimilar labor origin entries. If any two entries
029: * have the same values for the given fields, then they are similar and can be grouped.
030: */
031: public class LaborLedgerUnitOfWork {
032: private LaborOriginEntry workingEntry;
033: private List<String> keyFields;
034: private int numOfMember;
035:
036: /**
037: * Constructs a LaborLedgerUnitOfWork.java.
038: */
039: public LaborLedgerUnitOfWork() {
040: this (null);
041: }
042:
043: /**
044: * Constructs a LaborLedgerUnitOfWork.java.
045: *
046: * @param laborOriginEntry the given origin entry
047: */
048: public LaborLedgerUnitOfWork(LaborOriginEntry laborOriginEntry) {
049: this .resetLaborLedgerUnitOfWork(laborOriginEntry);
050: }
051:
052: /**
053: * Intialize the default key fields of the labor ledger unit of work with the given origin entry
054: *
055: * @param laborOriginEntry the given origin entry
056: */
057: public void resetLaborLedgerUnitOfWork(
058: LaborOriginEntry laborOriginEntry) {
059: this .resetLaborLedgerUnitOfWork(laborOriginEntry, null);
060: }
061:
062: /**
063: * Intialize the specified key fields of the labor ledger unit of work with the given origin entry
064: *
065: * @param laborOriginEntry the given origin entry
066: * @param keyFields the keys to which values will be assigned
067: */
068: public void resetLaborLedgerUnitOfWork(
069: LaborOriginEntry laborOriginEntry, List<String> keyFields) {
070: this .workingEntry = new LaborOriginEntry();
071: this .setKeyFields(keyFields);
072:
073: if (laborOriginEntry != null) {
074: ObjectUtil.buildObject(workingEntry, laborOriginEntry, this
075: .getKeyFields());
076:
077: boolean creditIndicator = KFSConstants.GL_CREDIT_CODE
078: .equals(laborOriginEntry
079: .getTransactionDebitCreditCode());
080: KualiDecimal entryAmount = laborOriginEntry
081: .getTransactionLedgerEntryAmount();
082: KualiDecimal unitAmount = creditIndicator ? entryAmount
083: .negated() : entryAmount;
084:
085: workingEntry.setTransactionLedgerEntryAmount(unitAmount);
086: workingEntry.setTransactionDebitCreditCode(laborOriginEntry
087: .getTransactionDebitCreditCode());
088: numOfMember = 1;
089: }
090: }
091:
092: /**
093: * add the given origin entry as a member of the working unit
094: *
095: * @param laborOriginEntry the given origin entry
096: * @return true if the given origin entry is successfully added into the current unit of work; otherwise, false
097: */
098: public boolean addEntryIntoUnit(LaborOriginEntry laborOriginEntry) {
099: if (this .hasSameKey(laborOriginEntry)) {
100: KualiDecimal unitAmount = workingEntry
101: .getTransactionLedgerEntryAmount();
102: KualiDecimal entryAmount = laborOriginEntry
103: .getTransactionLedgerEntryAmount();
104:
105: // if the input entry has a "credit" code , then subtract its amount from the unit total amount
106: boolean creditIndicator = KFSConstants.GL_CREDIT_CODE
107: .equals(laborOriginEntry
108: .getTransactionDebitCreditCode());
109: unitAmount = creditIndicator ? unitAmount
110: .subtract(entryAmount) : unitAmount
111: .add(entryAmount);
112:
113: workingEntry.setTransactionLedgerEntryAmount(unitAmount);
114: numOfMember++;
115:
116: return true;
117: }
118: return false;
119: }
120:
121: /**
122: * Determine if the given origin entry belongs to the current unit of work
123: *
124: * @param laborOriginEntry the given origin entry
125: * @return true if the given origin entry belongs to the current unit of work; otherwise, false
126: */
127: public boolean canContain(LaborOriginEntry laborOriginEntry) {
128: return this .hasSameKey(laborOriginEntry);
129: }
130:
131: /**
132: * Determine if the given origin entry has the same key as the current unit of work
133: *
134: * @param laborOriginEntry the given origin entry
135: * @return true if the given origin entry has the same key as the current unit of work; otherwise, false
136: */
137: public boolean hasSameKey(LaborOriginEntry laborOriginEntry) {
138: return ObjectUtil.compareObject(workingEntry, laborOriginEntry,
139: this .getKeyFields());
140: }
141:
142: /**
143: * @see java.lang.Object#toString()
144: */
145: @Override
146: public String toString() {
147: List<String> printablekeyFields = new ArrayList<String>(this
148: .getKeyFields());
149: printablekeyFields
150: .add(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT);
151: return ObjectUtil.buildPropertyMap(workingEntry,
152: printablekeyFields).toString();
153: }
154:
155: /**
156: * Gets the workingEntry attribute.
157: *
158: * @return Returns the workingEntry.
159: */
160: public LaborOriginEntry getWorkingEntry() {
161: return this .workingEntry;
162: }
163:
164: /**
165: * Sets the workingEntry attribute value.
166: *
167: * @param workingEntry The workingEntry to set.
168: */
169: public void setWorkingEntry(LaborOriginEntry workingEntry) {
170: this .workingEntry = workingEntry;
171: }
172:
173: /**
174: * Gets the keyFields attribute.
175: *
176: * @return Returns the keyFields.
177: */
178: public List<String> getKeyFields() {
179: return keyFields;
180: }
181:
182: /**
183: * Sets the keyFields attribute value.
184: *
185: * @param keyFields The keyFields to set.
186: */
187: public void setKeyFields(List<String> keyFields) {
188: this .keyFields = (keyFields != null) ? keyFields : this
189: .getDefaultKeyFields();
190: }
191:
192: /**
193: * Gets the numOfMember attribute.
194: *
195: * @return Returns the numOfMember.
196: */
197: public int getNumOfMember() {
198: return numOfMember;
199: }
200:
201: /**
202: * Sets the numOfMember attribute value.
203: *
204: * @param numOfMember The numOfMember to set.
205: */
206: public void setNumOfMember(int numOfMember) {
207: this .numOfMember = numOfMember;
208: }
209:
210: /**
211: * Get the default key fields as a list
212: */
213: private List<String> getDefaultKeyFields() {
214: List<String> defaultKeyFields = new ArrayList<String>(
215: LaborConstants.consolidationAttributesOfOriginEntry());
216: defaultKeyFields
217: .remove(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT);
218: defaultKeyFields
219: .remove(KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE);
220:
221: return defaultKeyFields;
222: }
223: }
|