1 24 package org.ofbiz.common.geo; 25 26 import org.ofbiz.entity.GenericValue; 27 import org.ofbiz.entity.GenericEntityException; 28 import org.ofbiz.entity.GenericDelegator; 29 import org.ofbiz.base.util.UtilMisc; 30 import org.ofbiz.base.util.Debug; 31 32 import java.util.List ; 33 import java.util.LinkedList ; 34 import java.util.Iterator ; 35 import java.util.ArrayList ; 36 import java.util.Set ; 37 38 import javolution.util.FastSet; 39 40 47 public class GeoWorker { 48 49 public static final String module = GeoWorker.class.getName(); 50 51 public static List expandGeoGroup(String geoId, GenericDelegator delegator) { 52 GenericValue geo = null; 53 try { 54 geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc.toMap("geoId", geoId)); 55 } catch (GenericEntityException e) { 56 Debug.logError(e, "Unable to look up Geo from geoId : " + geoId, module); 57 } 58 return expandGeoGroup(geo); 59 } 60 61 public static List expandGeoGroup(GenericValue geo) { 62 if (geo == null) { 63 return new ArrayList (); 64 } 65 if (!"GROUP".equals(geo.getString("geoTypeId"))) { 66 return UtilMisc.toList(geo); 67 } 68 69 71 List geoList = new LinkedList (); 72 List thisGeoAssoc = null; 73 try { 74 thisGeoAssoc = geo.getRelated("AssocGeoAssoc", UtilMisc.toMap("geoAssocTypeId", "GROUP_MEMBER"), null); 75 } catch (GenericEntityException e) { 76 Debug.logError(e, "Unable to get associated Geo GROUP_MEMBER relationship(s)", module); 77 } 78 if (thisGeoAssoc != null && thisGeoAssoc.size() > 0) { 79 Iterator gi = thisGeoAssoc.iterator(); 80 while (gi.hasNext()) { 81 GenericValue nextGeoAssoc = (GenericValue) gi.next(); 82 GenericValue nextGeo = null; 83 try { 84 nextGeo = nextGeoAssoc.getRelatedOne("MainGeo"); 85 } catch (GenericEntityException e) { 86 Debug.logError(e, "Unable to get related Geo", module); 87 } 88 geoList.addAll(expandGeoGroup(nextGeo)); 89 } 90 } else { 91 } 93 94 96 return geoList; 97 } 98 99 public static Set expandGeoRegionDeep(Set geoIdSet, 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", UtilMisc.toMap("geoIdTo", curGeoId, "geoAssocTypeId", "REGIONS")); 108 Iterator geoAssocIter = geoAssocList.iterator(); 109 while (geoAssocIter.hasNext()) { 110 GenericValue geoAssoc = (GenericValue) geoAssocIter.next(); 111 geoIdSetTemp.add(geoAssoc.get("geoId")); 112 } 113 } 114 geoIdSetTemp = expandGeoRegionDeep(geoIdSetTemp, delegator); 115 Set geoIdSetNew = FastSet.newInstance(); 116 geoIdSetNew.addAll(geoIdSet); 117 geoIdSetNew.addAll(geoIdSetTemp); 118 return geoIdSetNew; 119 } 120 121 public static boolean containsGeo(List geoList, String geoId, GenericDelegator delegator) { 122 GenericValue geo = null; 123 try { 124 geo = delegator.findByPrimaryKeyCache("Geo", UtilMisc.toMap("geoId", geoId)); 125 } catch (GenericEntityException e) { 126 Debug.logError(e, "Unable to look up Geo from geoId : " + geoId, module); 127 } 128 return containsGeo(geoList, geo); 129 } 130 131 public static boolean containsGeo(List geoList, GenericValue geo) { 132 if (geoList == null || geo == null) { 133 return false; 134 } 135 return geoList.contains(geo); 137 } 138 } 139 | Popular Tags |