Statistics
| Revision:

root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / ConnectionWithParamsSDE.java @ 11193

History | View | Annotate | Download (3.99 KB)

1
package com.iver.cit.gvsig.sde.gui.sdewizard2;
2

    
3
import java.sql.SQLException;
4

    
5
import org.apache.log4j.Logger;
6

    
7
import com.esri.sde.sdk.client.SeConnection;
8
import com.esri.sde.sdk.client.SeException;
9

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

    
20
        private static Logger logger = Logger.getLogger(ConnectionWithParamsSDE.class.getName());
21

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

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

    
34
    private boolean isNull = false;
35

    
36

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

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

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

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

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

    
91
        public SeConnection getConnection() {
92
                return conn;
93
        }
94

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

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

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

    
107

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

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

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

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

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

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

    
139
                try {
140
                        conn = new SeConnection(host, Integer.parseInt(port), db, user, _pw);
141
                } catch (NumberFormatException e) {
142
                        pw = null;
143
                        conn = null;
144
                        connected = false;
145

    
146
                        throw new SQLException(e.getMessage());
147
                } catch (SeException e) {
148
                        pw = null;
149
                        conn = null;
150
                        connected = false;
151

    
152
                        throw new SQLException(e.getMessage());
153
                }
154

    
155
                pw = _pw;
156
                connected = true;
157
        }
158

    
159
        /**
160
         * Disconnects the connection
161
         *
162
         */
163
        public void disconnect() {
164

    
165
                        try {
166
                                conn.close();
167
                        } catch (SeException e) {
168
                                logger.error("While closing connection: " + e.getMessage(), e);
169
                        }
170
                        pw = null;
171
                        conn = null;
172
                        connected = false;
173
        }
174

    
175

    
176
        public String getConnectionStr() {
177
                return connectionStr;
178
        }
179

    
180
        public void setConnectionStr(String connectionStr) {
181
                this.connectionStr = connectionStr;
182
        }
183

    
184
        public String getName() {
185
                return name;
186
        }
187

    
188
        public void setName(String name) {
189
                this.name = name;
190
        }
191

    
192
        public String getDb() {
193
                return db;
194
        }
195

    
196
        public void setDb(String db) {
197
                this.db = db;
198
        }
199

    
200
        public String getHost() {
201
                return host;
202
        }
203

    
204
        public void setHost(String host) {
205
                this.host = host;
206
        }
207

    
208
        public String getPort() {
209
                return port;
210
        }
211

    
212
        public void setPort(String port) {
213
                this.port = port;
214
        }
215

    
216
        /**
217
         *
218
         * @return whether or not this is the first item in the combo box.
219
         */
220
        public boolean isNull() {
221
                return isNull;
222
        }
223

    
224
}