KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > common > geo > GeoWorker


1 /*
2  * $Id: GeoWorker.java 6190 2005-11-24 10:16:14Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

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 JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import javolution.util.FastSet;
39
40 /**
41  * Worker methods for Geos
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 6190 $
45  * @since 3.0
46  */

47 public class GeoWorker {
48
49     public static final String JavaDoc module = GeoWorker.class.getName();
50
51     public static List JavaDoc expandGeoGroup(String JavaDoc 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 JavaDoc expandGeoGroup(GenericValue geo) {
62         if (geo == null) {
63             return new ArrayList JavaDoc();
64         }
65         if (!"GROUP".equals(geo.getString("geoTypeId"))) {
66             return UtilMisc.toList(geo);
67         }
68
69         //Debug.log("Expanding geo : " + geo, module);
70

71         List JavaDoc geoList = new LinkedList JavaDoc();
72         List JavaDoc 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 JavaDoc 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             //Debug.log("No associated geos with this group", module);
92
}
93
94         //Debug.log("Expanded to : " + geoList, module);
95

96         return geoList;
97     }
98     
99     public static Set JavaDoc expandGeoRegionDeep(Set JavaDoc geoIdSet, GenericDelegator delegator) throws GenericEntityException {
100         if (geoIdSet == null || geoIdSet.size() == 0) {
101             return geoIdSet;
102         }
103         Set JavaDoc geoIdSetTemp = FastSet.newInstance();
104         Iterator JavaDoc geoIdIter = geoIdSet.iterator();
105         while (geoIdIter.hasNext()) {
106             String JavaDoc curGeoId = (String JavaDoc) geoIdIter.next();
107             List JavaDoc geoAssocList = delegator.findByAndCache("GeoAssoc", UtilMisc.toMap("geoIdTo", curGeoId, "geoAssocTypeId", "REGIONS"));
108             Iterator JavaDoc 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 JavaDoc geoIdSetNew = FastSet.newInstance();
116         geoIdSetNew.addAll(geoIdSet);
117         geoIdSetNew.addAll(geoIdSetTemp);
118         return geoIdSetNew;
119     }
120
121     public static boolean containsGeo(List JavaDoc geoList, String JavaDoc 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 JavaDoc geoList, GenericValue geo) {
132         if (geoList == null || geo == null) {
133             return false;
134         }
135         //Debug.log("Contains Geo : " + geoList.contains(geo));
136
return geoList.contains(geo);
137     }
138 }
139
Popular Tags