Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / dbf / testediting / UpdateFeature.java @ 26033

History | View | Annotate | Download (5.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.dbf.testediting;
32

    
33
import java.util.ArrayList;
34
import java.util.Iterator;
35
import java.util.List;
36

    
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.EditableFeature;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
43
import org.gvsig.tools.evaluator.Evaluator;
44
import org.gvsig.tools.evaluator.EvaluatorData;
45
import org.gvsig.tools.evaluator.EvaluatorException;
46

    
47
/**
48
 * @author jmvivo
49
 *
50
 */
51
public class UpdateFeature extends EditStore {
52

    
53
        private List updateList = new ArrayList();
54

    
55
        public static final int UPDATE_ALL_FEATURES = -2;
56
        public static final int UPDATE_LAST_FEATURE = -1;
57

    
58
        /**
59
         * @param store
60
         * @param timeToWait
61
         */
62
        public UpdateFeature(String id, FeatureStore store) {
63
                super(id, store);
64
        }
65

    
66
        /**
67
         * @param store
68
         * @param timeToWait
69
         */
70
        public UpdateFeature(String id, FeatureStore store, int timeToWait) {
71
                super(id, store, timeToWait);
72
        }
73

    
74
        public void addUpdate(int featureToUpdate, String attName, Object newValue) {
75
                UpdateItem item = new UpdateItem();
76
                item.featureToUpdate = featureToUpdate;
77
                item.attributeName = attName;
78
                item.newValue = newValue;
79
                item.evaluator = null;
80
                this.updateList.add(item);
81
        }
82

    
83
        public void addUpdate(int featureToUpdate, String attName,
84
                        Evaluator evaluator) {
85
                UpdateItem item = new UpdateItem();
86
                item.featureToUpdate = featureToUpdate;
87
                item.attributeName = attName;
88
                item.newValue = null;
89
                item.evaluator = evaluator;
90
                this.updateList.add(item);
91
        }
92

    
93
        public Iterator getUpdateList() {
94
                return this.updateList.iterator();
95
        }
96

    
97
        public UpdateItem getUpdate(int index) {
98
                return (UpdateItem) this.updateList.get(index);
99
        }
100

    
101
        public int getUpdateListSize() {
102
                return this.updateList.size();
103
        }
104

    
105
        public boolean compareOriginalToUpdatedFeature(Feature original,
106
                        Feature updated, long index, long lastIndex) throws DataException,
107
                        EvaluatorException {
108
                Iterator iter = this.getUpdateList();
109
                boolean equals = true;
110

    
111
                return equals;
112

    
113
        }
114

    
115
        public EditableFeature applyUpdateList(Feature feature, long index,
116
                        long lastIndex)
117
                        throws DataException, EvaluatorException {
118
                Iterator iter = this.getUpdateList();
119
                EditableFeature editable = feature.getEditable();
120
                boolean hasChanges = false;
121
                while (iter.hasNext()) {
122
                        UpdateItem update = (UpdateItem) iter.next();
123
                        if (index == update.featureToUpdate
124
                                        || update.featureToUpdate == UPDATE_ALL_FEATURES
125
                                        || (update.featureToUpdate == UPDATE_LAST_FEATURE && (index == lastIndex))) {
126
                                if (update.evaluator != null) {
127
                                        editable.set(update.attributeName, update.evaluator
128
                                                        .evaluate((EvaluatorData) feature));
129
                                } else {
130
                                        editable.set(update.attributeName, update.newValue);
131
                                }
132
                                hasChanges = true;
133
                        }
134

    
135
                }
136
                if (hasChanges) {
137
                        return editable;
138
                }
139
                return null;
140
        }
141

    
142
        public void run() {
143
                if (!this.startProcess()) {
144
                        return;
145
                }
146
                if (this.updateList.size() == 0) {
147
                        finishedNoOk();
148
                        return;
149
                }
150
                FeatureSet set;
151
                try {
152
                        set = this.store.getFeatureSet();
153
                } catch (DataException e) {
154
                        finishedError(e);
155
                        return;
156
                }
157
                try {
158
                        long size = set.getSize();
159
                        EditableFeature newFeature = this.store.createNewFeature(true);
160

    
161
                        long i = 0;
162
                        Iterator iter = null;
163
                        try {
164

    
165
                                iter = set.iterator();
166
                                while (iter.hasNext()) {
167
                                        Feature feature = (Feature) iter.next();
168
                                        EditableFeature result = this.applyUpdateList(feature,
169
                                                        i, size - 1);
170
                                        if (result != null) {
171
                                                set.update(result);
172
                                        }
173
                                        i++;
174
                                }
175
                        } catch (ConcurrentDataModificationException e) {
176
                                finishedConcurrentError(e);
177
                                return;
178
                        } catch (DataException e) {
179
                                finishedError(e);
180
                                return;
181
                        }
182

    
183
                        if ((size == set.getSize()) && (set.getSize() == i)) {
184
                                finishedOk();
185
                        } else {
186
                                finishedNoOk();
187
                        }
188
                } catch (Throwable e) {
189
                        finishedError(e);
190
                        return;
191
                } finally {
192
                        set.dispose();
193
                }
194
        }
195

    
196

    
197
        public class UpdateItem{
198
                public long featureToUpdate = UPDATE_ALL_FEATURES;
199
                public String attributeName = null;
200
                public Evaluator evaluator = null;
201
                public Object newValue = null;
202

    
203
        }
204

    
205
}