Statistics
| Revision:

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

History | View | Annotate | Download (28.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package es.prodevelop.cit.gvsig.arcims.fmap.drivers;
44

    
45
import 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.object.ObjectSourceInfo;
51
import com.hardcode.gdbms.engine.instruction.EvaluationException;
52
import com.hardcode.gdbms.engine.instruction.SemanticException;
53
import com.hardcode.gdbms.engine.values.IntValue;
54
import com.hardcode.gdbms.engine.values.Value;
55
import com.hardcode.gdbms.engine.values.ValueFactory;
56
import com.hardcode.gdbms.parser.ParseException;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.ui.mdiManager.IWindow;
60

    
61
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.IFeature;
64
import com.iver.cit.gvsig.fmap.core.IGeometry;
65
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
66
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
67
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
68
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
69
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
70
import com.iver.cit.gvsig.fmap.layers.FBitSet;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
73
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
74
import com.iver.cit.gvsig.project.Project;
75
import com.iver.cit.gvsig.project.ProjectFactory;
76
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
77
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
78
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
79
import com.iver.cit.gvsig.project.documents.view.ProjectView;
80
import com.iver.cit.gvsig.project.documents.view.gui.View;
81

    
82
import es.prodevelop.cit.gvsig.arcims.fmap.drivers.ArcImsAttributesDataSourceAdapter;
83
import es.prodevelop.cit.gvsig.arcims.fmap.layers.FFeatureLyrArcIMS;
84
import es.prodevelop.cit.gvsig.arcims.gui.wizards.ArcImsWizard;
85

    
86
import org.apache.log4j.Logger;
87

    
88
import org.gvsig.remoteClient.arcims.ArcImsClientP;
89
import org.gvsig.remoteClient.arcims.ArcImsFeatureClient;
90
import org.gvsig.remoteClient.arcims.ArcImsStatus;
91
import org.gvsig.remoteClient.arcims.ArcImsVectStatus;
92
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
93
import org.gvsig.remoteClient.arcims.utils.FieldInformation;
94
import org.gvsig.remoteClient.arcims.utils.ServiceInfoTags;
95
import org.gvsig.remoteClient.arcims.utils.ServiceInformationLayerFeatures;
96
import org.gvsig.remoteClient.exceptions.ServerErrorException;
97
import org.gvsig.remoteClient.utils.BoundaryBox;
98
import org.gvsig.remoteClient.wms.ICancellable;
99

    
100
import java.awt.Rectangle;
101
import java.awt.geom.Rectangle2D;
102
import java.awt.geom.Rectangle2D.Double;
103

    
104
import java.io.File;
105
import java.io.IOException;
106

    
107
import java.net.ProtocolException;
108
import java.net.URL;
109

    
110
import java.util.ArrayList;
111
import java.util.HashMap;
112
import java.util.Random;
113
import java.util.TreeMap;
114

    
115
import javax.swing.JFileChooser;
116

    
117

    
118
public class FMapFeatureArcImsDriver implements ArcImsDriver, VectorialDriver {
119
    private static Logger logger = Logger.getLogger(FMapFeatureArcImsDriver.class.getName());
120
    private ArcImsFeatureClient client;
121
    private String server;
122
    private String service;
123
    private String serviceType;
124
    private String remoteLayerName;
125
    private ArcImsAttributesDataSourceAdapter attsDataSourceAdapter;
126
    private SelectableDataSource dataSource = null;
127
    private DriverAttributes drvAtts;
128
    private ArrayList geometries;
129
    private HashMap pseudoGeometries;
130
    private int shapeType = FShape.NULL;
131
    private FFeatureLyrArcIMS layer;
132
    private HashMap overallIndexToVisibleIndex;
133
    private HashMap visibleIndexToOverallIndex;
134
    private DataSourceFactory dataSourceFactory = null;
135
    private Rectangle2D fullExtent;
136
    private IGeometry invisiblePoint = ShapeFactory.createPoint2D(0.0, 0.0);
137
    private VectorialEditableAdapter eadapter;
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
        String theRemoteLayerName) {
148
        remoteLayerName = theRemoteLayerName;
149
        init(host, service);
150
        overallIndexToVisibleIndex = new HashMap();
151
        visibleIndexToOverallIndex = new HashMap();
152
        pseudoGeometries = new HashMap();
153
    }
154

    
155
    /**
156
    * The constructor needs the server's URL and the name of the service to be used.
157
    *
158
    * @param host server's URL
159
    * @param service name of the service, chosen among the ones retrieved after a
160
    * request with the parameter <tt>ServiceName=Catalog</tt>
161
    */
162
    public FMapFeatureArcImsDriver(String host, String service) {
163
        remoteLayerName = "None";
164
        init(host, service);
165
        overallIndexToVisibleIndex = new HashMap();
166
        visibleIndexToOverallIndex = new HashMap();
167
    }
168

    
169
    /**
170
     * This method is called by the constructor and creates the
171
     * <tt>client</tt> object.
172
     * @param host
173
     * @param service
174
     */
175
    public void init(String host, String svc) {
176
        server = host;
177
        service = svc;
178

    
179
        // This class is part of libArcIMS:
180
        client = new ArcImsFeatureClient(host, service,
181
                ServiceInfoTags.vFEATURESERVICE);
182
    }
183

    
184
    private String getIdName() {
185
        String id = (String) this.layer.getArcimsStatus().getLayerIds().get(0);
186
        ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
187
                                                                                       .getLayerById(id);
188

    
189
        ArrayList fi = silf.getFieldInformationByType(FieldInformation.ID);
190

    
191
        if (fi.size() != 1) {
192
            logger.error("ID does not have cardinality one: " + fi.size());
193
        }
194

    
195
        return ((FieldInformation) fi.get(0)).getName();
196
    }
197

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

    
204
        ArrayList resp = new ArrayList();
205

    
206
        for (int i = 0; i < length; i++) {
207
            resp.add(silf.getFieldInformation(i).getName());
208
        }
209

    
210
        return resp;
211
    }
