Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublishGeoserver / src / org / gvsig / publish / geoserver / model / GeoserverFeature.java @ 22616

History | View | Annotate | Download (12.2 KB)

1
/* 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

    
44
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
import org.gvsig.publish.exceptions.PublishException;
51
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
import org.gvsig.publish.ogcmetadata.IOGCCommonMetadata;
60
import org.gvsig.publish.ogcmetadata.OGCCommonMetadata;
61
import org.gvsig.publish.serversmodel.RemoteResource;
62

    
63
import com.iver.utiles.XMLEntity;
64
/**
65
 * This class represents a remote resource (a feature) which can be offered by Geoserver
66
 * This remote resource will be able to be accessed through WMS or WFS. 
67
 * It has the responsibility of persisting the configuration.
68
 * 
69
 * @author jvhigon
70
 *
71
 */
72
public class GeoserverFeature extends RemoteResource implements IOGCCommonMetadata {
73
        //Attributes
74
        private OGCCommonMetadata ogcMetadata= null;
75
        private String namespacePrefix =null;
76
        public static final String REGISTER_TAG="geoserver_feature";
77
        private boolean cacheEnabled=true;
78
        private int cacheMaxage=3600;
79
        private int numDecimals=0;
80

    
81
        /**
82
         * constructor
83
         * @param service
84
         */
85
        public GeoserverFeature(GeoserverWFSService service) {
86
                super(service);                
87
                ogcMetadata = new OGCCommonMetadata();
88
        }
89

    
90
        /** 
91
         * Overrides the method in order to initialize some variables like namespacePrefix.
92
         * Also overrides the method in order to avoid the creation of nested resources 
93
         * @see org.gvsig.publish.serversmodel.RemoteResource#setLayerInfo(org.gvsig.publish.infoproject.ILayerInfo)
94
         */
95

    
96
        public void setLayerInfo(ILayerInfo ilayer) {
97
                super.setLayerInfo(ilayer);
98
                setNamespacePrefix(getLayerInfo().getDataSource().getNamespacePrefix());
99
                setName(getFeatureTypeName());
100
                setTitle(getLayerInfo().getTitle());
101
                setAbstract("generated by gvSIG");
102
        }
103
        /**
104
         * 
105
         * @return feature name in Geoserver. It's the filename without extension for files or tablename for databases
106
         * 
107
         */
108
        private String getFeatureTypeName() {
109
                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
                        }
119
                }
120
                return aux;
121
        }
122
        /*
123
         * (non-Javadoc)
124
         * @see com.iver.utiles.IPersistance#getClassName()
125
         */
126
        public String getClassName() {                
127
                return "GeoserverFeature";
128
        }
129

    
130
        /**
131
         * 
132
         * @see org.gvsig.publish.serversmodel.RemoteResource#publish()
133
         */
134
        public void publish() throws PublishException{                
135
                //adds this feature to the server configuration                
136
                addFeatureToConfig(); 
137
                //adds its childs to the server configuration
138
                super.publish();
139
        }
140
        /**
141
         * Adds this remote resource into the server configuration
142
         * @throws PublishException  
143
         */
144
        private void addFeatureToConfig() throws PublishException{
145
                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

    
157
                //adds feature
158
                Geoserver server= (Geoserver) getService().getServer();
159

    
160
                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
                }                        
166
        }
167

    
168
        /**
169
         * TODO: at the moment only one namespace is possible (default)
170
         * @return
171
         */
