root / tags / v1_1_Build_1001 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDisconnectedDBAdapter.java @ 11984
History | View | Annotate | Download (8.41 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 Ib??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 com.iver.cit.gvsig.fmap.layers; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.ByteArrayOutputStream; |
45 |
import java.io.File; |
46 |
import java.io.FileOutputStream; |
47 |
import java.io.IOException; |
48 |
import java.io.ObjectOutputStream; |
49 |
import java.nio.ByteBuffer; |
50 |
import java.nio.channels.FileChannel; |
51 |
import java.sql.ResultSet; |
52 |
import java.sql.SQLException; |
53 |
import java.sql.Statement; |
54 |
|
55 |
import com.hardcode.driverManager.DriverLoadException; |
56 |
import com.hardcode.gdbms.engine.data.DataSource; |
57 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
58 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
59 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
60 |
import com.hardcode.gdbms.engine.values.Value; |
61 |
import com.iver.cit.gvsig.fmap.core.FShape; |
62 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
63 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
64 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
65 |
|
66 |
|
67 |
|
68 |
/**
|
69 |
* Adapta un driver de base de datos vectorial a la interfaz vectorial,
|
70 |
* manteniendo adem?s el estado necesario por una capa vectorial de base de
|
71 |
* datos (par?metros de la conexi?n)
|
72 |
*/
|
73 |
public class VectorialDisconnectedDBAdapter extends VectorialAdapter { |
74 |
private static final int REFRESH = 0; |
75 |
private static final int RECEIVING = 1; |
76 |
private static final int LOCAL = 2; |
77 |
|
78 |
private int numReg=-1; |
79 |
private SelectableDataSource ds = null; |
80 |
private int status = REFRESH; |
81 |
private VectorialDBAdapter connectedAdapter;
|
82 |
|
83 |
private File dataFile; |
84 |
private File indexFile; |
85 |
|
86 |
/**
|
87 |
* incrementa el contador de las veces que se ha abierto el fichero.
|
88 |
* Solamente cuando el contador est? a cero pide al driver que conecte con
|
89 |
* la base de datos
|
90 |
*/
|
91 |
public void start() { |
92 |
switch (status){
|
93 |
case REFRESH:
|
94 |
Thread t = new Thread(new Receiver()); |
95 |
t.run(); |
96 |
break;
|
97 |
case RECEIVING:
|
98 |
case LOCAL:
|
99 |
|
100 |
break;
|
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
107 |
* al driver que cierre la conexion con el servidor de base de datos
|
108 |
*/
|
109 |
public void stop() { |
110 |
((IVectorialDatabaseDriver)driver).close(); |
111 |
} |
112 |
|
113 |
/**
|
114 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
115 |
*/
|
116 |
public IGeometry getShape(int index) throws DriverIOException { |
117 |
try {
|
118 |
return ((IVectorialDatabaseDriver)driver).getShape(index);
|
119 |
} catch (IOException e) { |
120 |
throw new DriverIOException(e); |
121 |
} |
122 |
} |
123 |
|
124 |
private String getTableName(){ |
125 |
return ((IVectorialDatabaseDriver)driver).getTableName();
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
130 |
*/
|
131 |
public int getShapeType() throws DriverIOException { |
132 |
return FShape.MULTI;
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
137 |
*/
|
138 |
public SelectableDataSource getRecordset() throws DriverLoadException { |
139 |
if (driver instanceof ObjectDriver) |
140 |
{ |
141 |
String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
142 |
try {
|
143 |
ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
|
144 |
} catch (NoSuchTableException e) {
|
145 |
throw new RuntimeException(e); |
146 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
147 |
throw new RuntimeException(e); |
148 |
} |
149 |
} |
150 |
return ds;
|
151 |
} |
152 |
|
153 |
public class Receiver implements Runnable{ |
154 |
private ByteBuffer bb; |
155 |
private int currentCapacity = 0; |
156 |
private FileChannel channel; |
157 |
|
158 |
private ByteBuffer indexBuffer = ByteBuffer.allocate(4); |
159 |
private FileChannel indexChannel; |
160 |
|
161 |
private Rectangle2D extent = new Rectangle2D.Double(); |
162 |
|
163 |
public ByteBuffer getBuffer(int capacity){ |
164 |
if (capacity > currentCapacity){
|
165 |
bb = ByteBuffer.allocate(capacity);
|
166 |
currentCapacity = capacity; |
167 |
} |
168 |
|
169 |
return bb;
|
170 |
} |
171 |
|
172 |
private void writeObject(byte[] bytes) throws IOException{ |
173 |
//Se escribe al fichero de datos
|
174 |
bb = getBuffer(bytes.length + 4);
|
175 |
bb.clear(); |
176 |
bb.putInt(bytes.length); |
177 |
bb.put(bytes); |
178 |
bb.flip(); |
179 |
channel.write(bb); |
180 |
|
181 |
//Se actualiza el fichero de ?ndice
|
182 |
indexBuffer.clear(); |
183 |
indexBuffer.putInt((int) channel.position());
|
184 |
indexBuffer.flip(); |
185 |
indexChannel.write(indexBuffer); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* @see java.lang.Runnable#run()
|
190 |
*/
|
191 |
public void run() { |
192 |
try {
|
193 |
//Abrimos el fichero de datos
|
194 |
dataFile = new File("/root/tirar/gvSIGtmp.gvs"); |
195 |
FileOutputStream fs = new FileOutputStream(dataFile); |
196 |
channel = fs.getChannel(); |
197 |
|
198 |
//Abrimos el fichero de indice
|
199 |
indexFile = new File("/root/tirar/gvSIGindex.gvi"); |
200 |
FileOutputStream indexfs = new FileOutputStream(indexFile); |
201 |
indexChannel = indexfs.getChannel(); |
202 |
|
203 |
//Creamos un adaptador conectado para bajarnos los datos
|
204 |
IVectorialDatabaseDriver d = (IVectorialDatabaseDriver) VectorialDisconnectedDBAdapter.this.driver; |
205 |
connectedAdapter = new VectorialDBAdapter();
|
206 |
connectedAdapter.setDriver(d); |
207 |
|
208 |
//Escribimos el n?mero de campos
|
209 |
indexBuffer.clear(); |
210 |
indexBuffer.putInt(connectedAdapter.getRecordset().getFieldCount()); |
211 |
indexBuffer.flip(); |
212 |
indexChannel.write(indexBuffer); |
213 |
indexChannel.position(indexChannel.position() + 4);
|
214 |
|
215 |
//Reservamos espacio para el n?mero de campos
|
216 |
indexChannel.position(indexChannel.position() + 4 * 8); |
217 |
|
218 |
connectedAdapter.start(); |
219 |
|
220 |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
221 |
ObjectOutputStream oos = new ObjectOutputStream(bytes); |
222 |
int geom = 0; |
223 |
for (int i = 0; i < connectedAdapter.getShapeCount(); i++) { |
224 |
geom++; |
225 |
IGeometry g = connectedAdapter.getShape(i); |
226 |
extent.add(g.getBounds2D()); |
227 |
|
228 |
//Se obtienen los bytes del objeto a serializar
|
229 |
bytes = new ByteArrayOutputStream(); |
230 |
oos = new ObjectOutputStream(bytes); |
231 |
oos.writeObject(g); |
232 |
oos.close(); |
233 |
|
234 |
//Se escribe al fichero
|
235 |
writeObject(bytes.toByteArray()); |
236 |
|
237 |
for (int j = 0; j < connectedAdapter.getRecordset().getFieldCount(); j++) { |
238 |
Value v = connectedAdapter.getRecordset().getFieldValue(i, j); |
239 |
|
240 |
//Se obtienen los bytes del objeto a serializar
|
241 |
bytes = new ByteArrayOutputStream(); |
242 |
oos = new ObjectOutputStream(bytes); |
243 |
oos.writeObject(v); |
244 |
oos.close(); |
245 |
|
246 |
//Se escribe al fichero
|
247 |
writeObject(bytes.toByteArray()); |
248 |
} |
249 |
} |
250 |
|
251 |
//Escribimos el n?mero de geometr?as en el fichero de ?ndice
|
252 |
indexBuffer.clear(); |
253 |
indexBuffer.putInt(geom); |
254 |
indexBuffer.flip(); |
255 |
indexChannel.position(4);
|
256 |
indexChannel.write(indexBuffer); |
257 |
indexChannel.position(indexChannel.position() + 4);
|
258 |
|
259 |
//Escribimos el extent
|
260 |
ByteBuffer extentBuffer = ByteBuffer.allocate(4*8); |
261 |
extentBuffer.putDouble(extent.getMinX()); |
262 |
extentBuffer.putDouble(extent.getMinY()); |
263 |
extentBuffer.putDouble(extent.getMaxX()); |
264 |
extentBuffer.putDouble(extent.getMaxY()); |
265 |
extentBuffer.flip(); |
266 |
indexChannel.position(8);
|
267 |
indexChannel.write(extentBuffer); |
268 |
|
269 |
//Cerramos
|
270 |
channel.close(); |
271 |
indexChannel.close(); |
272 |
|
273 |
connectedAdapter.stop(); |
274 |
|
275 |
status = LOCAL; |
276 |
} catch (DriverIOException e) {
|
277 |
throw new RuntimeException(e); |
278 |
} catch (IOException e) { |
279 |
throw new RuntimeException(e); |
280 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
281 |
throw new RuntimeException(e); |
282 |
} catch (DriverLoadException e) {
|
283 |
throw new RuntimeException(e); |
284 |
} |
285 |
} |
286 |
|
287 |
} |
288 |
} |