Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / featureiterators / AttrQueryFeatureIterator.java @ 33330

History | View | Annotate | Download (4.98 KB)

1
/*
2
 * Created on 12-abr-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: AttrQueryFeatureIterator.java 33330 2010-08-24 09:12:44Z vcaballero $
47
 * $Log$
48
 * Revision 1.1  2007-05-29 19:08:11  azabala
49
 * first version in cvs
50
 *
51
 * Revision 1.1  2007/04/19 17:27:58  azabala
52
 * first version in cvs
53
 *
54
 *
55
 */
56
package com.iver.cit.gvsig.fmap.drivers.featureiterators;
57

    
58
import org.cresques.cts.IProjection;
59

    
60
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
61
import com.hardcode.gdbms.engine.data.DataSource;
62
import com.hardcode.gdbms.engine.data.DataSourceFactory;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
65
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
66
import com.iver.cit.gvsig.fmap.core.IFeature;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
69
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
70

    
71
/**
72
 * Iterates over the features of a vectorial data source
73
 * (readablevectorial, or vectorialadapter) which verify a SQL statement.
74
 * <br>
75
 * SQL syntax is very extrict, GDBMS based.
76
 * (for example, % character is not allowed to build strings expressions,
77
 * all SQL statement must end with ;, etc)
78
 *
79
 *
80
 *
81
 *
82
 * @author azabala
83
 *
84
 */
85
public class AttrQueryFeatureIterator extends DefaultFeatureIterator {
86

    
87
        private String sqlQuery;
88
        private long[] indexes;
89

    
90
        public AttrQueryFeatureIterator(ReadableVectorial source,
91
                        IProjection sourceProj,
92
                        IProjection targetProj,
93
                        String sqlQuery) throws ReadDriverException {
94
                super(source,sourceProj,targetProj,source.getRecordset().getFieldNames());
95
                this.sqlQuery = sqlQuery;
96

    
97
                try {
98
                        if(hasWhere(sqlQuery)){
99
                                DataSource datasource = LayerFactory.getDataSourceFactory().executeSQL(sqlQuery,
100
                                                DataSourceFactory.MANUAL_OPENING);
101
                                super.setFieldNames(source.getRecordset().getFieldNames());
102
                                indexes = datasource.getWhereFilter();
103
                        }else{
104
                                //TODO This is not very elegant: rethink
105
                                indexes = new long[source.getShapeCount()];
106
                                for(int i = 0; i < indexes.length; i++){
107
                                        indexes[i] = i;
108
                                }
109
                                super.setFieldNames(source.getRecordset().getFieldNames());
110
                        }
111

    
112

    
113
                        //check to avoid reprojections with the same projection
114
                        if(targetProj != null){
115
                                // FJP: Si la capa original no sabemos que proyeccion tiene, no hacemos nada
116
                                if (sourceProj != null) {
117
                                        if(!(targetProj.getAbrev().equalsIgnoreCase(sourceProj.getAbrev())))
118
                                                this.targetProjection = targetProj;
119
                                }
120
                        }
121

    
122
                } catch (Exception e){
123
                        throw new ReadDriverException("error ejecutando consulta sql para iterador", e);
124
                }
125
        }
126

    
127
        public boolean hasNext(){
128
                if(indexes != null && currentFeature < indexes.length)
129
                        return true;
130
                else
131
                        return false;
132
        }
133

    
134
        private boolean hasWhere(String expression) {
135
                String subExpression = expression.trim();
136
                int pos;
137

    
138
                // Remove last ';' if exists
139
                if (subExpression.charAt(subExpression.length() -1) == ';')
140
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
141

    
142
                // If there is no 'where' clause
143
                if ((pos = subExpression.indexOf("where")) == -1)
144
                        return false;
145

    
146
                // If there is no subexpression in the WHERE clause -> true
147
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
148
                if ( subExpression.length() == 0 )
149
                        return false;
150
                else
151
                        return true;
152
        }
153

    
154
        public IFeature next() throws ReadDriverException {
155
                IGeometry geom;
156
                try {
157
                        geom = chekIfCloned(source.getShape((int)indexes[currentFeature]));
158
                        reprojectIfNecessary(geom);
159
                } catch (ExpansionFileReadException e) {
160
                        throw new ReadDriverException("Error accediendo al driver", e);
161
                }
162

    
163
                Value[] regAtt = getValues((int)indexes[currentFeature]);
164
                DefaultFeature feat = new DefaultFeature(geom, regAtt, (int)indexes[currentFeature] + "");
165
                currentFeature++;
166
                return feat;
167
        }
168

    
169
        public String getSqlQuery() {
170
                return sqlQuery;
171
        }
172

    
173
}
174