gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.impl / src / main / java / org / gvsig / vectorediting / lib / impl / DefaultEditingProviderServices.java @ 78
History | View | Annotate | Download (8.65 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.vectorediting.lib.impl
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.vectorediting.lib.impl; |
8 |
|
9 |
import java.awt.geom.Point2D; |
10 |
import java.util.Iterator; |
11 |
|
12 |
import org.gvsig.editing.EditingNotification; |
13 |
import org.gvsig.editing.EditingNotificationManager; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
16 |
import org.gvsig.fmap.dal.feature.Feature; |
17 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
18 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
19 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 |
import org.gvsig.fmap.geom.Geometry; |
21 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
22 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
23 |
import org.gvsig.fmap.geom.GeometryLocator; |
24 |
import org.gvsig.fmap.geom.GeometryManager; |
25 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
26 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
27 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
28 |
import org.gvsig.fmap.geom.primitive.Arc; |
29 |
import org.gvsig.fmap.geom.primitive.Circle; |
30 |
import org.gvsig.fmap.geom.primitive.Curve; |
31 |
import org.gvsig.fmap.geom.primitive.Point; |
32 |
import org.gvsig.fmap.geom.type.GeometryType; |
33 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
34 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
35 |
import org.gvsig.tools.service.spi.AbstractProviderServices; |
36 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
37 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
public class DefaultEditingProviderServices extends AbstractProviderServices |
42 |
implements EditingProviderServices {
|
43 |
|
44 |
private static final Logger logger = LoggerFactory |
45 |
.getLogger(DefaultEditingProviderServices.class); |
46 |
|
47 |
|
48 |
protected GeometryManager geomManager = GeometryLocator
|
49 |
.getGeometryManager(); |
50 |
|
51 |
public void insertFeatureIntoFeatureStore(Feature feature, |
52 |
FeatureStore featureStore) { |
53 |
try {
|
54 |
EditableFeature eFeature; |
55 |
|
56 |
if (feature instanceof EditableFeature){ |
57 |
eFeature = (EditableFeature)feature; |
58 |
} else {
|
59 |
eFeature = feature.getEditable(); |
60 |
} |
61 |
|
62 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
63 |
.getEditingNotificationManager(); |
64 |
|
65 |
EditingNotification notification = editingNotificationManager |
66 |
.notifyObservers(this, // source |
67 |
EditingNotification.BEFORE_INSERT_FEATURE, // type
|
68 |
null,// document |
69 |
null,// layer |
70 |
featureStore,// store
|
71 |
eFeature// feature
|
72 |
); |
73 |
if (notification.isCanceled()) {
|
74 |
throw new CancelationException(""); |
75 |
} |
76 |
|
77 |
//FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en EditingNotification, deber?a quitarse el !
|
78 |
if (!notification.shouldValidateTheFeature()) {
|
79 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
80 |
throw new Exception(""); |
81 |
} |
82 |
} |
83 |
|
84 |
featureStore.insert(eFeature); |
85 |
editingNotificationManager.notifyObservers(this,
|
86 |
EditingNotification.AFTER_INSERT_FEATURE, null, null, |
87 |
featureStore, eFeature); |
88 |
} catch (Exception e) { |
89 |
logger.error(e.getMessage(), e); |
90 |
} |
91 |
} |
92 |
|
93 |
public void insertGeometryIntoFeatureStore(Geometry geometry, |
94 |
FeatureStore featureStore) { |
95 |
try {
|
96 |
EditableFeature eFeature = featureStore.createNewFeature(true);
|
97 |
|
98 |
eFeature.setGeometry(featureStore.getDefaultFeatureType() |
99 |
.getDefaultGeometryAttributeName(), geometry); |
100 |
|
101 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
102 |
.getEditingNotificationManager(); |
103 |
|
104 |
EditingNotification notification = editingNotificationManager |
105 |
.notifyObservers(this, // source |
106 |
EditingNotification.BEFORE_INSERT_FEATURE, // type
|
107 |
null,// document |
108 |
null,// layer |
109 |
featureStore,// store
|
110 |
eFeature// feature
|
111 |
); |
112 |
if (notification.isCanceled()) {
|
113 |
throw new CancelationException(""); |
114 |
} |
115 |
//FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en EditingNotification, deber?a quitarse el !
|
116 |
if (!notification.shouldValidateTheFeature()) {
|
117 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
118 |
throw new Exception(""); |
119 |
} |
120 |
} |
121 |
|
122 |
featureStore.insert(eFeature); |
123 |
|
124 |
editingNotificationManager.notifyObservers(this,
|
125 |
EditingNotification.AFTER_INSERT_FEATURE, null, null, featureStore, |
126 |
eFeature); |
127 |
|
128 |
} |
129 |
catch (Exception e) { |
130 |
logger.error(e.getMessage(), e); |
131 |
} |
132 |
} |
133 |
|
134 |
public void deleteFeatureFromFeatureStore(Feature feature, |
135 |
FeatureStore featureStore) { |
136 |
// TODO Auto-generated method stub
|
137 |
|
138 |
} |
139 |
|
140 |
public void deleteGeometryFromFeatureStore(Geometry geometry, |
141 |
FeatureStore featureStore) { |
142 |
// TODO Auto-generated method stub
|
143 |
|
144 |
} |
145 |
|
146 |
public void updateFeatureInFeatureStore(Feature feature, |
147 |
FeatureStore featureStore) { |
148 |
try {
|
149 |
EditableFeature eFeature; |
150 |
|
151 |
if (feature instanceof EditableFeature){ |
152 |
eFeature = (EditableFeature)feature; |
153 |
} else {
|
154 |
eFeature = feature.getEditable(); |
155 |
} |
156 |
|
157 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
158 |
.getEditingNotificationManager(); |
159 |
|
160 |
EditingNotification notification = editingNotificationManager |
161 |
.notifyObservers(this, // source |
162 |
EditingNotification.BEFORE_UPDATE_FEATURE, // type
|
163 |
null,// document |
164 |
null,// layer |
165 |
featureStore,// store
|
166 |
eFeature// feature
|
167 |
); |
168 |
if (notification.isCanceled()) {
|
169 |
throw new CancelationException(""); |
170 |
} |
171 |
//FIXME: Este m?todo est? hecho al rev?s. Cuando est? corregido en EditingNotification, deber?a quitarse el !
|
172 |
if (!notification.shouldValidateTheFeature()) {
|
173 |
if (!editingNotificationManager.validateFeature(eFeature)) {
|
174 |
throw new Exception(""); |
175 |
} |
176 |
} |
177 |
|
178 |
featureStore.update(eFeature); |
179 |
editingNotificationManager.notifyObservers(this,
|
180 |
EditingNotification.AFTER_UPDATE_FEATURE, null, null, |
181 |
featureStore, eFeature); |
182 |
} catch (Exception e) { |
183 |
logger.error(e.getMessage(), e); |
184 |
} |
185 |
} |
186 |
|
187 |
public void updateGeometryInFeatureStore(Geometry geometry, |
188 |
FeatureStore featureStore) { |
189 |
// TODO Auto-generated method stub
|
190 |
|
191 |
} |
192 |
|
193 |
public Circle createCircle(Point center, double radius, |
194 |
FeatureStore featureStore) |
195 |
throws CreateGeometryException, DataException {
|
196 |
Circle circle = null;
|
197 |
GeometryType featStoreGeomType = getGeomType(featureStore); |
198 |
circle = (Circle) geomManager.create(TYPES.CIRCLE, |
199 |
featStoreGeomType.getSubType()); |
200 |
circle.setPoints(center, radius); |
201 |
|
202 |
return circle;
|
203 |
} |
204 |
|
205 |
public Arc createArc(Point center, double radius, double startAngle, |
206 |
double angleExt, FeatureStore featureStore)
|
207 |
throws CreateGeometryException, DataException {
|
208 |
Arc arc = null;
|
209 |
GeometryType featStoreGeomType = getGeomType(featureStore); |
210 |
arc = (Arc) geomManager.create(TYPES.ARC, featStoreGeomType.getSubType()); |
211 |
arc.setPoints(center, radius, startAngle, angleExt); |
212 |
return arc;
|
213 |
} |
214 |
|
215 |
public Point createPoint(double x, double y, FeatureStore featureStore) |
216 |
throws CreateGeometryException, DataException {
|
217 |
Point point = null; |
218 |
point = (Point) geomManager.create(TYPES.POINT, getSubType(featureStore));
|
219 |
point.setX(x); |
220 |
point.setY(y); |
221 |
return point;
|
222 |
} |
223 |
|
224 |
public Curve createLine(double x1, double y1, double x2, double y2, |
225 |
FeatureStore featureStore) |
226 |
throws CreateGeometryException, DataException {
|
227 |
Curve line = null;
|
228 |
line = (Curve) geomManager.create(TYPES.CURVE, getSubType(featureStore)); |
229 |
line.addVertex(x1, y1); |
230 |
line.addVertex(x2, y2); |
231 |
return line;
|
232 |
} |
233 |
|
234 |
public int getSubType(FeatureStore featureStore) throws DataException { |
235 |
|
236 |
GeometryType geomType = getGeomType(featureStore); |
237 |
if (geomType != null) { |
238 |
return geomType.getSubType();
|
239 |
} |
240 |
return SUBTYPES.GEOM3D; // ??????? |
241 |
} |
242 |
|
243 |
public GeometryType getGeomType(FeatureStore featureStore)
|
244 |
throws DataException {
|
245 |
return featureStore.getDefaultFeatureType().getDefaultGeometryAttribute()
|
246 |
.getGeomType(); |
247 |
} |
248 |
|
249 |
public EditableFeature getFeatureCopyWithoutPK(FeatureStore featureStore, Feature feature)
|
250 |
throws DataException {
|
251 |
EditableFeature editableFeature = featureStore.createNewFeature( |
252 |
feature.getType(), true);
|
253 |
|
254 |
FeatureType type_src = feature.getType(); |
255 |
@SuppressWarnings("rawtypes") |
256 |
Iterator iterator = type_src.iterator();
|
257 |
FeatureType type_dest = editableFeature.getType(); |
258 |
|
259 |
while (iterator.hasNext()) {
|
260 |
FeatureAttributeDescriptor attribute_src = (FeatureAttributeDescriptor) iterator |
261 |
.next(); |
262 |
|
263 |
FeatureAttributeDescriptor attribute_dest = type_dest |
264 |
.getAttributeDescriptor(attribute_src.getName()); |
265 |
if (attribute_dest != null) { |
266 |
if (!attribute_dest.isPrimaryKey()) {
|
267 |
editableFeature.set(attribute_dest.getIndex(), |
268 |
feature.get(attribute_src.getIndex())); |
269 |
} |
270 |
} |
271 |
} |
272 |
return editableFeature;
|
273 |
} |
274 |
|
275 |
|
276 |
} |