gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.join / src / main / java / org / gvsig / vectorediting / lib / prov / join / JoinEditingProvider.java @ 493
History | View | Annotate | Download (12 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 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 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 |
* 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 |
|
25 |
package org.gvsig.vectorediting.lib.prov.join; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
33 |
import org.gvsig.fmap.dal.feature.Feature; |
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
38 |
import org.gvsig.fmap.geom.Geometry; |
39 |
import org.gvsig.fmap.geom.primitive.Point; |
40 |
import org.gvsig.fmap.geom.type.GeometryType; |
41 |
import org.gvsig.tools.dispose.DisposableIterator; |
42 |
import org.gvsig.tools.dynobject.DynObject; |
43 |
import org.gvsig.tools.exception.BaseException; |
44 |
import org.gvsig.tools.service.spi.ProviderServices; |
45 |
import org.gvsig.tools.visitor.VisitCanceledException; |
46 |
import org.gvsig.tools.visitor.Visitor; |
47 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
48 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
49 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
54 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
55 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
56 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
60 |
|
61 |
/**
|
62 |
* @author llmarques
|
63 |
*
|
64 |
*/
|
65 |
public class JoinEditingProvider extends AbstractEditingProvider implements |
66 |
EditingProvider { |
67 |
|
68 |
private EditingServiceParameter selectionParameter;
|
69 |
|
70 |
private FeatureStore featureStore;
|
71 |
|
72 |
private FeatureSelection featureSelection;
|
73 |
|
74 |
/**
|
75 |
* Default constructor.
|
76 |
*
|
77 |
* @param providerServices
|
78 |
* available services for this provider
|
79 |
* @param parameters
|
80 |
* of this provider
|
81 |
*/
|
82 |
public JoinEditingProvider(DynObject parameters, ProviderServices services) {
|
83 |
super(services);
|
84 |
|
85 |
this.featureStore =
|
86 |
(FeatureStore) parameters |
87 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
88 |
|
89 |
this.selectionParameter =
|
90 |
new DefaultEditingServiceParameter("selection", "selection", |
91 |
TYPE.SELECTION); |
92 |
} |
93 |
|
94 |
public EditingServiceParameter next() {
|
95 |
if (featureSelection == null) { |
96 |
return selectionParameter;
|
97 |
} |
98 |
return null; |
99 |
} |
100 |
|
101 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
102 |
throws DrawServiceException {
|
103 |
return null; |
104 |
} |
105 |
|
106 |
public void stop() throws StopServiceException { |
107 |
featureSelection = null;
|
108 |
} |
109 |
|
110 |
public List<EditingServiceParameter> getParameters() { |
111 |
List<EditingServiceParameter> parameters =
|
112 |
new ArrayList<EditingServiceParameter>(); |
113 |
parameters.add(selectionParameter); |
114 |
return parameters;
|
115 |
} |
116 |
|
117 |
public void setValue(Object value) throws InvalidEntryException { |
118 |
EditingServiceParameter parameter = next(); |
119 |
validateAndInsertValue(parameter, value); |
120 |
|
121 |
} |
122 |
|
123 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
124 |
Object value) throws InvalidEntryException { |
125 |
|
126 |
if (parameter == selectionParameter) {
|
127 |
|
128 |
if (value instanceof FeatureSelection) { |
129 |
FeatureSelection fSelection = (FeatureSelection) value; |
130 |
|
131 |
if (fSelection.getSelectedCount() > 1) { |
132 |
|
133 |
DisposableIterator it = null;
|
134 |
|
135 |
try {
|
136 |
it = fSelection.fastIterator(); |
137 |
|
138 |
Feature feature = (Feature) it.next(); |
139 |
// Gets geometry type of first selected feature
|
140 |
GeometryType geometryType = |
141 |
feature.getDefaultGeometry().getGeometryType(); |
142 |
|
143 |
// Iterate over remaining selected features
|
144 |
while (it.hasNext()) {
|
145 |
feature = (Feature) it.next(); |
146 |
|
147 |
// Remaining selected features must be the same or
|
148 |
// the equivalent multigeometry type of first
|
149 |
// feature. If one of reamining geometries is
|
150 |
// different, selection is not valid.
|
151 |
if (geometryType.isTypeOf(CURVE)
|
152 |
|| geometryType.isTypeOf(MULTICURVE)) { |
153 |
|
154 |
GeometryType featureGeoType = |
155 |
feature.getDefaultGeometry() |
156 |
.getGeometryType(); |
157 |
|
158 |
if (!(featureGeoType.isTypeOf(CURVE) || featureGeoType
|
159 |
.isTypeOf(MULTICURVE))) { |
160 |
throw new InvalidEntryException(null); |
161 |
} |
162 |
|
163 |
} else if (geometryType.isTypeOf(SURFACE) |
164 |
|| geometryType.isTypeOf(SURFACE)) { |
165 |
|
166 |
GeometryType featureGeoType = |
167 |
feature.getDefaultGeometry() |
168 |
.getGeometryType(); |
169 |
|
170 |
if (!(featureGeoType.isTypeOf(SURFACE) || featureGeoType
|
171 |
.isTypeOf(MULTISURFACE))) { |
172 |
throw new InvalidEntryException(null); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
featureSelection = fSelection; |
178 |
return;
|
179 |
} catch (DataException e) {
|
180 |
throw new InvalidEntryException(e); |
181 |
} finally {
|
182 |
it.dispose(); |
183 |
} |
184 |
} |
185 |
} |
186 |
} |
187 |
throw new InvalidEntryException(null); |
188 |
} |
189 |
|
190 |
public Geometry finish() throws FinishServiceException { |
191 |
|
192 |
if (featureSelection != null) { |
193 |
DisposableIterator it = null;
|
194 |
Geometry joinedGeometry; |
195 |
|
196 |
try {
|
197 |
it = featureSelection.fastIterator(); |
198 |
Feature feature = (Feature) it.next(); |
199 |
joinedGeometry = feature.getDefaultGeometry(); |
200 |
|
201 |
while (it.hasNext()) {
|
202 |
|
203 |
Feature tmpFeature = (Feature) it.next(); |
204 |
joinedGeometry = |
205 |
joinedGeometry.union(tmpFeature.getDefaultGeometry()); |
206 |
} |
207 |
|
208 |
} catch (BaseException e) {
|
209 |
throw new FinishServiceException(e); |
210 |
} finally {
|
211 |
it.dispose(); |
212 |
} |
213 |
|
214 |
return joinedGeometry;
|
215 |
} |
216 |
return null; |
217 |
} |
218 |
|
219 |
@SuppressWarnings("rawtypes") |
220 |
public void finishAndStore() throws FinishServiceException { |
221 |
Geometry geometry = finish(); |
222 |
|
223 |
final EditingProviderServices editingProviderServices =
|
224 |
(EditingProviderServices) getProviderServices(); |
225 |
|
226 |
FeatureType featureType = null;
|
227 |
EditableFeature eFeature = null;
|
228 |
|
229 |
try {
|
230 |
eFeature = featureStore.createNewFeature(); |
231 |
featureType = featureStore.getDefaultFeatureType(); |
232 |
} catch (DataException e) {
|
233 |
throw new FinishServiceException(e); |
234 |
} |
235 |
|
236 |
Iterator typeIterator = featureType.iterator();
|
237 |
// Iterate over feature type
|
238 |
while (typeIterator.hasNext()) {
|
239 |
boolean insertValue = true; |
240 |
FeatureAttributeDescriptor attribute = |
241 |
(FeatureAttributeDescriptor) typeIterator.next(); |
242 |
|
243 |
// If type is a primary key do nothing
|
244 |
if (!attribute.isPrimaryKey()) {
|
245 |
|
246 |
// int type = attribute.getType();
|
247 |
|
248 |
DisposableIterator featureIterator = null;
|
249 |
try {
|
250 |
featureIterator = featureSelection.fastIterator(); |
251 |
Feature feature = (Feature) featureIterator.next(); |
252 |
|
253 |
// Restart iterator and initilize it at second position
|
254 |
featureIterator.dispose(); |
255 |
featureIterator = featureSelection.fastIterator(1);
|
256 |
|
257 |
// Gets the type value of first feature
|
258 |
Object value = feature.get(attribute.getName());
|
259 |
|
260 |
// Compare first value with the rest of features. If values
|
261 |
// are different, break bucle and don't insert value at new
|
262 |
// feature.
|
263 |
if (value != null) { |
264 |
|
265 |
while (featureIterator.hasNext()) {
|
266 |
Feature nextFeature = |
267 |
(Feature) featureIterator.next(); |
268 |
Object nextValue =
|
269 |
nextFeature.get(attribute.getName()); |
270 |
|
271 |
if (nextValue == null || !value.equals(nextValue)) { |
272 |
insertValue = false;
|
273 |
break;
|
274 |
} |
275 |
} |
276 |
|
277 |
if (insertValue) {
|
278 |
eFeature.set(attribute.getName(), value); |
279 |
} |
280 |
} |
281 |
} catch (DataException e) {
|
282 |
throw new FinishServiceException(e); |
283 |
} finally {
|
284 |
featureIterator.dispose(); |
285 |
} |
286 |
} |
287 |
} |
288 |
|
289 |
// Inserts new feature
|
290 |
eFeature.setDefaultGeometry(geometry); |
291 |
editingProviderServices.insertFeatureIntoFeatureStore(eFeature, |
292 |
featureStore); |
293 |
|
294 |
// Deletes selected features
|
295 |
try {
|
296 |
featureSelection.accept(new Visitor() {
|
297 |
|
298 |
public void visit(Object obj) throws VisitCanceledException, |
299 |
BaseException { |
300 |
Feature feature = (Feature) obj; |
301 |
editingProviderServices.deleteFeatureFromFeatureStore( |
302 |
feature, featureStore); |
303 |
} |
304 |
}); |
305 |
featureStore.getFeatureSelection().deselectAll(); |
306 |
} catch (BaseException e) {
|
307 |
throw new FinishServiceException(e); |
308 |
} |
309 |
} |
310 |
|
311 |
public void start() throws StartServiceException, InvalidEntryException { |
312 |
FeatureSelection selected = null;
|
313 |
if (featureStore != null) { |
314 |
try {
|
315 |
selected = featureStore.getFeatureSelection(); |
316 |
} catch (DataException e) {
|
317 |
throw new StartServiceException(e); |
318 |
} |
319 |
if (selected.getSelectedCount() > 1) { |
320 |
try {
|
321 |
setValue(selected); |
322 |
} catch (InvalidEntryException e) {
|
323 |
throw new InvalidEntryException(e); |
324 |
} |
325 |
} |
326 |
} |
327 |
} |
328 |
|
329 |
public String getName() { |
330 |
return JoinEditingProviderFactory.PROVIDER_NAME;
|
331 |
} |
332 |
|
333 |
} |