001: /*
002: * Copyright 2006-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.financial.service.impl;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.service.BusinessObjectService;
023: import org.kuali.core.util.KualiDecimal;
024: import org.kuali.kfs.KFSConstants;
025: import org.kuali.module.financial.bo.CashDrawer;
026: import org.kuali.module.financial.service.CashDrawerService;
027: import org.springframework.transaction.annotation.Transactional;
028:
029: /**
030: * This is the default implementation of the CashDrawerService interface.
031: */
032: @Transactional
033: public class CashDrawerServiceImpl implements CashDrawerService {
034: private BusinessObjectService businessObjectService;
035:
036: /**
037: * Retrieves the CashDrawer associated with the workgroup provided and sets the state of the drawer to closed.
038: *
039: * @param workgroupName The name of the workgroup associated with the cash drawer being retrieved.
040: * @see org.kuali.module.financial.service.CashDrawerService#closeCashDrawer(java.lang.String)
041: */
042: public void closeCashDrawer(String workgroupName) {
043: CashDrawer drawer = getByWorkgroupName(workgroupName, true);
044: this .closeCashDrawer(drawer);
045: }
046:
047: /**
048: * Sets the status of the drawer provided to closed and saves the new state.
049: *
050: * @param drawer The instance of the cash drawer to be closed.
051: * @see org.kuali.module.financial.service.CashDrawerService#closeCashDrawer(org.kuali.module.financial.bo.CashDrawer)
052: */
053: public void closeCashDrawer(CashDrawer drawer) {
054: drawer
055: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_CLOSED);
056: drawer.setReferenceFinancialDocumentNumber(null);
057:
058: save(drawer);
059: }
060:
061: /**
062: * Retrieves an instance of a cash drawer based on the parameters provided and sets the status of the drawer to open,
063: * persists the state change and then returns an instance of the drawer in it's new state.
064: *
065: * @param workgroupName The workgroup name associated with the cash drawer we wish to retrieve and open.
066: * @param documentId The id of the reference document linked to the drawer.
067: * @return A new instance of the cash drawer in open status.
068: *
069: * @see org.kuali.module.financial.service.CashDrawerService#openCashDrawer(java.lang.String, java.lang.String)
070: */
071: public CashDrawer openCashDrawer(String workgroupName,
072: String documentId) {
073: if (StringUtils.isBlank(documentId)) {
074: throw new IllegalArgumentException(
075: "invalid (blank) documentId");
076: }
077:
078: CashDrawer drawer = getByWorkgroupName(workgroupName, true);
079: return this .openCashDrawer(drawer, documentId);
080: }
081:
082: /**
083: * Sets the status of the cash drawer provided to open, persists this new state and returns the drawer.
084: *
085: * @param drawer An instance of the drawer being opened.
086: * @param documentId The id of the reference document linked to the drawer.
087: * @return An instance of the cash drawer with a status of open.
088: *
089: * @see org.kuali.module.financial.service.CashDrawerService#openCashDrawer(org.kuali.module.financial.bo.CashDrawer, java.lang.String)
090: */
091: public CashDrawer openCashDrawer(CashDrawer drawer,
092: String documentId) {
093: if (StringUtils.isBlank(documentId)) {
094: throw new IllegalArgumentException(
095: "invalid (blank) documentId");
096: }
097:
098: drawer
099: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_OPEN);
100: drawer.setReferenceFinancialDocumentNumber(documentId);
101:
102: save(drawer);
103: return drawer;
104: }
105:
106: /**
107: * Retrieves a cash drawer using the workgroup name provided, updates the state to locked, then persists this state change.
108: *
109: * @param workgroupName The workgroup name associated with the cash drawer.
110: * @param documentId The reference document id to be set to the cash drawer.
111: *
112: * @see org.kuali.module.financial.service.CashDrawerService#lockCashDrawer(java.lang.String,java.lang.String)
113: */
114: public void lockCashDrawer(String workgroupName, String documentId) {
115: if (StringUtils.isBlank(documentId)) {
116: throw new IllegalArgumentException(
117: "invalid (blank) documentId");
118: }
119:
120: CashDrawer drawer = getByWorkgroupName(workgroupName, true);
121: this .lockCashDrawer(drawer, documentId);
122: }
123:
124: /**
125: * Sets the state of the cash drawer provided to locked and persists this new state.
126: *
127: * @param drawer The cash drawer to be locked.
128: * @param documentId The reference document id to be set to the cash drawer.
129: *
130: * @see org.kuali.module.financial.service.CashDrawerService#lockCashDrawer(org.kuali.module.financial.bo.CashDrawer, java.lang.String)
131: */
132: public void lockCashDrawer(CashDrawer drawer, String documentId) {
133: if (StringUtils.isBlank(documentId)) {
134: throw new IllegalArgumentException(
135: "invalid (blank) documentId");
136: }
137:
138: if (!StringUtils.equals(
139: KFSConstants.CashDrawerConstants.STATUS_OPEN, drawer
140: .getStatusCode())) {
141: throw new IllegalStateException("CashDrawer '"
142: + drawer.getWorkgroupName()
143: + "' cannot be locked because it is not open");
144: }
145: if (!StringUtils.equals(documentId, drawer
146: .getReferenceFinancialDocumentNumber())) {
147: throw new IllegalStateException(
148: "CashDrawer '"
149: + drawer.getWorkgroupName()
150: + "' cannot be locked because it was opened by document "
151: + drawer
152: .getReferenceFinancialDocumentNumber());
153: }
154:
155: drawer
156: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_LOCKED);
157: drawer.setReferenceFinancialDocumentNumber(documentId);
158:
159: save(drawer);
160: }
161:
162: /**
163: * Retrieves a cash drawer using the workgroup name provided, updates the state to open, then persists this state change.
164: *
165: * @param workgroupName The workgroup name associated with the cash drawer.
166: * @param documentId The reference document id to be set to the cash drawer.
167: *
168: * @see org.kuali.module.financial.service.CashDrawerService#unlockCashDrawer(java.lang.String,java.lang.String)
169: */
170: public void unlockCashDrawer(String workgroupName, String documentId) {
171: if (StringUtils.isBlank(documentId)) {
172: throw new IllegalArgumentException(
173: "invalid (blank) documentId");
174: }
175:
176: CashDrawer drawer = getByWorkgroupName(workgroupName, true);
177: this .unlockCashDrawer(drawer, documentId);
178: }
179:
180: /**
181: * Sets the state of the cash drawer provided to open and persists this new state.
182: *
183: * @param drawer The cash drawer to be unlocked.
184: * @param documentId The reference document id to be set to the cash drawer.
185: *
186: * @see org.kuali.module.financial.service.CashDrawerService#unlockCashDrawer(org.kuali.module.financial.bo.CashDrawer, java.lang.String)
187: */
188: public void unlockCashDrawer(CashDrawer drawer, String documentId) {
189: if (StringUtils.isBlank(documentId)) {
190: throw new IllegalArgumentException(
191: "invalid (blank) documentId");
192: }
193:
194: if (!StringUtils.equals(
195: KFSConstants.CashDrawerConstants.STATUS_LOCKED, drawer
196: .getStatusCode())) {
197: throw new IllegalStateException("CashDrawer '"
198: + drawer.getWorkgroupName()
199: + "' cannot be unlocked because it is not locked");
200: }
201: if (!StringUtils.equals(documentId, drawer
202: .getReferenceFinancialDocumentNumber())) {
203: throw new IllegalStateException(
204: "CashDrawer '"
205: + drawer.getWorkgroupName()
206: + "' cannot be unlocked because it was locked by document "
207: + drawer
208: .getReferenceFinancialDocumentNumber());
209: }
210:
211: drawer
212: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_OPEN);
213: drawer.setReferenceFinancialDocumentNumber(documentId);
214:
215: save(drawer);
216: }
217:
218: /**
219: * This method retrieves a cash drawer instance using the workgroup name provided as a search parameter. If no drawer can
220: * be found for the workgroup name provided and the autocreate flag is set to true, then a new instance of a cash drawer will
221: * be generated and returned. If the autocreate flag is false, then a null value will be returned.
222: *
223: * NOTE: The new instance created if autocreate is set to true is an unpersisted instance.
224: *
225: * @param workgroupName The workgroup name used to retrieve the cash drawer.
226: * @param autocreate Flag used to identify if a new cash drawer should be created if one cannot be found for the value provided.
227: * @return An instance of a cash drawer matching the value provided.
228: *
229: * @see org.kuali.module.financial.service.CashDrawerService#findByWorkgroupName(java.lang.String)
230: */
231: public CashDrawer getByWorkgroupName(String workgroupName,
232: boolean autocreate) {
233: if (StringUtils.isBlank(workgroupName)) {
234: throw new IllegalArgumentException(
235: "invalid (blank) workgroupName");
236: }
237:
238: CashDrawer cd = (CashDrawer) businessObjectService
239: .findByPrimaryKey(CashDrawer.class,
240: buildPrimaryKeyMap(workgroupName));
241: if (autocreate && (cd == null)) {
242: cd = newCashDrawer(workgroupName);
243: }
244: return cd;
245: }
246:
247: /**
248: * Persists the given CashDrawer instance.
249: *
250: * @param cashDrawer The cash drawer to be persisted.
251: */
252: private void save(CashDrawer cashDrawer) {
253: if (cashDrawer == null) {
254: throw new IllegalArgumentException(
255: "invalid (null) cashDrawer");
256: }
257:
258: businessObjectService.save(cashDrawer);
259: }
260:
261: /**
262: * This method creates a new cash drawer instance, assigns the workgroup name to the drawer, sets the status of the
263: * drawer to closed and returns this new instance.
264: *
265: * @param workgroupName The workgroup name associated with the cash drawer.
266: * @return A newly-created (unpersisted) CashDrawer instance for the given workgroupName.
267: */
268: private CashDrawer newCashDrawer(String workgroupName) {
269: CashDrawer drawer = new CashDrawer();
270: drawer.setWorkgroupName(workgroupName);
271: drawer
272: .setStatusCode(KFSConstants.CashDrawerConstants.STATUS_CLOSED);
273:
274: return drawer;
275: }
276:
277: /**
278: * This method creates a primary key map by adding the associated workgroup name to a new map instance and returning
279: * this map new instance.
280: *
281: * @param workgroupName The workgroup name to be added to the map.
282: * @return Map suitable for use with primaryKey-related OJB methods
283: */
284: private Map buildPrimaryKeyMap(String workgroupName) {
285: Map keyMap = new HashMap();
286: keyMap.put("WRKGRP_NM", workgroupName);
287: return keyMap;
288: }
289:
290: /**
291: * This method calculates the total of all the coins in the cash drawer. This is accomplished by totaling the values from
292: * each of the *CentAmount() methods (ie. getFinancialDocumentHundredCentAmount()) from the drawer and returning the resulting
293: * value.
294: *
295: * @param drawer The drawer being totaled.
296: * @return The sum of all the coin amounts in the drawer.
297: *
298: * @see org.kuali.module.financial.service.CashDrawerService#getCoinTotal(org.kuali.module.financial.bo.CashDrawer)
299: */
300: public KualiDecimal getCoinTotal(CashDrawer drawer) {
301: KualiDecimal sum = new KualiDecimal(0.0);
302: if (drawer != null) {
303: if (drawer.getFinancialDocumentHundredCentAmount() != null) {
304: sum.add(drawer.getFinancialDocumentHundredCentAmount());
305: }
306: if (drawer.getFinancialDocumentFiftyCentAmount() != null) {
307: sum.add(drawer.getFinancialDocumentFiftyCentAmount());
308: }
309: if (drawer.getFinancialDocumentTwentyFiveCentAmount() != null) {
310: sum.add(drawer
311: .getFinancialDocumentTwentyFiveCentAmount());
312: }
313: if (drawer.getFinancialDocumentTenCentAmount() != null) {
314: sum.add(drawer.getFinancialDocumentTenCentAmount());
315: }
316: if (drawer.getFinancialDocumentFiveCentAmount() != null) {
317: sum.add(drawer.getFinancialDocumentFiveCentAmount());
318: }
319: if (drawer.getFinancialDocumentOneCentAmount() != null) {
320: sum.add(drawer.getFinancialDocumentOneCentAmount());
321: }
322: if (drawer.getFinancialDocumentOtherCentAmount() != null) {
323: sum.add(drawer.getFinancialDocumentOtherCentAmount());
324: }
325: }
326: return sum;
327: }
328:
329: /**
330: * This method calculates the total of all the currency in the cash drawer. This is accomplished by totaling the values from
331: * each of the *DollarAmount() methods (ie. getFinancialDocumentHundredDollarAmount()) from the drawer and returning the resulting
332: * value.
333: *
334: * @param drawer The drawer being totaled.
335: * @return The sum of all the currency amounts in the drawer.
336: *
337: * @see org.kuali.module.financial.service.CashDrawerService#getCurrencyTotal(org.kuali.module.financial.bo.CashDrawer)
338: */
339: public KualiDecimal getCurrencyTotal(CashDrawer drawer) {
340: KualiDecimal sum = new KualiDecimal(0.0);
341: if (drawer != null) {
342: if (drawer.getFinancialDocumentHundredDollarAmount() != null) {
343: sum.add(drawer
344: .getFinancialDocumentHundredDollarAmount());
345: }
346: if (drawer.getFinancialDocumentFiftyDollarAmount() != null) {
347: sum.add(drawer.getFinancialDocumentFiftyDollarAmount());
348: }
349: if (drawer.getFinancialDocumentTwentyDollarAmount() != null) {
350: sum
351: .add(drawer
352: .getFinancialDocumentTwentyDollarAmount());
353: }
354: if (drawer.getFinancialDocumentTenDollarAmount() != null) {
355: sum.add(drawer.getFinancialDocumentTenDollarAmount());
356: }
357: if (drawer.getFinancialDocumentFiveDollarAmount() != null) {
358: sum.add(drawer.getFinancialDocumentFiveDollarAmount());
359: }
360: if (drawer.getFinancialDocumentTwoDollarAmount() != null) {
361: sum.add(drawer.getFinancialDocumentTwoDollarAmount());
362: }
363: if (drawer.getFinancialDocumentOneDollarAmount() != null) {
364: sum.add(drawer.getFinancialDocumentOneDollarAmount());
365: }
366: if (drawer.getFinancialDocumentOtherDollarAmount() != null) {
367: sum.add(drawer.getFinancialDocumentOtherDollarAmount());
368: }
369: }
370: return sum;
371: }
372:
373: // Spring injection
374: /**
375: * Gets the businessObjectService attribute value.
376: *
377: * @return The current value of businessObjectService.
378: */
379: public BusinessObjectService getBusinessObjectService() {
380: return businessObjectService;
381: }
382:
383: /**
384: * Sets the businessObjectService attribute value.
385: *
386: * @param businessObjectService The businessObjectService to set.
387: */
388: public void setBusinessObjectService(
389: BusinessObjectService businessObjectService) {
390: this.businessObjectService = businessObjectService;
391: }
392: }
|