1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.webphotos;
17
18 import com.sun.rowset.JdbcRowSetImpl;
19 import java.io.File;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import javax.sql.RowSet;
25 import javax.sql.rowset.JdbcRowSet;
26 import javax.swing.JOptionPane;
27 import javax.swing.UIManager;
28 import net.sf.webphotos.gui.util.Login;
29 import net.sf.webphotos.util.Util;
30 import org.apache.commons.configuration.Configuration;
31 import org.apache.log4j.Logger;
32
33
34
35
36
37
38
39 public class BancoImagem {
40
41 private static Logger log = Logger.getLogger(BancoImagem.class);
42 private static final BancoImagem instancia = new BancoImagem();
43 private static String titulo;
44 private static String descricao;
45 private static int categoriaID;
46 private static String url;
47 private static String driver;
48 private static Connection conn;
49 private static File albunsRoot;
50
51 private static boolean utilizarPonteWWW;
52 private static String webServer;
53 private static String chaveCripto;
54
55 private static String usuario = null;
56 private static char[] senha = null;
57
58 private static String usuarioFTP = null;
59 private static char[] senhaFTP = null;
60 private static Login login;
61 @Deprecated
62 private static RowSet rSet;
63
64
65 private BancoImagem() {
66 Configuration c = Util.getConfig();
67 log.info("inicializando banco de imagem...");
68 webServer = c.getString("enderecoWWW");
69 chaveCripto = c.getString("chaveCripto");
70
71 if (webServer != null && chaveCripto != null) {
72 log.info("utilizando ponte http");
73 utilizarPonteWWW = true;
74 }
75 }
76
77
78
79
80
81
82 public static BancoImagem getBancoImagem() {
83 return instancia;
84 }
85
86
87
88
89
90
91
92 public static String getLocalPath(int albumID) {
93 if (albunsRoot == null) {
94 albunsRoot = new File(Util.getConfig().getString("albunsRoot"));
95 }
96
97 File localFile = new File(albunsRoot, Integer.toString(albumID));
98 if (!localFile.isDirectory()) {
99 localFile.mkdirs();
100 }
101 return localFile.getAbsolutePath();
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public void configure(String dbUrl, String dbDriver)
119 throws ClassNotFoundException,
120 InstantiationException,
121 IllegalAccessException,
122 SQLException {
123 url = dbUrl;
124 driver = dbDriver;
125 Class.forName(dbDriver).newInstance();
126 log.info("Driver " + dbDriver + " carregado");
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static Connection getConnection() throws SQLException {
140 try {
141 if (conn != null
142 && conn.isClosed() == false) {
143
144 log.debug("Usando a conexão existente");
145 return conn;
146 }
147 } catch (AbstractMethodError amE) {
148 log.warn("Error getting conection", amE);
149 }
150
151 if (usuario == null) {
152 login();
153 }
154 conn = DriverManager.getConnection(url, usuario, (new String(senha)));
155 log.debug("Usando uma nova conexão");
156 return conn;
157 }
158
159
160
161
162
163
164
165
166 public static void closeConnection() throws SQLException {
167 if (conn.isClosed() == false) {
168 conn.close();
169 }
170 log.debug("Conexao ao banco de dados fechada");
171 }
172
173
174
175
176
177
178
179
180
181 public static boolean login() {
182 return login("WebPhotos - Login BD");
183 }
184
185
186
187
188
189
190
191
192
193
194
195 public static boolean login(String title) {
196 Login l = Login.getLogin(title);
197 boolean conectado = false;
198
199 do {
200 l.show();
201 if (l.getUser() == null) {
202 System.exit(0);
203 }
204
205 usuario = l.getUser();
206 senha = l.getPassword();
207 try {
208 conn = DriverManager.getConnection(url, usuario, (new String(senha)));
209
210 rSet = new JdbcRowSetImpl(conn);
211 rSet.setReadOnly(false);
212 rSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
213 rSet.setConcurrency(ResultSet.CONCUR_UPDATABLE);
214 ((JdbcRowSet) rSet).setAutoCommit(false);
215
216 conectado = true;
217 } catch (Exception e) {
218 String msg = "Erro na conexão ao banco de dados";
219 log.error(msg, e);
220 JOptionPane.showMessageDialog(null, e.getMessage(), msg, JOptionPane.ERROR_MESSAGE);
221 }
222 } while (!conectado);
223
224 Login.getTelaLogin().dispose();
225 return true;
226 }
227
228
229
230
231
232
233 public String getUser() {
234 return usuario;
235 }
236
237
238
239
240
241
242 public char[] getPassword() {
243 return senha;
244 }
245
246
247
248
249
250
251 public String getUserFTP() {
252 return usuarioFTP;
253 }
254
255
256
257
258
259
260 public char[] getPasswordFTP() {
261 return senhaFTP;
262 }
263
264
265
266
267
268
269 public void setUserFTP(String u) {
270 usuarioFTP = u;
271 }
272
273
274
275
276
277
278 public void setPasswordFTP(char[] p) {
279 senhaFTP = p;
280 }
281
282
283
284
285
286
287
288 @Deprecated
289 public static RowSet getRSet() {
290 return rSet;
291 }
292
293
294
295
296
297
298
299 @Deprecated
300 public static void setRSet(RowSet aRSet) {
301 rSet = aRSet;
302 }
303
304 @Override
305 public Object clone() throws CloneNotSupportedException {
306 throw new CloneNotSupportedException("Singleton Object");
307 }
308
309 public static void loadUIManager() {
310 String lookAndFeel = Util.getConfig().getString("UIManager.lookAndFeel");
311 try {
312 UIManager.setLookAndFeel(lookAndFeel);
313 } catch (Exception e) {
314 log.warn("Caution: Theme not correctly configured");
315
316 }
317 }
318
319 public static void loadDBDriver() throws IllegalAccessException, SQLException, ClassNotFoundException, InstantiationException {
320
321 url = Util.getConfig().getString("jdbc.url");
322 driver = Util.getConfig().getString("jdbc.driver");
323 getBancoImagem().configure(url, driver);
324 }
325 }