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 / feature / impl / FeatureTypeToStoreProviderAdapter.java @ 46078
History | View | Annotate | Download (14.1 KB)
1 | 44439 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
---|---|---|---|
2 | |||
3 | import java.util.AbstractList; |
||
4 | import java.util.Collections; |
||
5 | import java.util.HashMap; |
||
6 | import java.util.List; |
||
7 | import java.util.Map; |
||
8 | import org.gvsig.expressionevaluator.Expression; |
||
9 | import org.gvsig.fmap.dal.DALLocator; |
||
10 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
11 | import org.gvsig.fmap.dal.DataTypes; |
||
12 | import org.gvsig.fmap.dal.exception.DataException; |
||
13 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
14 | import org.gvsig.fmap.dal.exception.OpenException; |
||
15 | import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
||
16 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
18 | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
||
19 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
21 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
22 | import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
||
23 | import org.gvsig.fmap.dal.feature.spi.memory.MemoryResource; |
||
24 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
25 | import org.gvsig.fmap.dal.spi.DataStoreInitializer2; |
||
26 | import org.gvsig.fmap.dal.spi.DataStoreProvider; |
||
27 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
28 | import org.gvsig.fmap.dal.store.memory.MemoryStoreParameters; |
||
29 | import org.gvsig.fmap.geom.Geometry; |
||
30 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
31 | import org.gvsig.timesupport.Time; |
||
32 | |||
33 | /**
|
||
34 | *
|
||
35 | * @author jjdelcerro
|
||
36 | */
|
||
37 | public class FeatureTypeToStoreProviderAdapter extends AbstractMemoryStoreProvider { |
||
38 | |||
39 | private static final String NAME = "FeatureType"; |
||
40 | |||
41 | private static final int ATTR_NAME = 0; |
||
42 | private static final int ATTR_TYPE = 1; |
||
43 | private static final int ATTR_TYPENAME = 2; |
||
44 | private static final int ATTR_SIZE = 3; |
||
45 | private static final int ATTR_PRECISION = 4; |
||
46 | private static final int ATTR_LABEL = 5; |
||
47 | private static final int ATTR_SHORTLABEL = 6; |
||
48 | private static final int ATTR_DESCRIPTION = 7; |
||
49 | private static final int ATTR_PROFILE = 8; |
||
50 | private static final int ATTR_GROUP = 9; |
||
51 | private static final int ATTR_ORDER = 10; |
||
52 | private static final int ATTR_ISPK = 11; |
||
53 | private static final int ATTR_ISFK = 12; |
||
54 | private static final int ATTR_ISCLOSEDLIST = 13; |
||
55 | private static final int ATTR_ISREADONLY = 14; |
||
56 | private static final int ATTR_ISCOMPUTED = 15; |
||
57 | private static final int ATTR_ISCAUTOMATIC = 16; |
||
58 | private static final int ATTR_ISHIDDEN = 17; |
||
59 | private static final int ATTR_ALLOWNULLS = 18; |
||
60 | private static final int ATTR_EXPRESSION = 19; |
||
61 | private static final int ATTR_FKTABLE = 20; |
||
62 | private static final int ATTR_FKCODE = 21; |
||
63 | private static final int ATTR_FKLABEL = 22; |
||
64 | 44669 | jjdelcerro | private static final int ATTR_SCALE = 23; |
65 | 44439 | jjdelcerro | |
66 | private static Map<String,Integer> attrName2Index; |
||
67 | private static FeatureType featureTypeOfFeatureType; |
||
68 | |||
69 | |||
70 | private static class Features |
||
71 | extends AbstractList<FeatureProvider> |
||
72 | implements List<FeatureProvider> |
||
73 | { |
||
74 | |||
75 | private static class MyFeatureProvider implements FeatureProvider { |
||
76 | |||
77 | private FeatureAttributeDescriptor descriptor;
|
||
78 | |||
79 | public MyFeatureProvider(FeatureAttributeDescriptor descriptor) {
|
||
80 | this.descriptor = descriptor;
|
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public boolean isReadOnly(int i) { |
||
85 | return true; |
||
86 | } |
||
87 | |||
88 | @Override
|
||
89 | public void set(int i, Object value) { |
||
90 | } |
||
91 | |||
92 | @Override
|
||
93 | public Object get(int i) { |
||
94 | switch(i) {
|
||
95 | case ATTR_NAME:
|
||
96 | return this.descriptor.getName(); |
||
97 | case ATTR_SIZE:
|
||
98 | return this.descriptor.getSize(); |
||
99 | case ATTR_TYPE:
|
||
100 | return this.descriptor.getType(); |
||
101 | case ATTR_TYPENAME:
|
||
102 | return this.descriptor.getDataTypeName(); |
||
103 | case ATTR_PRECISION:
|
||
104 | return this.descriptor.getPrecision(); |
||
105 | 44669 | jjdelcerro | case ATTR_SCALE:
|
106 | return this.descriptor.getScale(); |
||
107 | 44439 | jjdelcerro | case ATTR_SHORTLABEL:
|
108 | return this.descriptor.getShortLabel(); |
||
109 | case ATTR_LABEL:
|
||
110 | return this.descriptor.getLabel(); |
||
111 | case ATTR_DESCRIPTION:
|
||
112 | return this.descriptor.getDescription(); |
||
113 | case ATTR_PROFILE:
|
||
114 | return this.descriptor.getDataProfileName(); |
||
115 | case ATTR_GROUP:
|
||
116 | return this.descriptor.getGroup(); |
||
117 | case ATTR_ORDER:
|
||
118 | return this.descriptor.getOder(); |
||
119 | case ATTR_ISPK:
|
||
120 | return this.descriptor.isPrimaryKey(); |
||
121 | case ATTR_ISFK:
|
||
122 | return this.descriptor.isForeingKey(); |
||
123 | case ATTR_ISCLOSEDLIST:
|
||
124 | if( this.descriptor.isForeingKey() ) { |
||
125 | return this.descriptor.getForeingKey().isClosedList(); |
||
126 | } |
||
127 | return false; |
||
128 | case ATTR_ISREADONLY:
|
||
129 | return this.descriptor.isReadOnly(); |
||
130 | case ATTR_ISCOMPUTED:
|
||
131 | return this.descriptor.isComputed(); |
||
132 | case ATTR_ISCAUTOMATIC:
|
||
133 | return this.descriptor.isAutomatic(); |
||
134 | case ATTR_ISHIDDEN:
|
||
135 | return this.descriptor.isHidden(); |
||
136 | case ATTR_ALLOWNULLS:
|
||
137 | return this.descriptor.allowNull(); |
||
138 | case ATTR_EXPRESSION: {
|
||
139 | FeatureAttributeEmulator x = this.descriptor.getFeatureAttributeEmulator();
|
||
140 | if( x instanceof FeatureAttributeEmulatorExpression ) { |
||
141 | Expression exp = ((FeatureAttributeEmulatorExpression) x).getExpression();
|
||
142 | if( exp != null ) { |
||
143 | return exp.getPhrase();
|
||
144 | } |
||
145 | } |
||
146 | return null; |
||
147 | } |
||
148 | case ATTR_FKTABLE:
|
||
149 | if( this.descriptor.isForeingKey() ) { |
||
150 | return this.descriptor.getForeingKey().getTableName(); |
||
151 | } |
||
152 | return null; |
||
153 | case ATTR_FKCODE:
|
||
154 | if( this.descriptor.isForeingKey() ) { |
||
155 | return this.descriptor.getForeingKey().getCodeName(); |
||
156 | } |
||
157 | return null; |
||
158 | case ATTR_FKLABEL:
|
||
159 | if( this.descriptor.isForeingKey() ) { |
||
160 | return this.descriptor.getForeingKey().getLabelFormula(); |
||
161 | } |
||
162 | return null; |
||
163 | |||
164 | |||
165 | default:
|
||
166 | return null; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | @Override
|
||
171 | public void set(String name, Object value) { |
||
172 | } |
||
173 | |||
174 | @Override
|
||
175 | public Object get(String name) { |
||
176 | int n = attrName2Index.get(name);
|
||
177 | return this.get(n); |
||
178 | } |
||
179 | |||
180 | @Override
|
||
181 | public void setOID(Object oid) { |
||
182 | } |
||
183 | |||
184 | @Override
|
||
185 | public Object getOID() { |
||
186 | 44454 | jjdelcerro | return (long) this.descriptor.getIndex(); |
187 | 44439 | jjdelcerro | } |
188 | |||
189 | @Override
|
||
190 | public FeatureType getType() {
|
||
191 | return featureTypeOfFeatureType;
|
||
192 | } |
||
193 | |||
194 | @Override
|
||
195 | public FeatureProvider getCopy() {
|
||
196 | return this; |
||
197 | } |
||
198 | |||
199 | @Override
|
||
200 | public Envelope getDefaultEnvelope() {
|
||
201 | return null; |
||
202 | } |
||
203 | |||
204 | @Override
|
||
205 | public Geometry getDefaultGeometry() {
|
||
206 | return null; |
||
207 | } |
||
208 | |||
209 | @Override
|
||
210 | public Time getDefaultTime() { |
||
211 | return null; |
||
212 | } |
||
213 | |||
214 | @Override
|
||
215 | public void setDefaultEnvelope(Envelope extent) { |
||
216 | } |
||
217 | |||
218 | @Override
|
||
219 | public void setDefaultGeometry(Geometry geom) { |
||
220 | } |
||
221 | |||
222 | @Override
|
||
223 | public void setDefaultTime(Time time) { |
||
224 | } |
||
225 | |||
226 | @Override
|
||
227 | public boolean isNull(int i) { |
||
228 | return false; |
||
229 | } |
||
230 | |||
231 | @Override
|
||
232 | public boolean isNull(String name) { |
||
233 | return false; |
||
234 | } |
||
235 | |||
236 | @Override
|
||
237 | public boolean isNew() { |
||
238 | return false; |
||
239 | } |
||
240 | |||
241 | @Override
|
||
242 | public void setNew(boolean isNew) { |
||
243 | } |
||
244 | |||
245 | @Override
|
||
246 | public void setExtraValue(int index, Object value) { |
||
247 | } |
||
248 | |||
249 | @Override
|
||
250 | public Object getExtraValue(int index) { |
||
251 | return null; |
||
252 | } |
||
253 | |||
254 | @Override
|
||
255 | public Object getExtraValue(String name) { |
||
256 | return null; |
||
257 | } |
||
258 | |||
259 | @Override
|
||
260 | public void setExtraValueNames(String[] extraValueNames) { |
||
261 | } |
||
262 | 44738 | jjdelcerro | |
263 | @Override
|
||
264 | public boolean hasExtraValue(String name) { |
||
265 | return false; |
||
266 | } |
||
267 | 44439 | jjdelcerro | |
268 | 44738 | jjdelcerro | |
269 | 44439 | jjdelcerro | } |
270 | |||
271 | private final FeatureType featureType; |
||
272 | |||
273 | public Features(FeatureType featureType) {
|
||
274 | this.featureType = featureType;
|
||
275 | } |
||
276 | |||
277 | @Override
|
||
278 | public FeatureProvider get(int index) { |
||
279 | return new MyFeatureProvider(this.featureType.get(index)); |
||
280 | } |
||
281 | |||
282 | @Override
|
||
283 | public int size() { |
||
284 | return this.featureType.size(); |
||
285 | } |
||
286 | |||
287 | } |
||
288 | |||
289 | private final FeatureType featureType; |
||
290 | private final ResourceProvider resource; |
||
291 | private final Double sourceID; |
||
292 | |||
293 | private FeatureTypeToStoreProviderAdapter(
|
||
294 | DataStoreParameters parameters, |
||
295 | DataStoreProviderServices storeServices, |
||
296 | FeatureType featureType |
||
297 | ) throws InitializeException {
|
||
298 | super(parameters, storeServices);
|
||
299 | this.featureType = featureType;
|
||
300 | this.data = new Features(featureType); |
||
301 | this.sourceID = Math.random(); |
||
302 | this.resource = createResource(
|
||
303 | MemoryResource.NAME, |
||
304 | new Object[] { NAME } |
||
305 | ); |
||
306 | |||
307 | if( featureTypeOfFeatureType==null ) { |
||
308 | this.initFeatureType();
|
||
309 | } |
||
310 | |||
311 | this.getStoreServices().setFeatureTypes(
|
||
312 | Collections.singletonList(featureTypeOfFeatureType),
|
||
313 | featureTypeOfFeatureType |
||
314 | ); |
||
315 | } |
||
316 | |||
317 | public static FeatureStore createFeatureStore(FeatureType featureType) { |
||
318 | try {
|
||
319 | DataStoreParameters parameters = FeatureTypeToStoreProviderAdapter.getParameters(featureType); |
||
320 | DefaultFeatureStore store = new DefaultFeatureStore();
|
||
321 | DataStoreProvider provider = new FeatureTypeToStoreProviderAdapter(
|
||
322 | parameters, |
||
323 | store, |
||
324 | featureType |
||
325 | ); |
||
326 | store.intialize(DALLocator.getDataManager(), parameters); |
||
327 | ((DataStoreInitializer2)store).setProvider(provider); |
||
328 | return store;
|
||
329 | } catch (Exception ex) { |
||
330 | return null; |
||
331 | } |
||
332 | |||
333 | } |
||
334 | |||
335 | private void initFeatureType() { |
||
336 | if( featureTypeOfFeatureType!=null ) { |
||
337 | return;
|
||
338 | } |
||
339 | EditableFeatureType ft = this.getStoreServices().createFeatureType(
|
||
340 | getName() |
||
341 | ); |
||
342 | ft.setHasOID(true);
|
||
343 | |||
344 | ft.add("name", DataTypes.STRING, 100); |
||
345 | ft.add("type", DataTypes.INT, 0); |
||
346 | ft.add("typeName", DataTypes.STRING, 100); |
||
347 | ft.add("size", DataTypes.INT, 0); |
||
348 | ft.add("precision", DataTypes.INT, 0); |
||
349 | ft.add("label", DataTypes.STRING, 100); |
||
350 | ft.add("shortlabel", DataTypes.STRING, 100); |
||
351 | ft.add("description", DataTypes.STRING, 1024); |
||
352 | ft.add("profile", DataTypes.STRING, 100); |
||
353 | ft.add("group", DataTypes.STRING, 100); |
||
354 | ft.add("order", DataTypes.INT, 0); |
||
355 | ft.add("isPk", DataTypes.BOOLEAN, 0); |
||
356 | ft.add("isFk", DataTypes.BOOLEAN, 0); |
||
357 | ft.add("isClosedList", DataTypes.BOOLEAN, 0); |
||
358 | ft.add("isReadOnly", DataTypes.BOOLEAN, 0); |
||
359 | ft.add("isComputed", DataTypes.BOOLEAN, 0); |
||
360 | ft.add("isAutomatic", DataTypes.BOOLEAN, 0); |
||
361 | ft.add("isHidden", DataTypes.BOOLEAN, 0); |
||
362 | ft.add("allowNulls", DataTypes.BOOLEAN, 0); |
||
363 | ft.add("expression", DataTypes.STRING, 1024); |
||
364 | ft.add("fkTable", DataTypes.STRING, 100); |
||
365 | ft.add("fkCode", DataTypes.STRING, 100); |
||
366 | ft.add("fkLabel", DataTypes.STRING, 1024); |
||
367 | |||
368 | featureTypeOfFeatureType = ft; |
||
369 | attrName2Index = new HashMap<>(); |
||
370 | for (FeatureAttributeDescriptor attr : ft) {
|
||
371 | attrName2Index.put(attr.getName(), attr.getIndex()); |
||
372 | } |
||
373 | } |
||
374 | |||
375 | private static MemoryStoreParameters getParameters(FeatureType featureType) { |
||
376 | MemoryStoreParameters params = new MemoryStoreParameters(NAME);
|
||
377 | params.setDynValue(MemoryStoreParameters.ORDER_PARAMETER_NAME, "name");
|
||
378 | return params;
|
||
379 | } |
||
380 | |||
381 | @Override
|
||
382 | public void open() throws OpenException { |
||
383 | } |
||
384 | |||
385 | @Override
|
||
386 | public FeatureProvider createFeatureProvider(FeatureType featureType)throws DataException { |
||
387 | return null; |
||
388 | } |
||
389 | |||
390 | @Override
|
||
391 | public ResourceProvider getResource() {
|
||
392 | return this.resource; |
||
393 | } |
||
394 | |||
395 | @Override
|
||
396 | public final Object getSourceId() { |
||
397 | return this.sourceID; |
||
398 | } |
||
399 | |||
400 | @Override
|
||
401 | public final String getProviderName() { |
||
402 | return NAME;
|
||
403 | } |
||
404 | |||
405 | @Override
|
||
406 | public final String getName() { |
||
407 | return this.featureType.getStore().getName() + "!" + this.featureType.getId(); |
||
408 | } |
||
409 | |||
410 | @Override
|
||
411 | public final String getFullName() { |
||
412 | return this.featureType.getStore().getFullName()+ "!" + this.featureType.getId(); |
||
413 | } |
||
414 | |||
415 | @Override
|
||
416 | public Object createNewOID() { |
||
417 | return null; |
||
418 | } |
||
419 | |||
420 | @Override
|
||
421 | public int getOIDType() { |
||
422 | return DataTypes.LONG;
|
||
423 | } |
||
424 | |||
425 | } |