Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / AddEventThemeDriver.java @ 8234

History | View | Annotate | Download (10.8 KB)

1
/*
2
 * Created on 10-nov-2005
3
 *
4
 * gvSIG. Sistema de Informacin Geogrfica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ibez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44

    
45
/*
46
 * $log$
47
 */
48
package com.iver.gvsig.addeventtheme;
49

    
50
import java.awt.geom.Rectangle2D;
51
import java.io.IOException;
52

    
53
import com.hardcode.driverManager.DriverLoadException;
54
import com.hardcode.gdbms.engine.data.DataSource;
55
import com.hardcode.gdbms.engine.data.DataSourceFactory;
56
import com.hardcode.gdbms.engine.data.IDataSourceListener;
57
import com.hardcode.gdbms.engine.data.NoSuchTableException;
58
import com.hardcode.gdbms.engine.data.SourceInfo;
59
import com.hardcode.gdbms.engine.data.driver.DriverException;
60
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
61
import com.hardcode.gdbms.engine.data.edition.DataWare;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.iver.andami.PluginServices;
64
import com.iver.cit.gvsig.ProjectExtension;
65
import com.iver.cit.gvsig.fmap.core.FGeometry;
66
import com.iver.cit.gvsig.fmap.core.FPoint2D;
67
import com.iver.cit.gvsig.fmap.core.FShape;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
70
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
71
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
73
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
74
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
75
import com.iver.utiles.IPersistance;
76
import com.iver.utiles.XMLEntity;
77

    
78
/**
79
 * The class AddEventThemeDriver allows to create a new FLayer from a
80
 * gvSIG DataSource.
81
 *
82
 * @author jmorell
83
 */
