Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / fmap / drivers / ArcImsAttributesDataSourceAdapter.java @ 20976

History | View | Annotate | Download (19.1 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.Driver;
46
import com.hardcode.driverManager.DriverLoadException;
47

    
48
import com.hardcode.gdbms.engine.data.DataSource;
49
import com.hardcode.gdbms.engine.data.DataSourceFactory;
50
import com.hardcode.gdbms.engine.data.IDataSourceListener;
51
import com.hardcode.gdbms.engine.data.SourceInfo;
52
import com.hardcode.gdbms.engine.data.driver.DriverException;
53
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
54
import com.hardcode.gdbms.engine.data.edition.DataWare;
55
import com.hardcode.gdbms.engine.data.object.ObjectSourceInfo;
56
import com.hardcode.gdbms.engine.data.persistence.DataSourceLayerMemento;
57
import com.hardcode.gdbms.engine.data.persistence.Memento;
58
import com.hardcode.gdbms.engine.data.persistence.MementoException;
59
import com.hardcode.gdbms.engine.instruction.EvaluationException;
60
import com.hardcode.gdbms.engine.instruction.SemanticException;
61
import com.hardcode.gdbms.engine.values.IntValue;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.hardcode.gdbms.engine.values.ValueCollection;
64
import com.hardcode.gdbms.engine.values.ValueFactory;
65
import com.hardcode.gdbms.parser.ParseException;
66

    
67
import com.iver.cit.gvsig.fmap.layers.FBitSet;
68

    
69
import es.prodevelop.cit.gvsig.arcims.gui.wizards.ArcImsWizard;
70

    
71
import org.apache.log4j.Logger;
72

    
73
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
74
import org.gvsig.remoteClient.arcims.utils.FieldInformation;
75
import org.gvsig.remoteClient.arcims.utils.GetFeaturesTags;
76

    
77
import java.io.File;
78
import java.io.IOException;
79

    
80
import java.sql.SQLException;
81
import java.sql.Types;
82

    
83
import java.util.ArrayList;
84
import java.util.BitSet;
85
import java.util.HashMap;
86

    
87

    
88
public class ArcImsAttributesDataSourceAdapter
89
    implements ArcImsAlphanumericDataSource, ObjectDriver {
90
    private static Logger logger = Logger.getLogger(ArcImsAttributesDataSourceAdapter.class.getName());
91
    private ArcImsInMemoryAttsTableDriver dataDriver;
92

    
93
    // private File csvFile;
94
    private String tableName;
95
    private DataSourceFactory dsFactory = null;
96
    private ObjectSourceInfo sourceInfo;
97
    private FMapFeatureArcImsDriver parentDriver;
98
    private int rowsPerRequest = 10;
99

    
100
    // private int privateKeyIndex = 0;
101

    
102
    // array of Value[]
103
    // private AttributesDataa attsData; 
104
    public ArcImsAttributesDataSourceAdapter(ArrayList col_Names,
105
        ArrayList col_Types, Value[] ids, DataSourceFactory dsf,
106
        FMapFeatureArcImsDriver drv, String forcedName) {
107
        // attsData = new AttributesDataa(col_Names, col_Types, ids, drv);
108
        parentDriver = drv;
109
        dsFactory = dsf;
110

    
111
        if (forcedName == null) {
112
            long timeid = System.currentTimeMillis();
113
            tableName = "table_" + timeid;
114
        }
115
        else {
116
            tableName = forcedName;
117
        }
118

    
119
        dataDriver = new ArcImsInMemoryAttsTableDriver(tableName, col_Names,
120
                col_Types);
121

    
122
        sourceInfo = new ObjectSourceInfo();
123
        sourceInfo.driver = this;
124
        sourceInfo.name = tableName;
125
        sourceInfo.driverName = dataDriver.getName();
126

    
127
        //                dsFactory = new DataSourceFactory();
128
        //                dsFactory.initialize();
129
        //                dsFactory.addDataSource(this, tableName);
130
        try {
131
            // dataDriver.open(dataDriver.getConnection(), tableName);
132

    
133
            //                        String sqlStr = ArcImsSqlUtils.getSqlSentenceCreateTableWithFieldsAndTypes(tableName, col_Names, col_Types, dataDriver.getRowIndexFieldName());
134
            //                        dataDriver.open(sqlStr);
135
            String idcolname = getIdColumnName(col_Names, col_Types);
136

    
137
            for (int i = 0; i < ids.length; i++) {
138
                Value id = ids[i];
139
                int rwcount = (int) dataDriver.getRowCount();
140

    
141
                //                                sqlStr = ArcImsSqlUtils.getSqlSentenceSetEmptyRowWithId(
142
                //                                                tableName, id, idcolname,
143
                //                                                rwcount,
144
                //                                                dataDriver.getRowIndexFieldName());
145
                dataDriver.addEmptyRowWithId(id, idcolname);
146

    
147
                // dataDriver.addInIndexToIdHashMap(rwcount, ((IntValue) id).intValue());
148
                // logger.debug("Adding id = " + ((IntValue) id).intValue() + " in row = " + rwcount);
149
                dataDriver.addInIdToIndexHashMap(((IntValue) id).intValue(),
150
                    rwcount);
151

    
152
                // dsFactory.executeSQL(sqlStr, DataSourceFactory.DATA_WARE_DIRECT_MODE);
153
                //                                dataDriver.execute(sqlStr);
154
                //                                dataDriver.increaseRowCount(false);
155
            }
156

    
157
            logger.info("Stored " + ids.length + " IDs");
158
            setRowsPerRequest();
159
        }
160
        catch (Exception e) {
161
            logger.error("While creating in-memory database ", e);
162
        }
163
    }
164

    
165
    public void deleteRow(long rowInd) throws DriverException {
166
        // DriverException de = new DriverException("cannot_delete");
167
        logger.warn("So far, you cannot delete ");
168

    
169
    }
170

    
171
    public void insertFilledRowAt(long index, Value[] values)
172
        throws DriverException {
173
        insertFilledRowAt(index, values, true);
174
    }
175

    
176
    public void insertFilledRowAt(long index, Value[] values, boolean req)
177
        throws DriverException {
178
        // dataDriver.setRequested((int) dataDriver.getRowCount(), req);
179
        // start();
180
        dataDriver.addRow(values, req);
181

    
182
        // stop();
183
        logger.warn("Row was added at the end (not inserted at position " +
184
            index + ")");
185

    
186
        // this.
187
        // dataDriver.addRow(dsFactory, values);
188
        // dataDriver.insertRow((int) index, values);
189
    }
190

    
191
    public void insertEmptyRowAt(long index) throws DriverException {
192
        // start();
193
        int count = dataDriver.getFieldCount();
194

    
195
        // stop();
196
        Value[] empty = new Value[count];
197

    
198
        for (int i = 0; i < count; i++) {
199
            empty[i] = ValueFactory.createNullValue();
200
        }
201

    
202
        insertFilledRowAt(index, empty, false);
203
    }
204

    
205
    public void start() throws DriverException {
206
    }
207

    
208
    public void stop() throws DriverException {
209
    }
210

    
211
    public String getName() {
212
        return tableName;
213
    }
214

    
215
    public String getAlias() {
216
        return tableName;
217

    
218
        // return tableAlias;
219
    }
220

    
221
    public long[] getWhereFilter() throws IOException {
222
        // dataDriver.
223
        return null;
224

    
225
        //                String message = "'Where' filter not used in method: ArcImsAttributesDataSourceAdapter.getWhereFilter()";
226
        //                IOException ioe = new IOException(message);
227
        //                throw ioe;
228
    }
229

    
230
    public DataSourceFactory getDataSourceFactory() {
231
        return dsFactory;
232
    }
233

    
234
    public Memento getMemento() throws MementoException {
235
        DataSourceLayerMemento m = new DataSourceLayerMemento(tableName,
236
                tableName);
237

    
238
        return m;
239
    }
240

    
241
    public void setDataSourceFactory(DataSourceFactory arg0) {
242
        dsFactory = arg0;
243
    }
244

    
245
    public void setSourceInfo(SourceInfo arg0) {
246
        sourceInfo = (ObjectSourceInfo) arg0;
247
    }
248

    
249
    public SourceInfo getSourceInfo() {
250
        return sourceInfo;
251
    }
252

    
253
    public String getAsString() throws DriverException {
254
        //                return attsData.getAsString();
255
        return dataDriver.toString();
256
    }
257

    
258
    public void remove() throws DriverException {
259
        if (dsFactory == null) {
260
            logger.error(
261
                "dsFactory is null (?) and cannot receive the remove method ");
262

    
263
            return;
264
        }
265

    
266
        try {
267
            dsFactory.remove(this);
268
        }
269
        catch (DriverException e) {
270
            String message = "Driver Exception in method: " +
271
                "ArcImsAttributesDataSourceAdapter.remove()\n" +
272
                e.getMessage();
273
            DriverException de = new DriverException(message);
274
            throw de;
275
        }
276
    }
277

    
278
    public int[] getPrimaryKeys() throws DriverException {
279
        int[] resp = new int[1];
280
        resp[0] = dataDriver.getIdFieldInd();
281

    
282
        return resp;
283
    }
284

    
285
    public ValueCollection getPKValue(long arg0) throws DriverException {
286
        ValueCollection vc = new ValueCollection();
287
        vc.add(ValueFactory.createValue(getRowId((int) arg0)));
288

    
289
        return vc;
290
    }
291

    
292
    public String getPKName(int arg0) throws DriverException {
293
        if (arg0 == 0) {
294
            return dataDriver.getIdFieldName();
295
        }
296
        else {
297
            DriverException de = new DriverException("pk_error");
298
            logger.error("Multiple primary key are never used. ", de);
299
            throw de;
300
        }
301
    }
302

    
303
    public String[] getPKNames() throws DriverException {
304
        String[] resp = new String[1];
305
        resp[0] = dataDriver.getIdFieldName();
306

    
307
        return resp;
308
    }
309

    
310
    public int getPKType(int arg0) throws DriverException {
311
        if (arg0 == 0) {
312
            //                        return attsData.getFieldType(attsData.getIdIndex());
313
            return Types.BIGINT;
314
        }
315
        else {
316
            DriverException de = new DriverException("pk_error");
317
            logger.error("Multiple primary key are never used. ", de);
318
            throw de;
319
        }
320
    }
321

    
322
    public int getPKCardinality() throws DriverException {
323
        // Multiple primary key are never used.
324
        return 1;
325
    }
326

    
327
    public Value[] getRowWithoutDownloading(long row) throws DriverException {
328
        // start();
329
        Value[] resp = dataDriver.getRow(dsFactory, (int) row);
330

    
331
        // stop();
332
        return resp;
333
    }
334

    
335
    public Value[] getRow(long arg0) throws DriverException {
336
        //                Value[] resp = attsData.getRow((int) arg0);
337
        //                return resp;
338
        // return null;
339
        FBitSet fbs = dataDriver.getThisAndNonRequestedUpTo((int) arg0,
340
                rowsPerRequest);
341

    
342
        try {
343
            parentDriver.requestFeatureAttributesWithoutChecking(fbs);
344
        }
345
        catch (ArcImsException e) {
346
            DriverException de = new DriverException(e.getMessage());
347
            logger.error("While getting row ", de);
348
            throw de;
349
        }
350

    
351
        // start();
352
        Value[] resp = dataDriver.getRow(dsFactory, (int) arg0);
353

    
354
        // stop();
355
        return resp;
356
    }
357

    
358
    public String[] getFieldNames() throws DriverException {
359
        //                String[] resp = attsData.getFieldNames();
360
        //                return resp;
361
        // return null;
362
        // start();
363
        int count = dataDriver.getFieldCount();
364
        String[] resp = new String[count];
365

    
366
        for (int i = 0; i < count; i++) {
367
            // resp[i] = dataDriver.getFieldName(i);
368
            resp[i] = dataDriver.getSqlFieldName(i);
369
        }
370

    
371
        // stop();
372
        return resp;
373

    
374
        // dataDriver.ge
375
    }
376

    
377
    public int getFieldIndexByName(String arg0) throws DriverException {
378
        //                int resp = attsData.getFieldIndexByName(arg0);
379
        //                return resp;
380
        // start();
381
        int resp = dataDriver.getFieldIndexByName(arg0);
382

    
383
        // stop();
384
        return resp;
385

    
386
        // return 0;
387
    }
388

    
389
    public DataWare getDataWare(int arg0) {
390
        //                DataWare resp = attsData.getDataWare(arg0);
391
        //                return resp;
392
        return null;
393
    }
394

    
395
    public Value getFieldValue(long row, int colind) throws DriverException {
396
        if (dataDriver.isNonRequestedRow((int) row)) {
397
            int[] lim = getBlockLimits((int) row);
398

    
399
            logger.debug("Se ha pedido fila no disponible: " + row);
400
            logger.debug("Limites: [ " + lim[0] + " , " + lim[1] + " ]");
401

    
402
            // FBitSet fbs = dataDriver.getThisAndNonRequestedUpTo((int) row, rowsPerRequest);
403
            try {
404
                parentDriver.requestBlockWithoutChecking(lim);
405
            }
406
            catch (ArcImsException e) {
407
                DriverException de = new DriverException(e.getMessage());
408
                logger.error("While getting row ", de);
409
                throw de;
410
            }
411
        }
412

    
413
        //                Value v = dataDriver.getCachedValue((int) row, colind);
414
        //                if (v != null) {
415
        //                        return v;
416
        //                }
417
        Value resp = dataDriver.getFieldValue(row, colind);
418

    
419
        //                stop();
420
        //                dataDriver.setCachedValue((int) row, colind, resp);
421
        return resp;
422
    }
423

    
424
    public int getFieldCount() throws DriverException {
425
        //                int resp = attsData.getFieldCount();
426
        //                return resp;
427
        // start();
428
        int resp = dataDriver.getFieldCount();
429

    
430
        // stop();
431
        return resp;
432

    
433
        // return 0;
434
    }
435

    
436
    public String getFieldName(int arg0) throws DriverException {
437
        //                String resp = attsData.getFieldName(arg0);
438
        //                return resp;
439
        // start();
440
        String resp = dataDriver.getSqlFieldName(arg0);
441

    
442
        // stop();
443
        return resp;
444

    
445
        // return "";
446
    }
447

    
448
    public long getRowCount() throws DriverException {
449
        //                long resp = (int) attsData.getRowCount();
450
        //                return resp;
451
        // start();
452
        long resp = (long) dataDriver.getRowCount();
453

    
454
        // stop();
455
        return resp;
456

    
457
        // return 0;
458
    }
459

    
460
    public int getFieldType(int arg0) throws DriverException {
461
        //                int resp = attsData.getFieldType(arg0);
462
        //                return resp;
463
        // start();
464
        int resp = dataDriver.getFieldType(arg0);
465

    
466
        // This is done because gvSIG doesnt know about
467
        // FieldInformation.ID and FieldInformation.SHAPE
468
        if (resp == FieldInformation.ID) {
469
            resp = FieldInformation.INTEGER;
470
        }
471

    
472
        if (resp == FieldInformation.SHAPE) {
473
            resp = FieldInformation.STRING;
474
        }
475

    
476
        // stop();
477
        return resp;
478

    
479
        // return 0;
480
    }
481

    
482
    // **************************
483
    // new_row[0] must contain ID
484
    // **************************
485
    // returns added row index 
486
    public void updateRow(Value[] new_row, String[] fld_query, int rowind)
487
        throws DriverException {
488
        // dataDriver.
489
        //                start();
490
        // int resp =
491
        dataDriver.updateRow(new_row, fld_query, rowind);
492

    
493
    }
494

    
495
    public String getIdColName() throws DriverException {
496
        String resp = dataDriver.getIdFieldName();
497

    
498
        return resp;
499

    
500
        //                return attsData.getIdColName();
501
        // return "";
502
    }
503

    
504
    public int getIdIndex() throws DriverException {
505
        int resp = dataDriver.getIdFieldInd();
506

    
507
        return resp;
508

    
509
        //                return attsData.getIdIndex();
510
        // return 0;
511
    }
512

    
513
    public int getRowId(int row) throws DriverException {
514
        //                start();
515
        //                int resp = dataDriver.getRowId(dsFactory, row);
516
        //                stop();
517
        int resp = dataDriver.getRowId(row);
518

    
519
        return resp;
520

    
521
    }
522

    
523
    public int getRowIndex(int id) throws DriverException {
524
        //                start();
525
        //                int resp = dataDriver.getRowIndex(dsFactory, id);
526
        //                stop();
527
        int resp = dataDriver.getRowIndex(id);
528

    
529
        return resp;
530
    }
531

    
532
    public boolean isNonRequestedRow(int rowind) {
533
        //                return attsData.isNonRequestedRow(rowind);
534
        return dataDriver.isNonRequestedRow(rowind);
535
    }
536

    
537
    public void write(DataWare arg0) throws DriverException {
538
        // TODO Auto-generated method stub
539
    }
540

    
541
    public String getTableName() {
542
        return tableName;
543
    }
544

    
545
    //        public String getOriginalColumnName(int idcolindex) {
546
    //                return dataDriver.getOriginalColumnName(idcolindex);
547
    //        }
548
    public void addAsRequested(FBitSet fbs) {
549
        dataDriver.addAsRequested(fbs);
550
    }
551

    
552
    public BitSet getNonRequestedFromHere(int start, int total) {
553
        return dataDriver.getThisAndNonRequestedUpTo(start, total);
554
    }
555

    
556
    public String getIdColumnName(ArrayList col_Names, ArrayList col_Types) {
557
        int coltype;
558
        int idindex = -1;
559

    
560
        for (int i = 0; i < col_Types.size(); i++) {
561
            coltype = ((Integer) col_Types.get(i)).intValue();
562

    
563
            if (coltype == FieldInformation.ID) {
564
                idindex = i;
565

    
566
                break;
567
            }
568
        }
569

    
570
        if (idindex == -1) {
571
            logger.error("ID not found ");
572
        }
573

    
574
        return (String) col_Names.get(idindex);
575
    }
576

    
577
    public int getRowsPerRequest() {
578
        return rowsPerRequest;
579
    }
580

    
581
    private void setRowsPerRequest() {
582
        int rowcount = (int) dataDriver.getRowCount();
583
        int sqroot = Math.round((float) Math.sqrt(1.0 * rowcount));
584

    
585
        rowsPerRequest = sqroot;
586

    
587
        if (rowsPerRequest > GetFeaturesTags.MAX_ROWS_PER_REQUEST) {
588
            rowsPerRequest = GetFeaturesTags.MAX_ROWS_PER_REQUEST;
589
        }
590

    
591
        if (rowsPerRequest < GetFeaturesTags.MIN_ROWS_PER_REQUEST) {
592
            rowsPerRequest = GetFeaturesTags.MIN_ROWS_PER_REQUEST;
593
        }
594

    
595
        if (rowsPerRequest > rowcount) {
596
            rowsPerRequest = rowcount;
597
        }
598

    
599
        logger.info("Rows per request was set to " + rowsPerRequest +
600
            " (rowcount = " + rowcount + ")");
601
    }
602

    
603
    public String getOriginalFieldName(int idcolindex) {
604
        return dataDriver.getOriginalFieldName(idcolindex);
605
    }
606

    
607
    public boolean isVirtualField(int fieldId) throws DriverException {
608
        // TODO Auto-generated method stub
609
        return false;
610
    }
611

    
612
    public int getFieldWidth(int i) throws DriverException {
613
        // TODO Auto-generated method stub
614
        return 10;
615
    }
616

    
617
    private int[] getBlockLimits(int row) {
618
        int[] resp = new int[2];
619

    
620
        int rcount = 0;
621

    
622
        try {
623
            rcount = (int) getRowCount();
624
        }
625
        catch (DriverException e) {
626
            logger.error("While getting rowcount ", e);
627
        }
628

    
629
        int rperreq = getRowsPerRequest();
630

    
631
        int bloque = (row / rperreq);
632
        int inicio = rperreq * bloque;
633

    
634
        int fin = (inicio + rperreq) - 1;
635

    
636
        if (fin > (rcount - 1)) {
637
            fin = (rcount - 1);
638
        }
639

    
640
        resp[0] = inicio;
641
        resp[1] = fin;
642

    
643
        return resp;
644
    }
645

    
646
    public Driver getDriver() {
647
        // TODO Auto-generated method stub
648
        return null;
649
    }
650

    
651
    public void reload() throws DriverException, IOException {
652
        // TODO Auto-generated method stub
653
    }
654

    
655
    public void addDataSourceListener(IDataSourceListener arg0) {
656
        // TODO Auto-generated method stub
657
    }
658

    
659
    public void removeDataSourceListener(IDataSourceListener arg0) {
660
        // TODO Auto-generated method stub
661
    }
662
}