212

    
213
    private ArrayList getClientsColumnTypes() {
214
        String id = (String) layer.getArcimsStatus().getLayerIds().get(0);
215
        ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
216
                                                                                       .getLayerById(id);
217
        int length = silf.getFieldsInfo().size();
218

    
219
        ArrayList resp = new ArrayList();
220

    
221
        for (int i = 0; i < length; i++) {
222
            resp.add(new Integer(silf.getFieldInformation(i).getType()));
223
        }
224

    
225
        return resp;
226
    }
227

    
228
    public void closeDataSource() {
229
        try {
230
            dataSource.stop();
231
        }
232
        catch (DriverException e) {
233
            logger.error("While closing data source ", e);
234
        }
235
    }
236

    
237
    public SelectableDataSource getRecordSet() {
238
        return getRecordSet(null);
239
    }
240

    
241
    public SelectableDataSource getRecordSet(String mustHaveThisName) {
242
        if (dataSource != null) {
243
            return dataSource;
244
        }
245

    
246
        String[] subf = new String[1];
247
        subf[0] = getIdName();
248

    
249
        ArrayList ids = new ArrayList();
250

    
251
        try {
252
            // ids = getTestIds(); 
253
            ids = client.getAttributes(layer.getArcimsStatus(), subf, "", null);
254
        }
255
        catch (ArcImsException e1) {
256
            logger.error("While getting attributes initially ", e1);
257
        }
258

    
259
        Value[] idval = getAsValueArray(ids);
260

    
261
        dataSourceFactory = LayerFactory.getDataSourceFactory();
262

    
263
        // dataSourceFactory.
264
        // FakeObjectDriver driver = new FakeObjectDriver();
265
        attsDataSourceAdapter = new ArcImsAttributesDataSourceAdapter(getClientsColumnNames(),
266
                getClientsColumnTypes(), idval, dataSourceFactory, this,
267
                mustHaveThisName);
268

    
269
        dataSourceFactory.addDataSource(attsDataSourceAdapter,
270
            attsDataSourceAdapter.getTableName());
271

    
272
        ObjectSourceInfo osi = (ObjectSourceInfo) dataSourceFactory.getDriverInfo(attsDataSourceAdapter.getTableName());
273
        osi.name = attsDataSourceAdapter.getTableName();
274
        osi.driverName = "ArcImsAttributesDataSourceAdapter";
275

    
276
        try {
277
            dataSource = new SelectableDataSource(attsDataSourceAdapter);
278
        }
279
        catch (DriverException e) {
280
            logger.error("While creating DataSource ", e);
281
        }
282

    
283
        return dataSource;
284
    }
