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