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