001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.common;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javolution.util.FastList;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.base.util.UtilMisc;
028: import org.ofbiz.base.util.UtilProperties;
029: import org.ofbiz.entity.GenericDelegator;
030: import org.ofbiz.entity.GenericEntityException;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.entity.condition.EntityCondition;
033: import org.ofbiz.entity.condition.EntityConditionList;
034: import org.ofbiz.entity.condition.EntityExpr;
035: import org.ofbiz.entity.condition.EntityOperator;
036:
037: /**
038: * Common Workers
039: */
040: public class CommonWorkers {
041:
042: public final static String module = CommonWorkers.class.getName();
043:
044: public static List getCountryList(GenericDelegator delegator) {
045: List geoList = FastList.newInstance();
046: String defaultCountry = UtilProperties.getPropertyValue(
047: "general.properties", "country.geo.id.default");
048: GenericValue defaultGeo = null;
049: if (defaultCountry != null && defaultCountry.length() > 0) {
050: try {
051: defaultGeo = delegator.findByPrimaryKeyCache("Geo",
052: UtilMisc.toMap("geoId", defaultCountry));
053: } catch (GenericEntityException e) {
054: Debug.logError(e, "Cannot lookup Geo", module);
055: }
056: }
057: try {
058: List countryGeoList = delegator.findByAndCache("Geo",
059: UtilMisc.toMap("geoTypeId", "COUNTRY"), UtilMisc
060: .toList("geoName"));
061: if (defaultGeo != null) {
062: geoList.add(defaultGeo);
063: geoList.addAll(countryGeoList);
064: } else {
065: geoList = countryGeoList;
066: }
067: } catch (GenericEntityException e) {
068: Debug.logError(e, "Cannot lookup Geo", module);
069: }
070: return geoList;
071: }
072:
073: public static List getStateList(GenericDelegator delegator) {
074: List geoList = new ArrayList();
075: EntityCondition condition = new EntityConditionList(UtilMisc
076: .toList(new EntityExpr("geoTypeId",
077: EntityOperator.EQUALS, "STATE"),
078: new EntityExpr("geoTypeId",
079: EntityOperator.EQUALS, "PROVINCE")),
080: EntityOperator.OR);
081: List sortList = UtilMisc.toList("geoName");
082: try {
083: geoList = delegator.findByConditionCache("Geo", condition,
084: null, sortList);
085: } catch (GenericEntityException e) {
086: Debug.logError(e, "Cannot lookup State Geos: "
087: + e.toString(), module);
088: }
089: return geoList;
090: }
091:
092: /**
093: * Returns a list of regional geo associations.
094: */
095: public static List getAssociatedStateList(
096: GenericDelegator delegator, String country) {
097: if (country == null || country.length() == 0) {
098: // Load the system default country
099: country = UtilProperties.getPropertyValue(
100: "general.properties", "country.geo.id.default");
101: }
102: List geoList = new ArrayList();
103: Map geoAssocFindMap = UtilMisc.toMap("geoId", country,
104: "geoAssocTypeId", "REGIONS");
105: List sortList = UtilMisc.toList("geoIdTo");
106: try {
107: geoList = delegator.findByAndCache("GeoAssoc",
108: geoAssocFindMap, sortList);
109: } catch (GenericEntityException e) {
110: Debug.logError(e, "Cannot lookup Geo", module);
111: }
112: return geoList;
113:
114: }
115: }
|