001: /*
002: * $Id: GeoWorker.java,v 1.3 2004/02/06 17:04:48 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 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: package org.ofbiz.common.geo;
025:
026: import org.ofbiz.entity.GenericValue;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.base.util.UtilMisc;
030: import org.ofbiz.base.util.Debug;
031:
032: import java.util.List;
033: import java.util.LinkedList;
034: import java.util.Iterator;
035: import java.util.ArrayList;
036:
037: /**
038: * Worker methods for Geos
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: * @version $Revision: 1.3 $
042: * @since 3.0
043: */
044: public class GeoWorker {
045:
046: public static final String module = GeoWorker.class.getName();
047:
048: public static List expandGeoGroup(String geoId,
049: GenericDelegator delegator) {
050: GenericValue geo = null;
051: try {
052: geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc
053: .toMap("geoId", geoId));
054: } catch (GenericEntityException e) {
055: Debug.logError(e, "Unable to look up Geo from geoId : "
056: + geoId, module);
057: }
058: return expandGeoGroup(geo);
059: }
060:
061: public static List expandGeoGroup(GenericValue geo) {
062: if (geo == null) {
063: return new ArrayList();
064: }
065: if (!"GROUP".equals(geo.getString("geoTypeId"))) {
066: return UtilMisc.toList(geo);
067: }
068:
069: //Debug.log("Expanding geo : " + geo, module);
070:
071: List geoList = new LinkedList();
072: List this GeoAssoc = null;
073: try {
074: this GeoAssoc = geo.getRelated("AssocGeoAssoc", UtilMisc
075: .toMap("geoAssocTypeId", "GROUP_MEMBER"), null);
076: } catch (GenericEntityException e) {
077: Debug
078: .logError(
079: e,
080: "Unable to get associated Geo GROUP_MEMBER relationship(s)",
081: module);
082: }
083: if (this GeoAssoc != null && this GeoAssoc.size() > 0) {
084: Iterator gi = this GeoAssoc.iterator();
085: while (gi.hasNext()) {
086: GenericValue nextGeoAssoc = (GenericValue) gi.next();
087: GenericValue nextGeo = null;
088: try {
089: nextGeo = nextGeoAssoc.getRelatedOne("MainGeo");
090: } catch (GenericEntityException e) {
091: Debug.logError(e, "Unable to get related Geo",
092: module);
093: }
094: geoList.addAll(expandGeoGroup(nextGeo));
095: }
096: } else {
097: //Debug.log("No associated geos with this group", module);
098: }
099:
100: //Debug.log("Expanded to : " + geoList, module);
101:
102: return geoList;
103: }
104:
105: public static boolean containsGeo(List geoList, String geoId,
106: GenericDelegator delegator) {
107: GenericValue geo = null;
108: try {
109: geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc
110: .toMap("geoId", geoId));
111: } catch (GenericEntityException e) {
112: Debug.logError(e, "Unable to look up Geo from geoId : "
113: + geoId, module);
114: }
115: return containsGeo(geoList, geo);
116: }
117:
118: public static boolean containsGeo(List geoList, GenericValue geo) {
119: if (geoList == null || geo == null) {
120: return false;
121: }
122: //Debug.log("Contains Geo : " + geoList.contains(geo));
123: return geoList.contains(geo);
124: }
125: }
|