Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / postgresql / PostgresqlFeatureCollectionEditingFiltered.java @ 24491

History | View | Annotate | Download (3.63 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.postgresql;
2

    
3

    
4
import java.util.ConcurrentModificationException;
5
import java.util.Iterator;
6
import java.util.NoSuchElementException;
7

    
8
import org.gvsig.fmap.dal.DataManager;
9
import org.gvsig.fmap.dal.DataStore;
10
import org.gvsig.fmap.dal.ReadException;
11
import org.gvsig.fmap.dal.feature.FeatureManager;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import org.gvsig.fmap.dal.feature.expressionevaluator.Filter;
14
import org.gvsig.fmap.data.feature.db.DBDataFeatureCollection;
15
import org.gvsig.fmap.data.feature.db.DBFeatureType;
16
import org.gvsig.tools.exception.BaseException;
17

    
18
public class PostgresqlFeatureCollectionEditingFiltered extends DBDataFeatureCollection {
19
        protected DBFeatureType featureType;
20
        protected String totalFilter;
21
        protected String filter;
22
        protected PostgresqlStore store;
23
        private int numReg=-1;
24
        private String sql;
25
        private String sqlCount;
26
        private String totalOrder;
27
        private int fetchSize=5000;
28
        private FeatureManager featureManager;
29
        private Filter parser;
30

    
31

    
32
        PostgresqlFeatureCollectionEditingFiltered(FeatureManager fm,
33
                        PostgresqlStore store, FeatureType type, String filter)
34
                        throws ReadException {
35
                this.featureManager=fm;
36
                this.store=store;
37
                this.featureType=(DBFeatureType)type;
38

    
39
                this.filter = filter;
40

    
41
                this.calculateWhere();
42
                this.totalOrder = store.getBaseOrder();
43

    
44
                this.sql = this.store.getSqlSelectPart();
45
                this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID();
46
                if (!isStringEmpty(this.totalFilter)){
47
                        this.sql= this.sql + " Where " + this.totalFilter;
48
                        this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
49
                }
50
                if (!isStringEmpty(this.totalOrder)){
51
                        this.sql= this.sql + " Order by " + this.totalOrder;
52
                }
53

    
54
                if (this.filter!=null) {
55
                        parser = DataManager.getManager().getExpressionParser()
56
                                        .parseFilter(filter);
57
                }
58

    
59
        }
60

    
61
        private void calculateWhere(){
62
                if (isStringEmpty(this.store.getBaseWhereClause())){
63
                        this.totalFilter = this.filter;
64
                } else {
65
                        this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
66
                }
67
        }
68

    
69
        public int size() {
70
                checkModified();
71
                if (this.numReg < 0){
72
                        this.numReg =0;
73
                        try{
74
                                Iterator iter = this.iterator();
75
                                while (true){
76
                                        iter.next();
77
                                        this.numReg++;
78
                                }
79
                        } catch (NoSuchElementException e){
80
                                //Normal condition exit
81
                        }
82
                }
83
                return this.numReg;
84

    
85
        }
86

    
87

    
88
    protected Iterator internalIterator(int index) {
89
        checkModified();
90
        try {
91
            PostgresIterator dbIter = new PostgresIterator(this.store,
92
                    this.featureType, this.sql, this.fetchSize,
93
                    this.featureManager, this.parser, index + 1);
94
            return dbIter;
95
        } catch (BaseException e) {
96
            throw new RuntimeException(e);
97
        }
98
    }
99

    
100
        protected class PostgresIterator extends AbstractPostgresqlDBIterator {
101
                public PostgresIterator(PostgresqlStore store,
102
                                DBFeatureType featureType, String sql, int fetchSize,
103
                                FeatureManager featureManager, Filter featureFilter,
104
                int initialPosition)
105
                                throws ReadException {
106
                        super(store, featureType, sql, fetchSize, featureManager,
107
                                        featureFilter, initialPosition);
108
                }
109

    
110
                protected void checkModified() {
111
                        if (modified) {
112
                                throw new ConcurrentModificationException(
113
                                                "FeatureCollection modified");
114
                        }
115
                }
116

    
117
        }
118

    
119

    
120

    
121

    
122
        public void dispose() {
123
                this.store.deleteObserver(this);
124
                this.store = null;
125
                this.featureManager = null;
126
                this.featureType = null;
127
                this.parser= null;
128

    
129
        }
130

    
131

    
132
        public boolean isFromStore(DataStore store) {
133
                return this.store.equals(store);
134
        }
135

    
136
        public FeatureType getFeatureType() {
137
                return this.featureType;
138
        }
139
}