KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > common > CommonWorkers


1 /*
2  * $Id: CommonWorkers.java 6538 2006-01-23 01:47:50Z 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 package org.ofbiz.common;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javolution.util.FastList;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilMisc;
35 import org.ofbiz.base.util.UtilProperties;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericEntityException;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entity.condition.EntityCondition;
40 import org.ofbiz.entity.condition.EntityConditionList;
41 import org.ofbiz.entity.condition.EntityExpr;
42 import org.ofbiz.entity.condition.EntityOperator;
43
44 /**
45  * Common Workers
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 6538 $
50  * @since 2.0
51  */

52 public class CommonWorkers {
53     
54     public final static String JavaDoc module = CommonWorkers.class.getName();
55
56     public static List JavaDoc getCountryList(GenericDelegator delegator) {
57         List JavaDoc geoList = FastList.newInstance();
58         String JavaDoc defaultCountry = UtilProperties.getPropertyValue("general.properties", "country.geo.id.default");
59         GenericValue defaultGeo = null;
60         if (defaultCountry != null && defaultCountry.length() > 0) {
61             try {
62                 defaultGeo = delegator.findByPrimaryKeyCache("Geo", UtilMisc.toMap("geoId", defaultCountry));
63             } catch (GenericEntityException e) {
64                 Debug.logError(e, "Cannot lookup Geo", module);
65             }
66         }
67         try {
68             List JavaDoc countryGeoList = delegator.findByAndCache("Geo", UtilMisc.toMap("geoTypeId", "COUNTRY"), UtilMisc.toList("geoName"));
69             if (defaultGeo != null) {
70                 geoList.add(defaultGeo);
71                 geoList.addAll(countryGeoList);
72             } else {
73                 geoList = countryGeoList;
74             }
75         } catch (GenericEntityException e) {
76             Debug.logError(e, "Cannot lookup Geo", module);
77         }
78         return geoList;
79     }
80     
81     public static List JavaDoc getStateList(GenericDelegator delegator) {
82         List JavaDoc geoList = new ArrayList JavaDoc();
83         EntityCondition condition = new EntityConditionList(UtilMisc.toList(
84                 new EntityExpr("geoTypeId", EntityOperator.EQUALS, "STATE"), new EntityExpr("geoTypeId", EntityOperator.EQUALS, "PROVINCE")), EntityOperator.OR);
85         List JavaDoc sortList = UtilMisc.toList("geoName");
86         try {
87             geoList = delegator.findByConditionCache("Geo", condition, null, sortList);
88         } catch (GenericEntityException e) {
89             Debug.logError(e, "Cannot lookup State Geos: " + e.toString(), module);
90         }
91         return geoList;
92     }
93
94     /**
95      * Returns a list of regional geo associations.
96      */

97     public static List JavaDoc getAssociatedStateList(GenericDelegator delegator, String JavaDoc country) {
98       if ( country == null || country.length() == 0 ) {
99         // Load the system default country
100
country = UtilProperties.getPropertyValue("general.properties", "country.geo.id.default");
101       }
102       List JavaDoc geoList = new ArrayList JavaDoc();
103       Map JavaDoc geoAssocFindMap = UtilMisc.toMap("geoId", country, "geoAssocTypeId", "REGIONS");
104       List JavaDoc sortList = UtilMisc.toList("geoIdTo");
105       try {
106           geoList = delegator.findByAndCache("GeoAssoc", geoAssocFindMap, sortList);
107       } catch (GenericEntityException e) {
108           Debug.logError(e, "Cannot lookup Geo", module);
109       }
110       return geoList;
111
112     }
113 }
114
Popular Tags