Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / main / java / org / gvsig / fmap / dal / store / h2 / H2SpatialConnectionParametersHelper.java @ 47716

History | View | Annotate | Download (5.18 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.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
    private static final String MAXSECONDSIDLE = "MaxSecondsIdle";
17
    private static final String SERVERPORT = "ServerPort";
18
    private static final String SERVERALLOWOTHERS = "ServerAllowOthers";
19
    private static final String SPLIT = "Split";
20
    private static final String MAINTAINGLOBALCONNECTION = "maintainGlobalConnection";
21
    
22

    
23
    public H2SpatialConnectionParametersHelper(JDBCConnectionParameters parameters) {
24
        this.parameters = parameters;
25
    }
26

    
27
    public String getUrl() {
28
        String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME);
29
        if( StringUtils.isBlank(url) ) {
30
            url = H2SpatialUtils.getConnectionURL((H2SpatialConnectionParameters) this.parameters);
31
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
32
        }
33
        return url;
34
    }
35

    
36
    public void validate() throws ValidateDataParametersException {
37
        if( this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null ) {
38
            this.setDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME,
39
                H2SpatialHelper.H2SPATIAL_JDBC_DRIVER
40
            );
41
        }
42
        if( getFile() == null ) {
43
            if( this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME) == null ) {
44
                throw new ValidateDataParametersException();
45
            } else {
46
                setFile(new File((String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME)));
47
            }
48
        } else {
49
            String dbName = (String) this.getDynValue(JDBCConnectionParameters.DBNAME_PARAMTER_NAME);
50
            if( StringUtils.isEmpty(dbName) ) {
51
                dbName = getDbNameFromFile(getFile());
52
                this.setDynValue(
53
                    JDBCConnectionParameters.DBNAME_PARAMTER_NAME,
54
                    dbName.toUpperCase()
55
                );
56
            }
57
        }        
58
        if( this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME) == null ) {
59
            String url = H2SpatialUtils.getConnectionURL((H2SpatialConnectionParameters) this.parameters);
60
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
61
        }
62
    }
63

    
64
    private Object getDynValue(String name) {
65
        return ((DataParameters) this.parameters).getDynValue(name);
66
    }
67

    
68
    private void setDynValue(String name, Object value) {
69
        ((DataParameters) this.parameters).setDynValue(name, value);
70
    }
71
    
72
    public int getMaxSecondsIdle() {
73
        Integer x = (Integer) this.getDynValue(MAXSECONDSIDLE);
74
        if( x == null ) {
75
            return -1;
76
        }
77
        return x;
78
    }
79
    
80
    public int getServerPort() {
81
        Integer x = (Integer) this.getDynValue(SERVERPORT);
82
        if( x == null ) {
83
            return -1;
84
        }
85
        return x;
86
    }
87
    
88
    public boolean getServerAllowOthers() {
89
        Boolean x = (Boolean) this.getDynValue(SERVERALLOWOTHERS);
90
        if( x == null ) {
91
            return false;
92
        }
93
        return x;
94
    }
95
            
96
    public File getFile() {
97
        File f = (File) this.getDynValue(DATABASE_FILE);
98
        if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null
99
            && f != null ) {
100
            String dbname = getDbNameFromFile(f);
101
            this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
102
        }
103
        return f;
104
    }
105

    
106
    public void setFile(File database) {
107
        this.setDynValue(DATABASE_FILE, database);
108
        if( this.getDynValue(DBParameters.DBNAME_PARAMTER_NAME) == null
109
            && database != null ) {
110
            String dbname = getDbNameFromFile(database);
111
            this.setDynValue(DBParameters.DBNAME_PARAMTER_NAME, dbname);
112
        }
113
    }
114

    
115
    public Properties getProperties() {
116
        Properties props = new Properties();
117
        return props;
118
    }
119
    
120
    private String getDbNameFromFile(File f) {
121
        String name = H2SpatialUtils.removeH2FileNameExtension(f.getName());
122
        return name;
123
    }
124

    
125
    public String getServerPortAsString() {
126
        int port = this.getServerPort();
127
        if( port<=0 ) {
128
            return null;
129
        }
130
        String port_s = String.valueOf(port);
131
        return port_s;
132
    }
133
    
134
    public int getSplitSize() {
135
        try {
136
            Number size = (Number) this.getDynValue(SPLIT);
137
            if (size == null || size.intValue() < 1) {
138
                return 0;
139
            }
140
            return size.intValue();
141
        } catch (Exception ex) {
142
            return 0;
143
        }
144
    }
145
    
146
    public boolean getMaintainGlobalConnection() {
147
        Boolean x = (Boolean) this.getDynValue(MAINTAINGLOBALCONNECTION);
148
        if( x == null ) {
149
            return false;
150
        }
151
        return x;
152
    }
153
}