285

    
286
    public void declareTable() {
287
        try {
288
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
289

    
290
            if (v == null) {
291
                ProjectView pv = new ProjectView();
292
                pv.setName("Nueva");
293

    
294
                View newview = (View) pv.createWindow();
295
                newview.setModel(pv);
296

    
297
                Project newproj = new Project();
298
                newproj.setName("nuevo");
299
                newproj.addDocument(pv);
300

    
301
                // newproj.addView(newprojview);
302
                v = newview;
303
            }
304

    
305
            ProjectView pv = (ProjectView) ((View) v).getModel();
306
            ProjectTable pt = ProjectTableFactory.createTable(attsDataSourceAdapter.getName(),
307
                    eadapter);
308
            pt.setProjectDocumentFactory(new ProjectTableFactory());
309

    
310
            // String r = pt.getProjectDocumentFactory().getRegisterName();
311
            // ProjectTable pt = ProjectFactory.createTable(attsDataSourceAdapter.getName(), eadapter);
312
            // ProjectDocumentFactory.
313
            pv.getProject().addDocument(pt);
314
        }
315
        catch (Exception e) {
316
            logger.error("While creating DataSource ", e);
317
            e.printStackTrace();
318
        }
319
    }
320

    
321
    public void loadRecordSet() {
322
        if (dataSource != null) {
323
            return;
324
        }
325

    
326
        String[] subf = new String[1];
327
        subf[0] = getIdName();
328

    
329
        ArrayList ids = new ArrayList();
330

    
331
        try {
332
            // ids = getTestIds(); 
333
            ids = client.getAttributes(layer.getArcimsStatus(), subf, "", null);
334
        }
335
        catch (ArcImsException e1) {
336
            logger.error("While getting attributes initially ", e1);
337
        }
338

    
339
        Value[] idval = getAsValueArray(ids);
340

    
341
        dataSourceFactory = LayerFactory.getDataSourceFactory();
342

    
343
        // dataSourceFactory.
344
        // FakeObjectDriver driver = new FakeObjectDriver();
345
        attsDataSourceAdapter = new ArcImsAttributesDataSourceAdapter(getClientsColumnNames(),
346
                getClientsColumnTypes(), idval, dataSourceFactory, this, null);
347
        dataSourceFactory.addDataSource(attsDataSourceAdapter,
348
            attsDataSourceAdapter.getTableName());
349

    
350
        try {
351
            dataSource = new SelectableDataSource(attsDataSourceAdapter);
352
        }
353
        catch (DriverException e) {
354
            logger.error("While loading selectable data source");
355
        }
356

    
357
    }
358

    
359
    private Value[] getAsValueArray(ArrayList ids) {
360
        Value[] resp = new Value[ids.size()];
361

    
362
        for (int i = 0; i < ids.size(); i++) {
363
            resp[i] = ((Value[]) ids.get(i))[0];
364
        }
365

    
366
        return resp;
367
    }
368

    
369
    public void getCapabilities(URL server, ICancellable cancel)
370
        throws ArcImsException, IOException, ProtocolException {
371
        logger.error(
372
            "Empty method: nothing done in method getCapabilities(URL) - class: " +
373
            getClass().toString());
374
    }
375

    
376
    public Object getMap(ArcImsStatus status)
377
        throws ArcImsException, IOException, ProtocolException {
378
        String[] fieldquery = ((ArcImsVectStatus) status).getSubfields();
379
        int nooffields = fieldquery.length;
380

    
381
        try {
382
            geometries = client.getMap((ArcImsVectStatus) status);
383

    
384
            if (geometries.size() > 0) {
385
                logger.info("Start updating attributes");
386
                updateIndexHashMaps(geometries, fieldquery, nooffields);
387
                logger.info("End updating attributes");
388
            }
389
            else {
390
                updateIndexHashMaps(geometries, fieldquery, nooffields);
391
                logger.warn("No geometries retrieved from client ");
392
            }
393
        }
394
        catch (ServerErrorException e) {
395
            ArcImsException aie = new ArcImsException(e.getMessage());
396
            logger.error("While getting map ", aie);
397
            throw aie;
398
        }
399

    
400
        return geometries;
401
    }
402

    
403
    // **************************
404
    // 
405
    // **************************
