Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.lib / src / main / java / org / gvsig / fmap / dal / store / db / DBStoreParameters.java @ 45652

History | View | Annotate | Download (15.8 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.db;
26

    
27
import org.cresques.cts.IProjection;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
30
import org.gvsig.fmap.dal.serverexplorer.db.DBConnectionParameter;
31
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
32
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.tools.dynobject.DelegatedDynObject;
36

    
37
/**
38
 * Abstract Data base Store Parameters
39
 *
40
 * @author jmvivo
41
 *
42
 */
43
public abstract class DBStoreParameters extends AbstractDataParameters
44
                implements
45
                OpenFeatureStoreParameters, DBConnectionParameter {
46

    
47
        public static final String PARAMETERS_DEFINITION_NAME = "DBStoreParameters";
48

    
49
        /**
50
         * Parameter name for <code>sql</code><br>
51
         *
52
         * @see #getSQL()
53
         * @see #setSQL(String)
54
         */
55
        public static final String SQL_PARAMTER_NAME = "SQL";
56

    
57
        /**
58
         * Parameter name for <code>fields</code><br>
59
         *
60
         * @see #getFields()
61
         * @see #getFieldsString()
62
         * @see #setFields(String)
63
         * @see #setFields(String[])
64
         */
65
        public static final String FIELDS_PARAMTER_NAME = "Fields";
66

    
67
        /**
68
         * Parameter name for <code>initial filter</code><br>
69
         *
70
         * @see #getBaseFilter()
71
         * @see #setBaseFilter(String)
72
         */
73
        public static final String BASEFILTER_PARAMTER_NAME = "BaseFilter";
74

    
75
        /**
76
         * Parameter name for <code>initial order</code><br>
77
         *
78
         * @see #getBaseOrder()
79
         * @see #setBaseOrder(String)
80
         */
81
        public static final String BASEORDER_PARAMTER_NAME = "BaseOrder";
82

    
83
        /**
84
         * Parameter name for <code>pk fields</code><br>
85
         *
86
         * @see #getPkFields()
87
         * @see #getPkFieldsString()
88
         * @see #setPkFields(String)
89
         * @see #setPkFields(String[])
90
         */
91
        public static final String PKFIELDS_PARAMTER_NAME = "PKFields";
92

    
93
        /**
94
         * Parameter name for <code>default geometry</code><br>
95
         *
96
         * @see #getDefaultGeometryField()
97
         * @see #setDefaultGeometryField(String)
98
         */
99
        public static final String DEFAULTGEOMETRY_PARAMTER_NAME = "DefaultGeometryField";
100

    
101
        /**
102
         * Parameter name for the name of <code>table</code><br>
103
         *
104
         * @see #getTable()
105
         * @see #setTable(String)
106
         */
107
        public static final String TABLE_PARAMTER_NAME = "Table";
108

    
109
        /**
110
         * Parameter name for <code>working area</code><br>O
111
         *
112
         * @see #getWorkingArea()
113
         * @see #setWorkingArea(Envelope)
114
         */
115
        public static final String WORKINGAREA_PARAMTER_NAME = "Workingarea";
116

    
117

    
118
        /**
119
         * Parameter name for <code>CRS</code><br>
120
         * 
121
         * @see #setSRSID(String)
122
         * @see #getSRS()
123
         * @see #setSRS(IProjection)
124
         */
125
        public static final String CRS_PARAMTER_NAME = "CRS";
126

    
127
        /**
128
         * Parameter name for <code>GeometryType</code><br>
129
         * 
130
         * @see #setGeometryType(int)
131
         * @see #getGeometryType()
132
         */
133
        public static final String GEOMETRYTYPE_PARAMTER_NAME = "GeometryType";
134
        /**
135
         * Parameter name for <code>GeometrySubtype</code><br>
136
         * 
137
         * @see #setGeometrySubtype(int)
138
         * @see #getGeometrySubtype()
139
         */
140
        public static final String GEOMETRYSUBTYPE_PARAMTER_NAME = "GeometrySubtype";
141

    
142
        /**
143
         * This instance contains the value of the current parameters.
144
         */
145
        private DelegatedDynObject parameters;
146

    
147
        public DBStoreParameters(String parametersDefinitionName, String providerName) {
148
                this.parameters  = (DelegatedDynObject) DBHelper.newParameters(parametersDefinitionName);
149
                this.setDynValue( DataStoreProviderServices.PROVIDER_PARAMTER_NAME, providerName);
150
        }
151
        
152
        protected DelegatedDynObject getDelegatedDynObject() {
153
                return parameters;
154
        }
155

    
156
        public String getDataStoreName() {
157
                return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
158
        }
159

    
160
        public String getDescription() {
161
                return this.getDynClass().getDescription();
162
        }
163

    
164
        public boolean isValid() {
165
                return this.getHost() != null;
166
        }
167

    
168
        public String getHost() {
169
                return (String) this.getDynValue(HOST_PARAMTER_NAME);
170
        }
171

    
172
        public Integer getPort() {
173
                return (Integer) this.getDynValue(PORT_PARAMTER_NAME);
174
        }
175

    
176
        public String getDBName() {
177
                return (String) this.getDynValue(DBNAME_PARAMTER_NAME);
178
        }
179

    
180
        public String getUser() {
181
                return (String) this.getDynValue(USER_PARAMTER_NAME);
182
        }
183

    
184
        public String getPassword() {
185
                return (String) this.getDynValue(PASSWORD_PARAMTER_NAME);
186
        }
187

    
188
        public void setHost(String host) {
189
                this.setDynValue(HOST_PARAMTER_NAME, host);
190
        }
191

    
192
        public void setPort(int port) {
193
                this.setDynValue(PORT_PARAMTER_NAME, new Integer(port));
194
        }
195

    
196
        /**
197
         * Set <code>port/code> parameter value
198
         *
199
         * @param port
200
         */
201
        public void setPort(Integer port) {
202
                this.setDynValue(PORT_PARAMTER_NAME, port);
203
        }
204

    
205
        /**
206
         * Set <code>data base name/code> parameter value
207
         *
208
         * @param data
209
         *            base name
210
         */
211
        public void setDBName(String dbName) {
212
                this.setDynValue(DBNAME_PARAMTER_NAME, dbName);
213
        }
214

    
215
        /**
216
         * Set <code>user/code> parameter value
217
         *
218
         * @param user
219
         */
220
        public void setUser(String user) {
221
                this.setDynValue(USER_PARAMTER_NAME, user);
222
        }
223

    
224
        /**
225
         * Set <code>password/code> parameter value
226
         *
227
         * @param password
228
         */
229
        public void setPassword(String password) {
230
                this.setDynValue(PASSWORD_PARAMTER_NAME, password);
231
        }
232

    
233
        /**
234
         * Get <code>table</code> parameter value<br>
235
         * <br>
236
         *
237
         * This parameters describes what table or view we want to connect.<br>
238
         *
239
         * Not used if <code>sql</code> parameter set.
240
         *
241
         *
242
         * @param password
243
         *
244
         * @see #setTable(String)
245
         * @see #getSQL()
246
         * @see #setSQL(String)
247
         */
248
        public String getTable() {
249
                return (String) this.getDynValue(TABLE_PARAMTER_NAME);
250
        }
251

    
252
        /**
253
         * Set <code>table</code> parameter value<br>
254
         * <br>
255
         *
256
         * This parameters describes what table or view we want to connect.<br>
257
         *
258
         * Not used if <code>sql</code> parameter set.
259
         *
260
         *
261
         * @param password
262
         *
263
         * @see #getTable(String)
264
         * @see #getSQL()
265
         * @see #setSQL(String)
266
         */
267
        public void setTable(String table) {
268
                this.setDynValue(TABLE_PARAMTER_NAME, table);
269
        }
270

    
271
        /**
272
         * Get a comma separated list of the field names that we want to use.<br>
273
         * <br>
274
         *
275
         * A <code>null</code> or empty string means that we want all fields
276
         * available.
277
         *
278
         * @return
279
         * @see #getFields()
280
         * @see #setFields(String)
281
         * @see #setFields(String[])
282
         */
283
        public String getFieldsString() {
284
                return (String) this.getDynValue(FIELDS_PARAMTER_NAME);
285
        }
286

    
287
        /**
288
         * Get an array of the field names that we want to use.<br>
289
         * <br>
290
         *
291
         * A <code>null</code> means that we want all fields available.
292
         *
293
         * @return
294
         * @see #getFieldsString()
295
         * @see #setFields(String)
296
         * @see #setFields(String[])
297
         */
298
        public String[] getFields() {
299
                String fields = (String) this.getDynValue(FIELDS_PARAMTER_NAME);
300
                if (fields == null) {
301
                        return null;
302
                }
303
                // FIXME check for fields with spaces and special chars
304
                return fields.split(",");
305
        }
306

    
307
        /**
308
         * Set a comma separated list of the field names that we want to use.<br>
309
         * <br>
310
         *
311
         * A <code>null</code> means that we want all fields available.
312
         *
313
         * @return
314
         * @see #getFields()
315
         * @see #getFieldsString()
316
         * @see #setFields(String[])
317
         */
318
        public void setFields(String fields) {
319
                this.setDynValue(FIELDS_PARAMTER_NAME, fields);
320
        }
321

    
322
        /**
323
         * Set an array of the field names that we want to use.<br>
324
         * <br>
325
         *
326
         * A <code>null</code> means that we want all fields available.
327
         *
328
         * @return
329
         * @see #getFieldsString()
330
         * @see #getFields()
331
         * @see #setFields(String)
332
         */
333
        public void setFields(String[] fields) {
334
                StringBuilder str = new StringBuilder();
335
                for (int i = 0; i < fields.length - 1; i++) {
336
                        str.append(fields[i]);
337
                        str.append(",");
338
                }
339
                str.append(fields.length - 1);
340

    
341
                this.setDynValue(FIELDS_PARAMTER_NAME, fields);
342
        }
343

    
344
        /**
345
         * Get the SQL to use as source of the store instead a table or a view.<br>
346
         * <br>
347
         *
348
         * If this property is set the store changes his work flow:
349
         * <ul>
350
         * <li>store is in <i>read only</i> mode.</li>
351
         * <li><code>table</code> property is ignored.</li>
352
         * <li><code>pkFields</code> is not identified automatically</li>
353
         * <li>filter and order will be resolved locally <i>(pour performance)</i></li>
354
         * </ul>
355
         *
356
         * @return sql to use
357
         *
358
         * @see #getTable()
359
         * @see #setTable()
360
         * @see #getPkFields()
361
         * @see #setPkFields(String)
362
         */
363
        public String getSQL() {
364
                return (String) this.getDynValue(SQL_PARAMTER_NAME);
365
        }
366

    
367
        /**
368
         * Set the SQL to use as source of the store instead a table or a view.<br>
369
         * <br>
370
         * <strong>Note:</strong>see {@link #getSQL()} for description
371
         *
372
         * @see #getSQL()
373
         * @see #getTable()
374
         * @see #setTable()
375
         * @see #getPkFields()
376
         * @see #setPkFields(String)
377
         */
378
        public void setSQL(String sql) {
379
                this.setDynValue(SQL_PARAMTER_NAME, sql);
380
        }
381

    
382
        /**
383
         * Get initial filter to use.<br>
384
         * <br>
385
         *
386
         * This filter is passed to BD provider on each request as a base filter.
387
         *
388
         * @return filter
389
         *
390
         * @see #setBaseFilter(String)
391
         */
392
        public String getBaseFilter() {
393
                return (String) this.getDynValue(BASEFILTER_PARAMTER_NAME);
394
        }
395

    
396
        /**
397
         * Set initial filter to use.<br>
398
         * <br>
399
         *
400
         * This filter is passed to BD provider on each request as a base filter.
401
         *
402
         * @return filter
403
         *
404
         * @see #getInitialFilter(String)
405
         */
406
        public void setBaseFilter(String filter) {
407
                this.setDynValue(BASEFILTER_PARAMTER_NAME, filter);
408
        }
409

    
410
        /**
411
         * Get initial order to use.<br>
412
         * <br>
413
         *
414
         * This order is used if no custom order is request in query.
415
         *
416
         * @return order
417
         *
418
         * @see #setBaseOrder(String)
419
         */
420
        public String getBaseOrder() {
421
                return (String) this.getDynValue(BASEORDER_PARAMTER_NAME);
422
        }
423

    
424
        /**
425
         * Set initial order to use.<br>
426
         * <br>
427
         *
428
         * This order is used if no custom order is request in query.
429
         *
430
         * @return filter
431
         *
432
         * @see #getBaseOrder()
433
         */
434
        public void setBaseOrder(String order) {
435
                this.setDynValue(BASEORDER_PARAMTER_NAME, order);
436
        }
437

    
438
        /**
439
         * Get a comma separated list of the field names that compound the primary
440
         * key.<br>
441
         * <br>
442
         *
443
         * A <code>null</code> or empty string means that library must detect this
444
         * information.<br>
445
         * <br>
446
         * <strong>Note:</strong>If this parameters is undefined, the library can't
447
         * do this detection, some services will don't be available for this store
448
         * (<i>like selection, editing or {@link Feature#getReference()}</i>)
449
         *
450
         * @return
451
         * @see #getPkFields()
452
         * @see #setPkFields(String)
453
         * @see #setPkFields(String[])
454
         */
455
        public String getPkFieldsString() {
456
                return (String) this.getDynValue(PKFIELDS_PARAMTER_NAME);
457
        }
458

    
459
        /**
460
         * Get an array of the field names that compound the primary key.<br>
461
         * <br>
462
         *
463
         * A <code>null</code> or empty string means that library must detect this
464
         * information.<br>
465
         * <br>
466
         * <strong>Note:</strong> see {@link #getPkFieldsString()}
467
         *
468
         * @return
469
         * @see #getPkFieldsString()
470
         * @see #setPkFields(String)
471
         * @see #setPkFields(String[])
472
         */
473
        public String[] getPkFields() {
474
                String fields = (String) this.getDynValue(PKFIELDS_PARAMTER_NAME);
475
                if (fields == null) {
476
                        return null;
477
                }
478
                // FIXME check for fields with spaces and special chars
479
                return fields.split(",");
480
        }
481

    
482
        /**
483
         * Set a comma separated list of the field names that compound the primary
484
         * key.<br>
485
         * <br>
486
         *
487
         * A <code>null</code> or empty string means that library must detect this
488
         * information.<br>
489
         * <br>
490
         * <strong>Note:</strong> see {@link #getPkFieldsString()}
491
         *
492
         * @return
493
         * @see #getPkFields()
494
         * @see #getPkFieldsString()
495
         * @see #setPkFields(String[])
496
         */
497
        public void setPkFields(String fields) {
498
                this.setDynValue(PKFIELDS_PARAMTER_NAME, fields);
499
        }
500

    
501
        /**
502
         * Set an array of the field names that compound the primary key.<br>
503
         * <br>
504
         *
505
         * A <code>null</code> or empty string means that library must detect this
506
         * information.<br>
507
         * <br>
508
         * <strong>Note:</strong> see {@link #getPkFieldsString()}
509
         *
510
         * @return
511
         * @see #getPkFieldsString()
512
         * @see #getPkFieldsString()
513
         * @see #setPkFields(String)
514
         */
515
        public void setPkFields(String[] fields) {
516
                StringBuilder str = new StringBuilder();
517
                for (int i = 0; i < fields.length - 1; i++) {
518
                        str.append(fields[i]);
519
                        str.append(",");
520
                }
521
                str.append(fields.length - 1);
522

    
523
                this.setDynValue(PKFIELDS_PARAMTER_NAME, fields);
524
        }
525

    
526
        /**
527
         * Return the geometry field to use like default geometry (<i>see
528
         * {@link Feature#getDefaultGeometry()}</i>)<br>
529
         * <br>
530
         *
531
         * This option is supported only in geometry providers.<br>
532
         * If this parameters is not set and store has only one field geometry then
533
         * library uses that field as default
534
         *
535
         * @return
536
         *
537
         * @see #setDefaultGeometryField(String)
538
         */
539
        public String getDefaultGeometryField() {
540
                return (String) this.getDynValue(DEFAULTGEOMETRY_PARAMTER_NAME);
541
        }
542

    
543
        /**
544
         * Set the geometry field to use like default geometry.<br>
545
         * See {@link #getDefaultGeometryField()} for description.
546
         *
547
         * @param geomName
548
         *
549
         * @see #getDefaultGeometryField()
550
         */
551
        public void setDefaultGeometryField(String geomName) {
552
                this.setDynValue(DEFAULTGEOMETRY_PARAMTER_NAME, geomName);
553
        }
554

    
555
        /**
556
         * Get the filter by area of {@link #getDefaultGeometryField()} used in this
557
         * store.<br>
558
         * <br>
559
         *
560
         * Supported only for stores with geometric fields
561
         *
562
         * @return
563
         */
564
        public Envelope getWorkingArea() {
565
                return (Envelope) this.getDynValue(WORKINGAREA_PARAMTER_NAME);
566
        }
567

    
568
        /**
569
         * Set the filter by area of {@link #getDefaultGeometryField()} used in this
570
         * store.<br>
571
         * <br>
572
         *
573
         * Supported only for stores with geometric fields
574
         *
575
         */
576
        public void setWorkingArea(Envelope workingArea) {
577
                this.setDynValue(WORKINGAREA_PARAMTER_NAME, workingArea);
578
        }
579

    
580
        /**
581
         * Set manually the SRS for the <code>default geometry</code> field of this
582
         * layer.<br>
583
         * <br>
584
         * 
585
         * if is set to <code>null</code> or empty string means that the library
586
         * must detect it automatically.<br>
587
         * <br>
588
         * 
589
         * Supported only for stores with geometric fields.
590
         * 
591
         * @return
592
         * 
593
         * @see #setSRSID(String)
594
         * @see #getSRSID()
595
         * @see #getSRS()
596
         */
597
        public void setCRS(IProjection srs) {
598
                setDynValue(CRS_PARAMTER_NAME, srs);
599
        }
600

    
601
        public void setCRS(String srs) {
602
                setDynValue(CRS_PARAMTER_NAME, srs);
603
        }
604

    
605
        /**
606
         * Get the SRS id string set manually for the <code>default geometry</code>
607
         * field of this layer.<br>
608
         * <br>
609
         *
610
         * if is set to <code>null</code> means that the library must detect it
611
         * automatically.<br>
612
         * <br>
613
         *
614
         * Supported only for stores with geometric fields.
615
         *
616
         * @return
617
         *
618
         * @see #setSRSID(String)
619
         * @see #getSRS()
620
         * @see #setSRS(IProjection)
621
         */
622
        public IProjection getCRS() {
623
                return (IProjection) getDynValue(CRS_PARAMTER_NAME);
624
        }
625

    
626
        public int getGeometryType() {
627
            Integer geomType = (Integer) this.getDynValue(GEOMETRYTYPE_PARAMTER_NAME);
628
            return geomType == null ? Geometry.TYPES.UNKNOWN:geomType;
629
        }
630

    
631
        public void setGeometryType(int geometryType) {
632
                this.setDynValue(GEOMETRYTYPE_PARAMTER_NAME, geometryType);
633
        }
634

    
635
        public int getGeometrySubtype() {
636
            Integer geomSubtype = (Integer) this.getDynValue(GEOMETRYSUBTYPE_PARAMTER_NAME);
637
            return geomSubtype == null ? Geometry.SUBTYPES.UNKNOWN:geomSubtype;
638
        }
639

    
640
        public void setGeometrySubtype(int geometrySubtype) {
641
                this.setDynValue(GEOMETRYSUBTYPE_PARAMTER_NAME, geometrySubtype);
642
        }
643

    
644
}