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.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / H2SpatialConnectionParametersHelper.java @ 43377
History | View | Annotate | Download (3.49 KB)
1 | 43377 | jjdelcerro | package org.gvsig.fmap.dal.store.h2; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.util.Properties; |
||
5 | import org.apache.commons.lang3.StringUtils; |
||
6 | import org.gvsig.fmap.dal.DataParameters; |
||
7 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
8 | import org.gvsig.fmap.dal.resource.db.DBParameters; |
||
9 | import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
||
10 | |||
11 | public class H2SpatialConnectionParametersHelper { |
||
12 | |||
13 | private final JDBCConnectionParameters parameters; |
||
14 | |||
15 | private static final String DATABASE_FILE = "database_file"; |
||
16 | |||
17 | public H2SpatialConnectionParametersHelper(JDBCConnectionParameters parameters) {
|
||
18 | this.parameters = parameters;
|
||
19 | } |
||
20 | |||
21 | public String getUrl() { |
||
22 | String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME); |
||
23 | if( StringUtils.isEmpty(url) ) {
|
||
24 | url = H2SpatialHelper.getConnectionURL((H2SpatialConnectionParameters) this.parameters);
|
||
25 | this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
|
||
26 | } |
||
27 | return url;
|
||
28 | } |
||
29 | |||
30 | public void validate() throws ValidateDataParametersException { |
||
31 | if( this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null ) { |
||
32 | this.setDynValue(
|
||
33 | JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME, |
||
34 | H2SpatialHelper.H2SpatialJDBCDriver |
||
35 | ); |
||
36 | } |
||
37 | if( getFile() == null ) { |
||
38 | if( this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME) == null ) { |
||
39 | throw new ValidateDataParametersException(); |
||
40 | } else {
|
||
41 | setFile(new File((String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME))); |
||
42 | } |
||
43 | } else {
|
||
44 | String dbName = (String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME); |
||
45 | if( StringUtils.isEmpty(dbName) ) {
|
||
46 | dbName = getFile().getName(); |
||
47 | int n = dbName.indexOf("."); |
||
48 | if( n > 0 ) { |
||
49 | dbName = dbName.substring(0, n);
|
||
50 | } |
||
51 | this.setDynValue(
|
||
52 | JDBCConnectionParameters.DBNAME_PARAMTER_NAME, |
||
53 | dbName.toUpperCase() |
||
54 | ); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | private Object getDynValue(String name) { |
||
60 | return ((DataParameters) this.parameters).getDynValue(name); |
||
61 | } |
||
62 | |||
63 | private void setDynValue(String name, Object value) { |
||
64 | ((DataParameters) this.parameters).setDynValue(name, value);
|
||
65 | } |
||
66 | |||
67 | public File getFile() { |
||
68 | File f = (File) this.getDynValue(DATABASE_FILE); |
||
69 | if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null |
||
70 | && f != null ) {
|
||
71 | String dbname = f.getName();
|
||
72 | if( dbname.endsWith(".mv.db") ) { |
||
73 | dbname = dbname.substring(0, dbname.length() - 6); |
||
74 | } |
||
75 | this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
||
76 | } |
||
77 | return f;
|
||
78 | } |
||
79 | |||
80 | public void setFile(File database) { |
||
81 | this.setDynValue(DATABASE_FILE, database);
|
||
82 | if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null |
||
83 | && database != null ) {
|
||
84 | String dbname = database.getName();
|
||
85 | if( dbname.endsWith(".mv.db") ) { |
||
86 | dbname = dbname.substring(0, dbname.length() - 6); |
||
87 | } |
||
88 | this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
||
89 | } |
||
90 | } |
||
91 | |||
92 | public Properties getProperties() { |
||
93 | Properties props = new Properties(); |
||
94 | return props;
|
||
95 | } |
||
96 | |||
97 | } |