172
        private GSNamespace create_namespace(){
173
                GSNamespace ns = new GSNamespace();                
174
                ns.setPrefix(getNamespacePrefix());
175
                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
        private GSDatastore create_datastore(GSFeature feature, GSNamespace ns) {
188
                GSDatastore ds = new GSDatastore();
189
                ds.setNamespace(ns);
190
                IDataSourceInfo dataSrc = getLayerInfo().getDataSource();                
191
                if (dataSrc.getType().equals(IDataSourceInfo.SHAPE_TYPE)){                        
192
                        config_datastore_shape(feature, ds);
193
                }
194
                if (dataSrc.getType().equals(IDataSourceInfo.POSTGIS_TYPE)){                        
195
                        config_datastore_postgis(feature, ds);
196
                }
197
                return ds;
198
        }
199
        /**
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
                String ds_id  = ((IDataBaseInfo)getLayerInfo().getDataSource()).getConnectionName();
207
                ds_id  = ds_id + "_" + ((IDataBaseInfo)getLayerInfo().getDataSource()).getSchemaName();
208
                ds.setId(ds_id);
209
                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
                //the feature id is namespace prefix + "_" + tablename
220
                String id = getLayerInfo().getDataSource().getNamespacePrefix() + "_" + ((IDataBaseInfo)getLayerInfo().getDataSource()).getTableName();
221
                feature.setId(id);
222
        }
223
        /**
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
                ds.setId(((IFileInfo)getLayerInfo().getDataSource()).getFilenameWithoutExtension());
232
                //TODO: get this from IDatasourceInfo
233
                ds.setCharset("UTF-8");                
234
                try {
235
                        IDataSourceInfo datasource = getLayerInfo().getDataSource();
236
                        String f = getService().getServer().getPublication().getServerSideFilePath(datasource);
237
                        ds.setURL(new URL("file:" + f));
238
                } catch (MalformedURLException e) {
239
                        PublishLogger.getLog().error("ERROR: " + getClassName() + ":malformed url", e);
240
                }        
241
                //the feature id is namespace + "_" + filename without extension
242
                String id = getLayerInfo().getDataSource().getNamespacePrefix() + "_" + getId(); 
243
                feature.setId(id);
244
        }
245
        /**
246
         * Creates the style configuration for Geoserver 
247
         * 
248
         * @return
249
         * 
250
         */
251
        private GSStyle create_style(){
252
                //create style
253
                GSStyle st = new GSStyle();
254
                st.setId(getId());
255
                if (getLayerInfo().getLegend()!= null){
256
                        st.setSLDString(getLayerInfo().getLegend().getSLDString());
257
                }
258
                return st;
259
        }
260

    
261
        /*
262
         * (non-Javadoc)
263
         * @see org.gvsig.publish.serversmodel.RemoteResource#getRegisterTag()
264
         */
265
        public String getRegisterTag() {
266

    
267
                return REGISTER_TAG;
268
        }
269
        /*
270
         * (non-Javadoc)
271
         * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getAbstract()
272
         */
273
        public String getAbstract() {
274

    
275
                return ogcMetadata.getAbstract();
276
        }
277
        /*
278
         * (non-Javadoc)
279
         * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getName()
280
         */
281
        public String getName() {
282

    
283
                return ogcMetadata.getName();
284
        }
285
        /*
286
         * (non-Javadoc)
287
         * @see org.gvsig.publish.ogcmetadata.IOGCCommonMetadata#getTitle()
288
         */
289
        public String getTitle() {
290

    
291
                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
                ogcMetadata.setAbstract(description);                
299
        }
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

    
307
        }
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
        public String toString(){
327
                return getTitle();
328
        }
329
        /* (non-Javadoc)
330
         * @see org.gvsig.publish.serversmodel.RemoteResource#getXMLEntity()
331
         */
332

    
333
        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
                xml.putProperty("namespace_prefix", getNamespacePrefix());
345
                xml.putProperty("is_cached", isCacheEnabled());
346
                xml.putProperty("cache_maxage", getCacheMaxage());
347
                return xml;
348
        }
349

    
350
        /* (non-Javadoc)
351
         * @see org.gvsig.publish.serversmodel.RemoteResource#setXMLEntity(com.iver.utiles.XMLEntity)
352
         */
353

    
354
        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
                setNamespacePrefix(xml.getStringProperty("namespace_prefix"));
367
                setCacheEnabled(xml.getBooleanProperty("is_cached"));
368
                setCacheMaxage(xml.getIntProperty("cache_maxage"));
369
        }
370

    
371
        /**
372
         * @return the cacheEnabled
373
         */
374
        public boolean isCacheEnabled() {
375
                return cacheEnabled;
376
        }
377

    
378
        /**
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

    
427
}