svn-gvsig-desktop / branches / v10 / extensions / extPublishGeoserver / src / org / gvsig / publish / geoserver / model / GeoserverFeature.java @ 22616
History | View | Annotate | Download (12.2 KB)
1 | 19432 | jvhigon | /* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ibañez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package org.gvsig.publish.geoserver.model; |
||
42 | |||
43 | 19901 | jvhigon | |
44 | 19432 | jvhigon | import java.net.MalformedURLException; |
45 | import java.net.URI; |
||
46 | import java.net.URISyntaxException; |
||
47 | import java.net.URL; |
||
48 | |||
49 | import org.gvsig.publish.PublishLogger; |
||
50 | 22551 | jvhigon | import org.gvsig.publish.exceptions.PublishException; |
51 | 19432 | jvhigon | import org.gvsig.publish.geoserver.conf.GSDatastore; |
52 | import org.gvsig.publish.geoserver.conf.GSFeature; |
||
53 | import org.gvsig.publish.geoserver.conf.GSNamespace; |
||
54 | import org.gvsig.publish.geoserver.conf.GSStyle; |
||
55 | import org.gvsig.publish.infoproject.IDataSourceInfo; |
||
56 | import org.gvsig.publish.infoproject.ILayerInfo; |
||
57 | import org.gvsig.publish.infoproject.datasources.IDataBaseInfo; |
||
58 | import org.gvsig.publish.infoproject.datasources.IFileInfo; |
||
59 | 19901 | jvhigon | import org.gvsig.publish.ogcmetadata.IOGCCommonMetadata; |
60 | import org.gvsig.publish.ogcmetadata.OGCCommonMetadata; |
||
61 | 19432 | jvhigon | import org.gvsig.publish.serversmodel.RemoteResource; |
62 | 21528 | jvhigon | |
63 | import com.iver.utiles.XMLEntity; |
||
64 | 19432 | jvhigon | /**
|
65 | * This class represents a remote resource (a feature) which can be offered by Geoserver
|
||
66 | 21528 | jvhigon | * This remote resource will be able to be accessed through WMS or WFS.
|
67 | * It has the responsibility of persisting the configuration.
|
||
68 | 19432 | jvhigon | *
|
69 | * @author jvhigon
|
||
70 | *
|
||
71 | */
|
||
72 | 22551 | jvhigon | public class GeoserverFeature extends RemoteResource implements IOGCCommonMetadata { |
73 | 22256 | jvhigon | //Attributes
|
74 | 21528 | jvhigon | private OGCCommonMetadata ogcMetadata= null; |
75 | 22256 | jvhigon | private String namespacePrefix =null; |
76 | 19901 | jvhigon | public static final String REGISTER_TAG="geoserver_feature"; |
77 | 22256 | jvhigon | private boolean cacheEnabled=true; |
78 | 22616 | jvhigon | private int cacheMaxage=3600; |
79 | private int numDecimals=0; |
||
80 | |||
81 | 19432 | jvhigon | /**
|
82 | * constructor
|
||
83 | * @param service
|
||
84 | */
|
||
85 | 22256 | jvhigon | public GeoserverFeature(GeoserverWFSService service) {
|
86 | 19432 | jvhigon | super(service);
|
87 | 19901 | jvhigon | ogcMetadata = new OGCCommonMetadata();
|
88 | 19432 | jvhigon | } |
89 | |||
90 | /**
|
||
91 | 22256 | jvhigon | * Overrides the method in order to initialize some variables like namespacePrefix.
|
92 | 19901 | jvhigon | * Also overrides the method in order to avoid the creation of nested resources
|
93 | 19432 | jvhigon | * @see org.gvsig.publish.serversmodel.RemoteResource#setLayerInfo(org.gvsig.publish.infoproject.ILayerInfo)
|
94 | */
|
||
95 | 22616 | jvhigon | |
96 | 19432 | jvhigon | public void setLayerInfo(ILayerInfo ilayer) { |
97 | super.setLayerInfo(ilayer);
|
||
98 | 22616 | jvhigon | setNamespacePrefix(getLayerInfo().getDataSource().getNamespacePrefix()); |
99 | setName(getFeatureTypeName()); |
||
100 | setTitle(getLayerInfo().getTitle()); |
||
101 | setAbstract("generated by gvSIG");
|
||
102 | 19432 | jvhigon | } |
103 | /**
|
||
104 | 22256 | jvhigon | *
|
105 | * @return feature name in Geoserver. It's the filename without extension for files or tablename for databases
|
||
106 | 22616 | jvhigon | *
|
107 | 19432 | jvhigon | */
|
108 | 22616 | jvhigon | private String getFeatureTypeName() { |
109 | 22256 | jvhigon | String aux="unknowfeature"; |
110 | IDataSourceInfo ds = getLayerInfo().getDataSource(); |
||
111 | if (ds instanceof IFileInfo){ |
||
112 | IFileInfo ifile = (IFileInfo)ds; |
||
113 | aux = ifile.getFilenameWithoutExtension(); |
||
114 | }else{
|
||
115 | if (ds instanceof IDataBaseInfo){ |
||
116 | IDataBaseInfo idb = (IDataBaseInfo) ds; |
||
117 | aux = idb.getTableName(); |
||
118 | 19432 | jvhigon | } |
119 | 19901 | jvhigon | } |
120 | 22256 | jvhigon | return aux;
|
121 | 19432 | jvhigon | } |
122 | 22256 | jvhigon | /*
|
123 | * (non-Javadoc)
|
||
124 | * @see com.iver.utiles.IPersistance#getClassName()
|
||
125 | */
|
||
126 | public String getClassName() { |
||
127 | return "GeoserverFeature"; |
||
128 | } |
||
129 | 22616 | jvhigon | |
130 | 19432 | jvhigon | /**
|
131 | *
|
||
132 | * @see org.gvsig.publish.serversmodel.RemoteResource#publish()
|
||
133 | */
|
||
134 | 22551 | jvhigon | public void publish() throws PublishException{ |
135 | 22616 | jvhigon | //adds this feature to the server configuration
|
136 | addFeatureToConfig(); |
||
137 | 22256 | jvhigon | //adds its childs to the server configuration
|
138 | 19901 | jvhigon | super.publish();
|
139 | 19432 | jvhigon | } |
140 | 19901 | jvhigon | /**
|
141 | 22256 | jvhigon | * Adds this remote resource into the server configuration
|
142 | 22616 | jvhigon | * @throws PublishException
|
143 | 19901 | jvhigon | */
|
144 | 22616 | jvhigon | private void addFeatureToConfig() throws PublishException{ |
145 | 22256 | jvhigon | GSFeature config = new GSFeature(this); |
146 | //create namespace
|
||
147 | GSNamespace ns = create_namespace(); |
||
148 | //create datastore
|
||
149 | GSDatastore ds = create_datastore(config, ns); |
||
150 | //create style
|
||
151 | GSStyle st = create_style(); |
||
152 | //create feature
|
||
153 | config.setDatastore(ds); |
||
154 | config.setNamespace(ns); |
||
155 | config.setStyle(st); |
||
156 | 22616 | jvhigon | |
157 | 22256 | jvhigon | //adds feature
|
158 | Geoserver server= (Geoserver) getService().getServer(); |
||
159 | 22616 | jvhigon | |
160 | 22256 | jvhigon | boolean isadded = server.getConfiguration().addFeatureType(config);
|
161 | if (isadded == false && getLayerInfo().getDataSource().getType()==IDataSourceInfo.SHAPE_TYPE){ |
||
162 | PublishLogger.getLog().error("ERROR GeoserverFeature: cannot add a feature because you can't have two features with the same shapefile in the same namespace");
|
||
163 | this.remove();
|
||
164 | throw new PublishException("cannot_publish_two_remoteresources_with_same_shape"); |
||
165 | 22551 | jvhigon | } |
166 | 19432 | jvhigon | } |
167 | 22256 | jvhigon | |
168 | 19432 | jvhigon | /**
|
169 | 19901 | jvhigon | * TODO: at the moment only one namespace is possible (default)
|
170 | 19432 | jvhigon | * @return
|
171 | */
|
||
172 | private GSNamespace create_namespace(){
|
||
173 | 22256 | jvhigon | GSNamespace ns = new GSNamespace();
|
174 | ns.setPrefix(getNamespacePrefix()); |
||
175 | 19432 | jvhigon | try {
|
176 | ns.setURI(new URI(getService().getServer().getServerURL().toString())); |
||
177 | } catch (URISyntaxException e1) { |
||
178 | PublishLogger.getLog().error("ERROR " + getClassName() + ":uri syntax error", e1); |
||
179 | } |
||
180 | return ns;
|
||
181 | } |
||
182 | /**
|
||
183 | *
|
||
184 | * @param ns
|
||
185 | * @return
|
||
186 | */
|
||
187 | 22616 | jvhigon | private GSDatastore create_datastore(GSFeature feature, GSNamespace ns) {
|
188 | 19432 | jvhigon | GSDatastore ds = new GSDatastore();
|
189 | ds.setNamespace(ns); |
||
190 | 19901 | jvhigon | IDataSourceInfo dataSrc = getLayerInfo().getDataSource(); |
191 | 19432 | jvhigon | if (dataSrc.getType().equals(IDataSourceInfo.SHAPE_TYPE)){
|
192 | 22256 | jvhigon | config_datastore_shape(feature, ds); |
193 | 19432 | jvhigon | } |
194 | if (dataSrc.getType().equals(IDataSourceInfo.POSTGIS_TYPE)){
|
||
195 | 22256 | jvhigon | config_datastore_postgis(feature, ds); |
196 | 19432 | jvhigon | } |
197 | return ds;
|
||
198 | } |
||
199 | 22616 | jvhigon | /**
|
200 | * Creates the datastore configuration for Geoserver
|
||
201 | *
|
||
202 | * @param feature
|
||
203 | * @param ds
|
||
204 | */
|
||
205 | private void config_datastore_postgis(GSFeature feature, GSDatastore ds) { |
||
206 | 22588 | jvhigon | String ds_id = ((IDataBaseInfo)getLayerInfo().getDataSource()).getConnectionName();
|
207 | ds_id = ds_id + "_" + ((IDataBaseInfo)getLayerInfo().getDataSource()).getSchemaName();
|
||
208 | ds.setId(ds_id); |
||
209 | 19432 | jvhigon | ds.setAbstract("Datastore description generated by gvSIG");
|
210 | ds.setLooseBbox(true);
|
||
211 | ds.setUser(((IDataBaseInfo)getLayerInfo().getDataSource()).getUser()); |
||
212 | ds.setPassword(((IDataBaseInfo)getLayerInfo().getDataSource()).getPassword()); |
||
213 | ds.setEstimatedExtent(false);
|
||
214 | ds.setHost(((IDataBaseInfo)getLayerInfo().getDataSource()).getHost()); |
||
215 | ds.setSchema(((IDataBaseInfo)getLayerInfo().getDataSource()).getSchemaName()); |
||
216 | ds.setPort(new Integer(((IDataBaseInfo)getLayerInfo().getDataSource()).getPort()).intValue()); |
||
217 | ds.setDatabase(((IDataBaseInfo)getLayerInfo().getDataSource()).getDatabaseName()); |
||
218 | ds.setDbType("postgis");
|
||
219 | 22256 | jvhigon | //the feature id is namespace prefix + "_" + tablename
|
220 | String id = getLayerInfo().getDataSource().getNamespacePrefix() + "_" + ((IDataBaseInfo)getLayerInfo().getDataSource()).getTableName(); |
||
221 | feature.setId(id); |
||
222 | 19432 | jvhigon | } |
223 | 22616 | jvhigon | /**
|
224 | * Creates the datastore configuration for Geoserver
|
||
225 | * @param feature
|
||
226 | * @param ds
|
||
227 | *
|
||
228 | */
|
||
229 | private void config_datastore_shape(GSFeature feature, GSDatastore ds) { |
||
230 | |||
231 | 22256 | jvhigon | ds.setId(((IFileInfo)getLayerInfo().getDataSource()).getFilenameWithoutExtension()); |
232 | 19432 | jvhigon | //TODO: get this from IDatasourceInfo
|
233 | ds.setCharset("UTF-8");
|
||
234 | try {
|
||
235 | 22275 | jvhigon | IDataSourceInfo datasource = getLayerInfo().getDataSource(); |
236 | String f = getService().getServer().getPublication().getServerSideFilePath(datasource);
|
||
237 | 19432 | jvhigon | ds.setURL(new URL("file:" + f)); |
238 | } catch (MalformedURLException e) { |
||
239 | PublishLogger.getLog().error("ERROR: " + getClassName() + ":malformed url", e); |
||
240 | 22256 | jvhigon | } |
241 | //the feature id is namespace + "_" + filename without extension
|
||
242 | String id = getLayerInfo().getDataSource().getNamespacePrefix() + "_" + getId(); |
||
243 | feature.setId(id); |
||
244 | 19432 | jvhigon | } |
245 | /**
|
||
246 | 22616 | jvhigon | * Creates the style configuration for Geoserver
|
247 | 19432 | jvhigon | *
|
248 | * @return
|
||
249 | 22616 | jvhigon | *
|
250 | 19432 | jvhigon | */
|
251 | 22616 | jvhigon | private GSStyle create_style(){
|
252 | 19432 | jvhigon | //create style
|
253 | GSStyle st = new GSStyle();
|
||
254 | 22588 | jvhigon | st.setId(getId()); |
255 | 19432 | jvhigon | if (getLayerInfo().getLegend()!= null){ |
256 | st.setSLDString(getLayerInfo().getLegend().getSLDString()); |
||
257 | } |
||
258 | return st;
|
||
259 | } |
||
260 | 22616 | jvhigon | |
261 | 19901 | jvhigon | /*
|
262 | * (non-Javadoc)
|
||
263 | * @see org.gvsig.publish.serversmodel.RemoteResource#getRegisterTag()
|
||
264 | */
|
||
265 | public String getRegisterTag() { |
||
266 | 22616 | jvhigon | |
267 | 19901 | jvhigon | return REGISTER_TAG;
|
268 | } |
||
269 | /*
|
||
270 | * (non-Javadoc)
|
||
271 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getAbstract()
|
||
272 | */
|
||
273 | public String getAbstract() { |
||
274 | 22616 | jvhigon | |
275 | 19901 | jvhigon | return ogcMetadata.getAbstract();
|
276 | } |
||
277 | /*
|
||
278 | * (non-Javadoc)
|
||
279 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getName()
|
||
280 | */
|
||
281 | public String getName() { |
||
282 | 22616 | jvhigon | |
283 | 19901 | jvhigon | return ogcMetadata.getName();
|
284 | } |
||
285 | /*
|
||
286 | * (non-Javadoc)
|
||
287 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getTitle()
|
||
288 | */
|
||
289 | public String getTitle() { |
||
290 | 22616 | jvhigon | |
291 | 19901 | jvhigon | return ogcMetadata.getTitle();
|
292 | } |
||
293 | /*
|
||
294 | * (non-Javadoc)
|
||
295 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#setAbstract(java.lang.String)
|
||
296 | */
|
||
297 | public void setAbstract(String description) { |
||
298 | 21528 | jvhigon | ogcMetadata.setAbstract(description); |
299 | 19901 | jvhigon | } |
300 | /*
|
||
301 | * (non-Javadoc)
|
||
302 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#setName(java.lang.String)
|
||
303 | */
|
||
304 | public void setName(String name) { |
||
305 | ogcMetadata.setName(name); |
||
306 | 22616 | jvhigon | |
307 | 19901 | jvhigon | } |
308 | /*
|
||
309 | * (non-Javadoc)
|
||
310 | * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#setTitle(java.lang.String)
|
||
311 | */
|
||
312 | public void setTitle(String title) { |
||
313 | ogcMetadata.setTitle(title); |
||
314 | } |
||
315 | /*
|
||
316 | * (non-Javadoc)
|
||
317 | * @see org.gvsig.publish.IPublishPersistence#getVersion()
|
||
318 | */
|
||
319 | public int getVersion() { |
||
320 | return 1; |
||
321 | } |
||
322 | /*
|
||
323 | * (non-Javadoc)
|
||
324 | * @see java.lang.Object#toString()
|
||
325 | */
|
||
326 | 22616 | jvhigon | public String toString(){ |
327 | return getTitle();
|
||
328 | } |
||
329 | /* (non-Javadoc)
|
||
330 | 21528 | jvhigon | * @see org.gvsig.publish.serversmodel.RemoteResource#getXMLEntity()
|
331 | */
|
||
332 | 22616 | jvhigon | |
333 | 21528 | jvhigon | public XMLEntity getXMLEntity() {
|
334 | XMLEntity xml=super.getXMLEntity();
|
||
335 | //put version and name
|
||
336 | xml.setName(getClassName()); |
||
337 | xml.putProperty("version", getVersion());
|
||
338 | //put associations
|
||
339 | XMLEntity child = new XMLEntity();
|
||
340 | child.setName("ogcmetadata");
|
||
341 | child.addChild(ogcMetadata.getXMLEntity()); |
||
342 | xml.addChild(child); |
||
343 | //put config properties
|
||
344 | 22256 | jvhigon | xml.putProperty("namespace_prefix", getNamespacePrefix());
|
345 | xml.putProperty("is_cached", isCacheEnabled());
|
||
346 | xml.putProperty("cache_maxage", getCacheMaxage());
|
||
347 | 21528 | jvhigon | return xml;
|
348 | } |
||
349 | |||
350 | /* (non-Javadoc)
|
||
351 | * @see org.gvsig.publish.serversmodel.RemoteResource#setXMLEntity(com.iver.utiles.XMLEntity)
|
||
352 | */
|
||
353 | 22616 | jvhigon | |
354 | 21528 | jvhigon | public void setXMLEntity(XMLEntity xml) { |
355 | //check version
|
||
356 | int version = xml.getIntProperty("version"); |
||
357 | if (version != getVersion()){
|
||
358 | PublishLogger.getLog().error("ERROR: " + getClassName() + ": the version doesn't match!"); |
||
359 | return;
|
||
360 | } |
||
361 | super.setXMLEntity(xml);
|
||
362 | //set associations
|
||
363 | XMLEntity child = xml.firstChild("name","ogcmetadata"); |
||
364 | ogcMetadata.setXMLEntity(child.getChild(0));
|
||
365 | //set config properties
|
||
366 | 22256 | jvhigon | setNamespacePrefix(xml.getStringProperty("namespace_prefix"));
|
367 | setCacheEnabled(xml.getBooleanProperty("is_cached"));
|
||
368 | setCacheMaxage(xml.getIntProperty("cache_maxage"));
|
||
369 | 21528 | jvhigon | } |
370 | 22616 | jvhigon | |
371 | 22256 | jvhigon | /**
|
372 | * @return the cacheEnabled
|
||
373 | */
|
||
374 | public boolean isCacheEnabled() { |
||
375 | return cacheEnabled;
|
||
376 | } |
||
377 | 21528 | jvhigon | |
378 | 22256 | jvhigon | /**
|
379 | * @param cacheEnabled the cacheEnabled to set
|
||
380 | */
|
||
381 | public void setCacheEnabled(boolean cacheEnabled) { |
||
382 | this.cacheEnabled = cacheEnabled;
|
||
383 | } |
||
384 | |||
385 | /**
|
||
386 | * @return the cacheMaxage
|
||
387 | */
|
||
388 | public int getCacheMaxage() { |
||
389 | return cacheMaxage;
|
||
390 | } |
||
391 | |||
392 | /**
|
||
393 | * @param cacheMaxage the cacheMaxage to set
|
||
394 | */
|
||
395 | public void setCacheMaxage(int cacheMaxage) { |
||
396 | this.cacheMaxage = cacheMaxage;
|
||
397 | } |
||
398 | |||
399 | /**
|
||
400 | * @return the numDecimals
|
||
401 | */
|
||
402 | public int getNumDecimals() { |
||
403 | return numDecimals;
|
||
404 | } |
||
405 | |||
406 | /**
|
||
407 | * @param numDecimals the numDecimals to set
|
||
408 | */
|
||
409 | public void setNumDecimals(int numDecimals) { |
||
410 | this.numDecimals = numDecimals;
|
||
411 | } |
||
412 | |||
413 | /**
|
||
414 | * @return the namespacePrefix
|
||
415 | */
|
||
416 | public String getNamespacePrefix() { |
||
417 | return namespacePrefix;
|
||
418 | } |
||
419 | |||
420 | /**
|
||
421 | * @param namespacePrefix the namespacePrefix to set
|
||
422 | */
|
||
423 | public void setNamespacePrefix(String namespacePrefix) { |
||
424 | this.namespacePrefix = namespacePrefix;
|
||
425 | } |
||
426 | 22616 | jvhigon | |
427 | 19432 | jvhigon | } |