Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / db / utils / ConnectionWithParams.java @ 11971

History | View | Annotate | Download (4.07 KB)

1
package com.iver.cit.gvsig.fmap.drivers.db.utils;
2

    
3
import org.apache.log4j.Logger;
4

    
5
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory;
6
import com.iver.cit.gvsig.fmap.drivers.DBException;
7
import com.iver.cit.gvsig.fmap.drivers.IConnection;
8

    
9
/**
10
 * Utility class to keep the connection parameters. It is used as a item in the
11
 * single connections manager tree and in the available connections combo box
12
 * (wizard db)
13
 *
14
 * @author jldominguez
15
 *
16
 */
17
public class ConnectionWithParams {
18

    
19
        private static Logger logger = Logger.getLogger(ConnectionWithParams.class.getName());
20

    
21
        private IConnection conn = null;
22
        private String connectionStr = "";
23
        private String drvName = "";
24
        private String user = "";
25
        private String pw = "";
26
        private String name = "";
27
        private boolean connected = false;
28

    
29
        private String host;
30
    private String port;
31
    private String db;
32

    
33
    private String schema;
34
    
35
    private boolean isNull = false;
36

    
37

    
38
    /**
39
     * Utility constructor to indicate an empty item.
40
     * It is used as the first item in the wizard's combo box
41
     * so that when the wizard is loaded, no query is done to any database.
42
     *
43
     */
44
        public ConnectionWithParams() {
45
                isNull = true;
46
        }
47

    
48
        /**
49
         * Class Constructor.
50
         *
51
         * @param _conn_str connection string
52
         * @param _c connection object
53
         * @param _drvName driver name
54
         * @param _user user name
55
         * @param _pw password
56
         * @param _name connection name (freely chosen by user)
57
         * @param _host host's url
58
         * @param _port port number as a string
59
         * @param _db database name
60
         * @param _isConn whether the connection is open or not
61
         */
62
        public ConnectionWithParams(
63
                        String _conn_str,
64
                        IConnection _c,
65
                        String _drvName,
66
                        String _user,
67
                        String _pw,
68
                        String _name,
69
                        String _host,
70
                        String _port,
71
                        String _db,
72
                        boolean _isConn) {
73

    
74
                connectionStr = _conn_str;
75
                connected = _isConn;
76
                conn = _c;
77
                drvName = _drvName;
78
                user = _user;
79
                pw = _pw;
80
                name = _name;
81

    
82
                host = _host;
83
                port = _port;
84
                db = _db;
85

    
86
                if (!connected) {
87
                        pw = null;
88
                        conn = null;
89
                }
90
        }
91

    
92
        public IConnection getConnection() {
93
                return conn;
94
        }
95

    
96
        public String getDrvName() {
97
                return drvName;
98
        }
99

    
100
        public String getPw() {
101
                return pw;
102
        }
103

    
104
        public String getUser() {
105
                return user;
106
        }
107

    
108

    
109
        /**
110
         * Used to paint the object in lists and trees
111
         */
112
        public String toString() {
113

    
114
                if (isNull) {
115
                        return "";
116
                }
117

    
118
                if (connected) {
119
                        return "[C] " + name + " (" + drvName + ")";
120
                } else {
121
                        return name + " (" + drvName + ")";
122
                }
123
        }
124

    
125
        public boolean isConnected() {
126
                return connected;
127
        }
128

    
129
        public void setConnected(boolean c) {
130
                connected = c;
131
        }
132

    
133
        /**
134
         * Tries to connects the connection object with the given password.
135
         * @param _pw password
136
         * @throws SQLException
137
         */
138
        public void connect(String _pw) throws DBException {
139

    
140
                try {
141
                        conn = ConnectionFactory.createConnection(connectionStr, user, _pw);//DriverManager.getConnection(connectionStr, user, _pw);
142
                } catch (DBException e) {
143

    
144
                        pw = null;
145
                        conn = null;
146
                        connected = false;
147

    
148
                        throw new DBException(e);
149
                }
150

    
151
                pw = _pw;
152
                connected = true;
153
        }
154

    
155
        /**
156
         * Disconnects the connection
157
         *
158
         */
159
        public void disconnect() {
160

    
161
                        try {
162
                                conn.close();
163
                        } catch (DBException e) {
164
                                logger.error("While closing connection: " + e.getMessage(), e);
165
                        }
166
                        pw = null;
167
                        conn = null;
168
                        connected = false;
169
        }
170

    
171

    
172
        public String getConnectionStr() {
173
                return connectionStr;
174
        }
175

    
176
        public void setConnectionStr(String connectionStr) {
177
                this.connectionStr = connectionStr;
178
        }
179

    
180
        public String getName() {
181
                return name;
182
        }
183

    
184
        public void setName(String name) {
185
                this.name = name;
186
        }
187

    
188
        public String getDb() {
189
                return db;
190
        }
191

    
192
        public void setDb(String db) {
193
                this.db = db;
194
        }
195

    
196
        public String getHost() {
197
                return host;
198
        }
199

    
200
        public void setHost(String host) {
201
                this.host = host;
202
        }
203

    
204
        public String getPort() {
205
                return port;
206
        }
207

    
208
        public void setPort(String port) {
209
                this.port = port;
210
        }
211

    
212
        /**
213
         *
214
         * @return whether or not this is the first item in the combo box.
215
         */
216
        public boolean isNull() {
217
                return isNull;
218
        }
219

    
220
        public String getSchema() {
221
                return schema;
222
        }
223

    
224
        public void setSchema(String schema) {
225
                this.schema = schema;
226
        }
227

    
228
}