001: /*
002: * $Id: CommonWorkers.java,v 1.2 2003/11/27 17:42:42 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.common;
026:
027: import java.util.ArrayList;
028: import java.util.Enumeration;
029: import java.util.List;
030: import java.util.Map;
031:
032: import javax.servlet.ServletRequest;
033: import javax.servlet.jsp.PageContext;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilFormatOut;
037: import org.ofbiz.base.util.UtilMisc;
038: import org.ofbiz.base.util.UtilProperties;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.GenericEntityException;
041: import org.ofbiz.entity.GenericValue;
042:
043: /**
044: * Common Workers
045: *
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.2 $
049: * @since 2.0
050: */
051: public class CommonWorkers {
052:
053: public final static String module = CommonWorkers.class.getName();
054:
055: public static String makeLoginUrl(PageContext pageContext) {
056: return makeLoginUrl(pageContext, "checkLogin");
057: }
058:
059: public static String makeLoginUrl(ServletRequest request) {
060: return makeLoginUrl(request, "checkLogin");
061: }
062:
063: public static String makeLoginUrl(PageContext pageContext,
064: String requestName) {
065: return makeLoginUrl(pageContext.getRequest(), requestName);
066: }
067:
068: public static String makeLoginUrl(ServletRequest request,
069: String requestName) {
070: String queryString = null;
071:
072: Enumeration parameterNames = request.getParameterNames();
073:
074: while (parameterNames != null
075: && parameterNames.hasMoreElements()) {
076: String paramName = (String) parameterNames.nextElement();
077:
078: if (paramName != null) {
079: if (queryString == null)
080: queryString = paramName + "="
081: + request.getParameter(paramName);
082: else
083: queryString = queryString + "&" + paramName + "="
084: + request.getParameter(paramName);
085: }
086: }
087:
088: String loginUrl = "/"
089: + requestName
090: + "/"
091: + UtilFormatOut.checkNull((String) request
092: .getAttribute("_CURRENT_VIEW_"));
093:
094: if (queryString != null)
095: loginUrl = loginUrl + "?"
096: + UtilFormatOut.checkNull(queryString);
097:
098: return loginUrl;
099: }
100:
101: public static List getCountryList(GenericDelegator delegator) {
102: List geoList = new ArrayList();
103: String defaultCountry = UtilProperties.getPropertyValue(
104: "general.properties", "country.uom.id.default");
105: GenericValue defaultGeo = null;
106: if (defaultCountry != null && defaultCountry.length() > 0) {
107: try {
108: defaultGeo = delegator.findByPrimaryKeyCache("Geo",
109: UtilMisc.toMap("geoId", defaultCountry));
110: } catch (GenericEntityException e) {
111: Debug.logError(e, "Cannot lookup Geo", module);
112: }
113: }
114: Map findMap = UtilMisc.toMap("geoTypeId", "COUNTRY");
115: List sortList = UtilMisc.toList("geoName");
116: try {
117: geoList = delegator
118: .findByAndCache("Geo", findMap, sortList);
119: } catch (GenericEntityException e) {
120: Debug.logError(e, "Cannot lookup Geo", module);
121: }
122: if (defaultGeo != null) {
123: geoList.add(0, defaultGeo);
124: }
125: return geoList;
126: }
127:
128: public static List getStateList(GenericDelegator delegator) {
129: List geoList = new ArrayList();
130: Map findMap = UtilMisc.toMap("geoTypeId", "STATE");
131: List sortList = UtilMisc.toList("geoName");
132: try {
133: geoList = delegator
134: .findByAndCache("Geo", findMap, sortList);
135: } catch (GenericEntityException e) {
136: Debug.logError(e, "Cannot lookup Geo", module);
137: }
138: return geoList;
139: }
140:
141: /**
142: * Returns a list of regional geo associations.
143: */
144: public static List getAssociatedStateList(
145: GenericDelegator delegator, String country) {
146: if (country == null || country.length() == 0) {
147: // Load the system default country
148: country = UtilProperties.getPropertyValue(
149: "general.properties", "country.uom.id.default");
150: }
151: List geoList = new ArrayList();
152: Map geoAssocFindMap = UtilMisc.toMap("geoId", country,
153: "geoAssocTypeId", "REGIONS");
154: List sortList = UtilMisc.toList("geoIdTo");
155: try {
156: geoList = delegator.findByAndCache("GeoAssoc",
157: geoAssocFindMap, sortList);
158: } catch (GenericEntityException e) {
159: Debug.logError(e, "Cannot lookup Geo", module);
160: }
161: return geoList;
162:
163: }
164: }
|