-
-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesse Eichar
committed
Nov 15, 2012
1 parent
0fc112e
commit 6577bb9
Showing
11 changed files
with
457 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
web/src/main/java/org/fao/geonet/services/region/CountryMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.fao.geonet.services.region; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import jeeves.server.context.ServiceContext; | ||
|
||
import org.geotools.data.simple.SimpleFeatureSource; | ||
import org.jdom.JDOMException; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import org.opengis.filter.FilterFactory2; | ||
|
||
public class CountryMapper extends DatastoreMapper { | ||
private static final String COUNTRY_DESC = "DESC"; | ||
private static final String COUNTRY_NAME = "LAND"; | ||
private static final String COUNTRY_ID = "ID"; | ||
private static final String CATEGORY_ID = "country"; | ||
private static final String PREFIX = CATEGORY_ID+":"; | ||
private static final String SIMPLIFIED_BACKING_DS = "countries"; | ||
private static final String BACKING_DS = "countries_search"; | ||
|
||
public CountryMapper(ServiceContext context, DatastoreCache datastoreCache, FilterFactory2 filterFactory, WeakHashMap<String,Map<String,String>> categoryIdMap) { | ||
super(context, categoryIdMap, filterFactory, datastoreCache); | ||
} | ||
@Override | ||
public boolean accepts(String regionId) { | ||
return regionId.startsWith(PREFIX); | ||
} | ||
|
||
@Override | ||
public String categoryId() { | ||
return CATEGORY_ID; | ||
} | ||
|
||
@Override | ||
protected String idPropertyName() { | ||
return COUNTRY_ID; | ||
} | ||
|
||
@Override | ||
public String[] propNames(boolean simplified, boolean includeGeom) { | ||
if(simplified) { | ||
if(includeGeom) { | ||
return new String[]{COUNTRY_ID, COUNTRY_NAME, COUNTRY_DESC, THE_GEOM}; | ||
} else { | ||
return new String[]{COUNTRY_ID, COUNTRY_NAME, COUNTRY_DESC}; | ||
} | ||
} else { | ||
if(includeGeom) { | ||
return new String[]{COUNTRY_ID, COUNTRY_NAME, THE_GEOM}; | ||
} else { | ||
return new String[]{COUNTRY_ID, COUNTRY_NAME}; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Region constructRegion(SimpleFeature next) throws JDOMException, IOException { | ||
return super.constructRegion(next, PREFIX,COUNTRY_NAME, CATEGORY_ID); | ||
} | ||
|
||
@Override | ||
public String getBackingDatastoreName(boolean simplified) { | ||
return simplified ? SIMPLIFIED_BACKING_DS: BACKING_DS ; | ||
} | ||
|
||
@Override | ||
protected SimpleFeatureSource getFeatureSource(boolean simplified) throws IOException { | ||
return datastoreCache.getCached(context, this, simplified); | ||
} | ||
|
||
} |
57 changes: 26 additions & 31 deletions
57
web/src/main/java/org/fao/geonet/services/region/DatastoreCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,55 @@ | ||
package org.fao.geonet.services.region; | ||
|
||
import static org.fao.geonet.services.region.GeocatRegionsRequest.*; | ||
import java.io.IOException; | ||
|
||
import jeeves.server.context.ServiceContext; | ||
|
||
import org.fao.geonet.constants.Geonet; | ||
import org.geotools.data.DataStore; | ||
import org.geotools.data.Query; | ||
import org.geotools.data.SchemaNotFoundException; | ||
import org.geotools.data.memory.MemoryDataStore; | ||
import org.geotools.data.simple.SimpleFeatureCollection; | ||
import org.geotools.data.simple.SimpleFeatureSource; | ||
import org.geotools.data.simple.SimpleFeatureStore; | ||
import org.geotools.feature.simple.SimpleFeatureTypeBuilder; | ||
import org.opengis.feature.type.AttributeDescriptor; | ||
import org.opengis.filter.Filter; | ||
|
||
public class DatastoreCache { | ||
private static final String THE_GEOM = "the_geom"; | ||
private MemoryDataStore cache = null; | ||
|
||
public synchronized SimpleFeatureSource getKantons(ServiceContext context) throws IOException { | ||
SimpleFeatureSource featureSource = cache.getFeatureSource(KANTON); | ||
if(featureSource == null) { | ||
featureSource = loadIntoMemory(context, "kantoneBB", KANTON, new String[]{KANTONE_NAME, THE_GEOM}); | ||
} | ||
return featureSource; | ||
} | ||
|
||
public synchronized SimpleFeatureSource getCountries(ServiceContext context) throws IOException { | ||
SimpleFeatureSource featureSource = cache.getFeatureSource(COUNTRY); | ||
if(featureSource == null) { | ||
featureSource = loadIntoMemory(context, "country", COUNTRY, new String[]{COUNTRY_NAME, COUNTRY_DESC, THE_GEOM}); | ||
} | ||
return featureSource; | ||
} | ||
private MemoryDataStore cache = new MemoryDataStore(); | ||
|
||
public synchronized SimpleFeatureSource getGemeindens(ServiceContext context) throws IOException { | ||
DataStore postgis = (DataStore) context.getApplicationContext().getBean(Geonet.BeanId.DATASTORE); | ||
return postgis.getFeatureSource("gemeindenBB"); | ||
} | ||
|
||
|
||
private SimpleFeatureSource loadIntoMemory(ServiceContext context, String sourceTypeName, String cacheTypeName, String[] attributes) | ||
private SimpleFeatureSource loadIntoMemory(ServiceContext context, DatastoreMapper mapper, boolean simplified) | ||
throws IOException { | ||
SimpleFeatureSource featureSource; | ||
String[] propNames = mapper.propNames(simplified, true); | ||
String sourceTypeName = mapper.getBackingDatastoreName(simplified); | ||
String cacheTypeName = mapper.getBackingDatastoreName(simplified); | ||
|
||
DataStore postgis = (DataStore) context.getApplicationContext().getBean(Geonet.BeanId.DATASTORE); | ||
SimpleFeatureSource kantonBB = postgis.getFeatureSource(sourceTypeName); | ||
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); | ||
for (String att : attributes) { | ||
builder.add(kantonBB.getSchema().getDescriptor(att)); | ||
for (String att : propNames) { | ||
AttributeDescriptor descriptor = kantonBB.getSchema().getDescriptor(att); | ||
builder.add(descriptor); | ||
} | ||
builder.setName(cacheTypeName); | ||
cache.createSchema(builder.buildFeatureType()); | ||
SimpleFeatureStore featureStore = (SimpleFeatureStore) cache.getFeatureSource(cacheTypeName); | ||
featureSource = featureStore; | ||
featureStore.addFeatures(kantonBB.getFeatures(new Query(sourceTypeName, Filter.INCLUDE, attributes))); | ||
SimpleFeatureCollection features = kantonBB.getFeatures(new Query(sourceTypeName, Filter.INCLUDE, propNames)); | ||
featureStore.addFeatures(features); | ||
return featureStore; | ||
} | ||
|
||
public SimpleFeatureSource getCached(ServiceContext context, DatastoreMapper mapper, boolean simplified) throws IOException { | ||
SimpleFeatureSource featureSource; | ||
String typeName = mapper.getBackingDatastoreName(simplified); | ||
try { | ||
featureSource = cache.getFeatureSource(typeName); | ||
} catch (SchemaNotFoundException e) { | ||
featureSource = loadIntoMemory(context, mapper, simplified); | ||
} | ||
return featureSource; | ||
} | ||
|
||
|
||
} |
125 changes: 125 additions & 0 deletions
125
web/src/main/java/org/fao/geonet/services/region/DatastoreMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package org.fao.geonet.services.region; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import jeeves.server.context.ServiceContext; | ||
|
||
import org.fao.geonet.util.LangUtils; | ||
import org.geotools.data.Query; | ||
import org.geotools.data.simple.SimpleFeatureIterator; | ||
import org.geotools.data.simple.SimpleFeatureSource; | ||
import org.geotools.geometry.jts.ReferencedEnvelope; | ||
import org.jdom.JDOMException; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import org.opengis.filter.Filter; | ||
import org.opengis.filter.FilterFactory2; | ||
import org.opengis.filter.expression.Expression; | ||
|
||
import com.vividsolutions.jts.geom.Geometry; | ||
|
||
public abstract class DatastoreMapper { | ||
|
||
protected static final String THE_GEOM = "the_geom"; | ||
|
||
protected final ServiceContext context; | ||
protected final WeakHashMap<String, Map<String, String>> categoryIdMap; | ||
protected final FilterFactory2 filterFactory; | ||
protected DatastoreCache datastoreCache; | ||
|
||
public DatastoreMapper(ServiceContext context, | ||
WeakHashMap<String, Map<String, String>> categoryIdMap, FilterFactory2 filterFactory, DatastoreCache datastoreCache) { | ||
this.context = context; | ||
this.categoryIdMap = categoryIdMap; | ||
this.filterFactory = filterFactory; | ||
this.datastoreCache = datastoreCache; | ||
} | ||
|
||
public abstract boolean accepts(String regionId); | ||
|
||
protected abstract String idPropertyName(); | ||
|
||
public abstract String categoryId(); | ||
|
||
protected abstract SimpleFeatureSource getFeatureSource(boolean simplified) throws IOException; | ||
|
||
public abstract String[] propNames(boolean simplified, boolean includeGeom); | ||
|
||
public abstract Region constructRegion(SimpleFeature next) throws JDOMException, IOException; | ||
|
||
public abstract String getBackingDatastoreName(boolean simplified); | ||
|
||
public final Filter idFilter(String regionId) { | ||
Expression propertyExpression = filterFactory.property(idPropertyName()); | ||
Expression requiredValue = filterFactory.literal(regionId.substring(categoryId().length()+1)); | ||
return filterFactory.equal(propertyExpression, requiredValue, false); | ||
} | ||
|
||
public final void loadRegions(Collection<Region> results, int maxRegions,Filter filter) throws IOException, | ||
JDOMException { | ||
boolean simplified = true; | ||
SimpleFeatureSource featureSource = getFeatureSource(simplified); | ||
Query query = createQuery(simplified, maxRegions, filter); | ||
SimpleFeatureIterator features = featureSource.getFeatures(query).features(); | ||
try { | ||
while (features.hasNext()) { | ||
SimpleFeature next = features.next(); | ||
results.add(constructRegion(next)); | ||
} | ||
} finally { | ||
features.close(); | ||
} | ||
} | ||
|
||
private Query createQuery(boolean simplified, int maxRegions, Filter filter) { | ||
Query query = new Query(getBackingDatastoreName(simplified), filter); | ||
query.setMaxFeatures(maxRegions); | ||
query.setPropertyNames(propNames(simplified, false)); | ||
return query; | ||
} | ||
|
||
public Region constructRegion(SimpleFeature feature, String prefix, String labelAttName, | ||
String categoryId) throws JDOMException, IOException { | ||
|
||
String id = prefix+feature.getID(); | ||
Map<String, String> labels = new HashMap<String, String>(); | ||
String label = feature.getAttribute(labelAttName).toString(); | ||
labels.put("eng", label); | ||
labels.put("ger", label); | ||
labels.put("fre", label); | ||
labels.put("ita", label); | ||
boolean hasGeom = true; | ||
ReferencedEnvelope bbox = new ReferencedEnvelope(feature.getBounds()); | ||
Map<String, String> kantonLabels = categoryIdMap.get(categoryId); | ||
if(kantonLabels == null) { | ||
kantonLabels = LangUtils.translate(context, categoryId); | ||
categoryIdMap.put(categoryId, kantonLabels); | ||
} | ||
return new Region(id, labels, categoryId, kantonLabels, hasGeom, bbox); | ||
|
||
} | ||
|
||
public Geometry getGeometry(boolean simplified, String regionId) throws IOException { | ||
SimpleFeatureSource featureSource = getFeatureSource(simplified); | ||
SimpleFeatureIterator features = featureSource.getFeatures(idFilter(regionId)).features(); | ||
try { | ||
if(features.hasNext()) { | ||
SimpleFeature feature = features.next(); | ||
if(features.hasNext()) { | ||
throw new IllegalStateException("there is more than one region found"); | ||
} | ||
return (Geometry) feature.getDefaultGeometry(); | ||
} else { | ||
return null; | ||
} | ||
|
||
} finally { | ||
features.close(); | ||
} | ||
} | ||
|
||
|
||
} |
80 changes: 80 additions & 0 deletions
80
web/src/main/java/org/fao/geonet/services/region/GemeindenMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.fao.geonet.services.region; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import jeeves.server.context.ServiceContext; | ||
|
||
import org.fao.geonet.constants.Geonet; | ||
import org.geotools.data.DataStore; | ||
import org.geotools.data.simple.SimpleFeatureSource; | ||
import org.jdom.JDOMException; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import org.opengis.filter.FilterFactory2; | ||
|
||
public class GemeindenMapper extends DatastoreMapper { | ||
|
||
private static final String GEMEINDEN_DESC = "DESC"; | ||
private static final String GEMEINDEN_NAME = "GEMNAME"; | ||
private static final String GEMEINDEN_ID = "OBJECTVAL"; | ||
private static final String CATEGORY_ID = "gemeinden"; | ||
private static final String PREFIX = CATEGORY_ID + ":"; | ||
private static final String SIMPLIFIED_BACKING_DS = "gemeindenBB"; | ||
private static final String BACKING_DS = "gemeinden_search"; | ||
|
||
public GemeindenMapper(ServiceContext context, | ||
DatastoreCache datastoreCache, FilterFactory2 filterFactory, | ||
WeakHashMap<String, Map<String, String>> categoryIdMap) { | ||
super(context, categoryIdMap, filterFactory, datastoreCache); | ||
} | ||
|
||
@Override | ||
public boolean accepts(String regionId) { | ||
return regionId.startsWith(PREFIX); | ||
} | ||
|
||
@Override | ||
protected String idPropertyName() { | ||
return GEMEINDEN_ID; | ||
} | ||
@Override | ||
public String categoryId() { | ||
return CATEGORY_ID; | ||
} | ||
|
||
@Override | ||
public String[] propNames(boolean simplified, boolean includeGeom) { | ||
if(simplified) { | ||
if(includeGeom) { | ||
return new String[]{GEMEINDEN_ID, GEMEINDEN_NAME, GEMEINDEN_DESC, THE_GEOM}; | ||
} else { | ||
return new String[]{GEMEINDEN_ID, GEMEINDEN_NAME, GEMEINDEN_DESC}; | ||
} | ||
} else { | ||
if(includeGeom) { | ||
return new String[]{GEMEINDEN_ID, GEMEINDEN_NAME, THE_GEOM}; | ||
} else { | ||
return new String[]{GEMEINDEN_ID, GEMEINDEN_NAME}; | ||
} | ||
} | ||
} | ||
@Override | ||
public Region constructRegion(SimpleFeature next) throws JDOMException, | ||
IOException { | ||
return super.constructRegion(next, PREFIX, GEMEINDEN_NAME, CATEGORY_ID); | ||
} | ||
|
||
@Override | ||
public String getBackingDatastoreName(boolean simplified) { | ||
return simplified ? SIMPLIFIED_BACKING_DS: BACKING_DS ; | ||
} | ||
|
||
@Override | ||
protected SimpleFeatureSource getFeatureSource(boolean simplified) throws IOException { | ||
String typeName = getBackingDatastoreName(simplified); | ||
DataStore ds = context.getApplicationContext().getBean(Geonet.BeanId.DATASTORE, DataStore.class); | ||
return ds.getFeatureSource(typeName); | ||
} | ||
|
||
} |
Oops, something went wrong.