Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / feature / testmulithread / UpdateFeature.java @ 40435

History | View | Annotate | Download (4.94 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.feature.testmulithread;
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.tools.dispose.DisposableIterator;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
44
import org.gvsig.tools.evaluator.Evaluator;
45
import org.gvsig.tools.evaluator.EvaluatorData;
46
import org.gvsig.tools.evaluator.EvaluatorException;
47

    
48
/**
49
 * @author jmvivo
50
 *
51
 */
52
public class UpdateFeature extends StoreTask {
53

    
54
        private List updateList = new ArrayList();
55

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

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

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

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

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

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

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

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

    
106
        public EditableFeature applyUpdateList(Feature feature, long index,
107
                        long lastIndex)
108
                        throws DataException, EvaluatorException {
109
                Iterator iter = this.getUpdateList();
110
                EditableFeature editable = feature.getEditable();
111
                boolean hasChanges = false;
112
                while (iter.hasNext()) {
113
                        UpdateItem update = (UpdateItem) iter.next();
114
                        if (index == update.featureToUpdate
115
                                        || update.featureToUpdate == UPDATE_ALL_FEATURES
116
                                        || (update.featureToUpdate == UPDATE_LAST_FEATURE && (index == lastIndex))) {
117
                                if (update.evaluator != null) {
118
                                        editable.set(update.attributeName, update.evaluator
119
                                                        .evaluate((EvaluatorData) feature));
120
                                } else {
121
                                        editable.set(update.attributeName, update.newValue);
122
                                }
123
                                hasChanges = true;
124
                        }
125

    
126
                }
127
                if (hasChanges) {
128
                        return editable;
129
                }
130
                return null;
131
        }
132

    
133
        public void run() {
134
                if (!this.startProcess()) {
135
                        return;
136
                }
137
                if (this.updateList.size() == 0) {
138
                        finishedNoOk();
139
                        return;
140
                }
141
                FeatureSet set;
142
                DisposableIterator iter = null;
143
                try {
144
                        set = this.store.getFeatureSet();
145
                } catch (DataException e) {
146
                        finishedError(e);
147
                        return;
148
                }
149
                try {
150
                        long size = set.getSize();
151
                        EditableFeature newFeature = this.store.createNewFeature(true);
152

    
153
                        long i = 0;
154
                        try {
155

    
156
                                iter = set.iterator();
157
                                while (iter.hasNext()) {
158
                                        Feature feature = (Feature) iter.next();
159
                                        EditableFeature result = this.applyUpdateList(feature,
160
                                                        i, size - 1);
161
                                        if (result != null) {
162
                                                set.update(result);
163
                                        }
164
                                        i++;
165
                                }
166
                        } catch (ConcurrentDataModificationException e) {
167
                                finishedConcurrentError(e);
168
                                return;
169
                        } catch (DataException e) {
170
                                finishedError(e);
171
                                return;
172
                        }
173

    
174
                        if ((size == set.getSize()) && (set.getSize() == i)) {
175
                                finishedOk();
176
                        } else {
177
                                finishedNoOk();
178
                        }
179
                } catch (Throwable e) {
180
                        finishedError(e);
181
                        return;
182
                } finally {
183
                        iter.dispose();
184
                        set.dispose();
185
                }
186
        }
187

    
188

    
189
        public class UpdateItem{
190
                public long featureToUpdate = UPDATE_ALL_FEATURES;
191
                public String attributeName = null;
192
                public Evaluator evaluator = null;
193
                public Object newValue = null;
194

    
195
        }
196

    
197
}