Statistics
| Revision:

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

History | View | Annotate | Download (8.04 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
import java.awt.geom.Rectangle2D;
44
import java.io.File;
45
import java.net.MalformedURLException;
46
import java.net.URI;
47
import java.net.URISyntaxException;
48
import java.net.URL;
49

    
50
import org.gvsig.publish.PublishLogger;
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.GSService;
55
import org.gvsig.publish.geoserver.conf.GSStyle;
56
import org.gvsig.publish.infoproject.IDataSourceInfo;
57
import org.gvsig.publish.infoproject.ILayerInfo;
58
import org.gvsig.publish.infoproject.datasources.IDataBaseInfo;
59
import org.gvsig.publish.infoproject.datasources.IFileInfo;
60
import org.gvsig.publish.ogcmetadata.CommonOGCMetadata;
61
import org.gvsig.publish.serversmodel.PublishException;
62
import org.gvsig.publish.serversmodel.RemoteResource;
63
import org.gvsig.publish.serversmodel.Service;
64

    
65
import com.iver.utiles.FileUtils;
66
/**
67
 * This class represents a remote resource (a feature) which can be offered by Geoserver
68
 * This remote resource will be able to be accessed through WMS or WFS 
69
 * 
70
 * @author jvhigon
71
 *
72
 */
73
public class GSFeatureResource extends RemoteResource {
74
        //Attributes
75
        private CommonOGCMetadata ogcMetadata= null;
76
        
77
        public static final String REGISTER_TAG="gsfeature";
78
        public final String VERSION="_v1";
79
        private GSFeature feature=null;
80
        /**
81
         * constructor
82
         * @param service
83
         */
84
        public GSFeatureResource(GSWFSService service) {
85
                super(service);                
86
                ogcMetadata = new CommonOGCMetadata();
87
                
88
        }
89
        /**
90
         * @return the ogcMetadata
91
         */
92
        public CommonOGCMetadata getOgcMetadata() {
93
                return ogcMetadata;
94
        }
95

    
96
        /**
97
         * @param ogcMetadata the ogcMetadata to set
98
         */
99
        public void setOgcMetadata(CommonOGCMetadata ogcMetadata) {
100
                this.ogcMetadata = ogcMetadata;
101
                //update config. Note that config name is not the same as ogcmetadata name                
102
                getConfig().setTitle(ogcMetadata.getTitle());
103
                getConfig().setAbstract(ogcMetadata.getAbstract());
104
        }
105

    
106
        /** 
107
         * Overrides the method in order to initialize some variables like ogcmetadata
108
         * @see org.gvsig.publish.serversmodel.RemoteResource#setLayerInfo(org.gvsig.publish.infoproject.ILayerInfo)
109
         */
110
        
111
        public void setLayerInfo(ILayerInfo ilayer) {
112
                super.setLayerInfo(ilayer);
113
                getOgcMetadata().setName(getConfig().getNamespace().getPrefix() + ":" + ilayer.getName());
114
                getOgcMetadata().setTitle(ilayer.getTitle());
115
                setName(getOgcMetadata().getName());
116
                setTitle(getOgcMetadata().getTitle());
117
        }
118

    
119
        public String getType() {
120

    
121
                return REGISTER_TAG;
122
        }
123

    
124
        public String getClassName() {
125
                
126
                return getClass().getName() + VERSION;
127
        }
128
        /**
129
         * This method can be used to acces the remote resource configuration directly. Then you can use
130
         * directly from the GUI.
131
         * @return the geoserver feature configuration
132
         */
133
        public GSFeature getConfig(){
134
                if (feature == null){
135
                        feature = new GSFeature();
136
                        //create namespace
137
                        GSNamespace ns = create_namespace();
138
                        //create datastore
139
                        GSDatastore ds = create_datastore(ns);
140
                        //create style 
141
                        GSStyle st = create_style();
142
                        //create feature
143
                        feature.setDatastore(ds);
144
                        feature.setNamespace(ns);
145
                        feature.setStyle(st);
146
                        //TODO: Can it be easier?
147
                        IDataSourceInfo aux = getLayerInfo().getDataSource();
148
                        String aux2 = getName();
149
                        if ( aux instanceof IFileInfo){
150
                                aux2 = ((IFileInfo)aux).getFilenameWithoutExtension();
151
                        }
152
                        if (aux instanceof IDataBaseInfo){
153
                                aux2 = ((IDataBaseInfo)aux).getTableName(); 
154
                        }                        
155
                        feature.setName(aux2);
156
                        
157
                        int srid = getLayerInfo().getSRID();
158
                        feature.setSRID(srid);
159
                        feature.setLatLonBBox(getLayerInfo().getDataSource().getLatLonBBox());
160
                        feature.setNativeBBox(getLayerInfo().getDataSource().getNativeBBox());
161
                        //adds feature
162
                        Geoserver server= (Geoserver) getService().getServer();
163
                        server.getConfig().addFeatureType(feature);        
164

    
165
                }
166
                //update from the ogcmetadata
167
                feature.setTitle(getOgcMetadata().getTitle());
168
                feature.setAbstract(getOgcMetadata().getAbstract());
169
                //feature.addKeyword("sfRoads");
170
                //feature.addKeyword("roads");
171
                return feature;
172
        }
173
        /**
174
         * 
175
         * @see org.gvsig.publish.serversmodel.RemoteResource#publish()
176
         */
177
        public void publish() throws PublishException {                
178
                super.publish();                
179
                checkDatasources();
180
                getConfig();                
181
        }
182
        private void checkDatasources() throws PublishException {
183
                if (getLayerInfo().getDataSource().getType().equals(IDataSourceInfo.RASTER_TYPE)){
184
                        throw new PublishException("datasource_not_supported");
185
                }                
186
        }
187
        /**
188
         * 
189
         * @return
190
         */
191
        private GSNamespace create_namespace(){
192
                GSNamespace ns = new GSNamespace();
193
                //TODO:
194
                ns.setPrefix(getLayerInfo().getDataSource().getConnectionName());
195
                ns.setPrefix("default");
196
                try {
197
                        ns.setURI(new URI(getService().getServer().getServerURL().toString()));
198
                } catch (URISyntaxException e1) {                        
199
                        PublishLogger.getLog().error("ERROR " + getClassName() + ":uri syntax error", e1);
200
                }
201
                return ns;
202
        }
203
        /**
204
         * 
205
         * @param ns
206
         * @return
207
         */
208
        private GSDatastore create_datastore(GSNamespace ns){
209
                GSDatastore ds = new GSDatastore();
210
                ds.setNamespace(ns);
211
                IDataSourceInfo dataSrc = getLayerInfo().getDataSource();
212
                if (dataSrc.getType().equals(IDataSourceInfo.SHAPE_TYPE)){                        
213
                        config_datastore_shape(ds);
214
                }
215
                if (dataSrc.getType().equals(IDataSourceInfo.POSTGIS_TYPE)){                        
216
                        config_datastore_postgis(ds);
217
                }
218
                return ds;
219
        }
220
        
221
        private void config_datastore_postgis(GSDatastore ds) {
222
                ds.setId(getLayerInfo().getDataSource().getConnectionName());
223
                ds.setAbstract("Datastore description generated by gvSIG");
224
                ds.setLooseBbox(true);
225
                ds.setUser(((IDataBaseInfo)getLayerInfo().getDataSource()).getUser());
226
                ds.setPassword(((IDataBaseInfo)getLayerInfo().getDataSource()).getPassword());
227
                ds.setEstimatedExtent(false);
228
                ds.setHost(((IDataBaseInfo)getLayerInfo().getDataSource()).getHost());
229
                ds.setSchema(((IDataBaseInfo)getLayerInfo().getDataSource()).getSchemaName());
230
                ds.setPort(new Integer(((IDataBaseInfo)getLayerInfo().getDataSource()).getPort()).intValue());
231
                ds.setDatabase(((IDataBaseInfo)getLayerInfo().getDataSource()).getDatabaseName());
232
                ds.setDbType("postgis");                
233
        }
234
        
235
        private void config_datastore_shape(GSDatastore ds) {
236
                ds.setId(getLayerInfo().getDataSource().getConnectionName());
237
                //TODO: get this from IDatasourceInfo
238
                ds.setCharset("UTF-8");                
239
                try {
240
                        String f = ((IFileInfo)getLayerInfo().getDataSource()).getAbsolutePath();
241
                        ds.setURL(new URL("file:" + f));
242
                } catch (MalformedURLException e) {
243
                        PublishLogger.getLog().error("ERROR: " + getClassName() + ":malformed url", e);
244
                }                
245
        }
246
        /**
247
         * 
248
         * @return
249
         */
250
        private GSStyle create_style(){
251
                //create style
252
                GSStyle st = new GSStyle();
253
                st.setId(getLayerInfo().getName());
254
//                URL sld = getClass().getClassLoader().getResource("config/example.sld");
255
//                st.setSLDFile(new File (sld.toString()));
256
                if (getLayerInfo().getLegend()!= null){
257
                        st.setSLDString(getLayerInfo().getLegend().getSLDString());
258
                }
259
                return st;
260
        }
261

    
262
}