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