KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > common > period > PeriodServices


1 /*
2  * $Id: PeriodServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 /*
26  * Services for finding time periods
27  * @author Si Chen (sichen@opensourcestrategies.com)
28  */

29
30 package org.ofbiz.common.period;
31
32 import java.sql.Timestamp JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.ofbiz.base.util.UtilDateTime;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.entity.GenericDelegator;
39 import org.ofbiz.entity.GenericEntityException;
40 import org.ofbiz.entity.GenericValue;
41 import org.ofbiz.entity.condition.EntityConditionList;
42 import org.ofbiz.entity.condition.EntityExpr;
43 import org.ofbiz.entity.condition.EntityOperator;
44 import org.ofbiz.service.DispatchContext;
45 import org.ofbiz.service.ServiceUtil;
46
47 public class PeriodServices {
48     public static String JavaDoc module = PeriodServices.class.getName();
49     
50     /* find the date of the last closed CustomTimePeriod, or, if none available, the earliest date available of any
51      * CustomTimePeriod
52      */

53     public static Map JavaDoc findLastClosedDate(DispatchContext dctx, Map JavaDoc context) {
54         GenericDelegator delegator = dctx.getDelegator();
55         String JavaDoc organizationPartyId = (String JavaDoc) context.get("organizationPartyId"); // input parameters
56
String JavaDoc periodTypeId = (String JavaDoc) context.get("periodTypeId");
57         Timestamp JavaDoc findDate = (Timestamp JavaDoc) context.get("findDate");
58         
59         // default findDate to now
60
if (findDate == null) {
61             findDate = UtilDateTime.nowTimestamp();
62         }
63         
64         Timestamp JavaDoc lastClosedDate = null; // return parameters
65
GenericValue lastClosedTimePeriod = null;
66         Map JavaDoc result = ServiceUtil.returnSuccess();
67         
68         try {
69             // try to get the ending date of the most recent accounting time period before findDate which has been closed
70
List JavaDoc findClosedConditions = UtilMisc.toList(new EntityExpr("organizationPartyId", EntityOperator.EQUALS, organizationPartyId),
71                     new EntityExpr("thruDate", EntityOperator.LESS_THAN_EQUAL_TO, findDate),
72                     new EntityExpr("isClosed", EntityOperator.EQUALS, "Y"));
73             if ((periodTypeId != null) && !(periodTypeId.equals(""))) {
74                 // if a periodTypeId was supplied, use it
75
findClosedConditions.add(new EntityExpr("periodTypeId", EntityOperator.EQUALS, periodTypeId));
76             }
77             List JavaDoc closedTimePeriods = delegator.findByCondition("CustomTimePeriod", new EntityConditionList(findClosedConditions, EntityOperator.AND),
78                     UtilMisc.toList("customTimePeriodId", "periodTypeId", "isClosed", "fromDate", "thruDate"),
79                     UtilMisc.toList("thruDate DESC"));
80
81             if ((closedTimePeriods != null) && (closedTimePeriods.size() > 0) && (((GenericValue) closedTimePeriods.get(0)).get("thruDate") != null)) {
82                 lastClosedTimePeriod = (GenericValue) closedTimePeriods.get(0);
83                 lastClosedDate = UtilDateTime.toTimestamp(lastClosedTimePeriod.getDate("thruDate"));
84             } else {
85                 // uh oh, no time periods have been closed? in that case, just find the earliest beginning of a time period for this organization
86
// and optionally, for this period type
87
Map JavaDoc findParams = UtilMisc.toMap("organizationPartyId", organizationPartyId);
88                 if ((periodTypeId != null) && !(periodTypeId.equals(""))) {
89                     findParams.put("periodTypeId", periodTypeId);
90                 }
91                 List JavaDoc timePeriods = delegator.findByAnd("CustomTimePeriod", findParams, UtilMisc.toList("fromDate ASC"));
92                 if ((timePeriods != null) && (timePeriods.size() > 0) && (((GenericValue) timePeriods.get(0)).get("fromDate") != null)) {
93                     lastClosedDate = UtilDateTime.toTimestamp(((GenericValue) timePeriods.get(0)).getDate("fromDate"));
94                 } else {
95                     return ServiceUtil.returnError("Cannot get a starting date for net income");
96                 }
97             }
98             
99             result.put("lastClosedTimePeriod", lastClosedTimePeriod); // ok if this is null - no time periods have been closed
100
result.put("lastClosedDate", lastClosedDate); // should have a value - not null
101
return result;
102         } catch (GenericEntityException ex) {
103             return(ServiceUtil.returnError(ex.getMessage()));
104         }
105     }
106 }
Popular Tags