406
    private void updateIndexHashMaps(ArrayList iFeatArrayList,
407
        String[] f_query, int n_of_fields) {
408
        int added_row_ind;
409

    
410
        // ------------ get id position --------------
411
        int idfieldind = -1;
412

    
413
        for (int i = 0; i < n_of_fields; i++) {
414
            if (f_query[i].compareTo(getIdName()) == 0) {
415
                idfieldind = i;
416
            }
417
        }
418

    
419
        if (idfieldind == -1) {
420
            logger.error("ID not found in query ");
421
        }
422

    
423
        // -------------------------------------------
424
        IntValue id;
425

    
426
        try {
427
            overallIndexToVisibleIndex.clear();
428
            visibleIndexToOverallIndex.clear();
429

    
430
            for (int i = 0; i < iFeatArrayList.size(); i++) {
431
                IFeature ifeat = (IFeature) iFeatArrayList.get(i);
432
                id = (IntValue) ifeat.getAttribute(idfieldind);
433
                added_row_ind = attsDataSourceAdapter.getRowIndex(id.getValue());
434

    
435
                addPseudoGeometry(ifeat.getGeometry(), added_row_ind);
436
                overallIndexToVisibleIndex.put(new Integer(added_row_ind),
437
                    new Integer(i));
438
                visibleIndexToOverallIndex.put(new Integer(i),
439
                    new Integer(added_row_ind));
440
            }
441

    
442
            //                        }
443
        }
444
        catch (DriverException e) {
445
            logger.error("While updating attributes ", e);
446
        }
447
    }
448

    
449
    public String getFeatureInfo(ArcImsStatus status, int i, int j,
450
        int max_value) throws ArcImsException, IOException, ProtocolException {
451
        // TODO comment
452
        return "";
453
    }
454

    
455
    public boolean connect(ICancellable cancel) {
456
        return client.connect(false, cancel);
457
    }
458

    
459
    /**
460
    * Gets available layers from the current server and service
461
    *
462
    * @return a TreeMap with available layers
463
     */
464
    public TreeMap getLayers() {
465
        return null;
466
        // This method is in libArcIMS
467
        // return client.getLayers();
468
    }
469

    
470
    /**
471
     * Gets the <tt>ArcImsClientP client</tt> object, onto which requests
472
     * are passed.
473
     *
474
     * @return the inner object that actually performs requests.
475
     */
476
    public ArcImsClientP getClient() {
477
        if (client == null) {
478
            init(server, service);
479
        }
480

    
481
        return client;
482
    }
483

    
484
    // ========== VECTORIAL DRIVER INTERFACE == START =======
485
    public int getShapeType() {
486
        if (shapeType == FShape.NULL) {
487
            ServiceInformationLayerFeatures silf = (ServiceInformationLayerFeatures) client.getServiceInformation()
488
                                                                                           .getLayerById(remoteLayerName);
489

    
490
            shapeType = libArcImsShapeTypeToGvSigType(silf.getIntFclassType());
491
        }
492

    
493
        return shapeType;
494
    }
495

    
496
    public int getShapeCount() throws IOException {
497
        try {
498
            // return geometries.size();
499
            return (int) getRecordSet().getRowCount();
500
        }
501
        catch (DriverException e) {
502
            IOException ioe = new IOException(e.getMessage());
503
            throw ioe;
504
        }
505
    }
506

    
507
    public DriverAttributes getDriverAttributes() {
508
        if (drvAtts != null) {
509
            return drvAtts;
510
        }
511

    
512
        drvAtts = new DriverAttributes();
513
        drvAtts.setLoadedInMemory(true);
514

    
515
        return drvAtts;
516
    }
517

    
518
    public Rectangle2D getFullExtent() throws IOException {
519
        return fullExtent;
520
    }
521

    
522
    //        public IGeometry getShape(int index) throws IOException {
523
    //                return ((IFeature) geometries.get(index)).getGeometry();
524
    //        }
525
    private void addPseudoGeometry(IGeometry igeom, int row) {
526
        pseudoGeometries.put(new Integer(row), igeom);
527
    }
528

    
529
    public IGeometry getShape(int index) throws IOException {
530
        Integer overallIndex = new Integer(index);
531
        Integer visibleIndex = (Integer) overallIndexToVisibleIndex.get(overallIndex);
532

    
533
        if (visibleIndex == null) {
534
            // Somebody asked for a non-visible shape.
535
            IGeometry igeo = (IGeometry) pseudoGeometries.get(overallIndex);
536

    
537
            if (igeo != null) {
538
                return igeo;
539
            }
540

    
541
            // We dont have the geometry and we dont have its pseudo envelope
542
            // (this happens when loading a project with a selection and the user
543
            // performs a zoom to selection)
544
            String id = layer.getLayerQuery();
545
            BoundaryBox bb = client.getServiceInformation().getLayerById(id)
546
                                   .getEnvelope();
547

    
548
            double[] fullx = new double[4];
549
            double[] fully = new double[4];
550

    
551
            fullx[0] = bb.getXmin();
552
            fullx[1] = bb.getXmin();
553
            fullx[2] = bb.getXmax();
554
            fullx[3] = bb.getXmax();
555
            fully[0] = bb.getYmin();
556
            fully[1] = bb.getYmax();
557
            fully[2] = bb.getYmin();
558
            fully[3] = bb.getYmax();
559

    
560
            IGeometry full = ShapeFactory.createMultipoint2D(fullx, fully);
561

    
562
            return full;
563
        }
564
        else {
565
            return ((IFeature) geometries.get(visibleIndex.intValue())).getGeometry();
566
        }
567
    }
