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 @ 44198
History | View | Annotate | Download (3.73 KB)
1 |
package org.gvsig.fmap.dal.store.h2; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Properties; |
5 |
import org.apache.commons.io.FilenameUtils; |
6 |
import org.apache.commons.lang3.StringUtils; |
7 |
import org.gvsig.fmap.dal.DataParameters; |
8 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
9 |
import org.gvsig.fmap.dal.resource.db.DBParameters; |
10 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
11 |
|
12 |
public class H2SpatialConnectionParametersHelper { |
13 |
|
14 |
private final JDBCConnectionParameters parameters; |
15 |
|
16 |
private static final String DATABASE_FILE = "database_file"; |
17 |
|
18 |
public H2SpatialConnectionParametersHelper(JDBCConnectionParameters parameters) {
|
19 |
this.parameters = parameters;
|
20 |
} |
21 |
|
22 |
public String getUrl() { |
23 |
String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME); |
24 |
if( StringUtils.isEmpty(url) ) {
|
25 |
url = H2SpatialHelper.getConnectionURL((H2SpatialConnectionParameters) this.parameters);
|
26 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
|
27 |
} |
28 |
return url;
|
29 |
} |
30 |
|
31 |
public void validate() throws ValidateDataParametersException { |
32 |
if( this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null ) { |
33 |
this.setDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME,
|
34 |
H2SpatialHelper.H2SPATIAL_JDBC_DRIVER |
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 = getDbNameFromFile(getFile()); |
47 |
this.setDynValue(
|
48 |
JDBCConnectionParameters.DBNAME_PARAMTER_NAME, |
49 |
dbName.toUpperCase() |
50 |
); |
51 |
} |
52 |
} |
53 |
if( this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME) == null ) { |
54 |
String url = H2SpatialHelper.getConnectionURL((H2SpatialConnectionParameters) this.parameters); |
55 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
|
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 = getDbNameFromFile(f);
|
72 |
this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
73 |
} |
74 |
return f;
|
75 |
} |
76 |
|
77 |
public void setFile(File database) { |
78 |
this.setDynValue(DATABASE_FILE, database);
|
79 |
if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null |
80 |
&& database != null ) {
|
81 |
String dbname = getDbNameFromFile(database);
|
82 |
this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
|
83 |
} |
84 |
} |
85 |
|
86 |
public Properties getProperties() { |
87 |
Properties props = new Properties(); |
88 |
return props;
|
89 |
} |
90 |
|
91 |
private String getDbNameFromFile(File f) { |
92 |
String fname = f.getName();
|
93 |
if( fname.toLowerCase().endsWith(".mv.db") ) { |
94 |
fname = fname.substring(0, fname.length()-6); |
95 |
} else {
|
96 |
fname = FilenameUtils.getBaseName(fname); |
97 |
} |
98 |
return fname;
|
99 |
} |
100 |
|
101 |
} |