Statistics
| Revision:

svn-gvsig-desktop / tags / v1_11_0_Build_1306 / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / fmap / drivers / FMapFeatureArcImsDriver.java @ 35731

History | View | Annotate | Download (30.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 java.awt.geom.Rectangle2D;
46
import java.io.IOException;
47
import java.net.ProtocolException;
48
import java.net.URL;
49
import java.util.ArrayList;
50
import java.util.HashMap;
51
import java.util.TreeMap;
52

    
53
import org.apache.log4j.Logger;
54
import org.gvsig.remoteClient.arcims.ArcImsClientP;
55
import org.gvsig.remoteClient.arcims.ArcImsFeatureClient;
56
import org.gvsig.remoteClient.arcims.ArcImsStatus;
57
import org.gvsig.remoteClient.arcims.ArcImsVectStatus;
58
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
59
import org.gvsig.remoteClient.arcims.utils.FieldInformation;
60
import org.gvsig.remoteClient.arcims.utils.ServiceInfoTags;
61
import org.gvsig.remoteClient.arcims.utils.ServiceInformationLayerFeatures;
62
import org.gvsig.remoteClient.exceptions.ServerErrorException;
63
import org.gvsig.remoteClient.utils.BoundaryBox;
64
import org.gvsig.remoteClient.wms.ICancellable;
65

    
66
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
67
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
68
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
69
import com.hardcode.gdbms.engine.data.DataSourceFactory;
70
import com.hardcode.gdbms.engine.data.driver.DriverException;
71
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
72
import com.hardcode.gdbms.engine.data.edition.DataWare;
73
import com.hardcode.gdbms.engine.data.object.ObjectSourceInfo;
74
import com.hardcode.gdbms.engine.values.IntValue;
75
import com.hardcode.gdbms.engine.values.Value;
76
import com.iver.andami.PluginServices;
77
import com.iver.andami.ui.mdiManager.IWindow;
78
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
79
import com.iver.cit.gvsig.fmap.core.FShape;
80
import com.iver.cit.gvsig.fmap.core.IFeature;
81
import com.iver.cit.gvsig.fmap.core.IGeometry;
82
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
83
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
84
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
85
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
86
import com.iver.cit.gvsig.fmap.layers.FBitSet;
87
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
88
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
89
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
90
import com.iver.cit.gvsig.project.Project;
91
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
92
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
93
import com.iver.cit.gvsig.project.documents.view.ProjectView;
94
import com.iver.cit.gvsig.project.documents.view.gui.View;
95

    
96
import es.prodevelop.cit.gvsig.arcims.fmap.layers.FFeatureLyrArcIMS;
97

    
98

    
99
public class FMapFeatureArcImsDriver implements ArcImsDriver, VectorialDriver, ObjectDriver {
100
    private static Logger logger = Logger.getLogger(FMapFeatureArcImsDriver.class.getName());
101
    private ArcImsFeatureClient client;
102
    private String server;
103
    private String service;
104
    private String serviceType;
105
    private String remoteLayerName;
106
    private ArcImsAttributesDataSourceAdapter attsDataSourceAdapter;
107
    private SelectableDataSource dataSource = null;
108
    private DriverAttributes drvAtts;
109
    private ArrayList geometries;
110
    private HashMap pseudoGeometries;
111
    private int shapeType = FShape.NULL;
112
    private FFeatureLyrArcIMS layer;
113
    private HashMap overallIndexToVisibleIndex;
114
    private HashMap visibleIndexToOverallIndex;
115
    private Rectangle2D fullExtent;
116
    private IGeometry invisiblePoint = ShapeFactory.createPoint2D(0.0, 0.0);
117
    private VectorialEditableAdapter eadapter;
118
    
119
    private ArcImsInMemoryAttsTableDriver dataTable;
120
    private DataSourceFactory dsFactory;
121

    
122
    /**
123
    * The constructor needs the server's URL and the name of the service to be used.
124
    *
125
    * @param host server's URL
126
    * @param service name of the service, chosen among the ones retrieved after a
127
    * request with the parameter <tt>ServiceName=Catalog</tt>
128
    */
129
    public FMapFeatureArcImsDriver(String host, String service,
130
        String theRemoteLayerName) {
131
        remoteLayerName = theRemoteLayerName;
132
        init(host, service);
133
        overallIndexToVisibleIndex = new HashMap();
134
        visibleIndexToOverallIndex = new HashMap();
135
        pseudoGeometries = new HashMap();
136
        
137
    }
138

    
139
    /**
140
    * The constructor needs the server's URL and the name of the service to be used.
141
    *
142
    * @param host server's URL
143
    * @param service name of the service, chosen among the ones retrieved after a
144
    * request with the parameter <tt>ServiceName=Catalog</tt>
145
    */
146
    public FMapFeatureArcImsDriver(String host, String service) {
147
        remoteLayerName = "None";
148
        init(host, service);
149
        overallIndexToVisibleIndex = new HashMap();
150
        visibleIndexToOverallIndex = new HashMap();
151
    }
152

    
153
    /**
154
     * This method is called by the constructor and creates the
155
     * <tt>client</tt> object.
156
     * @param host
157
     * @param service
158
     */
159
    public void init(String host, String svc) {
160
        server = host;
161
        service = svc;
162

    
163
        // This class is part of libArcIMS:
164
        client = new ArcImsFeatureClient(host, service,
165
                ServiceInfoTags.vFEATURESERVICE);
166
    }
167

    
168
    private String getIdName() {
169
        String id = (String) this.layer.getArcimsStatus().getLayerIds().get(0);
170
        ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
171
                                                                                       .getLayerById(id);
172

    
173
        ArrayList fi = silf.getFieldInformationByType(FieldInformation.ID);
174

    
175
        if (fi.size() != 1) {
176
            logger.error("ID does not have cardinality one: " + fi.size());
177
        }
178

    
179
        return ((FieldInformation) fi.get(0)).getName();
180
    }
181

    
182
    private ArrayList getClientsColumnNames() {
183
        String id = (String) layer.getArcimsStatus().getLayerIds().get(0);
184
        ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
185
                                                                                       .getLayerById(id);
186
        int length = silf.getFieldsInfo().size();
187

    
188
        ArrayList resp = new ArrayList();
189

    
190
        for (int i = 0; i < length; i++) {
191
            resp.add(silf.getFieldInformation(i).getName());
192
        }
193

    
194
        return resp;
195
    }
196

    
197
    private ArrayList getClientsColumnTypes() {
198
        String id = (String) layer.getArcimsStatus().getLayerIds().get(0);
199
        ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
200
                                                                                       .getLayerById(id);
201
        int length = silf.getFieldsInfo().size();
202

    
203
        ArrayList resp = new ArrayList();
204

    
205
        for (int i = 0; i < length; i++) {
206
            resp.add(new Integer(silf.getFieldInformation(i).getType()));
207
        }
208

    
209
        return resp;
210
    }
211

    
212
    public void closeDataSource() {
213
        try {
214
            dataSource.stop();
215
        }
216
        catch (ReadDriverException e) {
217
            logger.error("While closing data source ", e);
218
        }
219
    }
220

    
221
    public SelectableDataSource getRecordSet() {
222
        return getRecordSet(null);
223
    }
224

    
225
    public SelectableDataSource getRecordSet(String mustHaveThisName) {
226
        if (dataSource != null) {
227
            return dataSource;
228
        }
229

    
230
        String[] subf = new String[1];
231
        subf[0] = getIdName();
232

    
233
        ArrayList ids = new ArrayList();
234

    
235
        try {
236
            // ids = getTestIds(); 
237
            ids = client.getAttributes(layer.getArcimsStatus(), subf, "", null);
238
        }
239
        catch (ArcImsException e1) {
240
            logger.error("While getting attributes initially ", e1);
241
        }
242

    
243
        Value[] idval = getAsValueArray(ids);
244

    
245
        setDataSourceFactory(LayerFactory.getDataSourceFactory());
246

    
247
        // dataSourceFactory.
248
        // FakeObjectDriver driver = new FakeObjectDriver();
249
        attsDataSourceAdapter = new ArcImsAttributesDataSourceAdapter(getClientsColumnNames(),
250
                getClientsColumnTypes(), idval, getDataSourceFactory(), this,
251
                mustHaveThisName);
252

    
253
        getDataSourceFactory().addDataSource(this, attsDataSourceAdapter.getTableName());
254

    
255
        ObjectSourceInfo osi = (ObjectSourceInfo) getDataSourceFactory().getDriverInfo(attsDataSourceAdapter.getTableName());
256
        osi.name = attsDataSourceAdapter.getTableName();
257
        osi.driverName = "ArcImsAttributesDataSourceAdapter";
258

    
259
        try {
260
            dataSource = new SelectableDataSource(attsDataSourceAdapter);
261
        }
262
        catch (ReadDriverException e) {
263
            logger.error("While creating DataSource ", e);
264
        }
265

    
266
        return dataSource;
267
    }
268

    
269
    private DataSourceFactory getDataSourceFactory() {
270
            if (dsFactory == null) {
271
                    return dataTable.getDataSourceFactory();
272
            }
273
            return dsFactory;
274
        }
275

    
276
        public void declareTable(AlphanumericData layer) {
277
        try {
278
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
279

    
280
            if (v == null) {
281
                ProjectView pv = new ProjectView();
282
                pv.setName("Nueva");
283

    
284
                View newview = (View) pv.createWindow();
285
                newview.setModel(pv);
286

    
287
                Project newproj = new Project();
288
                newproj.setName("nuevo");
289
                newproj.addDocument(pv);
290

    
291
                // newproj.addView(newprojview);
292
                v = newview;
293
            }
294

    
295
            ProjectView pv = (ProjectView) ((View) v).getModel();
296
            ProjectTable pt = ProjectTableFactory.createTable(attsDataSourceAdapter.getName(),
297
                    eadapter);
298
            pt.setProjectDocumentFactory(new ProjectTableFactory());
299
            pt.setAssociatedTable(layer);
300

    
301
            // String r = pt.getProjectDocumentFactory().getRegisterName();
302
            // ProjectTable pt = ProjectFactory.createTable(attsDataSourceAdapter.getName(), eadapter);
303
            // ProjectDocumentFactory.
304
            pv.getProject().addDocument(pt);
305
        }
306
        catch (Exception e) {
307
            logger.error("While creating DataSource ", e);
308
            e.printStackTrace();
309
        }
310
    }
311

    
312
    public void loadRecordSet() {
313
        if (dataSource != null) {
314
            return;
315
        }
316

    
317
        String[] subf = new String[1];
318
        subf[0] = getIdName();
319

    
320
        ArrayList ids = new ArrayList();
321

    
322
        try {
323
            // ids = getTestIds(); 
324
            ids = client.getAttributes(layer.getArcimsStatus(), subf, "", null);
325
        }
326
        catch (ArcImsException e1) {
327
            logger.error("While getting attributes initially ", e1);
328
        }
329

    
330
        Value[] idval = getAsValueArray(ids);
331

    
332
        setDataSourceFactory(LayerFactory.getDataSourceFactory());
333

    
334
        // dataSourceFactory.
335
        // FakeObjectDriver driver = new FakeObjectDriver();
336
        attsDataSourceAdapter = new ArcImsAttributesDataSourceAdapter(getClientsColumnNames(),
337
                getClientsColumnTypes(), idval, getDataSourceFactory(), this, null);
338
        getDataSourceFactory().addDataSource(this, attsDataSourceAdapter.getTableName());
339

    
340
        try {
341
            dataSource = new SelectableDataSource(attsDataSourceAdapter);
342
        }
343
        catch (ReadDriverException e) {
344
            logger.error("While loading selectable data source");
345
        }
346

    
347
    }
348

    
349
    private Value[] getAsValueArray(ArrayList ids) {
350
        Value[] resp = new Value[ids.size()];
351

    
352
        for (int i = 0; i < ids.size(); i++) {
353
            resp[i] = ((Value[]) ids.get(i))[0];
354
        }
355

    
356
        return resp;
357
    }
358

    
359
    public void getCapabilities(URL server, ICancellable cancel)
360
        throws ArcImsException, IOException, ProtocolException {
361
        logger.error(
362
            "Empty method: nothing done in method getCapabilities(URL) - class: " +
363
            getClass().toString());
364
    }
365

    
366
    public Object getMap(ArcImsStatus status)
367
        throws ArcImsException, IOException, ProtocolException {
368
        String[] fieldquery = ((ArcImsVectStatus) status).getSubfields();
369
        int nooffields = fieldquery.length;
370

    
371
        try {
372
            geometries = client.getMap((ArcImsVectStatus) status);
373

    
374
            if (geometries.size() > 0) {
375
                logger.info("Start updating attributes");
376
                updateIndexHashMaps(geometries, fieldquery, nooffields);
377
                logger.info("End updating attributes");
378
            }
379
            else {
380
                updateIndexHashMaps(geometries, fieldquery, nooffields);
381
                logger.warn("No geometries retrieved from client ");
382
            }
383
        }
384
        catch (ServerErrorException e) {
385
            ArcImsException aie = new ArcImsException(e.getMessage());
386
            logger.error("While getting map ", aie);
387
            throw aie;
388
        }
389

    
390
        return geometries;
391
    }
392

    
393
    // **************************
394
    // 
395
    // **************************
396
    private void updateIndexHashMaps(ArrayList iFeatArrayList,
397
        String[] f_query, int n_of_fields) {
398
        int added_row_ind;
399

    
400
        // ------------ get id position --------------
401
        int idfieldind = -1;
402

    
403
        for (int i = 0; i < n_of_fields; i++) {
404
            if (f_query[i].compareTo(getIdName()) == 0) {
405
                idfieldind = i;
406
                break;
407
            }
408
        }
409

    
410
        if (idfieldind == -1) {
411
            logger.error("ID not found in query ");
412
        }
413

    
414
        // -------------------------------------------
415
        IntValue id;
416

    
417
        try {
418
            overallIndexToVisibleIndex.clear();
419
            visibleIndexToOverallIndex.clear();
420

    
421
            for (int i = 0; i < iFeatArrayList.size(); i++) {
422
                IFeature ifeat = (IFeature) iFeatArrayList.get(i);
423
                id = (IntValue) ifeat.getAttribute(idfieldind);
424
                added_row_ind = attsDataSourceAdapter.getRowIndex(id.getValue());
425

    
426
                addPseudoGeometry(ifeat.getGeometry(), added_row_ind);
427
                overallIndexToVisibleIndex.put(new Integer(added_row_ind),
428
                    new Integer(i));
429
                visibleIndexToOverallIndex.put(new Integer(i),
430
                    new Integer(added_row_ind));
431
            }
432

    
433
            //                        }
434
        }
435
        catch (DriverException e) {
436
            logger.error("While updating attributes ", e);
437
        }
438
    }
439

    
440
    public String getFeatureInfo(ArcImsStatus status, int i, int j,
441
        int max_value) throws ArcImsException, IOException, ProtocolException {
442
        // TODO comment
443
        return "";
444
    }
445

    
446
    public boolean connect(ICancellable cancel) {
447
        return client.connect(false, cancel);
448
    }
449

    
450
    /**
451
    * Gets available layers from the current server and service
452
    *
453
    * @return a TreeMap with available layers
454
     */
455
    public TreeMap getLayers() {
456
        return null;
457
        // This method is in libArcIMS
458
        // return client.getLayers();
459
    }
460

    
461
    /**
462
     * Gets the <tt>ArcImsClientP client</tt> object, onto which requests
463
     * are passed.
464
     *
465
     * @return the inner object that actually performs requests.
466
     */
467
    public ArcImsClientP getClient() {
468
        if (client == null) {
469
            init(server, service);
470
        }
471

    
472
        return client;
473
    }
474

    
475
    // ========== VECTORIAL DRIVER INTERFACE == START =======
476
    public int getShapeType() {
477
        if (shapeType == FShape.NULL) {
478
            ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
479
                                                                                           .getLayerById(remoteLayerName);
480

    
481
            shapeType = libArcImsShapeTypeToGvSigType(silf.getIntFclassType());
482
        }
483

    
484
        return shapeType;
485
    }
486

    
487
    public int getShapeCount() throws ReadDriverException {
488
        try {
489
            // return geometries.size();
490
            return (int) getRecordSet().getRowCount();
491
        }
492
        catch (ReadDriverException e) {
493
                ReadDriverException ioe = new ReadDriverException("In getShapeCount: ", e);
494
            throw ioe;
495
        }
496
    }
497

    
498
    public DriverAttributes getDriverAttributes() {
499
        if (drvAtts != null) {
500
            return drvAtts;
501
        }
502

    
503
        drvAtts = new DriverAttributes();
504
        drvAtts.setLoadedInMemory(true);
505

    
506
        return drvAtts;
507
    }
508

    
509
    public Rectangle2D getFullExtent() throws ReadDriverException {
510
        return fullExtent;
511
    }
512

    
513
    //        public IGeometry getShape(int index) throws IOException {
514
    //                return ((IFeature) geometries.get(index)).getGeometry();
515
    //        }
516
    private void addPseudoGeometry(IGeometry igeom, int row) {
517
        pseudoGeometries.put(new Integer(row), igeom);
518
    }
519

    
520
    public IGeometry getShape(int index) throws ReadDriverException {
521
        Integer overallIndex = new Integer(index);
522
        Integer visibleIndex = (Integer) overallIndexToVisibleIndex.get(overallIndex);
523

    
524
        if (visibleIndex == null) {
525
            // Somebody asked for a non-visible shape.
526
            IGeometry igeo = (IGeometry) pseudoGeometries.get(overallIndex);
527

    
528
            if (igeo != null) {
529
                return igeo;
530
            }
531

    
532
            // We dont have the geometry and we dont have its pseudo envelope
533
            // (this happens when loading a project with a selection and the user
534
            // performs a zoom to selection)
535
            String id = layer.getLayerQuery();
536
            BoundaryBox bb = client.getServiceInformation().getLayerById(id)
537
                                   .getEnvelope();
538

    
539
            double[] fullx = new double[4];
540
            double[] fully = new double[4];
541

    
542
            fullx[0] = bb.getXmin();
543
            fullx[1] = bb.getXmin();
544
            fullx[2] = bb.getXmax();
545
            fullx[3] = bb.getXmax();
546
            fully[0] = bb.getYmin();
547
            fully[1] = bb.getYmax();
548
            fully[2] = bb.getYmin();
549
            fully[3] = bb.getYmax();
550

    
551
            IGeometry full = ShapeFactory.createMultipoint2D(fullx, fully);
552

    
553
            return full;
554
        }
555
        else {
556
            return ((IFeature) geometries.get(visibleIndex.intValue())).getGeometry();
557
        }
558
    }
559

    
560
    public String getName() {
561
        return "FMapFeatureArcImsDriver";
562
    }
563

    
564
    // ========== VECTORIAL DRIVER INTERFACE == END =======
565

    
566
    //        private void setTestGeometries() {
567
    //                IGeometry g;
568
    //                File wktfile = null;
569
    //                JFileChooser jfc = new JFileChooser();
570
    //                try {
571
    //                        jfc.setDialogTitle("Indice archivo WKT con " + getShapeCount() + " geometrias");
572
    //                } catch (IOException e) {
573
    //                        logger.error("Error while opening WKT file. ", e);
574
    //                }
575
    //            int returnVal = jfc.showOpenDialog(null);
576
    //            if (returnVal != JFileChooser.APPROVE_OPTION) return;
577
    //            wktfile = jfc.getSelectedFile();
578
    //                if (! jfc.accept(wktfile)) return;
579
    //                WKTFGeometryReader reader = new WKTFGeometryReader(wktfile);
580
    //                g = reader.getNextFGeometry();
581
    //                geometries = new ArrayList();
582
    //                while (g != null) {
583
    //                        geometries.add(g);
584
    //                        g = reader.getNextFGeometry();;
585
    //                }
586
    //        }
587
    public void setFullExtent(Rectangle2D fullExtent) {
588
        this.fullExtent = fullExtent;
589
    }
590

    
591
    public FFeatureLyrArcIMS getLayer() {
592
        return layer;
593
    }
594

    
595
    public void setLayer(FFeatureLyrArcIMS layer) {
596
        this.layer = layer;
597
    }
598

    
599
    //        private ArrayList getTestIds() throws ArcImsException {
600
    //                Value[] ids = new Value[252];
601
    //                
602
    //                for (int i=0; i<252; i++) {
603
    //                        ids[i] = ValueFactory.createValue(i);
604
    //                }
605
    //                ArrayList resp = new ArrayList();
606
    //                resp.add(ids);
607
    //                return resp;
608
    //        }
609
    public int getOverallIndex(int i) {
610
        Integer ov = (Integer) visibleIndexToOverallIndex.get(new Integer(i));
611

    
612
        if (ov == null) {
613
                return -1;
614
        } else {
615
                return ov.intValue();
616
        }
617
        
618
    }
619

    
620
    public void requestFeatureAttributes(FBitSet fbs) throws ArcImsException {
621
        if (fbs.cardinality() == 0) {
622
            return;
623
        }
624

    
625
        FBitSet needRequest = leaveBitsThatNeedRequest(fbs);
626

    
627
        requestFeatureAttributesWithoutChecking(needRequest);
628
    }
629

    
630
    public void requestBlockWithoutChecking(int[] indlimits)
631
        throws ArcImsException {
632
        FBitSet fbs = new FBitSet();
633
        fbs.set(indlimits[0], indlimits[1] + 1);
634

    
635
        int length = fbs.cardinality();
636

    
637
        if (length == 0) {
638
            return;
639
        }
640

    
641
        int[] rowinds = new int[length];
642

    
643
        try {
644
                int[] req_indices = enumarate(indlimits[0], indlimits[1]);
645
                int sz = req_indices.length;
646
                int[] req_ids = new int[sz];
647
                for (int i=0; i<sz; i++)
648
                        req_ids[i] = attsDataSourceAdapter.getRowId(req_indices[i]);
649

    
650
            int idcolindex = attsDataSourceAdapter.getIdIndex();
651
            String idFieldName = attsDataSourceAdapter.getOriginalFieldName(idcolindex);
652
            String inParenthesis = getInIntParenthesis(req_ids);
653
            String whereClause = idFieldName + " " + inParenthesis;
654

    
655
            String[] subflds = new String[1];
656
            subflds[0] = "#ALL#";
657

    
658
            logger.debug("Justo antes de llamar a client.getAttributes(...)");
659

    
660
            ArrayList atts = client.getAttributesWithEnvelope(layer.getArcimsStatus(),
661
                    subflds, whereClause, null);
662
            
663
            rowinds = getRowIndicesFromResponse(atts, idcolindex);
664

    
665
            // --------------------------------------------------------
666
            logger.debug("Justo despues de llamar a client.getAttributes(...)");
667

    
668
            logger.debug(
669
                "Justo antes de llamar a attsDataSourceAdapter.updateRow(...) " +
670
                atts.size() + " veces");
671

    
672
            for (int i = (atts.size() - 1); i >= 0; i--) {
673
                Value[] newrow = ((DefaultFeature) atts.get(i)).getAttributes();
674

    
675
                attsDataSourceAdapter.updateRow(newrow, subflds, rowinds[i]);
676
            }
677

    
678
            // -------------- pseudo geometries:
679
            for (int i = (atts.size() - 1); i >= 0; i--) {
680
                IGeometry ig = ((DefaultFeature) atts.get(i)).getGeometry();
681
                addPseudoGeometry(ig, rowinds[i]);
682
            }
683

    
684
            // -------------- 
685
            logger.debug(
686
                "Justo despues de llamar a attsDataSourceAdapter.updateRow(...)");
687
            logger.debug(
688
                "Justo antes de llamar a attsDataSourceAdapter.addAsRequested(...)");
689

    
690
            attsDataSourceAdapter.addAsRequested(fbs);
691

    
692
            logger.debug(
693
                "Justo despues de llamar a attsDataSourceAdapter.addAsRequested(...)");
694
        }
695
        catch (DriverException e) {
696
            ArcImsException aie = new ArcImsException("datasource_error");
697
            logger.error("While requesting features ", aie);
698
            throw aie;
699
        }
700
    }
701

    
702
    private int[] enumarate(int a, int b) {
703

    
704
            int[] resp = null;
705
            if (a == b) {
706
                    resp = new int[1];
707
                    resp[0] = a;
708
            } else {
709
                    
710
                    resp = new int[Math.abs(b-a+1)];
711
                    if (a < b) {
712
                        for (int i=a; i<=b; i++) resp[i-a] = i;
713
                    } else {
714
                        for (int i=b; i<=a; i++) resp[i-b] = i;
715
                    }
716
            }
717
            return resp;
718
            
719
        }
720

    
721
        private int[] getRowIndicesFromResponse(ArrayList resp_list, int id_col_ind) {
722
            
723
            int sz = resp_list.size();
724
            int[] resp = new int[sz];
725
            int row_ind = 0;
726
            for (int i=0; i<sz; i++) {
727
                    Value[] item_row = ((DefaultFeature) resp_list.get(i)).getAttributes();
728
                    IntValue idv = (IntValue) item_row[id_col_ind];
729
                    row_ind = idToRow(idv.intValue());
730
                    resp[i] = row_ind;
731
            }
732
                return resp;
733
        }
734

    
735
        private int idToRow(int i) {
736
                try {
737
                        return attsDataSourceAdapter.getRowIndex(i);
738
                } catch (DriverException e) {
739
                        logger.error("While getting row index of id: " + i + " : " + e.getMessage());
740
                        return 0;
741
                }
742
        }
743

    
744
        public void requestFeatureAttributesWithoutChecking(FBitSet fbs)
745
        throws ArcImsException {
746
        int length = fbs.cardinality();
747

    
748
        if (length == 0) {
749
            return;
750
        }
751

    
752
        int rowcount = 0;
753
        int firstnonreq = 0;
754

    
755
        try {
756
            rowcount = (int) attsDataSourceAdapter.getRowCount();
757
        }
758
        catch (ReadDriverException e1) {
759
            logger.error("Unexpected error while getting row count ");
760

    
761
            return;
762
        }
763

    
764
        firstnonreq = fbs.nextClearBit(fbs.nextSetBit(0));
765

    
766
        if (firstnonreq >= rowcount) {
767
            firstnonreq = 0;
768
        }
769

    
770
        if (length < 10) {
771
            fbs.or(attsDataSourceAdapter.getNonRequestedFromHere(firstnonreq,
772
                    attsDataSourceAdapter.getRowsPerRequest() - length));
773
            length = fbs.cardinality();
774
        }
775

    
776
        int[] ids = new int[length];
777
        int[] rowinds = new int[length];
778
        int idind = 0;
779

    
780
        try {
781
            for (int i = fbs.nextSetBit(0); i >= 0;
782
                    i = fbs.nextSetBit(i + 1)) {
783
                ids[idind] = attsDataSourceAdapter.getRowId(i);
784
                rowinds[idind] = i;
785
                idind++;
786
            }
787

    
788
            String inParenthesis = getInIntParenthesis(ids);
789
            int idcolindex = attsDataSourceAdapter.getIdIndex();
790
            String idFieldName = attsDataSourceAdapter.getOriginalFieldName(idcolindex);
791
            String whereClause = idFieldName + " " + inParenthesis;
792
            String[] subflds = new String[1];
793
            subflds[0] = "#ALL#";
794

    
795
            // -------------- with geometries -------------------------
796
            ArrayList atts = client.getAttributesWithEnvelope(layer.getArcimsStatus(),
797
                    subflds, whereClause, null);
798

    
799
            //                        ArrayList atts = client.getAttributes(layer.getArcimsStatus(),
800
            //                                        subflds, whereClause, null);
801
            // --------------------------------------------------------
802
            for (int i = (atts.size() - 1); i >= 0; i--) {
803
                IGeometry ig = ((DefaultFeature) atts.get(i)).getGeometry();
804
                addPseudoGeometry(ig, rowinds[i]);
805
            }
806

    
807
            // -------------- 
808
            for (int i = (atts.size() - 1); i >= 0; i--) {
809
                Value[] newrow = ((DefaultFeature) atts.get(i)).getAttributes();
810

    
811
                // Value[] newrow = (Value[]) atts.get(i);
812
                attsDataSourceAdapter.updateRow(newrow, subflds, rowinds[i]);
813
            }
814

    
815
            attsDataSourceAdapter.addAsRequested(fbs);
816
        }
817
        catch (DriverException e) {
818
            ArcImsException aie = new ArcImsException("datasource_error");
819
            logger.error("While requesting features ", aie);
820
            throw aie;
821
        }
822
    }
823

    
824
    private FBitSet leaveBitsThatNeedRequest(FBitSet fbs) {
825
        FBitSet resp = (FBitSet) fbs.clone();
826

    
827
        for (int i = resp.nextSetBit(0); i >= 0; i = resp.nextSetBit(i + 1)) {
828
            if (!attsDataSourceAdapter.isNonRequestedRow(i)) {
829
                resp.clear(i);
830
            }
831
        }
832

    
833
        return resp;
834
    }
835

    
836
    private String getInIntParenthesis(int[] ids) {
837
        String resp = "IN (";
838

    
839
        for (int i = 0; i < ids.length; i++) {
840
            resp = resp + " " + ids[i] + ",";
841
        }
842

    
843
        resp = resp.substring(0, resp.length() - 1);
844
        resp = resp + ")";
845

    
846
        return resp;
847
    }
848

    
849
    private int libArcImsShapeTypeToGvSigType(int type) {
850
        int resp = type;
851

    
852
        switch (type) {
853
        case FShape.MULTIPOINT:
854
            resp = FShape.POINT;
855

    
856
            break;
857
        }
858

    
859
        return resp;
860
    }
861

    
862
    public void setAdapter(VectorialEditableAdapter adapter) {
863
        eadapter = adapter;
864
    }
865

    
866
    public VectorialEditableAdapter getAdapter() {
867
        return eadapter;
868
    }
869

    
870
    public void reload() throws ReloadDriverException {
871
    }
872

    
873
    public boolean isWritable() {
874
        return false;
875
    }
876

    
877
    public String getRemoteLayerName() {
878
        return remoteLayerName;
879
    }
880

    
881
    public void setRemoteLayerName(String remoteLayerName) {
882
        this.remoteLayerName = remoteLayerName;
883
    }
884

    
885
        public int[] getPrimaryKeys() throws ReadDriverException {
886
                // TODO Auto-generated method stub
887
                return dataTable.getPrimaryKeys();
888
        }
889

    
890
        public void write(DataWare dataWare) throws WriteDriverException,
891
                        ReadDriverException {
892
                // TODO Auto-generated method stub
893
                
894
        }
895

    
896
        public void setDataSourceFactory(DataSourceFactory dsf) {
897
                dsFactory = dsf;
898
                this.getDataSourceFactory();
899
                if (dataTable != null) {
900
                        dataTable.setDataSourceFactory(dsf);
901
                }
902
        }
903

    
904
        public int getFieldCount() throws ReadDriverException {
905
                // TODO Auto-generated method stub
906
                return dataTable.getFieldCount();
907
        }
908

    
909
        public String getFieldName(int i) throws ReadDriverException {
910
                // TODO Auto-generated method stub
911
                return dataTable.getFieldName(i);
912
        }
913

    
914
        public int getFieldType(int i) throws ReadDriverException {
915
                // TODO Auto-generated method stub
916
                return dataTable.getFieldType(i);
917
        }
918

    
919
        public Value getFieldValue(long rowIndex, int fieldId)
920
                        throws ReadDriverException {
921
                // TODO Auto-generated method stub
922
                return dataTable.getFieldValue(rowIndex, fieldId);
923
        }
924

    
925
        public int getFieldWidth(int i) throws ReadDriverException {
926
                return dataTable.getFieldWidth(i);
927
        }
928

    
929
        public long getRowCount() throws ReadDriverException {
930
                // TODO Auto-generated method stub
931
                return dataTable.getRowCount();
932
        }
933

    
934
        public void setDataTable(ArcImsInMemoryAttsTableDriver dt) {
935
                dataTable = dt;
936
        }
937
        
938
        public ArcImsInMemoryAttsTableDriver getDataTable() {
939
                return dataTable;
940
        }
941
}