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 @ 42488
History | View | Annotate | Download (4.91 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.testmulithread; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.tools.dispose.DisposableIterator; |
32 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
33 |
import org.gvsig.fmap.dal.feature.Feature; |
34 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException; |
37 |
import org.gvsig.tools.evaluator.Evaluator; |
38 |
import org.gvsig.tools.evaluator.EvaluatorData; |
39 |
import org.gvsig.tools.evaluator.EvaluatorException; |
40 |
|
41 |
/**
|
42 |
* @author jmvivo
|
43 |
*
|
44 |
*/
|
45 |
public class UpdateFeature extends StoreTask { |
46 |
|
47 |
private List updateList = new ArrayList(); |
48 |
|
49 |
public static final int UPDATE_ALL_FEATURES = -2; |
50 |
public static final int UPDATE_LAST_FEATURE = -1; |
51 |
|
52 |
/**
|
53 |
* @param store
|
54 |
* @param timeToWait
|
55 |
*/
|
56 |
public UpdateFeature(String id, FeatureStore store) { |
57 |
super(id, store);
|
58 |
} |
59 |
|
60 |
/**
|
61 |
* @param store
|
62 |
* @param timeToWait
|
63 |
*/
|
64 |
public UpdateFeature(String id, FeatureStore store, int timeToWait) { |
65 |
super(id, store, timeToWait);
|
66 |
} |
67 |
|
68 |
public void addUpdate(int featureToUpdate, String attName, Object newValue) { |
69 |
UpdateItem item = new UpdateItem();
|
70 |
item.featureToUpdate = featureToUpdate; |
71 |
item.attributeName = attName; |
72 |
item.newValue = newValue; |
73 |
item.evaluator = null;
|
74 |
this.updateList.add(item);
|
75 |
} |
76 |
|
77 |
public void addUpdate(int featureToUpdate, String attName, |
78 |
Evaluator evaluator) { |
79 |
UpdateItem item = new UpdateItem();
|
80 |
item.featureToUpdate = featureToUpdate; |
81 |
item.attributeName = attName; |
82 |
item.newValue = null;
|
83 |
item.evaluator = evaluator; |
84 |
this.updateList.add(item);
|
85 |
} |
86 |
|
87 |
public Iterator getUpdateList() { |
88 |
return this.updateList.iterator(); |
89 |
} |
90 |
|
91 |
public UpdateItem getUpdate(int index) { |
92 |
return (UpdateItem) this.updateList.get(index); |
93 |
} |
94 |
|
95 |
public int getUpdateListSize() { |
96 |
return this.updateList.size(); |
97 |
} |
98 |
|
99 |
public EditableFeature applyUpdateList(Feature feature, long index, |
100 |
long lastIndex)
|
101 |
throws DataException, EvaluatorException {
|
102 |
Iterator iter = this.getUpdateList(); |
103 |
EditableFeature editable = feature.getEditable(); |
104 |
boolean hasChanges = false; |
105 |
while (iter.hasNext()) {
|
106 |
UpdateItem update = (UpdateItem) iter.next(); |
107 |
if (index == update.featureToUpdate
|
108 |
|| update.featureToUpdate == UPDATE_ALL_FEATURES |
109 |
|| (update.featureToUpdate == UPDATE_LAST_FEATURE && (index == lastIndex))) { |
110 |
if (update.evaluator != null) { |
111 |
editable.set(update.attributeName, update.evaluator |
112 |
.evaluate((EvaluatorData) feature)); |
113 |
} else {
|
114 |
editable.set(update.attributeName, update.newValue); |
115 |
} |
116 |
hasChanges = true;
|
117 |
} |
118 |
|
119 |
} |
120 |
if (hasChanges) {
|
121 |
return editable;
|
122 |
} |
123 |
return null; |
124 |
} |
125 |
|
126 |
public void run() { |
127 |
if (!this.startProcess()) { |
128 |
return;
|
129 |
} |
130 |
if (this.updateList.size() == 0) { |
131 |
finishedNoOk(); |
132 |
return;
|
133 |
} |
134 |
FeatureSet set; |
135 |
DisposableIterator iter = null;
|
136 |
try {
|
137 |
set = this.store.getFeatureSet();
|
138 |
} catch (DataException e) {
|
139 |
finishedError(e); |
140 |
return;
|
141 |
} |
142 |
try {
|
143 |
long size = set.getSize();
|
144 |
EditableFeature newFeature = this.store.createNewFeature(true); |
145 |
|
146 |
long i = 0; |
147 |
try {
|
148 |
|
149 |
iter = set.fastIterator(); |
150 |
while (iter.hasNext()) {
|
151 |
Feature feature = (Feature) iter.next(); |
152 |
EditableFeature result = this.applyUpdateList(feature,
|
153 |
i, size - 1);
|
154 |
if (result != null) { |
155 |
set.update(result); |
156 |
} |
157 |
i++; |
158 |
} |
159 |
} catch (ConcurrentDataModificationException e) {
|
160 |
finishedConcurrentError(e); |
161 |
return;
|
162 |
} catch (DataException e) {
|
163 |
finishedError(e); |
164 |
return;
|
165 |
} |
166 |
|
167 |
if ((size == set.getSize()) && (set.getSize() == i)) {
|
168 |
finishedOk(); |
169 |
} else {
|
170 |
finishedNoOk(); |
171 |
} |
172 |
} catch (Throwable e) { |
173 |
finishedError(e); |
174 |
return;
|
175 |
} finally {
|
176 |
iter.dispose(); |
177 |
set.dispose(); |
178 |
} |
179 |
} |
180 |
|
181 |
|
182 |
public class UpdateItem{ |
183 |
public long featureToUpdate = UPDATE_ALL_FEATURES; |
184 |
public String attributeName = null; |
185 |
public Evaluator evaluator = null; |
186 |
public Object newValue = null; |
187 |
|
188 |
} |
189 |
|
190 |
} |