Statistics
| Revision:

root / trunk / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / fmap / drivers / ArcImsAttributesDataSourceAdapter.java @ 8110

History | View | Annotate | Download (17.4 KB)

1
package es.prodevelop.cit.gvsig.arcims.fmap.drivers;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.sql.SQLException;
6
import java.sql.Types;
7
import java.util.ArrayList;
8
import java.util.BitSet;
9
import java.util.HashMap;
10

    
11
import org.apache.log4j.Logger;
12
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
13
import org.gvsig.remoteClient.arcims.utils.FieldInformation;
14
import org.gvsig.remoteClient.arcims.utils.GetFeaturesTags;
15

    
16
import com.hardcode.driverManager.Driver;
17
import com.hardcode.driverManager.DriverLoadException;
18
import com.hardcode.gdbms.engine.data.DataSource;
19
import com.hardcode.gdbms.engine.data.DataSourceFactory;
20
import com.hardcode.gdbms.engine.data.IDataSourceListener;
21
import com.hardcode.gdbms.engine.data.SourceInfo;
22
import com.hardcode.gdbms.engine.data.driver.DriverException;
23
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
24
import com.hardcode.gdbms.engine.data.edition.DataWare;
25
import com.hardcode.gdbms.engine.data.object.ObjectSourceInfo;
26
import com.hardcode.gdbms.engine.data.persistence.DataSourceLayerMemento;
27
import com.hardcode.gdbms.engine.data.persistence.Memento;
28
import com.hardcode.gdbms.engine.data.persistence.MementoException;
29
import com.hardcode.gdbms.engine.instruction.EvaluationException;
30
import com.hardcode.gdbms.engine.instruction.SemanticException;
31
import com.hardcode.gdbms.engine.values.IntValue;
32
import com.hardcode.gdbms.engine.values.Value;
33
import com.hardcode.gdbms.engine.values.ValueCollection;
34
import com.hardcode.gdbms.engine.values.ValueFactory;
35
import com.hardcode.gdbms.parser.ParseException;
36
import com.iver.cit.gvsig.fmap.layers.FBitSet;
37

    
38
import es.prodevelop.cit.gvsig.arcims.gui.wizards.ArcImsWizard;
39

    
40
public class ArcImsAttributesDataSourceAdapter implements ArcImsAlphanumericDataSource, ObjectDriver {
41
        
42
        private static Logger logger = Logger.getLogger(ArcImsAttributesDataSourceAdapter.class.getName());
43
        
44
        private ArcImsInMemoryAttsTableDriver dataDriver;
45
        // private File csvFile;
46
        private String tableName;
47
        private DataSourceFactory dsFactory = null;
48
        private ObjectSourceInfo sourceInfo;
49
        private FMapFeatureArcImsDriver parentDriver;
50
        private int rowsPerRequest = 10;
51
        // private int privateKeyIndex = 0;
52
        
53
        // array of Value[]
54
        // private AttributesDataa attsData; 
55
        
56
        public ArcImsAttributesDataSourceAdapter(ArrayList col_Names, ArrayList col_Types,
57
                        Value[] ids, DataSourceFactory dsf, FMapFeatureArcImsDriver drv, String forcedName) {
58
                
59
                // attsData = new AttributesDataa(col_Names, col_Types, ids, drv);
60
                
61
                parentDriver = drv;
62
                dsFactory = dsf;
63

    
64
                if (forcedName == null) {
65
                        long timeid = System.currentTimeMillis();
66
                        tableName = "table_" + timeid;
67
                } else {
68
                        tableName = forcedName;
69
                }
70

    
71
                dataDriver = new ArcImsInMemoryAttsTableDriver(tableName, col_Names, col_Types);
72
                
73
                sourceInfo = new ObjectSourceInfo();
74
                sourceInfo.driver = this;
75
                sourceInfo.name = tableName;
76
                sourceInfo.driverName = dataDriver.getName();
77
                
78
//                dsFactory = new DataSourceFactory();
79
//                dsFactory.initialize();
80
//                dsFactory.addDataSource(this, tableName);
81
                
82
                // dsFactory.crea
83
                
84
                try {
85
                        // dataDriver.open(dataDriver.getConnection(), tableName);
86
                        
87
//                        String sqlStr = ArcImsSqlUtils.getSqlSentenceCreateTableWithFieldsAndTypes(tableName, col_Names, col_Types, dataDriver.getRowIndexFieldName());
88
//                        dataDriver.open(sqlStr);
89
                        
90
                        // sqlStr = ArcImsSqlUtils.getSqlSentenceCreateIndex(tableName);
91
                        // dataDriver.execute(sqlStr);
92
                        // dsFactory.executeSQL(sqlStr, DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
93
                        
94
                        String idcolname = getIdColumnName(col_Names, col_Types);
95
                        for (int i=0; i<ids.length; i++) {
96
                                Value id = ids[i];
97
                                int rwcount = (int) dataDriver.getRowCount(); 
98
//                                sqlStr = ArcImsSqlUtils.getSqlSentenceSetEmptyRowWithId(
99
//                                                tableName, id, idcolname,
100
//                                                rwcount,
101
//                                                dataDriver.getRowIndexFieldName());
102
                                dataDriver.addEmptyRowWithId(id, idcolname);
103
                                // dataDriver.addInIndexToIdHashMap(rwcount, ((IntValue) id).intValue());
104
                                // logger.debug("Adding id = " + ((IntValue) id).intValue() + " in row = " + rwcount);
105
                                dataDriver.addInIdToIndexHashMap(((IntValue) id).intValue(), rwcount);
106
                                // dsFactory.executeSQL(sqlStr, DataSourceFactory.DATA_WARE_DIRECT_MODE);
107
//                                dataDriver.execute(sqlStr);
108
//                                dataDriver.increaseRowCount(false);
109
                        }
110
                        logger.info("Stored " + ids.length + " IDs");
111
                        setRowsPerRequest();
112
                        // String idcolname = dataDriver.executeCreateTableWithFieldsAndTypes(tableName, col_Names, col_Types);
113
                        // dataDriver.executeSetEmptyRowsWithIds(tableName, ids, idcolname);
114
                        // System.err.println("dataDriver.getFieldValue(0, 0) = " + dataDriver.getFieldValue(0, 0).getStringValue(dataDriver.internalValueWriter));
115
                        // getRowId(5);
116
                        // stop();
117
                        // logger.debug("Done: dataDriver.close();");
118
                        
119
                } catch (Exception e) {
120
                        logger.error("While creating in-memory database ", e);
121
                }
122
                
123
        }
124

    
125
        public void deleteRow(long rowInd) throws DriverException {
126
                // DriverException de = new DriverException("cannot_delete");
127
                logger.warn("So far, you cannot delete ");
128
                // throw de;
129
//                // dataDriver.setRequested((int) rowInd, false);
130
//                start();
131
//                dataDriver.deleteRow(dsFactory, (int) rowInd);
132
//                stop();
133
//                // dataDriver.deleteRow(dsFactory, (int) rowInd);
134
        }
135

    
136
        public void insertFilledRowAt(long index, Value[] values) throws DriverException {
137
                insertFilledRowAt(index, values, true);
138
        }
139
        
140
        public void insertFilledRowAt(long index, Value[] values, boolean req) throws DriverException {
141
                // dataDriver.setRequested((int) dataDriver.getRowCount(), req);
142
                // start();
143
                dataDriver.addRow(values, req);
144
                // stop();
145
                logger.warn("Row was added at the end (not inserted at position " + index + ")");
146
                // this.
147
                // dataDriver.addRow(dsFactory, values);
148
                // dataDriver.insertRow((int) index, values);
149
        }
150

    
151
        public void insertEmptyRowAt(long index) throws DriverException {
152
                
153
                // start();
154
                int count = dataDriver.getFieldCount();
155
                // stop();
156
                
157
                Value[] empty = new Value[count];
158
                for (int i=0; i<count; i++) {
159
                        empty[i] = ValueFactory.createNullValue();
160
                }
161
                insertFilledRowAt(index, empty, false);
162
        }
163

    
164
        public void start() throws DriverException {
165
//                try {
166
//                        // dataDriver.open("");
167
//                        dataDriver.open("select * from " + tableName);
168
//                } catch (SQLException e) {
169
//                        DriverException de = new DriverException(e.getMessage());
170
//                        logger.error("While starting datasource ", e);
171
//                        throw de;
172
//                }
173
//                // logger.debug("start()");
174
//                // dataDriver.open("");
175
        }
176

    
177
        public void stop() throws DriverException {
178
//                try {
179
//                        dataDriver.close();
180
//                } catch (SQLException e) {
181
//                        DriverException de = new DriverException(e.getMessage());
182
//                        throw de;
183
//                }
184
        }
185

    
186
        public String getName() {
187
                return tableName;
188
        }
189

    
190
        public String getAlias() {
191
                return tableName;
192
                // return tableAlias;
193
        }
194

    
195
        public long[] getWhereFilter() throws IOException {
196
                // dataDriver.
197
                return null;
198
//                String message = "'Where' filter not used in method: ArcImsAttributesDataSourceAdapter.getWhereFilter()";
199
//                IOException ioe = new IOException(message);
200
//                throw ioe;
201
        }
202

    
203
        public DataSourceFactory getDataSourceFactory() {
204
                return dsFactory;
205
        }
206

    
207
        public Memento getMemento() throws MementoException {
208
        DataSourceLayerMemento m = new DataSourceLayerMemento(tableName, tableName);
209
        return m;
210
        }
211

    
212
        public void setDataSourceFactory(DataSourceFactory arg0) {
213
                dsFactory = arg0;
214
        }
215

    
216
        public void setSourceInfo(SourceInfo arg0) {
217
                sourceInfo = (ObjectSourceInfo) arg0;
218
        }
219

    
220
        public SourceInfo getSourceInfo() {
221
                return sourceInfo;
222
        }
223

    
224
        public String getAsString() throws DriverException {
225
//                return attsData.getAsString();
226
                return dataDriver.toString();
227
        }
228

    
229
        public void remove() throws DriverException {
230
                if (dsFactory == null) {
231
                        logger.error("dsFactory is null (?) and cannot receive the remove method ");
232
                        return;
233
                }
234
                
235
                try {
236
                        dsFactory.remove(this);
237
                } catch (DriverException e) {
238
                        String message = "Driver Exception in method: "
239
                                + "ArcImsAttributesDataSourceAdapter.remove()\n"
240
                                + e.getMessage();
241
                        DriverException de = new DriverException(message);
242
                        throw de;
243
                }
244
        }
245

    
246
        public int[] getPrimaryKeys() throws DriverException {
247
                int[] resp = new int[1];
248
                resp[0] = dataDriver.getIdFieldInd();
249
                return resp;
250
        }
251

    
252
        public ValueCollection getPKValue(long arg0) throws DriverException {
253
                ValueCollection vc = new ValueCollection();
254
                vc.add( ValueFactory.createValue(getRowId((int) arg0)) );
255
                return vc;
256
        }
257

    
258
        public String getPKName(int arg0) throws DriverException {
259
                if (arg0 == 0) {
260
                        return dataDriver.getIdFieldName();
261
                } else {
262
                        DriverException de = new DriverException("pk_error");
263
                        logger.error("Multiple primary key are never used. ", de);
264
                        throw de;
265
                }
266
        }
267

    
268
        public String[] getPKNames() throws DriverException {
269
                String[] resp = new String[1];
270
                resp[0] = dataDriver.getIdFieldName();
271
                return resp;
272
                
273
        }
274

    
275
        public int getPKType(int arg0) throws DriverException {
276
                if (arg0 == 0) {
277
//                        return attsData.getFieldType(attsData.getIdIndex());
278
                        return Types.BIGINT;
279
                } else {
280
                        DriverException de = new DriverException("pk_error");
281
                        logger.error("Multiple primary key are never used. ", de);
282
                        throw de;
283
                }
284
        }
285

    
286
        public int getPKCardinality() throws DriverException {
287
                // Multiple primary key are never used.
288
                return 1;
289
        }
290

    
291
        public Value[] getRowWithoutDownloading(long row) throws DriverException {
292
                // start();
293
                Value[] resp = dataDriver.getRow(dsFactory, (int) row);
294
                // stop();
295
                return resp;
296
        }
297
        
298
        public Value[] getRow(long arg0) throws DriverException {
299
//                Value[] resp = attsData.getRow((int) arg0);
300
//                return resp;
301
                // return null;
302
                FBitSet fbs = dataDriver.getThisAndNonRequestedUpTo((int) arg0, rowsPerRequest);
303
                try {
304
                        parentDriver.requestFeatureAttributesWithoutChecking(fbs);
305
                } catch (ArcImsException e) {
306
                        DriverException de = new DriverException(e.getMessage());
307
                        logger.error("While getting row ", de);
308
                        throw de;
309
                }
310
                // start();
311
                Value[] resp = dataDriver.getRow(dsFactory, (int) arg0);
312
                // stop();
313
                return resp;
314
        }
315

    
316
        public String[] getFieldNames() throws DriverException {
317
//                String[] resp = attsData.getFieldNames();
318
//                return resp;
319
                // return null;
320
                // start();
321
                int count = dataDriver.getFieldCount();
322
                String[] resp = new String[count];
323
                for (int i=0; i<count; i++) {
324
                        // resp[i] = dataDriver.getFieldName(i);
325
                        resp[i] = dataDriver.getSqlFieldName(i);
326
                }
327
                // stop();
328
                return resp;
329
                
330
                // dataDriver.ge
331
        }
332

    
333
        public int getFieldIndexByName(String arg0) throws DriverException {
334
//                int resp = attsData.getFieldIndexByName(arg0);
335
//                return resp;
336
                // start();
337
                int resp = dataDriver.getFieldIndexByName(arg0);
338
                // stop();
339
                return resp;
340
                
341
                // return 0;
342
        }
343

    
344
        public DataWare getDataWare(int arg0) {
345
//                DataWare resp = attsData.getDataWare(arg0);
346
//                return resp;
347
                return null;
348
        }
349

    
350
        public Value getFieldValue(long row, int colind) throws DriverException {
351
                
352
                if (dataDriver.isNonRequestedRow((int) row)) {
353
                        
354
                        int[] lim = getBlockLimits((int) row);
355
                        
356
                        System.err.println("Se ha pedido fila no disponible: " + row);
357
                        System.err.println("Limites: [ " + lim[0] + " , " + lim[1] + " ]");
358
                        // FBitSet fbs = dataDriver.getThisAndNonRequestedUpTo((int) row, rowsPerRequest);
359
                        try {
360
                                parentDriver.requestBlockWithoutChecking(lim);
361
                        } catch (ArcImsException e) {
362
                                DriverException de = new DriverException(e.getMessage());
363
                                logger.error("While getting row ", de);
364
                                throw de;
365
                        }
366
                }
367
                
368
//                Value v = dataDriver.getCachedValue((int) row, colind);
369
//                if (v != null) {
370
//                        return v;
371
//                }
372
                
373
//                start();
374
                Value resp = dataDriver.getFieldValue(row, colind);
375
//                stop();
376
//                dataDriver.setCachedValue((int) row, colind, resp);
377
                return resp;
378
        }
379

    
380
        public int getFieldCount() throws DriverException {
381
//                int resp = attsData.getFieldCount();
382
//                return resp;
383
                // start();
384
                int resp = dataDriver.getFieldCount();
385
                // stop();
386
                return resp;
387
                
388
                // return 0;
389
        }
390

    
391
        public String getFieldName(int arg0) throws DriverException {
392
//                String resp = attsData.getFieldName(arg0);
393
//                return resp;
394
                // start();
395
                String resp = dataDriver.getSqlFieldName(arg0);
396
                // stop();
397
                return resp;
398
                // return "";
399
        }
400

    
401
        public long getRowCount() throws DriverException {
402
//                long resp = (int) attsData.getRowCount();
403
//                return resp;
404
                // start();
405
                long resp = (long) dataDriver.getRowCount();
406
                // stop();
407
                return resp;
408
                // return 0;
409
        }
410

    
411
        public int getFieldType(int arg0) throws DriverException {
412
//                int resp = attsData.getFieldType(arg0);
413
//                return resp;
414
                // start();
415
                int resp = dataDriver.getFieldType(arg0);
416
                // stop();
417
                return resp;
418
                // return 0;
419
        }
420

    
421
        // **************************
422
        // new_row[0] must contain ID
423
        // **************************
424
        // returns added row index 
425
        public void updateRow(Value[] new_row, String[] fld_query, int rowind) throws DriverException {
426
                // dataDriver.
427
//                start();
428
                // int resp =
429
                dataDriver.updateRow(new_row, fld_query, rowind);
430
//                stop();
431
                // return resp;
432
                // return 0;
433
                
434
//                if (! ((new_row.length == fld_query.length)
435
//                                || ((new_row.length == getFieldCount()) && (fld_query[0].compareTo("#ALL#") == 0)))) {
436
//                        DriverException de = new DriverException("array_error");
437
//                        logger.error("Wrong array sizes while updating row ", de);
438
//                        throw de;
439
//                }
440
//                
441
//                if (new_row.length == 0) {
442
//                        DriverException de = new DriverException("array_error");
443
//                        logger.error("Wrong array sizes while updating row ", de);
444
//                        throw de;
445
//                }
446
//                
447
//                if (fld_query[0].compareTo("#ALL#") == 0) {
448
//                        int length = getFieldCount();
449
//                        String newfldquery[] = new String[length];
450
//                        for (int i=0; i<length; i++) {
451
//                                newfldquery[i] = getFieldName(i);
452
//                        }
453
//                        return attsData.updateRow(new_row, newfldquery);
454
//                }
455
//                
456
//                return attsData.updateRow(new_row, fld_query);
457
        }
458
        
459
        public String getIdColName() throws DriverException {
460
                String resp = dataDriver.getIdFieldName();
461
                return resp;
462
//                return attsData.getIdColName();
463
                // return "";
464
        }
465

    
466
        public int getIdIndex() throws DriverException {
467
                int resp = dataDriver.getIdFieldInd();
468
                return resp;
469
//                return attsData.getIdIndex();
470
                // return 0;
471
        }
472
        
473
        public int getRowId(int row) throws DriverException {
474
//                start();
475
//                int resp = dataDriver.getRowId(dsFactory, row);
476
//                stop();
477
                int resp = dataDriver.getRowId(row);
478
                return resp;
479
                // System.out.println("dataDriver.getRowId(dsFactory, " + row + ") = " + dataDriver.getRowId(dsFactory, row));
480
                // return 0;
481
                // return dataDriver.getRowId(row);
482
//                int resp = -1;
483
//                try {
484
//                        resp =  attsData.getRowId(row);
485
//                } catch (DriverException e) {
486
//                        logger.error("While getting row ID ", e);
487
//                }
488
//                return resp;
489
        }
490
        
491
        public int getRowIndex(int id) throws DriverException {
492
//                start();
493
//                int resp = dataDriver.getRowIndex(dsFactory, id);
494
//                stop();
495
                int resp = dataDriver.getRowIndex(id);
496
                return resp;
497
        }
498
        
499
        public boolean isNonRequestedRow(int rowind) {
500
//                return attsData.isNonRequestedRow(rowind);
501
                return dataDriver.isNonRequestedRow(rowind);
502
        }
503

    
504
        public void write(DataWare arg0) throws DriverException {
505
                // TODO Auto-generated method stub
506
        }
507

    
508
        public String getTableName() {
509
                return tableName;
510
        }
511
        
512
//        public String getOriginalColumnName(int idcolindex) {
513
//                return dataDriver.getOriginalColumnName(idcolindex);
514
//        }
515

    
516
        public void addAsRequested(FBitSet fbs) {
517
                dataDriver.addAsRequested(fbs);
518
        }
519

    
520
        public BitSet getNonRequestedFromHere(int start, int total) {
521
                return dataDriver.getThisAndNonRequestedUpTo(start, total);
522
        }
523
        
524
        public String getIdColumnName(ArrayList col_Names, ArrayList col_Types) {
525
                
526
                int coltype, idindex = -1;
527
                for (int i=0; i<col_Types.size(); i++) {
528
                        coltype = ((Integer) col_Types.get(i)).intValue();
529
                        if (coltype == FieldInformation.ID) {
530
                                idindex = i;
531
                                break;
532
                        }
533
                }
534
                if (idindex == -1) {
535
                        logger.error("ID not found ");
536
                }
537
                return (String) col_Names.get(idindex);
538
        }
539

    
540
        public int getRowsPerRequest() {
541
                return rowsPerRequest;
542
        }
543
        
544
        private void setRowsPerRequest() {
545
                
546
                int rowcount = (int) dataDriver.getRowCount();
547
                int sqroot = Math.round( (float) Math.sqrt(1.0 * rowcount) );
548
                
549
                rowsPerRequest = sqroot;
550
                
551
                if (rowsPerRequest > GetFeaturesTags.MAX_ROWS_PER_REQUEST) {
552
                        rowsPerRequest = GetFeaturesTags.MAX_ROWS_PER_REQUEST;
553
                }
554
                
555
                if (rowsPerRequest < GetFeaturesTags.MIN_ROWS_PER_REQUEST) {
556
                        rowsPerRequest = GetFeaturesTags.MIN_ROWS_PER_REQUEST;
557
                }
558
                
559
                if (rowsPerRequest > rowcount) {
560
                        rowsPerRequest = rowcount;
561
                }
562
                logger.info("Rows per request was set to " + rowsPerRequest + " (rowcount = " + rowcount + ")");
563
        }
564

    
565
        public String getOriginalFieldName(int idcolindex) {
566
                return dataDriver.getOriginalFieldName(idcolindex);
567
        }
568

    
569
        public boolean isVirtualField(int fieldId) throws DriverException {
570
                // TODO Auto-generated method stub
571
                return false;
572
        }
573

    
574
        public int getFieldWidth(int i) throws DriverException {
575
                // TODO Auto-generated method stub
576
                return 10;
577
        }
578
        
579
        private int[] getBlockLimits(int row) {
580
                int[] resp = new int[2];
581
                
582
                int rcount = 0;
583
                try {
584
                        rcount = (int) getRowCount();
585
                } catch (DriverException e) {
586
                        logger.error("While getting rowcount ", e);
587
                }
588
                int rperreq = getRowsPerRequest();
589
                
590
                int bloque = (row / rperreq);
591
                int inicio = rperreq * bloque;
592
                
593
                int fin = inicio + rperreq - 1;
594
                if (fin > (rcount-1)) {
595
                        fin = (rcount-1);
596
                }
597
                
598
                resp[0] = inicio;
599
                resp[1] = fin;
600
                return resp;
601
        }
602

    
603
        public Driver getDriver() {
604
                // TODO Auto-generated method stub
605
                return null;
606
        }
607

    
608
        public void reload() throws DriverException, IOException {
609
                // TODO Auto-generated method stub
610
                
611
        }
612

    
613
        public void addDataSourceListener(IDataSourceListener arg0) {
614
                // TODO Auto-generated method stub
615
                
616
        }
617

    
618
        public void removeDataSourceListener(IDataSourceListener arg0) {
619
                // TODO Auto-generated method stub
620
                
621
        }
622
        
623
        
624
}
625

    
626