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