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 @ 47436
History | View | Annotate | Download (14.2 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 | 47436 | fdiaz | |
268 | @Override
|
||
269 | public boolean isBroken() { |
||
270 | return false; |
||
271 | } |
||
272 | 44439 | jjdelcerro | |
273 | 44738 | jjdelcerro | |
274 | 44439 | jjdelcerro | } |
275 | |||
276 | private final FeatureType featureType; |
||
277 | |||
278 | public Features(FeatureType featureType) {
|
||
279 | this.featureType = featureType;
|
||
280 | } |
||
281 | |||
282 | @Override
|
||
283 | public FeatureProvider get(int index) { |
||
284 | return new MyFeatureProvider(this.featureType.get(index)); |
||
285 | } |
||
286 | |||
287 | @Override
|
||
288 | public int size() { |
||
289 | return this.featureType.size(); |
||
290 | } |
||
291 | |||
292 | } |
||
293 | |||
294 | private final FeatureType featureType; |
||
295 | private final ResourceProvider resource; |
||
296 | private final Double sourceID; |
||
297 | |||
298 | private FeatureTypeToStoreProviderAdapter(
|
||
299 | DataStoreParameters parameters, |
||
300 | DataStoreProviderServices storeServices, |
||
301 | FeatureType featureType |
||
302 | ) throws InitializeException {
|
||
303 | super(parameters, storeServices);
|
||
304 | this.featureType = featureType;
|
||
305 | this.data = new Features(featureType); |
||
306 | this.sourceID = Math.random(); |
||
307 | this.resource = createResource(
|
||
308 | MemoryResource.NAME, |
||
309 | new Object[] { NAME } |
||
310 | ); |
||
311 | |||
312 | if( featureTypeOfFeatureType==null ) { |
||
313 | this.initFeatureType();
|
||
314 | } |
||
315 | |||
316 | this.getStoreServices().setFeatureTypes(
|
||
317 | Collections.singletonList(featureTypeOfFeatureType),
|
||
318 | featureTypeOfFeatureType |
||
319 | ); |
||
320 | } |
||
321 | |||
322 | public static FeatureStore createFeatureStore(FeatureType featureType) { |
||
323 | try {
|
||
324 | DataStoreParameters parameters = FeatureTypeToStoreProviderAdapter.getParameters(featureType); |
||
325 | DefaultFeatureStore store = new DefaultFeatureStore();
|
||
326 | DataStoreProvider provider = new FeatureTypeToStoreProviderAdapter(
|
||
327 | parameters, |
||
328 | store, |
||
329 | featureType |
||
330 | ); |
||
331 | store.intialize(DALLocator.getDataManager(), parameters); |
||
332 | ((DataStoreInitializer2)store).setProvider(provider); |
||
333 | return store;
|
||
334 | } catch (Exception ex) { |
||
335 | return null; |
||
336 | } |
||
337 | |||
338 | } |
||
339 | |||
340 | private void initFeatureType() { |
||
341 | if( featureTypeOfFeatureType!=null ) { |
||
342 | return;
|
||
343 | } |
||
344 | EditableFeatureType ft = this.getStoreServices().createFeatureType(
|
||
345 | getName() |
||
346 | ); |
||
347 | ft.setHasOID(true);
|
||
348 | |||
349 | ft.add("name", DataTypes.STRING, 100); |
||
350 | ft.add("type", DataTypes.INT, 0); |
||
351 | ft.add("typeName", DataTypes.STRING, 100); |
||
352 | ft.add("size", DataTypes.INT, 0); |
||
353 | ft.add("precision", DataTypes.INT, 0); |
||
354 | ft.add("label", DataTypes.STRING, 100); |
||
355 | ft.add("shortlabel", DataTypes.STRING, 100); |
||
356 | ft.add("description", DataTypes.STRING, 1024); |
||
357 | ft.add("profile", DataTypes.STRING, 100); |
||
358 | ft.add("group", DataTypes.STRING, 100); |
||
359 | ft.add("order", DataTypes.INT, 0); |
||
360 | ft.add("isPk", DataTypes.BOOLEAN, 0); |
||
361 | ft.add("isFk", DataTypes.BOOLEAN, 0); |
||
362 | ft.add("isClosedList", DataTypes.BOOLEAN, 0); |
||
363 | ft.add("isReadOnly", DataTypes.BOOLEAN, 0); |
||
364 | ft.add("isComputed", DataTypes.BOOLEAN, 0); |
||
365 | ft.add("isAutomatic", DataTypes.BOOLEAN, 0); |
||
366 | ft.add("isHidden", DataTypes.BOOLEAN, 0); |
||
367 | ft.add("allowNulls", DataTypes.BOOLEAN, 0); |
||
368 | ft.add("expression", DataTypes.STRING, 1024); |
||
369 | ft.add("fkTable", DataTypes.STRING, 100); |
||
370 | ft.add("fkCode", DataTypes.STRING, 100); |
||
371 | ft.add("fkLabel", DataTypes.STRING, 1024); |
||
372 | |||
373 | featureTypeOfFeatureType = ft; |
||
374 | attrName2Index = new HashMap<>(); |
||
375 | for (FeatureAttributeDescriptor attr : ft) {
|
||
376 | attrName2Index.put(attr.getName(), attr.getIndex()); |
||
377 | } |
||
378 | } |
||
379 | |||
380 | private static MemoryStoreParameters getParameters(FeatureType featureType) { |
||
381 | MemoryStoreParameters params = new MemoryStoreParameters(NAME);
|
||
382 | params.setDynValue(MemoryStoreParameters.ORDER_PARAMETER_NAME, "name");
|
||
383 | return params;
|
||
384 | } |
||
385 | |||
386 | @Override
|
||
387 | public void open() throws OpenException { |
||
388 | } |
||
389 | |||
390 | @Override
|
||
391 | public FeatureProvider createFeatureProvider(FeatureType featureType)throws DataException { |
||
392 | return null; |
||
393 | } |
||
394 | |||
395 | @Override
|
||
396 | public ResourceProvider getResource() {
|
||
397 | return this.resource; |
||
398 | } |
||
399 | |||
400 | @Override
|
||
401 | public final Object getSourceId() { |
||
402 | return this.sourceID; |
||
403 | } |
||
404 | |||
405 | @Override
|
||
406 | public final String getProviderName() { |
||
407 | return NAME;
|
||
408 | } |
||
409 | |||
410 | @Override
|
||
411 | public final String getName() { |
||
412 | return this.featureType.getStore().getName() + "!" + this.featureType.getId(); |
||
413 | } |
||
414 | |||
415 | @Override
|
||
416 | public final String getFullName() { |
||
417 | return this.featureType.getStore().getFullName()+ "!" + this.featureType.getId(); |
||
418 | } |
||
419 | |||
420 | @Override
|
||
421 | public Object createNewOID() { |
||
422 | return null; |
||
423 | } |
||
424 | |||
425 | @Override
|
||
426 | public int getOIDType() { |
||
427 | return DataTypes.LONG;
|
||
428 | } |
||
429 | |||
430 | } |