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