svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / testediting / UpdateFeature.java @ 26061
History | View | Annotate | Download (4.86 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.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 EditableFeature applyUpdateList(Feature feature, long index, |
106 |
long lastIndex)
|
107 |
throws DataException, EvaluatorException {
|
108 |
Iterator iter = this.getUpdateList(); |
109 |
EditableFeature editable = feature.getEditable(); |
110 |
boolean hasChanges = false; |
111 |
while (iter.hasNext()) {
|
112 |
UpdateItem update = (UpdateItem) iter.next(); |
113 |
if (index == update.featureToUpdate
|
114 |
|| update.featureToUpdate == UPDATE_ALL_FEATURES |
115 |
|| (update.featureToUpdate == UPDATE_LAST_FEATURE && (index == lastIndex))) { |
116 |
if (update.evaluator != null) { |
117 |
editable.set(update.attributeName, update.evaluator |
118 |
.evaluate((EvaluatorData) feature)); |
119 |
} else {
|
120 |
editable.set(update.attributeName, update.newValue); |
121 |
} |
122 |
hasChanges = true;
|
123 |
} |
124 |
|
125 |
} |
126 |
if (hasChanges) {
|
127 |
return editable;
|
128 |
} |
129 |
return null; |
130 |
} |
131 |
|
132 |
public void run() { |
133 |
if (!this.startProcess()) { |
134 |
return;
|
135 |
} |
136 |
if (this.updateList.size() == 0) { |
137 |
finishedNoOk(); |
138 |
return;
|
139 |
} |
140 |
FeatureSet set; |
141 |
try {
|
142 |
set = this.store.getFeatureSet();
|
143 |
} catch (DataException e) {
|
144 |
finishedError(e); |
145 |
return;
|
146 |
} |
147 |
try {
|
148 |
long size = set.getSize();
|
149 |
EditableFeature newFeature = this.store.createNewFeature(true); |
150 |
|
151 |
long i = 0; |
152 |
Iterator iter = null; |
153 |
try {
|
154 |
|
155 |
iter = set.iterator(); |
156 |
while (iter.hasNext()) {
|
157 |
Feature feature = (Feature) iter.next(); |
158 |
EditableFeature result = this.applyUpdateList(feature,
|
159 |
i, size - 1);
|
160 |
if (result != null) { |
161 |
set.update(result); |
162 |
} |
163 |
i++; |
164 |
} |
165 |
} catch (ConcurrentDataModificationException e) {
|
166 |
finishedConcurrentError(e); |
167 |
return;
|
168 |
} catch (DataException e) {
|
169 |
finishedError(e); |
170 |
return;
|
171 |
} |
172 |
|
173 |
if ((size == set.getSize()) && (set.getSize() == i)) {
|
174 |
finishedOk(); |
175 |
} else {
|
176 |
finishedNoOk(); |
177 |
} |
178 |
} catch (Throwable e) { |
179 |
finishedError(e); |
180 |
return;
|
181 |
} finally {
|
182 |
set.dispose(); |
183 |
} |
184 |
} |
185 |
|
186 |
|
187 |
public class UpdateItem{ |
188 |
public long featureToUpdate = UPDATE_ALL_FEATURES; |
189 |
public String attributeName = null; |
190 |
public Evaluator evaluator = null; |
191 |
public Object newValue = null; |
192 |
|
193 |
} |
194 |
|
195 |
} |