Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / DataManager.java @ 23339

History | View | Annotate | Download (16.9 KB)

1
package org.gvsig.fmap.data;
2

    
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import org.gvsig.fmap.data.feature.expressionevaluator.ExpressionParser;
8
import org.gvsig.fmap.data.operation.DataStoreOperation;
9
import org.gvsig.fmap.data.operation.DataStoreOperationContext;
10
import org.gvsig.fmap.data.operation.DataStoreOperationException;
11
import org.gvsig.fmap.data.operation.DataStoreOperationNotSupportedException;
12

    
13
import com.iver.utiles.NotExistInXMLEntity;
14
import com.iver.utiles.XMLEntity;
15
import com.iver.utiles.XMLException;
16

    
17

    
18
public class DataManager {
19
        private static ArrayList registers = new ArrayList();
20

    
21
        private static DataManager manager = null;
22

    
23
        private ExpressionParser expressionParser = null;
24

    
25

    
26
        public static DataManager getManager() {
27
                if( manager == null ) {
28
                        manager = new DataManager();
29
                }
30
                return manager;
31
        }
32

    
33
        /**
34
         * 
35
         * @return ResourceManager
36
         */
37

    
38
        public static ResourceManager getResourceManager() {
39
                return ResourceManager.getResourceManager();
40
        }
41

    
42
        private List dataStoreOperations = new ArrayList();
43

    
44
        public DataManager() {
45
                //                org.gvsig.fmap.data.feature.driver.shp.Register.selfRegister();
46
                //                org.gvsig.fmap.data.feature.driver.Register.selfRegister();
47

    
48
//                org.gvsig.fmap.data.gml.Register.selfRegister();
49
        }
50

    
51
        public void registerGeometryHandler(String dataStoreID, Class geometryHandlerClass) {
52

    
53
        }
54

    
55
        /**
56
         * 
57
         * @param String
58
         *            name
59
         * @param Class
60
         *            dataStoreClass
61
         * @param Class
62
         *            parametersClass
63
         */
64

    
65
        public void registerDataStore(String name, Class dataStoreClass, Class parametersClass) {
66
                RegisterInfo register = new RegisterInfo(RegisterInfo.TYPE_STORE, name,
67
                                dataStoreClass, parametersClass);
68
                if (registers.contains(register)){
69
                        return;
70
                }
71
                registers.add(register);
72
        }
73

    
74
        /**
75
         * Levanta una instancia de los parametros solicitados inicializa el nombre
76
         * en la instancia y la devuelve.
77
         * 
78
         * @throws InitializeException
79
         *             TODO
80
         **/
81
        /**
82
         * Start a instance of the solicited parameters inicialize the instance name
83
         * and returns it.
84
         * 
85
         * @param String
86
         *            name
87
         * @throws InitializeException
88
         *             TODO
89
         **/
90
        public DataStoreParameters createDataStoreParameters(String name) throws InitializeException{
91

    
92
                RegisterInfo registerInfo = this.getRegisterByName(name);
93
                if (registerInfo == null){
94
                        throw new InitializeException("Not registered in manager",name);
95
                } else if (registerInfo.type != RegisterInfo.TYPE_STORE) {
96
                        throw new InitializeException("Not register as Store", name);
97
                }
98

    
99
                try {
100
                        return (DataStoreParameters)registerInfo.parametersClazz.newInstance();
101
                } catch (InstantiationException e) {
102
                        throw new InitializeException(name,e);
103
                } catch (IllegalAccessException e) {
104
                        throw new InitializeException(name,e);
105
                }
106
        }
107

    
108

    
109

    
110
        public DataStoreParameters createDataStoreParameters(XMLEntity xmlEntity)
111
                        throws InitializeException {
112
                String name;
113
                try {
114
                        name = xmlEntity.getStringProperty("dataStoreName");
115
                } catch (NotExistInXMLEntity e) {
116
                        throw new InitializeException("dataStoreName property not set",
117
                                        "DataManager");
118
                }
119

    
120
                DataStoreParameters params = this.createDataStoreParameters(name);
121

    
122
                params.loadFromXMLEntity(xmlEntity);
123
                return params;
124

    
125
        }
126

    
127
        /**
128
         * Levanta la instancia del datasource,
129
         * levanta una instancia del driver que precisa el datasource
130
         * y por ultimo invoca al metodo init del datasource.
131
         * @throws InitializeException
132
         **/
133
        /**
134
         * Start a instance of datasource, start a instance of driver that
135
         * datasource needs at ends invoke datasource init method
136
         * 
137
         * @params DataStoreParameters parameters
138
         * @throws InitializeException
139
         **/
140
        public DataStore createDataStore(DataStoreParameters parameters) throws InitializeException {
141
                String name = parameters.getDataStoreName();
142

    
143
                RegisterInfo registerInfo = this.getRegisterByName(name);
144
                if (registerInfo == null){
145
                        throw new InitializeException("Not registered in manager",name);
146
                }
147

    
148
                try {
149
                        DataStore dataStore= (DataStore)registerInfo.clazz.newInstance();
150
                        dataStore.init(parameters);
151
                        return dataStore;
152
                } catch (InstantiationException e) {
153
                        throw new InitializeException(name,e);
154
                } catch (IllegalAccessException e) {
155
                        throw new InitializeException(name,e);
156
                }
157
        }
158

    
159
        public DataStore createDataStore(XMLEntity xmlEntity)
160
                        throws InitializeException {
161
                String name;
162
                try {
163
                        name = xmlEntity.getStringProperty("dataStoreName");
164
                } catch (NotExistInXMLEntity e) {
165
                        throw new InitializeException("dataStoreName property not set",
166
                                        "DataManager");
167
                }
168

    
169
                RegisterInfo registerInfo = this.getRegisterByName(name);
170
                if (registerInfo == null) {
171
                        throw new InitializeException("Not registered in manager", name);
172
                }
173

    
174
                try {
175
                        DataStore dataStore = (DataStore) registerInfo.clazz.newInstance();
176
                        dataStore.setXMLEntity(xmlEntity);
177
                        return dataStore;
178
                } catch (InstantiationException e) {
179
                        throw new InitializeException(name, e);
180
                } catch (IllegalAccessException e) {
181
                        throw new InitializeException(name, e);
182
                } catch (XMLException e) {
183
                        throw new InitializeException(name, e);
184
                }
185
        }
186

    
187

    
188
        /** Como conjunto de propiedades genericas a un tipo de DataStore
189
         * a las que se puede acceder sin tener que crear un DataStore/Driver.
190
         *
191
         * Por ejemplo para "DriverDataSource.shp" podria tener el
192
         * tamaƱo de identificador de campo.
193
         *
194
         * En "DriverDataSource.postgres" podria aportar informacion sobre
195
         * las tablas disponibles o cosas asi. Hay que ver que precisa
196
         * GeoDB y de donde lo puede obtener.
197
         *
198
         * Hay que pensarlo bien.
199
         *
200
         */
201

    
202
        /**
203
         * Collection of generic properties of a DataStore type which can access
204
         * without create a DataStore/Driver.
205
         * 
206
         * For example for "DriverDataSource.shp" could have the size of the data
207
         * identification.
208
         * 
209
         * In "DriverDataSource.postgres" could add information about availables
210
         * tables or things like that. We`ll have to see what GeoDB needs and where
211
         * it can get.
212
         * 
213
         * We have to think it good.
214
         */
215
        /**
216
         * @param String name
217
         * @param Class dataSourceClass
218
         * @param Class parametersClass
219
         */
220

    
221

    
222
        public void registerDataExplorer(String name, Class dataSourceClass, Class parametersClass) {
223
                RegisterInfo register = new RegisterInfo(RegisterInfo.TYPE_EXPLORER,
224
                                name, dataSourceClass, parametersClass);
225
                if (registers.contains(register)){
226
                        return;
227
                }
228
                registers.add(register);
229
        }
230

    
231
        /**
232
         * Levanta una instancia de los parametros solicitados
233
         * inicializa el nombre en la instancia
234
         * y la devuelve.
235
         * @throws InitializeException TODO
236
         **/
237
        /**
238
         * Start a instance with solicited parameters starts name in the instance
239
         * and returns it.
240
         * 
241
         * @param String
242
         *            name
243
         * @throws InitializeException
244
         *             TODO
245
         **/
246

    
247
        public DataExplorerParameters createDataExplorerParameters(String name) throws InitializeException {
248
                RegisterInfo registerInfo = this.getRegisterByName(name);
249
                if (registerInfo == null){
250
                        throw new InitializeException("Not registered in manager",name);
251
                } else if (registerInfo.type != RegisterInfo.TYPE_EXPLORER) {
252
                        throw new InitializeException("Not register as Explorer", name);
253
                }
254

    
255
                try {
256
                        return (DataExplorerParameters)registerInfo.parametersClazz.newInstance();
257
                } catch (InstantiationException e) {
258
                        throw new InitializeException(name,e);
259
                } catch (IllegalAccessException e) {
260
                        throw new InitializeException(name,e);
261
                } catch (Exception e) {
262
                        // TODO: de momento para los class en el caso de que no exista el explorer.
263
                        throw new InitializeException(name, e);
264
                }
265
        }
266

    
267
        /**
268
         * 
269
         * @param DataExplorerParameters
270
         *            parameters
271
         * @return DataExplorer
272
         * @throws InitializeException
273
         */
274

    
275
        public DataExplorerParameters createDataExplorerParameters(
276
                        XMLEntity xmlEntity) throws InitializeException {
277
                String name = null;
278
                try {
279
                        name = xmlEntity.getStringProperty("dataExplorerName");
280
                } catch (NotExistInXMLEntity e) {
281
                        throw new InitializeException("dataExplorerName property not set",
282
                                        "DataManager");
283
                }
284
                DataExplorerParameters params = this.createDataExplorerParameters(name);
285

    
286
                params.loadFromXMLEntity(xmlEntity);
287

    
288
                return params;
289
        }
290

    
291
        public DataExplorer createDataExplorer(DataExplorerParameters parameters) throws InitializeException {
292
                RegisterInfo registerInfo = this.getRegisterByName(parameters.getDataExplorerName());
293
                if (registerInfo == null){
294
                        throw new InitializeException("Not registered in manager",parameters.getDataExplorerName());
295
                }
296
                try {
297
                        DataExplorer dataSource= (DataExplorer)registerInfo.clazz.newInstance();
298
                        dataSource.init(parameters);
299
                        return dataSource;
300
                } catch (InstantiationException e) {
301
                        throw new InitializeException(parameters.getDataExplorerName(),e);
302
                } catch (IllegalAccessException e) {
303
                        throw new InitializeException(parameters.getDataExplorerName(),e);
304
                }
305
        }
306

    
307
        /**
308
         * 
309
         * @param String
310
         *            name
311
         * @return RegisterInfo
312
         */
313

    
314

    
315
        public DataExplorer createDataExplorer(XMLEntity xmlEntity)
316
                        throws InitializeException {
317

    
318
                String name;
319
                try {
320
                        name = xmlEntity.getStringProperty("dataExplorerName");
321
                } catch (NotExistInXMLEntity e) {
322
                        throw new InitializeException("dataExplorerName property not set",
323
                                        "DataManager");
324
                }
325

    
326
                RegisterInfo registerInfo = this.getRegisterByName(name);
327
                if (registerInfo == null) {
328
                        throw new InitializeException("Not registered in manager",
329
                                        name);
330
                }
331
                try {
332
                        DataExplorer dataSource = (DataExplorer) registerInfo.clazz
333
                                        .newInstance();
334
                        dataSource.init(xmlEntity);
335
                        return dataSource;
336
                } catch (InstantiationException e) {
337
                        throw new InitializeException(name, e);
338
                } catch (IllegalAccessException e) {
339
                        throw new InitializeException(name, e);
340
                }
341
        }
342

    
343
        private RegisterInfo getRegisterByName(String name){
344
                Iterator iterator=registers.iterator();
345
                while (iterator.hasNext()) {
346
                        RegisterInfo registerInfo = (RegisterInfo) iterator.next();
347
                        if (name.equals(registerInfo.name)){
348
                                return registerInfo;
349
                        }
350
                }
351
                return null;
352
        }
353

    
354
        /**
355
         * 
356
         * @param String
357
         *            storeName
358
         * @param String
359
         *            operationName
360
         * @param DataStoreOperation
361
         *            operation
362
         * @return int index
363
         * @throws IllegalArgumentException
364
         */
365

    
366

    
367

    
368
        public int registerDataStoreOperation(String storeName, String operationName, DataStoreOperation operation) {
369
                if (operationName == null){
370
                        throw new IllegalArgumentException("operationName cannot be null.");
371
                }
372
                if (operation == null){
373
                        throw new IllegalArgumentException("operation cannot be null.");
374
                }
375

    
376
                RegisterInfo register = this.getRegisterByName(storeName);
377
                if (register == null){
378
                        throw new IllegalArgumentException(storeName + " not registered");
379
                }
380
                int index = registerGeometryOperationName(operationName);
381

    
382
                register.registerOperation(index, operation);
383

    
384
                return index;
385
        }
386

    
387
        /**
388
         * 
389
         * @param String
390
         *            operationName
391
         * @return int index
392
         * @throws IllegalArgumentException
393
         */
394

    
395
        private int registerGeometryOperationName(String operationName) {
396
                if (operationName == null) {
397
                        throw new IllegalArgumentException("operationName cannot be null.");
398
                }
399

    
400
                int index = dataStoreOperations .indexOf(operationName);
401
                if (index == -1) {
402
                        dataStoreOperations.add(operationName);
403
                        index = dataStoreOperations.indexOf(operationName);
404
                }
405
                return index;
406
        }
407

    
408
        /**
409
         * 
410
         * @param String
411
         *            operationName
412
         * @return int index
413
         */
414

    
415
        private int getOperationIndex(String operationName) {
416
                return  dataStoreOperations .indexOf(operationName);
417
        }
418

    
419
        /**
420
         * 
421
         * @param DataStore
422
         *            store
423
         * @param int code
424
         * @param DataStoreOperationContext
425
         *            context
426
         * @return Object
427
         * @throws DataStoreOperationException
428
         * @throws DataStoreOperationNotSupportedException
429
         */
430

    
431
        public Object invokeDataStoreOperation(DataStore store,int code,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{
432
                if (code < 0 || code >= dataStoreOperations.size()){
433
                        throw new DataStoreOperationNotSupportedException(code,"{not registered}",store.getName());
434
                }
435
                RegisterInfo register = this.getRegisterByName(store.getName());
436
                DataStoreOperation operation = register.getOperation(code);
437
                if (operation == null){
438
                        throw new DataStoreOperationNotSupportedException(code,(String)this.dataStoreOperations.get(code),store.getName());
439
                }
440
                return operation.invoke(store, context);
441

    
442
        }
443

    
444
        /**
445
         * 
446
         * @param DataStore
447
         *            store
448
         * @param String
449
         *            operationName
450
         * @param DataStoreOperationContext
451
         *            context
452
         * @return Object
453
         * @throws DataStoreOperationException
454
         * @throws DataStoreOperationNotSupportedException
455
         */
456

    
457
        public Object invokeDataStoreOperation(DataStore store,String operationName,DataStoreOperationContext context) throws DataStoreOperationException, DataStoreOperationNotSupportedException{
458
                return this.invokeDataStoreOperation(store, this.getOperationIndex(operationName), context);
459
        }
460

    
461
        private class RegisterInfo{
462
                public static final String TYPE_STORE = "store";
463
                public static final String TYPE_EXPLORER = "explorer";
464

    
465
                private String name;
466
                private String type;
467
                private Class clazz;
468
                private Class parametersClazz;
469
                private List operations = new ArrayList();
470

    
471
                /**
472
                 * 
473
                 * @param String
474
                 *            type
475
                 * @param String
476
                 *            name
477
                 * @param Class
478
                 *            dsc
479
                 * @param Class
480
                 *            pc
481
                 */
482

    
483
                public RegisterInfo(String type, String name, Class dsc, Class pc) {
484
                        this.type = type;
485
                        this.name=name;
486
                        this.clazz=dsc;
487
                        this.parametersClazz=pc;
488
                }
489

    
490
                /*
491
                 * (non-Javadoc)
492
                 * @see java.lang.Object#equals(java.lang.Object)
493
                 */
494

    
495
                public boolean equals(Object obj) {
496
                        if (obj instanceof RegisterInfo){
497
                                RegisterInfo ri = (RegisterInfo)obj;
498
                                return ri.name.equals(this.name) &&
499
                                        ri.clazz.equals(this.clazz) &&
500
                                        ri.parametersClazz.equals(this.parametersClazz);
501
                        }
502
                        return super.equals(obj);
503
                }
504

    
505
                /**
506
                 * 
507
                 * @param int index
508
                 * @param DataStoreOperation
509
                 *            operation
510
                 */
511

    
512
                public void registerOperation(int index, DataStoreOperation operation){
513

    
514
                        while (index > operations.size()) {
515
                                operations.add(null);
516
                        }
517

    
518
                        if (index == operations.size()) {
519
                                operations.add(operation);
520
                        } else {
521
                                operations.set(index, operation);
522
                        }
523
                }
524

    
525
                /**
526
                 * 
527
                 * @param int index
528
                 * @return DataStoreOperation
529
                 */
530

    
531
                public DataStoreOperation getOperation(int index){
532
                        try{
533
                                return (DataStoreOperation) this.operations.get(index);
534
                        }catch (IndexOutOfBoundsException  e) {
535
                                return null;
536
                        }
537
                }
538

    
539

    
540
        }
541

    
542
        /**
543
         * 
544
         * @param String
545
         *            toreName
546
         * @param String
547
         *            operationName
548
         * @return boolean
549
         */
550

    
551
        public boolean implementsDataStoreOperation(
552
                        String storeName, String operationName) {
553
                return this.implementsDataStoreOperation(storeName, this.getOperationIndex(operationName));
554
        }
555

    
556
        /**
557
         * 
558
         * @param String
559
         *            storeName
560
         * @param int operationCode
561
         * @return boolean
562
         */
563

    
564
        public boolean implementsDataStoreOperation(
565
                        String storeName, int operationCode) {
566
                RegisterInfo register = this.getRegisterByName(storeName);
567
                DataStoreOperation operation = register.getOperation(operationCode);
568
                if (operation == null){
569
                        return false;
570
                }
571
                return true;
572
        }
573

    
574
        /**
575
         * 
576
         * @return String[] reg
577
         */
578

    
579
        public String[] getRegisters() {
580
                String[] reg=new String[registers.size()];
581
                for (int i = 0; i < registers.size(); i++) {
582
                        reg[i]=((RegisterInfo)registers.get(i)).name;
583
                }
584
                return reg;
585
        }
586

    
587
        /**
588
         * 
589
         * @return String[] TYPE_STORE
590
         */
591

    
592
        public String[] getRegistersStores() {
593
                return this.getRegisterOfType(RegisterInfo.TYPE_STORE);
594
        }
595

    
596
        /**
597
         * 
598
         * @return String[] TYPE_EXPLORER
599
         */
600

    
601
        public String[] getRegistersExplorers() {
602
                return this.getRegisterOfType(RegisterInfo.TYPE_EXPLORER);
603
        }
604

    
605
        /**
606
         * 
607
         * @param String
608
         *            type
609
         * @return String[]
610
         */
611

    
612
        private String[] getRegisterOfType(String type) {
613
                List reg = new ArrayList();
614
                RegisterInfo info = null;
615
                for (int i = 0; i < registers.size(); i++) {
616
                        info =(RegisterInfo) registers.get(i);
617
                        if (info.type == type) {
618
                                reg.add(info.name);
619
                        }
620
                }
621
                return (String[]) reg.toArray(new String[0]);
622

    
623
        }
624

    
625

    
626
        /**
627
         * @param expressionParser the expressionParser to set
628
         */
629
        public void setExpressionParser(ExpressionParser expressionParser) {
630
                this.expressionParser = expressionParser;
631
        }
632

    
633
        /**
634
         * @return the expressionParser
635
         */
636
        public ExpressionParser getExpressionParser() {
637
                return expressionParser;
638
        }
639

    
640

    
641

    
642

    
643
}