svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / store / memory / MemoryStoreProvider.java @ 40435
History | View | Annotate | Download (10.6 KB)
1 | 40435 | jjdelcerro | /* 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.} {Task}
|
||
26 | */
|
||
27 | |||
28 | package org.gvsig.fmap.dal.store.memory; |
||
29 | |||
30 | import java.util.ArrayList; |
||
31 | import java.util.Hashtable; |
||
32 | import java.util.Iterator; |
||
33 | import java.util.List; |
||
34 | import java.util.Map; |
||
35 | |||
36 | import org.slf4j.Logger; |
||
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | import org.gvsig.fmap.dal.DataStore; |
||
40 | import org.gvsig.fmap.dal.DataTypes; |
||
41 | import org.gvsig.fmap.dal.exception.DataException; |
||
42 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
43 | import org.gvsig.fmap.dal.exception.OpenException; |
||
44 | import org.gvsig.fmap.dal.feature.Feature; |
||
45 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
46 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
47 | import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
||
48 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
49 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
50 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
||
51 | import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
||
52 | import org.gvsig.fmap.dal.feature.spi.memory.MemoryResource; |
||
53 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
54 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
55 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
56 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
57 | import org.gvsig.metadata.MetadataLocator; |
||
58 | import org.gvsig.metadata.MetadataManager; |
||
59 | import org.gvsig.metadata.exceptions.MetadataException; |
||
60 | import org.gvsig.tools.ToolsLocator; |
||
61 | import org.gvsig.tools.dispose.DisposableIterator; |
||
62 | import org.gvsig.tools.dynobject.DynObject; |
||
63 | import org.gvsig.tools.dynobject.DynStruct; |
||
64 | import org.gvsig.tools.exception.BaseException; |
||
65 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
66 | import org.gvsig.tools.visitor.Visitor; |
||
67 | |||
68 | /**
|
||
69 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
||
70 | *
|
||
71 | * TODO: take into account the parameter "autoOrderAttributeName" to
|
||
72 | * order the {@link Feature}s by default by that attribute, in ascending
|
||
73 | * order.
|
||
74 | */
|
||
75 | public class MemoryStoreProvider extends AbstractMemoryStoreProvider { |
||
76 | |||
77 | private static final Logger logger = LoggerFactory |
||
78 | .getLogger(MemoryStoreProvider.class); |
||
79 | |||
80 | public static final String NAME = "Memory"; |
||
81 | public static final String DESCRIPTION = "Memory provider"; |
||
82 | |||
83 | public static final String METADATA_DEFINITION_NAME = "MemoryProvider"; |
||
84 | |||
85 | private long counterNewsOIDs = 0; |
||
86 | private Envelope envelope = null; |
||
87 | private Map oids = null; |
||
88 | |||
89 | private Double sourceID = null; |
||
90 | |||
91 | private ResourceProvider memoryResource;
|
||
92 | |||
93 | public static DynObject newMetadataContainer(String name) { |
||
94 | return ToolsLocator.getDynObjectManager().createDynObject(
|
||
95 | MetadataLocator.getMetadataManager().getDefinition(name)); |
||
96 | } |
||
97 | |||
98 | public MemoryStoreProvider(MemoryStoreParameters memoryStoreParameters,
|
||
99 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
100 | super(memoryStoreParameters, storeServices,
|
||
101 | newMetadataContainer(METADATA_DEFINITION_NAME)); |
||
102 | sourceID = new Double(Math.random()); |
||
103 | data = new ArrayList(); |
||
104 | oids = new Hashtable(); |
||
105 | initialize(); |
||
106 | memoryResource = |
||
107 | createResource(MemoryResource.NAME, |
||
108 | new Object[] { memoryStoreParameters.getName() }); |
||
109 | } |
||
110 | |||
111 | public String getName() { |
||
112 | return this.sourceID.toString(); |
||
113 | } |
||
114 | |||
115 | public String getFullName() { |
||
116 | return "memory:" + this.sourceID.toString(); |
||
117 | } |
||
118 | |||
119 | private void initialize() { |
||
120 | FeatureType featureType = |
||
121 | getStoreServices().createFeatureType().getNotEditableCopy(); |
||
122 | List types = new ArrayList(1); |
||
123 | types.add(featureType); |
||
124 | getStoreServices().setFeatureTypes(types, featureType); |
||
125 | } |
||
126 | |||
127 | /*
|
||
128 | * (non-Javadoc)
|
||
129 | *
|
||
130 | * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createNewOID()
|
||
131 | */
|
||
132 | public Object createNewOID() { |
||
133 | return new Long(counterNewsOIDs++); |
||
134 | } |
||
135 | |||
136 | /*
|
||
137 | * (non-Javadoc)
|
||
138 | *
|
||
139 | * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getName()
|
||
140 | */
|
||
141 | public String getProviderName() { |
||
142 | return NAME;
|
||
143 | } |
||
144 | |||
145 | /*
|
||
146 | * (non-Javadoc)
|
||
147 | *
|
||
148 | * @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getOIDType()
|
||
149 | */
|
||
150 | public int getOIDType() { |
||
151 | return DataTypes.LONG;
|
||
152 | } |
||
153 | |||
154 | /*
|
||
155 | * (non-Javadoc)
|
||
156 | *
|
||
157 | * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getSourceId()
|
||
158 | */
|
||
159 | public Object getSourceId() { |
||
160 | return sourceID;
|
||
161 | } |
||
162 | |||
163 | public void open() throws OpenException { |
||
164 | } |
||
165 | |||
166 | public static void registerMetadataDefinition() throws MetadataException { |
||
167 | MetadataManager manager = MetadataLocator.getMetadataManager(); |
||
168 | if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
||
169 | DynStruct metadataDefinition = |
||
170 | manager.addDefinition(METADATA_DEFINITION_NAME, "Memory Store");
|
||
171 | metadataDefinition.extend(manager |
||
172 | .getDefinition(DataStore.SPATIAL_METADATA_DEFINITION_NAME)); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | public boolean allowWrite() { |
||
177 | return true; |
||
178 | } |
||
179 | |||
180 | public void performChanges(Iterator deleteds, Iterator inserteds, |
||
181 | Iterator updateds, Iterator originalFeatureTypesUpdated) |
||
182 | throws PerformEditingException {
|
||
183 | FeatureSet set = null;
|
||
184 | DisposableIterator iter = null;
|
||
185 | try {
|
||
186 | FeatureType featureType = |
||
187 | this.getStoreServices().getDefaultFeatureType();
|
||
188 | List types = new ArrayList(1); |
||
189 | types.add(featureType); |
||
190 | getStoreServices().setFeatureTypes(types, featureType); |
||
191 | |||
192 | set = this.getFeatureStore().getFeatureSet();
|
||
193 | |||
194 | // Create a visitor to generate new data
|
||
195 | EditionVisitor visitor = new EditionVisitor(set.getSize());
|
||
196 | |||
197 | // collect new data
|
||
198 | set.accept(visitor); |
||
199 | |||
200 | // update data objects
|
||
201 | data = visitor.getData(); |
||
202 | oids = visitor.getOids(); |
||
203 | envelope = visitor.getEnvelope(); |
||
204 | |||
205 | } catch (DataException e) {
|
||
206 | logger.error("Error adding features", e);
|
||
207 | } catch (BaseException e) {
|
||
208 | logger.error("Error performing edition", e);
|
||
209 | } finally {
|
||
210 | dispose(iter); |
||
211 | dispose(set); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | public Envelope getEnvelope() throws DataException { |
||
216 | return envelope;
|
||
217 | } |
||
218 | |||
219 | protected FeatureProvider internalGetFeatureProviderByReference(
|
||
220 | FeatureReferenceProviderServices reference) throws DataException {
|
||
221 | return (FeatureProvider) oids.get(reference.getOID());
|
||
222 | } |
||
223 | |||
224 | public ResourceProvider getResource() {
|
||
225 | return memoryResource;
|
||
226 | } |
||
227 | |||
228 | public boolean supportsAppendMode() { |
||
229 | return true; |
||
230 | } |
||
231 | |||
232 | public void append(FeatureProvider featureProvider) throws DataException { |
||
233 | FeatureProvider newFeatureProvider = featureProvider.getCopy(); |
||
234 | if (data.isEmpty()) {
|
||
235 | try {
|
||
236 | envelope = |
||
237 | (Envelope) newFeatureProvider.getDefaultEnvelope().clone(); |
||
238 | } catch (CloneNotSupportedException e) { |
||
239 | envelope = newFeatureProvider.getDefaultEnvelope(); |
||
240 | } |
||
241 | } else {
|
||
242 | envelope.add(newFeatureProvider.getDefaultEnvelope()); |
||
243 | } |
||
244 | |||
245 | newFeatureProvider.setNew(false);
|
||
246 | data.add(newFeatureProvider); |
||
247 | oids.put(newFeatureProvider.getOID(), newFeatureProvider); |
||
248 | logger.debug("Envelope after adding feature: ", envelope.toString());
|
||
249 | } |
||
250 | |||
251 | public void beginAppend() throws DataException { |
||
252 | // Nothing to do
|
||
253 | } |
||
254 | |||
255 | public void endAppend() throws DataException { |
||
256 | // Nothing to do
|
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * Visitor to create new data objects from finish edition operation
|
||
261 | *
|
||
262 | * @author jmvivo ( jmvivo at disid dot com)
|
||
263 | *
|
||
264 | */
|
||
265 | private class EditionVisitor implements Visitor { |
||
266 | |||
267 | private List data; |
||
268 | private Map oids; |
||
269 | private Envelope envelope;
|
||
270 | private FeatureStoreProviderServices services;
|
||
271 | |||
272 | /**
|
||
273 | * @param size
|
||
274 | * to use to initialize data structures
|
||
275 | * @throws CreateEnvelopeException
|
||
276 | */
|
||
277 | public EditionVisitor(long size) { |
||
278 | data = new ArrayList((int) size); |
||
279 | oids = new Hashtable((int) size); |
||
280 | envelope = null;
|
||
281 | services = MemoryStoreProvider.this.getStoreServices(); |
||
282 | } |
||
283 | |||
284 | public void visit(Object obj) throws VisitCanceledException, |
||
285 | BaseException { |
||
286 | Feature feature = (Feature) obj; |
||
287 | FeatureProvider featureProvider = |
||
288 | services.getFeatureProviderFromFeature(feature); |
||
289 | featureProvider.setNew(false);
|
||
290 | data.add(featureProvider); |
||
291 | oids.put(featureProvider.getOID(), featureProvider); |
||
292 | if (envelope == null) { |
||
293 | try {
|
||
294 | envelope = (Envelope) feature.getDefaultEnvelope().clone(); |
||
295 | } catch (CloneNotSupportedException e) { |
||
296 | envelope = feature.getDefaultEnvelope(); |
||
297 | } |
||
298 | } else {
|
||
299 | envelope.add(feature.getDefaultEnvelope()); |
||
300 | } |
||
301 | Envelope aux = feature.getDefaultEnvelope(); |
||
302 | if (aux != null) { //There are geometries |
||
303 | if (envelope == null) { |
||
304 | try {
|
||
305 | envelope = (Envelope) aux.clone(); |
||
306 | } catch (CloneNotSupportedException e) { |
||
307 | envelope = feature.getDefaultEnvelope(); |
||
308 | } |
||
309 | } else {
|
||
310 | envelope.add(feature.getDefaultEnvelope()); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | public List getData() { |
||
316 | return data;
|
||
317 | } |
||
318 | |||
319 | public Map getOids() { |
||
320 | return oids;
|
||
321 | } |
||
322 | |||
323 | public Envelope getEnvelope() {
|
||
324 | return envelope;
|
||
325 | } |
||
326 | } |
||
327 | |||
328 | } |