KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: $
3  *
4  * Copyright 2006-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18
19 /*
20  * Utilities for time periods
21  * @author Leon Torres (leon@opensourcestrategies.com)
22  */

23
24 package org.ofbiz.common.period;
25
26 import java.sql.Timestamp JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.ofbiz.base.util.UtilDateTime;
30 import org.ofbiz.base.util.UtilMisc;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.entity.condition.EntityCondition;
33 import org.ofbiz.entity.condition.EntityConditionList;
34 import org.ofbiz.entity.condition.EntityExpr;
35 import org.ofbiz.entity.condition.EntityOperator;
36
37 public class PeriodWorker {
38
39     public static String JavaDoc module = PeriodWorker.class.getName();
40     
41     /**
42      * Method to get a condition that checks that the given fieldName is in a given timePeriod.
43      */

44     public static EntityCondition getFilterByPeriodExpr(String JavaDoc fieldName, GenericValue timePeriod) {
45         Timestamp JavaDoc fromDate;
46         Timestamp JavaDoc thruDate;
47         if (timePeriod.get("fromDate") instanceof Timestamp JavaDoc) {
48             fromDate = timePeriod.getTimestamp("fromDate");
49             thruDate = timePeriod.getTimestamp("thruDate");
50         } else {
51             fromDate = UtilDateTime.toTimestamp(timePeriod.getDate("fromDate"));
52             thruDate = UtilDateTime.toTimestamp(timePeriod.getDate("thruDate"));
53         }
54
55         EntityConditionList betweenCondition = new EntityConditionList(UtilMisc.toList(
56                     new EntityExpr( fieldName, EntityOperator.GREATER_THAN, fromDate ),
57                     new EntityExpr( fieldName, EntityOperator.LESS_THAN_EQUAL_TO, thruDate )
58                     ), EntityOperator.AND);
59         List JavaDoc conditions = UtilMisc.toList(new EntityExpr( fieldName, EntityOperator.NOT_EQUAL, null ), betweenCondition);
60         return new EntityConditionList(UtilMisc.toList(conditions), EntityOperator.AND);
61     }
62 }
63
Popular Tags