84
public class AddEventThemeDriver implements VectorialDriver, ObjectDriver, BoundedShapes, IPersistance, IDataSourceListener {
85
    private Rectangle2D fullExtent = null;
86
    private SelectableDataSource ds;
87
    private int xFieldIndex;
88
    private int yFieldIndex;
89

    
90
    /**
91
     * Initializes this.
92
     * @param ds
93
     * @param xFieldIndex
94
     * @param yFieldIndex
95
     * @throws DriverException 
96
     */
97
    public void setData(DataSource ds, int xFieldIndex, int yFieldIndex) throws DriverException {            
98
        this.ds = new SelectableDataSource(ds);
99
        this.ds.addDataSourceListener(this);
100
        this.xFieldIndex = xFieldIndex;
101
        this.yFieldIndex = yFieldIndex;
102
    }
103
    public int[] getFieldsIndex(){
104
            int[] n=new int[2];
105
            n[0]=xFieldIndex;
106
            n[1]=yFieldIndex;
107
            return n;
108
    }
109
    public int getShapeType() {
110
        return FShape.POINT;
111
    }
112

    
113
    public int getShapeCount() throws IOException {
114
        try {
115
            return (int) ds.getRowCount();
116
        } catch (DriverException e) {
117
            // TODO Auto-generated catch block
118
            e.printStackTrace();
119
        }
120
        return 0;
121
    }
122

    
123
    public DriverAttributes getDriverAttributes() {
124
        // TODO Auto-generated method stub
125
        return null;
126
    }
127

    
128
    private double getX(int row) throws DriverException {
129
            try {
130
                    return (new Double(((Value)ds.getFieldValue((int)row, xFieldIndex)).toString())).doubleValue();
131
            } catch(NumberFormatException e) {
132
                    return 0;
133
            }
134
    }
135
    private double getY(int row) throws DriverException {
136
            try {
137
                    return (new Double(((Value)ds.getFieldValue((int)row, yFieldIndex)).toString())).doubleValue();
138
            } catch(NumberFormatException e) {
139
                    return 0;
140
            }
141
    }
142
    
143
    public Rectangle2D getFullExtent() throws IOException {
144
        if (fullExtent == null) {
145
            try {
146
                for (int i=0;i<ds.getRowCount();i++) {
147
                    double x = this.getX(i); //(new Double(((Value)ds.getFieldValue((int)i, xFieldIndex)).toString())).doubleValue();
148
                    double y = this.getY(i); //(new Double(((Value)ds.getFieldValue((int)i, yFieldIndex)).toString())).doubleValue();
149
                    FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
150
                    Rectangle2D rect = geometry.getBounds2D();
151
                    if (fullExtent == null) {
152
                        fullExtent = rect;
153
                    } else {
154
                        fullExtent.add(rect);
155
                    }
156
                }
157
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
158
                // TODO Auto-generated catch block
159
                e1.printStackTrace();
160
            }
161
        }
162
        return fullExtent;
163
    }
164

    
165
    public IGeometry getShape(int index){
166
        double x;
167
        double y;
168
        try {
169
            x = this.getX(index); //(new Double(((Value)ds.getFieldValue(index, xFieldIndex)).toString())).doubleValue();
170
            y = this.getY(index); //(new Double(((Value)ds.getFieldValue(index, yFieldIndex)).toString())).doubleValue();
171
            //System.err.println("La X = "+x+" , La Y = "+y);
172
            FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
173
            return geometry;
174
        } catch (NumberFormatException e) {
175
            // TODO Auto-generated catch block
176
            e.printStackTrace();
177
        } catch (DriverException e) {
178
            // TODO Auto-generated catch block
179
            e.printStackTrace();
180
        }
181
        return null;
182
    }
183

    
184
    public String getName() {
185
        return "Add Event Layer Driver";
186
    }
187

    
188
    public int[] getPrimaryKeys() throws DriverException {
189
        // TODO Auto-generated method stub
190
        return null;
191
    }
192

    
193
    public void write(DataWare dataWare) throws DriverException {
194
        // TODO Auto-generated method stub
195

    
196
    }
197

    
198
    public void setDataSourceFactory(DataSourceFactory arg0) {
199
        // TODO Auto-generated method stub
200

    
201
    }
202

    
203
    public Value getFieldValue(long rowIndex, int fieldId) throws DriverException {
204
        return ds.getFieldValue(rowIndex, fieldId);
205
    }
206

    
207
    public int getFieldCount() throws DriverException {
208
        return ds.getFieldCount();
209
    }
210

    
211
    public String getFieldName(int fieldId) throws DriverException {
212
        return ds.getFieldName(fieldId);
213
    }
214

    
215
    public long getRowCount() throws DriverException {
216
        return ds.getRowCount();
217
    }
218

    
219
    public int getFieldType(int i) throws DriverException {
220
        return ds.getFieldType(i);
221
    }
222

    
223
    public String getClassName() {
224
        return this.getClass().getName();
225
    }
226
    
227

    
228
    // Para guardar en el xml file
229
    public XMLEntity getXMLEntity() {
230
        XMLEntity xml = new XMLEntity();
231
        xml.putProperty("className", this.getClass().getName());
232
        xml.putProperty("xFieldIndex", xFieldIndex);
233
        xml.putProperty("yFieldIndex", yFieldIndex);
234
        xml.putProperty("tableName", ds.getName());
235
        XMLEntity dsXML = null;
236
                        
237
                dsXML = this.getDataSourceXML();
238
                if (dsXML != null) {
239
                        xml.putProperty("hasDSInfo", true);
240
                        xml.addChild(dsXML);
241
                } else {
242
                        xml.putProperty("hasDSInfo", false);
243
                }
244
                return xml;
245
    }
246

    
247
    // Para recuperar del xml file
248
    public void setXMLEntity(XMLEntity xml) {
249
        int xFieldIndex = xml.getIntProperty("xFieldIndex");
250
        int yFieldIndex = xml.getIntProperty("yFieldIndex");
251
        String tableName = xml.getStringProperty("tableName");
252
        DataSource ds;
253
        try {
254
            ds = LayerFactory.getDataSourceFactory().createRandomDataSource(tableName, DataSourceFactory.AUTOMATIC_OPENING);            
255
        } catch (NoSuchTableException e) {
256
                if (!xml.contains("hasDSInfo") || !xml.getBooleanProperty("hasDSInfo")) {
257
                        // No esta registrado el DS y no tenemos su informacion en el
258
                        // xml (proyecto viejo)... no podemos arreglarlo
259
                        throw new RuntimeException(e);
260
                }
261
                //Intentar reconstruir el DS!!!!!!!!!!
262
                try {
263
                                ds = this.getDataSourceFromXML(xml.getChild(0));
264
                        } catch (Exception e1) {
265
                                throw new RuntimeException(e1);
266
                        }            
267
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
268
            throw new RuntimeException(e);
269
        } catch (DriverLoadException e) {
270
            // TODO Auto-generated catch block
271
                throw new RuntimeException(e);
272
        }
273
        try {
274
                        setData(ds, xFieldIndex, yFieldIndex);
275
                } catch (DriverException e) {
276
                        // TODO Auto-generated catch block
277
                        throw new RuntimeException(e);
278
                }        
279
    }
280
   
281

    
282
        public int getFieldWidth(int i) throws DriverException {
283
                return ds.getFieldWidth(i);
284
        }
285

    
286
        public Rectangle2D getShapeBounds(int index) throws IOException {
287
                return getShape(index).getBounds2D();
288
        }
289

    
290
        public int getShapeType(int index) {
291
                return getShape(index).getGeometryType();
292
        }
293
        public boolean isWritable() {
294
                return true;
295
        }
296
        
297
        public void reloaded(DataSource dataSource) {
298
                this.fullExtent = null;
299
                
300
        }
301
        
302
        public void reload() throws IOException, DriverException{
303
                this.ds.reload();
304
                
305
        }
306
            
307
    private XMLEntity getDataSourceXML() {            
308
            SourceInfo di = this.ds.getSourceInfo();
309
            ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
310
            
311
            return ext.getProject().getSourceInfoXMLEntity(di);
312
    }
313
    
314
    private DataSource getDataSourceFromXML(XMLEntity xml) throws Exception{
315
                if (xml.getStringProperty("type").equals("otherDriverFile")) {
316
                        LayerFactory.getDataSourceFactory().addFileDataSource(xml.getStringProperty(
317
                                        "driverName"), xml.getStringProperty("gdbmsname"),
318
                                xml.getStringProperty("file"));
319
                } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
320
                } else if (xml.getStringProperty("type").equals("db")) {
321
                        LayerFactory.getDataSourceFactory().addDBDataSourceByTable(xml.getStringProperty(
322
                                        "gdbmsname"), xml.getStringProperty("host"),
323
                                xml.getIntProperty("port"),
324
                                xml.getStringProperty("user"),
325
                                xml.getStringProperty("password"),
326
                                xml.getStringProperty("dbName"),
327
                                xml.getStringProperty("tableName"),
328
                                xml.getStringProperty("driverInfo"));
329
                }
330
                
331
        
332
                DataSource ds = LayerFactory.getDataSourceFactory().createRandomDataSource(xml.getStringProperty(
333
                "gdbmsname"), DataSourceFactory.AUTOMATIC_OPENING);
334
                
335
                return ds;
336
            
337
    }
338

    
339
}