1 package org
.argeo
.geotools
.jcr
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Collections
;
8 import java
.util
.TreeMap
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.ArgeoException
;
17 import org
.argeo
.geotools
.GeoToolsConstants
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.argeo
.jcr
.gis
.GisTypes
;
20 import org
.geotools
.data
.DataStore
;
21 import org
.geotools
.data
.FeatureSource
;
22 import org
.opengis
.feature
.simple
.SimpleFeature
;
23 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
24 import org
.opengis
.feature
.type
.Name
;
26 public class SimpleGeoJcrMapper
implements GeoJcrMapper
{
27 private final static Log log
= LogFactory
.getLog(SimpleGeoJcrMapper
.class);
29 private String dataStoresBasePath
= "/gis/dataStores";
31 private Map
<String
, DataStore
> registeredDataStores
= Collections
32 .synchronizedSortedMap(new TreeMap
<String
, DataStore
>());
34 private Session session
;
36 public Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> getPossibleFeatureSources() {
37 Map
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>> res
= new TreeMap
<String
, List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>>();
38 dataStores
: for (String alias
: registeredDataStores
.keySet()) {
39 DataStore dataStore
= registeredDataStores
.get(alias
);
42 names
= dataStore
.getNames();
43 } catch (IOException e
) {
44 log
.warn("Cannot list features sources of data store " + alias
,
48 List
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>> lst
= new ArrayList
<FeatureSource
<SimpleFeatureType
, SimpleFeature
>>();
49 for (Name name
: names
) {
51 lst
.add(dataStore
.getFeatureSource(name
));
52 } catch (IOException e
) {
53 if (log
.isTraceEnabled())
54 log
.trace("Skipping " + name
+ " of data store "
55 + alias
+ " because it is probably"
56 + " not a feature source", e
);
64 public Node
getNode(String dataStoreAlias
,
65 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
,
66 SimpleFeature feature
) {
67 StringBuffer pathBuf
= new StringBuffer(dataStoresBasePath
);
68 pathBuf
.append('/').append(dataStoreAlias
);
69 pathBuf
.append('/').append(featureSource
.getName());
71 // TODO: use centroid or bbox to create some depth
72 // Geometry geometry = (Geometry)feature.getDefaultGeometry();
73 // Point centroid = geometry.getCentroid();
75 pathBuf
.append('/').append(feature
.getID());
77 String path
= pathBuf
.toString();
79 if (session
.itemExists(path
))
80 return session
.getNode(path
);
82 return JcrUtils
.mkdirs(session
, path
);
83 } catch (RepositoryException e
) {
84 throw new ArgeoException("Cannot get feature node for " + path
, e
);
88 protected Node
getNode(String dataStoreAlias
) {
91 if (!session
.itemExists(dataStoresBasePath
))
92 dataStores
= JcrUtils
.mkdirs(session
, dataStoresBasePath
);
94 dataStores
= session
.getNode(dataStoresBasePath
);
97 if (dataStores
.hasNode(dataStoreAlias
))
98 dataStoreNode
= dataStores
.getNode(dataStoreAlias
);
100 dataStoreNode
= dataStores
.addNode(dataStoreAlias
,
101 GisTypes
.GIS_DATA_STORE
);
102 return dataStoreNode
;
103 } catch (RepositoryException e
) {
104 throw new ArgeoException("Cannot get node for data store "
105 + dataStoreAlias
, e
);
109 public Node
getNode(String dataStoreAlias
,
110 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
) {
112 String name
= featureSource
.getName().toString();
113 Node dataStoreNode
= getNode(dataStoreAlias
);
114 if (dataStoreNode
.hasNode(name
))
115 return dataStoreNode
.getNode(name
);
117 return dataStoreNode
.addNode(name
, GisTypes
.GIS_FEATURE_SOURCE
);
118 } catch (RepositoryException e
) {
119 throw new ArgeoException(
120 "Cannot get feature source node for data store "
121 + dataStoreAlias
+ " and feature source "
122 + featureSource
.getName(), e
);
126 public FeatureSource
<SimpleFeatureType
, SimpleFeature
> getFeatureSource(
129 Node dataStoreNode
= node
.getParent();
130 // TODO: check a dataStore type
131 if (!registeredDataStores
.containsKey(dataStoreNode
.getName()))
132 throw new ArgeoException("No data store registered under "
134 DataStore dataStore
= registeredDataStores
.get(dataStoreNode
.getName());
135 return dataStore
.getFeatureSource(node
.getName());
136 } catch (Exception e
) {
137 throw new ArgeoException("Cannot find feature source " + node
, e
);
141 public SimpleFeature
getFeature(Node node
) {
142 // TODO Auto-generated method stub
146 public void register(DataStore dataStore
, Map
<String
, String
> properties
) {
147 if (!properties
.containsKey(GeoToolsConstants
.ALIAS_KEY
)) {
148 log
.warn("Cannot register data store " + dataStore
149 + " since it has no '" + GeoToolsConstants
.ALIAS_KEY
153 registeredDataStores
.put(properties
.get(GeoToolsConstants
.ALIAS_KEY
),
157 public void unregister(DataStore dataStore
, Map
<String
, String
> properties
) {
158 if (!properties
.containsKey(GeoToolsConstants
.ALIAS_KEY
)) {
159 log
.warn("Cannot unregister data store " + dataStore
160 + " since it has no '" + GeoToolsConstants
.ALIAS_KEY
165 .remove(properties
.get(GeoToolsConstants
.ALIAS_KEY
));
168 public void setSession(Session session
) {
169 this.session
= session
;