568

    
569
    public String getName() {
570
        return "FMapFeatureArcImsDriver";
571
    }
572

    
573
    // ========== VECTORIAL DRIVER INTERFACE == END =======
574

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

    
600
    public FFeatureLyrArcIMS getLayer() {
601
        return layer;
602
    }
603

    
604
    public void setLayer(FFeatureLyrArcIMS layer) {
605
        this.layer = layer;
606
    }
607

    
608
    //        private ArrayList getTestIds() throws ArcImsException {
609
    //                Value[] ids = new Value[252];
610
    //                
611
    //                for (int i=0; i<252; i++) {
612
    //                        ids[i] = ValueFactory.createValue(i);
613
    //                }
614
    //                ArrayList resp = new ArrayList();
615
    //                resp.add(ids);
616
    //                return resp;
617
    //        }
618
    public int getOverallIndex(int i) {
619
        Integer ov = (Integer) visibleIndexToOverallIndex.get(new Integer(i));
620

    
621
        return ov.intValue();
622
    }
623

    
624
    public void requestFeatureAttributes(FBitSet fbs) throws ArcImsException {
625
        if (fbs.cardinality() == 0) {
626
            return;
627
        }
628

    
629
        FBitSet needRequest = leaveBitsThatNeedRequest(fbs);
630

    
631
        requestFeatureAttributesWithoutChecking(needRequest);
632
    }
633

    
634
    public void requestBlockWithoutChecking(int[] indlimits)
