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.geo;
019:
020: import org.ofbiz.entity.GenericValue;
021: import org.ofbiz.entity.GenericEntityException;
022: import org.ofbiz.entity.GenericDelegator;
023: import org.ofbiz.base.util.UtilMisc;
024: import org.ofbiz.base.util.Debug;
025:
026: import java.util.List;
027: import java.util.LinkedList;
028: import java.util.Iterator;
029: import java.util.ArrayList;
030: import java.util.Set;
031:
032: import javolution.util.FastSet;
033:
034: /**
035: * Worker methods for Geos
036: */
037: public class GeoWorker {
038:
039: public static final String module = GeoWorker.class.getName();
040:
041: public static List expandGeoGroup(String geoId,
042: GenericDelegator delegator) {
043: GenericValue geo = null;
044: try {
045: geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc
046: .toMap("geoId", geoId));
047: } catch (GenericEntityException e) {
048: Debug.logError(e, "Unable to look up Geo from geoId : "
049: + geoId, module);
050: }
051: return expandGeoGroup(geo);
052: }
053:
054: public static List expandGeoGroup(GenericValue geo) {
055: if (geo == null) {
056: return new ArrayList();
057: }
058: if (!"GROUP".equals(geo.getString("geoTypeId"))) {
059: return UtilMisc.toList(geo);
060: }
061:
062: //Debug.log("Expanding geo : " + geo, module);
063:
064: List geoList = new LinkedList();
065: List this GeoAssoc = null;
066: try {
067: this GeoAssoc = geo.getRelated("AssocGeoAssoc", UtilMisc
068: .toMap("geoAssocTypeId", "GROUP_MEMBER"), null);
069: } catch (GenericEntityException e) {
070: Debug
071: .logError(
072: e,
073: "Unable to get associated Geo GROUP_MEMBER relationship(s)",
074: module);
075: }
076: if (this GeoAssoc != null && this GeoAssoc.size() > 0) {
077: Iterator gi = this GeoAssoc.iterator();
078: while (gi.hasNext()) {
079: GenericValue nextGeoAssoc = (GenericValue) gi.next();
080: GenericValue nextGeo = null;
081: try {
082: nextGeo = nextGeoAssoc.getRelatedOne("MainGeo");
083: } catch (GenericEntityException e) {
084: Debug.logError(e, "Unable to get related Geo",
085: module);
086: }
087: geoList.addAll(expandGeoGroup(nextGeo));
088: }
089: } else {
090: //Debug.log("No associated geos with this group", module);
091: }
092:
093: //Debug.log("Expanded to : " + geoList, module);
094:
095: return geoList;
096: }
097:
098: public static Set expandGeoRegionDeep(Set geoIdSet,
099: GenericDelegator delegator) throws GenericEntityException {
100: if (geoIdSet == null || geoIdSet.size() == 0) {
101: return geoIdSet;
102: }
103: Set geoIdSetTemp = FastSet.newInstance();
104: Iterator geoIdIter = geoIdSet.iterator();
105: while (geoIdIter.hasNext()) {
106: String curGeoId = (String) geoIdIter.next();
107: List geoAssocList = delegator.findByAndCache("GeoAssoc",
108: UtilMisc.toMap("geoIdTo", curGeoId,
109: "geoAssocTypeId", "REGIONS"));
110: Iterator geoAssocIter = geoAssocList.iterator();
111: while (geoAssocIter.hasNext()) {
112: GenericValue geoAssoc = (GenericValue) geoAssocIter
113: .next();
114: geoIdSetTemp.add(geoAssoc.get("geoId"));
115: }
116: }
117: geoIdSetTemp = expandGeoRegionDeep(geoIdSetTemp, delegator);
118: Set geoIdSetNew = FastSet.newInstance();
119: geoIdSetNew.addAll(geoIdSet);
120: geoIdSetNew.addAll(geoIdSetTemp);
121: return geoIdSetNew;
122: }
123:
124: public static boolean containsGeo(List geoList, String geoId,
125: GenericDelegator delegator) {
126: GenericValue geo = null;
127: try {
128: geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc
129: .toMap("geoId", geoId));
130: } catch (GenericEntityException e) {
131: Debug.logError(e, "Unable to look up Geo from geoId : "
132: + geoId, module);
133: }
134: return containsGeo(geoList, geo);
135: }
136:
137: public static boolean containsGeo(List geoList, GenericValue geo) {
138: if (geoList == null || geo == null) {
139: return false;
140: }
141: //Debug.log("Contains Geo : " + geoList.contains(geo));
142: return geoList.contains(geo);
143: }
144: }
|