svn-gvsig-desktop / tags / v1_11_0_Build_1306 / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / fmap / drivers / ArcImsInMemoryAttsTableDriver.java @ 35731
History | View | Annotate | Download (17.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop 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 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package es.prodevelop.cit.gvsig.arcims.fmap.drivers; |
44 |
|
45 |
import java.awt.Dimension; |
46 |
import java.sql.SQLException; |
47 |
import java.util.ArrayList; |
48 |
import java.util.HashMap; |
49 |
|
50 |
import org.apache.log4j.Logger; |
51 |
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException; |
52 |
import org.gvsig.remoteClient.arcims.utils.FieldInformation; |
53 |
|
54 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
55 |
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException; |
56 |
import com.hardcode.gdbms.driver.exceptions.WriteDriverException; |
57 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
58 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
59 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
60 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
61 |
import com.hardcode.gdbms.engine.values.IntValue; |
62 |
import com.hardcode.gdbms.engine.values.Value; |
63 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
64 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
65 |
|
66 |
|
67 |
public class ArcImsInMemoryAttsTableDriver { |
68 |
private static Logger logger = Logger.getLogger(FMapFeatureArcImsDriver.class.getName()); |
69 |
private String tableName = ""; |
70 |
|
71 |
// private String dataBName = "";
|
72 |
private String idOriginalFieldName = ""; |
73 |
private String idSqlFieldName = ""; |
74 |
|
75 |
// private String rowIndexFieldName = "INMEMORYROWNUMBER";
|
76 |
private int idFieldInd = -1; |
77 |
private FBitSet requested;
|
78 |
private ArrayList sqlColumnNames = new ArrayList(); |
79 |
private ArrayList originalColumnNames = new ArrayList(); |
80 |
|
81 |
// private ArrayList originalColumnNames = new ArrayList();
|
82 |
private ArrayList columnTypes = new ArrayList(); |
83 |
|
84 |
// private int theRowCount = 0;
|
85 |
private int theFieldCount = 0; |
86 |
private HashMap idToIndex; |
87 |
private Dimension lastRequest = new Dimension(-1, -1); |
88 |
|
89 |
// ------------------------------------------------
|
90 |
private ArrayList data = new ArrayList(); |
91 |
private ArrayList id = new ArrayList(); |
92 |
|
93 |
// ------------------------------------------------
|
94 |
private FMapFeatureArcImsDriver theParentDrv;
|
95 |
private ArcImsAttributesDataSourceAdapter theParentAdapter;
|
96 |
private DataSourceFactory dsFactory;
|
97 |
|
98 |
// private ArcImsAttributesTableCachee attsCache;
|
99 |
// private static Logger logger = Logger.getLogger(FMapFeatureArcImsDriver.class.getName());
|
100 |
public ArcImsInMemoryAttsTableDriver(String tb_name, ArrayList colnames, |
101 |
ArrayList coltypes, FMapFeatureArcImsDriver p, ArcImsAttributesDataSourceAdapter pad) {
|
102 |
|
103 |
|
104 |
// super();
|
105 |
|
106 |
theParentDrv = p; |
107 |
theParentAdapter = pad; |
108 |
tableName = tb_name; |
109 |
|
110 |
// dataBName = dbName;
|
111 |
idFieldInd = getIdColumnInd(coltypes); |
112 |
|
113 |
idOriginalFieldName = (String) colnames.get(idFieldInd);
|
114 |
idSqlFieldName = replaceUnwantedCharacters((String) colnames.get(
|
115 |
idFieldInd)); |
116 |
|
117 |
requested = new FBitSet();
|
118 |
|
119 |
for (int i = 0; i < colnames.size(); i++) { |
120 |
sqlColumnNames.add(replaceUnwantedCharacters( |
121 |
(String) colnames.get(i)));
|
122 |
originalColumnNames.add(colnames.get(i)); |
123 |
columnTypes.add(coltypes.get(i)); |
124 |
theFieldCount++; |
125 |
} |
126 |
|
127 |
idToIndex = new HashMap(); |
128 |
} |
129 |
|
130 |
/**
|
131 |
* user = "as", password = "", dbName = ?
|
132 |
*
|
133 |
* @see com.hardcode.gdbms.engine.data.driver.DBDriver#getConnection(java.lang.String,
|
134 |
* int, java.lang.String, java.lang.String, java.lang.String)
|
135 |
*/
|
136 |
|
137 |
// public Connection getConnection(String host, int port, String _dbName,
|
138 |
// String _user, String _password) throws SQLException {
|
139 |
//
|
140 |
//// if (driverException != null) {
|
141 |
//// throw new RuntimeException(driverException);
|
142 |
//// }
|
143 |
// String connectionString = "jdbc:hsqldb:mem:" + dataBName;
|
144 |
// Connection c = DriverManager.getConnection(connectionString, "sa", "");
|
145 |
// return c;
|
146 |
// }
|
147 |
|
148 |
// public Connection getConnection() throws SQLException {
|
149 |
// return getConnection("", 0, "", "", "");
|
150 |
// }
|
151 |
|
152 |
/**
|
153 |
* @see com.hardcode.driverManager.Driver#getName()
|
154 |
*/
|
155 |
public String getName() { |
156 |
return "ArcIMS in-memory attributes data driver"; |
157 |
} |
158 |
|
159 |
public void open(String sql) throws SQLException { |
160 |
// open(getConnection(), sql);
|
161 |
} |
162 |
|
163 |
public void execute(String sql) throws SQLException { |
164 |
// execute(getConnection(), sql);
|
165 |
} |
166 |
|
167 |
public String getDbName() { |
168 |
return "No database"; |
169 |
} |
170 |
|
171 |
public int getIdFieldInd() { |
172 |
return idFieldInd;
|
173 |
} |
174 |
|
175 |
public String getIdFieldName() { |
176 |
return idSqlFieldName;
|
177 |
} |
178 |
|
179 |
// public void addRow(DataSourceFactory dsf, Value[] values) throws DriverException {
|
180 |
public void addRow(Value[] values, boolean filled) |
181 |
throws DriverException {
|
182 |
if (values.length < theFieldCount) {
|
183 |
logger.error("Cannot add row with length < field count. ");
|
184 |
|
185 |
return;
|
186 |
} |
187 |
|
188 |
if (values[idFieldInd].toString().compareTo("") != 0) { |
189 |
// not null value
|
190 |
int theid = ((IntValue) values[idFieldInd]).intValue();
|
191 |
idToIndex.put(new Integer(theid), new Integer(data.size())); |
192 |
} |
193 |
|
194 |
requested.set(data.size(), filled); |
195 |
data.add(values); |
196 |
|
197 |
// String fieldNames = "";
|
198 |
// for (int i=0; i < (values.length + 1); i++) {
|
199 |
// fieldNames = fieldNames + ((String) columnNames.get(i)) + ", ";
|
200 |
// }
|
201 |
// fieldNames = fieldNames.substring(0, fieldNames.length() - 2);
|
202 |
// // + ArcImsSqlUtils.rowIndexFieldName;
|
203 |
//
|
204 |
// String fieldValues = "";
|
205 |
// for (int i=0; i<values.length; i++) {
|
206 |
// fieldValues = fieldValues + getValueInSqlFormat(values[i]) + ", ";
|
207 |
// }
|
208 |
// fieldValues = fieldValues + getRowCount();
|
209 |
//
|
210 |
// String sqlStr = "insert into " + tableName + " (";
|
211 |
// sqlStr = sqlStr + fieldNames + ") values (";
|
212 |
// sqlStr = sqlStr + fieldValues + ")";
|
213 |
// /*
|
214 |
// * INSERT INTO Table1 (Column1, Column2, Column3?)
|
215 |
// * VALUES (Value1, Value2, Value3?)
|
216 |
// */
|
217 |
// try {
|
218 |
// // dsf.executeSQL(sqlStr, DataSourceFactory.AUTOMATIC_OPENING);
|
219 |
// execute(sqlStr);
|
220 |
// theRowCount++;
|
221 |
// } catch (Exception e) {
|
222 |
// DriverException de = new DriverException(e.getMessage());
|
223 |
// throw de;
|
224 |
// }
|
225 |
} |
226 |
|
227 |
// public void deleteRow(DataSourceFactory dsf, int i) throws DriverException {
|
228 |
public void deleteRow(int i) throws DriverException { |
229 |
/*
|
230 |
DELETE FROM Table1 WHERE Some_Column = Some_Value
|
231 |
*/
|
232 |
logger.warn("deleteRow was invoqued, i = " + i);
|
233 |
|
234 |
int theid = getRowId(i);
|
235 |
idToIndex.remove(new Integer(theid)); |
236 |
|
237 |
requested.set(i, false);
|
238 |
data.set(i, null);
|
239 |
|
240 |
// String sqlStr = "delete from " + tableName + " where ";
|
241 |
// sqlStr = sqlStr + rowIndexFieldName + " = ";
|
242 |
// sqlStr = sqlStr + i;
|
243 |
// /*
|
244 |
// * INSERT INTO Table1 (Column1, Column2, Column3?)
|
245 |
// * VALUES (Value1, Value2, Value3?)
|
246 |
// */
|
247 |
// try {
|
248 |
// // dsf.executeSQL(sqlStr, DataSourceFactory.AUTOMATIC_OPENING);
|
249 |
// execute(sqlStr);
|
250 |
// theRowCount--;
|
251 |
// } catch (Exception e) {
|
252 |
// DriverException de = new DriverException(e.getMessage());
|
253 |
// throw de;
|
254 |
// }
|
255 |
} |
256 |
|
257 |
// private String getValueInSqlFormat(Value v) {
|
258 |
// String tmp = v.getStringValue(internalValueWriter);
|
259 |
//// if (v instanceof StringValue) {
|
260 |
//// tmp = "'" + tmp + "'";
|
261 |
//// }
|
262 |
// return tmp;
|
263 |
// }
|
264 |
private boolean isEmptyRow(int n) { |
265 |
return (data.get(n) == null); |
266 |
} |
267 |
|
268 |
public int getRowId(int row) throws DriverException { |
269 |
Integer rowid = (Integer) id.get(row); |
270 |
|
271 |
return rowid.intValue();
|
272 |
|
273 |
// Value[] therow = (Value []) data.get(row);
|
274 |
// return ((IntValue) therow[idFieldInd]).intValue();
|
275 |
// return ((Integer) indexToId.get(new Integer(row))).intValue();
|
276 |
} |
277 |
|
278 |
public int getRowIndex(int id) throws DriverException { |
279 |
// logger.debug("Searching for id: " + id);
|
280 |
return ((Integer) idToIndex.get(new Integer(id))).intValue(); |
281 |
} |
282 |
|
283 |
|
284 |
public boolean isNonRequestedRow(int rowind) { |
285 |
// TODO Auto-generated method stub
|
286 |
return (!requested.get(rowind));
|
287 |
} |
288 |
|
289 |
// TODO so far, this method is called with
|
290 |
// a full field query (except rowindex)
|
291 |
public void updateRow(Value[] upd_row, String[] fld_q, int rowind) |
292 |
throws DriverException {
|
293 |
if (!isEmptyRow(rowind)) {
|
294 |
int newid = 0; |
295 |
int oldid = getRowId(rowind);
|
296 |
boolean done = false; |
297 |
|
298 |
for (int i = 0; i < fld_q.length; i++) { |
299 |
if (fld_q[i].compareTo(idSqlFieldName) == 0) { |
300 |
newid = ((IntValue) upd_row[i]).intValue(); |
301 |
done = true;
|
302 |
} |
303 |
} |
304 |
|
305 |
if (done) {
|
306 |
idToIndex.remove(new Integer(oldid)); |
307 |
idToIndex.put(new Integer(newid), new Integer(rowind)); |
308 |
} |
309 |
} |
310 |
|
311 |
Value[] newrow = new Value[theFieldCount]; |
312 |
|
313 |
if (data.get(rowind) == null) { |
314 |
data.set(rowind, newrow); |
315 |
} |
316 |
else {
|
317 |
newrow = (Value[]) data.get(rowind);
|
318 |
} |
319 |
|
320 |
if (fld_q[0].compareTo("#ALL#") == 0) { |
321 |
for (int i = 0; i < upd_row.length; i++) { |
322 |
newrow[i] = upd_row[i]; |
323 |
} |
324 |
} |
325 |
else {
|
326 |
int fldind = 0; |
327 |
|
328 |
for (int i = 0; i < fld_q.length; i++) { |
329 |
String sqlname = replaceUnwantedCharacters(fld_q[i]);
|
330 |
fldind = getFieldIndexByName(sqlname); |
331 |
newrow[fldind] = upd_row[i]; |
332 |
} |
333 |
} |
334 |
|
335 |
} |
336 |
|
337 |
public int getFieldIndexByName(String arg0) throws DriverException { |
338 |
// String arg00 = ArcImsSqlUtils.getSqlCompliantFieldName(arg0);
|
339 |
if (arg0 == null) { |
340 |
logger.error("Somebody asked for the index of a null field ");
|
341 |
|
342 |
return -1; |
343 |
} |
344 |
|
345 |
for (int i = 0; i < sqlColumnNames.size(); i++) { |
346 |
if (((String) sqlColumnNames.get(i)).compareTo(arg0) == 0) { |
347 |
return i;
|
348 |
} |
349 |
} |
350 |
|
351 |
logger.error("Field not found ");
|
352 |
|
353 |
return -1; |
354 |
} |
355 |
|
356 |
public Value[] getRow(DataSourceFactory dsf, int n) |
357 |
throws DriverException {
|
358 |
return (Value[]) data.get(n); |
359 |
|
360 |
} |
361 |
|
362 |
public void setRequested(int n, boolean req) { |
363 |
requested.set(n, req); |
364 |
} |
365 |
|
366 |
public void addTheseAsRequested(FBitSet fbs) { |
367 |
requested.or(fbs); |
368 |
} |
369 |
|
370 |
public long getRowCount() { |
371 |
return (long) data.size(); |
372 |
} |
373 |
|
374 |
public int getFieldCount() { |
375 |
return theFieldCount;
|
376 |
} |
377 |
|
378 |
public String getFieldName(int i) { |
379 |
return (String) sqlColumnNames.get(i); |
380 |
} |
381 |
|
382 |
public int getFieldType(int i) { |
383 |
int resp = ((Integer) columnTypes.get(i)).intValue(); |
384 |
|
385 |
// This is done because gvSIG doesnt know about
|
386 |
// FieldInformation.ID and FieldInformation.SHAPE
|
387 |
if (resp == FieldInformation.ID) {
|
388 |
resp = FieldInformation.INTEGER; |
389 |
} |
390 |
|
391 |
if (resp == FieldInformation.SHAPE) {
|
392 |
resp = FieldInformation.STRING; |
393 |
} |
394 |
|
395 |
// stop();
|
396 |
return resp;
|
397 |
} |
398 |
|
399 |
public void addInIdToIndexHashMap(int i, int rwcount) { |
400 |
idToIndex.put(new Integer(i), new Integer(rwcount)); |
401 |
} |
402 |
|
403 |
public void addAsRequested(FBitSet fbs) { |
404 |
requested.or(fbs); |
405 |
} |
406 |
|
407 |
public FBitSet getThisAndNonRequestedUpTo(int _thisone, int total) { |
408 |
int thisone = _thisone;
|
409 |
int rowcount = (int) getRowCount(); |
410 |
|
411 |
if (_thisone >= rowcount) {
|
412 |
thisone = _thisone % rowcount; |
413 |
} |
414 |
|
415 |
FBitSet fbs = new FBitSet();
|
416 |
fbs.set(thisone); |
417 |
|
418 |
if (total < 2) { |
419 |
return fbs;
|
420 |
} |
421 |
|
422 |
int othersMustBeSet = total - 1; |
423 |
int checkind;
|
424 |
|
425 |
for (int j = 1; j < rowcount; j++) { |
426 |
// checkind = (i + j) % noOfRows;
|
427 |
checkind = (thisone + j) % rowcount; |
428 |
|
429 |
if (isNonRequestedRow(checkind)) {
|
430 |
fbs.set(checkind); |
431 |
othersMustBeSet--; |
432 |
} |
433 |
|
434 |
if (othersMustBeSet == 0) { |
435 |
break;
|
436 |
} |
437 |
} |
438 |
|
439 |
return fbs;
|
440 |
} |
441 |
|
442 |
public void addEmptyRowWithId(Value idval, String idcolname) { |
443 |
data.add(null);
|
444 |
id.add(new Integer(((IntValue) idval).intValue())); |
445 |
} |
446 |
|
447 |
public Value doGetFieldValue(long row, int colind) { |
448 |
// printLastAndUpdate(row, colind);
|
449 |
if ((row >= getRowCount()) || (row < 0)) { |
450 |
logger.error("Row index out of limits ");
|
451 |
|
452 |
return null; |
453 |
} |
454 |
|
455 |
Value[] therow = (Value[]) data.get((int) row); |
456 |
|
457 |
if (therow == null) { |
458 |
logger.error("Tried to get non available value (?) ");
|
459 |
|
460 |
return null; |
461 |
} |
462 |
|
463 |
if ((colind >= therow.length) || (colind < 0)) { |
464 |
logger.error("Field index out of limits ");
|
465 |
|
466 |
return null; |
467 |
} |
468 |
|
469 |
if (therow[colind] == null) { |
470 |
logger.warn("In order to avoid returning element [ " + row + ", " + |
471 |
colind + |
472 |
" ] (which is null), returned ValueFactory.createNullValue()");
|
473 |
|
474 |
return ValueFactory.createNullValue();
|
475 |
} |
476 |
else {
|
477 |
return therow[colind];
|
478 |
} |
479 |
} |
480 |
|
481 |
|
482 |
|
483 |
public Value getFieldValue(long row, int colind) throws ReadDriverException { |
484 |
if (isNonRequestedRow((int) row)) { |
485 |
int[] lim = theParentAdapter.getBlockLimits((int) row); |
486 |
|
487 |
System.err.println("Se ha pedido fila no disponible: " + row); |
488 |
System.err.println("Limites: [ " + lim[0] + " , " + lim[1] + " ]"); |
489 |
|
490 |
// FBitSet fbs = dataDriver.getThisAndNonRequestedUpTo((int) row, rowsPerRequest);
|
491 |
try {
|
492 |
theParentDrv.requestBlockWithoutChecking(lim); |
493 |
} |
494 |
catch (ArcImsException e) {
|
495 |
ReadDriverException de = new ReadDriverException("In getFieldValue.", e); |
496 |
logger.error("While getting row: " + e.getMessage());
|
497 |
throw de;
|
498 |
} |
499 |
} |
500 |
|
501 |
// Value v = dataDriver.getCachedValue((int) row, colind);
|
502 |
// if (v != null) {
|
503 |
// return v;
|
504 |
// }
|
505 |
Value resp = doGetFieldValue(row, colind); |
506 |
|
507 |
// stop();
|
508 |
// dataDriver.setCachedValue((int) row, colind, resp);
|
509 |
return resp;
|
510 |
} |
511 |
|
512 |
private void printLastAndUpdate(long row, int colind) { |
513 |
logger.debug("Last request: [ " + lastRequest.height + " , " + |
514 |
lastRequest.width + " ]");
|
515 |
lastRequest.height = (int) row;
|
516 |
lastRequest.width = colind; |
517 |
logger.debug("Current request: [ " + lastRequest.height + " , " + |
518 |
lastRequest.width + " ]");
|
519 |
} |
520 |
|
521 |
public int getIdColumnInd(ArrayList col_Types) { |
522 |
int coltype;
|
523 |
int idindex = -1; |
524 |
|
525 |
for (int i = 0; i < col_Types.size(); i++) { |
526 |
coltype = ((Integer) col_Types.get(i)).intValue();
|
527 |
|
528 |
if (coltype == FieldInformation.ID) {
|
529 |
idindex = i; |
530 |
|
531 |
break;
|
532 |
} |
533 |
} |
534 |
|
535 |
if (idindex == -1) { |
536 |
logger.error("ID not found ");
|
537 |
} |
538 |
|
539 |
return idindex;
|
540 |
} |
541 |
|
542 |
public String getTableName() { |
543 |
return tableName;
|
544 |
} |
545 |
|
546 |
public static String replaceUnwantedCharacters(String str) { |
547 |
String resp = str;
|
548 |
resp = resp.replace('.', '_'); |
549 |
resp = resp.replace('#', 'z'); |
550 |
|
551 |
return resp;
|
552 |
} |
553 |
|
554 |
public String getOriginalFieldName(int idcolindex) { |
555 |
return (String) originalColumnNames.get(idcolindex); |
556 |
} |
557 |
|
558 |
public String getSqlFieldName(int idcolindex) { |
559 |
return (String) sqlColumnNames.get(idcolindex); |
560 |
} |
561 |
|
562 |
public int[] getPrimaryKeys() throws ReadDriverException { |
563 |
int[] resp = new int[1]; |
564 |
resp[0] = getIdFieldInd();
|
565 |
|
566 |
return resp;
|
567 |
|
568 |
} |
569 |
|
570 |
public void reload() throws ReloadDriverException { |
571 |
// TODO Auto-generated method stub
|
572 |
|
573 |
} |
574 |
|
575 |
public void write(DataWare dataWare) throws WriteDriverException, |
576 |
ReadDriverException { |
577 |
// TODO Auto-generated method stub
|
578 |
|
579 |
} |
580 |
|
581 |
public void setDataSourceFactory(DataSourceFactory arg0) { |
582 |
dsFactory = arg0; |
583 |
} |
584 |
|
585 |
public int getFieldWidth(int i) throws ReadDriverException { |
586 |
return 40; |
587 |
} |
588 |
|
589 |
public DataSourceFactory getDataSourceFactory() {
|
590 |
return dsFactory;
|
591 |
} |
592 |
|
593 |
|
594 |
// ================== Object driver
|
595 |
} |