635
        throws ArcImsException {
636
        FBitSet fbs = new FBitSet();
637
        fbs.set(indlimits[0], indlimits[1] + 1);
638

    
639
        int length = fbs.cardinality();
640

    
641
        if (length == 0) {
642
            return;
643
        }
644

    
645
        int[] rowinds = new int[length];
646

    
647
        try {
648
                int[] req_indices = enumarate(indlimits[0], indlimits[1]);
649
                int sz = req_indices.length;
650
                int[] req_ids = new int[sz];
651
                for (int i=0; i<sz; i++)
652
                        req_ids[i] = attsDataSourceAdapter.getRowId(req_indices[i]);
653

    
654
            int idcolindex = attsDataSourceAdapter.getIdIndex();
655
            String idFieldName = attsDataSourceAdapter.getOriginalFieldName(idcolindex);
656
            String inParenthesis = getInIntParenthesis(req_ids);
657
            String whereClause = idFieldName + " " + inParenthesis;
658

    
659
            String[] subflds = new String[1];
660
            subflds[0] = "#ALL#";
661

    
662
            logger.debug("Justo antes de llamar a client.getAttributes(...)");
663

    
664
            ArrayList atts = client.getAttributesWithEnvelope(layer.getArcimsStatus(),
665
                    subflds, whereClause, null);
666
            
667
            rowinds = getRowIndicesFromResponse(atts, idcolindex);
668

    
669
            // --------------------------------------------------------
670
            logger.debug("Justo despues de llamar a client.getAttributes(...)");
671

    
672
            logger.debug(
673
                "Justo antes de llamar a attsDataSourceAdapter.updateRow(...) " +
674
                atts.size() + " veces");
675

    
676
            for (int i = (atts.size() - 1); i >= 0; i--) {
677
                Value[] newrow = ((DefaultFeature) atts.get(i)).getAttributes();
678

    
679
                attsDataSourceAdapter.updateRow(newrow, subflds, rowinds[i]);
680
            }
681

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

    
688
            // -------------- 
689
            logger.debug(
690
                "Justo despues de llamar a attsDataSourceAdapter.updateRow(...)");
691
            logger.debug(
692
                "Justo antes de llamar a attsDataSourceAdapter.addAsRequested(...)");
693

    
694
            attsDataSourceAdapter.addAsRequested(fbs);
695

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

    
706
    private int[] enumarate(int a, int b) {
707

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

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

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

    
748
        public void requestFeatureAttributesWithoutChecking(FBitSet fbs)
749
        throws ArcImsException {
750
        int length = fbs.cardinality();
751

    
752
        if (length == 0) {
753
            return;
754
        }
755

    
756
        int rowcount = 0;
757
        int firstnonreq = 0;
758

    
759
        try {
760
            rowcount = (int) attsDataSourceAdapter.getRowCount();
761
        }
762
        catch (DriverException e1) {
763
            logger.error("Unexpected error while getting row count ");
764

    
765
            return;
766
        }
767

    
768
        firstnonreq = fbs.nextClearBit(fbs.nextSetBit(0));
769

    
770
        if (firstnonreq >= rowcount) {
771
            firstnonreq = 0;
772
        }
773

    
774
        if (length < 10) {
775
            fbs.or(attsDataSourceAdapter.getNonRequestedFromHere(firstnonreq,
776
                    attsDataSourceAdapter.getRowsPerRequest() - length));
777
            length = fbs.cardinality();
778
        }
779

    
780
        int[] ids = new int[length];
781
        int[] rowinds = new int[length];
782
        int idind = 0;
783

    
784
        try {
785
            for (int i = fbs.nextSetBit(0); i >= 0;
786
                    i = fbs.nextSetBit(i + 1)) {
787
                ids[idind] = attsDataSourceAdapter.getRowId(i);
788
                rowinds[idind] = i;
789
                idind++;
790
            }
791

    
792
            String inParenthesis = getInIntParenthesis(ids);
793
            int idcolindex = attsDataSourceAdapter.getIdIndex();
794
            String idFieldName = attsDataSourceAdapter.getOriginalFieldName(idcolindex);
795
            String whereClause = idFieldName + " " + inParenthesis;
796
            String[] subflds = new String[1];
797
            subflds[0] = "#ALL#";
798

    
799
            // -------------- with geometries -------------------------
800
            ArrayList atts = client.getAttributesWithEnvelope(layer.getArcimsStatus(),
801
                    subflds, whereClause, null);
802

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

    
811
            // -------------- 
812
            for (int i = (atts.size() - 1); i >= 0; i--) {
813
                Value[] newrow = ((DefaultFeature) atts.get(i)).getAttributes();
814

    
815
                // Value[] newrow = (Value[]) atts.get(i);
816
                attsDataSourceAdapter.updateRow(newrow, subflds, rowinds[i]);
817
            }
818

    
819
            attsDataSourceAdapter.addAsRequested(fbs);
820
        }
821
        catch (DriverException e) {
822
            ArcImsException aie = new ArcImsException("datasource_error");
823
            logger.error("While requesting features ", aie);
824
            throw aie;
825
        }
826
    }
827

    
828
    private FBitSet leaveBitsThatNeedRequest(FBitSet fbs) {
829
        FBitSet resp = (FBitSet) fbs.clone();
830

    
831
        for (int i = resp.nextSetBit(0); i >= 0; i = resp.nextSetBit(i + 1)) {
832
            if (!attsDataSourceAdapter.isNonRequestedRow(i)) {
833
                resp.clear(i);
834
            }
835
        }
836

    
837
        return resp;
838
    }
839

    
840
    private String getInIntParenthesis(int[] ids) {
841
        String resp = "IN (";
842

    
843
        for (int i = 0; i < ids.length; i++) {
844
            resp = resp + " " + ids[i] + ",";
845
        }
846

    
847
        resp = resp.substring(0, resp.length() - 1);
848
        resp = resp + ")";
849

    
850
        return resp;
851
    }
852

    
853
    private int libArcImsShapeTypeToGvSigType(int type) {
854
        int resp = type;
855

    
856
        switch (type) {
857
        case FShape.MULTIPOINT:
858
            resp = FShape.POINT;
859

    
860
            break;
861
        }
862

    
863
        return resp;
864
    }
865

    
866
    public void setAdapter(VectorialEditableAdapter adapter) {
867
        eadapter = adapter;
868
    }
869

    
870
    public VectorialEditableAdapter getAdapter() {
871
        return eadapter;
872
    }
873

    
874
    public void reload() throws IOException, DriverException {
875
    }
876

    
877
    public boolean isWritable() {
878
        return false;
879
    }
880

    
881
    public String getRemoteLayerName() {
882
        return remoteLayerName;
883
    }
884

    
885
    public void setRemoteLayerName(String remoteLayerName) {
886
        this.remoteLayerName = remoteLayerName;
887
    }
888
}