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