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