| 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 | 0 | private static Logger log = Logger.getLogger(BancoImagem.class); |
| 42 | 0 | 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 | 0 | private static String usuario = null; |
| 56 | 0 | private static char[] senha = null; |
| 57 | |
|
| 58 | 0 | private static String usuarioFTP = null; |
| 59 | 0 | private static char[] senhaFTP = null; |
| 60 | |
private static Login login; |
| 61 | |
@Deprecated |
| 62 | |
private static RowSet rSet; |
| 63 | |
|
| 64 | |
|
| 65 | 0 | private BancoImagem() { |
| 66 | 0 | Configuration c = Util.getConfig(); |
| 67 | 0 | log.info("inicializando banco de imagem..."); |
| 68 | 0 | webServer = c.getString("enderecoWWW"); |
| 69 | 0 | chaveCripto = c.getString("chaveCripto"); |
| 70 | |
|
| 71 | 0 | if (webServer != null && chaveCripto != null) { |
| 72 | 0 | log.info("utilizando ponte http"); |
| 73 | 0 | utilizarPonteWWW = true; |
| 74 | |
} |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public static BancoImagem getBancoImagem() { |
| 83 | 0 | return instancia; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public static String getLocalPath(int albumID) { |
| 93 | 0 | if (albunsRoot == null) { |
| 94 | 0 | albunsRoot = new File(Util.getConfig().getString("albunsRoot")); |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | File localFile = new File(albunsRoot, Integer.toString(albumID)); |
| 98 | 0 | if (!localFile.isDirectory()) { |
| 99 | 0 | localFile.mkdirs(); |
| 100 | |
} |
| 101 | 0 | 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 | 0 | url = dbUrl; |
| 124 | 0 | driver = dbDriver; |
| 125 | 0 | Class.forName(dbDriver).newInstance(); |
| 126 | 0 | log.info("Driver " + dbDriver + " carregado"); |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public static Connection getConnection() throws SQLException { |
| 140 | |
try { |
| 141 | 0 | if (conn != null |
| 142 | |
&& conn.isClosed() == false) { |
| 143 | |
|
| 144 | 0 | log.debug("Usando a conexão existente"); |
| 145 | 0 | return conn; |
| 146 | |
} |
| 147 | 0 | } catch (AbstractMethodError amE) { |
| 148 | 0 | log.warn("Error getting conection", amE); |
| 149 | 0 | } |
| 150 | |
|
| 151 | 0 | if (usuario == null) { |
| 152 | 0 | login(); |
| 153 | |
} |
| 154 | 0 | conn = DriverManager.getConnection(url, usuario, (new String(senha))); |
| 155 | 0 | log.debug("Usando uma nova conexão"); |
| 156 | 0 | return conn; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public static void closeConnection() throws SQLException { |
| 167 | 0 | if (conn.isClosed() == false) { |
| 168 | 0 | conn.close(); |
| 169 | |
} |
| 170 | 0 | log.debug("Conexao ao banco de dados fechada"); |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public static boolean login() { |
| 182 | 0 | 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 | 0 | Login l = Login.getLogin(title); |
| 197 | 0 | boolean conectado = false; |
| 198 | |
|
| 199 | |
do { |
| 200 | 0 | l.show(); |
| 201 | 0 | if (l.getUser() == null) { |
| 202 | 0 | System.exit(0); |
| 203 | |
} |
| 204 | |
|
| 205 | 0 | usuario = l.getUser(); |
| 206 | 0 | senha = l.getPassword(); |
| 207 | |
try { |
| 208 | 0 | conn = DriverManager.getConnection(url, usuario, (new String(senha))); |
| 209 | |
|
| 210 | 0 | rSet = new JdbcRowSetImpl(conn); |
| 211 | 0 | rSet.setReadOnly(false); |
| 212 | 0 | rSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE); |
| 213 | 0 | rSet.setConcurrency(ResultSet.CONCUR_UPDATABLE); |
| 214 | 0 | ((JdbcRowSet) rSet).setAutoCommit(false); |
| 215 | |
|
| 216 | 0 | conectado = true; |
| 217 | 0 | } catch (Exception e) { |
| 218 | 0 | String msg = "Erro na conexão ao banco de dados"; |
| 219 | 0 | log.error(msg, e); |
| 220 | 0 | JOptionPane.showMessageDialog(null, e.getMessage(), msg, JOptionPane.ERROR_MESSAGE); |
| 221 | 0 | } |
| 222 | 0 | } while (!conectado); |
| 223 | |
|
| 224 | 0 | Login.getTelaLogin().dispose(); |
| 225 | 0 | return true; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
public String getUser() { |
| 234 | 0 | return usuario; |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public char[] getPassword() { |
| 243 | 0 | return senha; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
public String getUserFTP() { |
| 252 | 0 | return usuarioFTP; |
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
public char[] getPasswordFTP() { |
| 261 | 0 | return senhaFTP; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public void setUserFTP(String u) { |
| 270 | 0 | usuarioFTP = u; |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
public void setPasswordFTP(char[] p) { |
| 279 | 0 | senhaFTP = p; |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
@Deprecated |
| 289 | |
public static RowSet getRSet() { |
| 290 | 0 | return rSet; |
| 291 | |
} |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
@Deprecated |
| 300 | |
public static void setRSet(RowSet aRSet) { |
| 301 | 0 | rSet = aRSet; |
| 302 | 0 | } |
| 303 | |
|
| 304 | |
@Override |
| 305 | |
public Object clone() throws CloneNotSupportedException { |
| 306 | 0 | throw new CloneNotSupportedException("Singleton Object"); |
| 307 | |
} |
| 308 | |
|
| 309 | |
public static void loadUIManager() { |
| 310 | 0 | String lookAndFeel = Util.getConfig().getString("UIManager.lookAndFeel"); |
| 311 | |
try { |
| 312 | 0 | UIManager.setLookAndFeel(lookAndFeel); |
| 313 | 0 | } catch (Exception e) { |
| 314 | 0 | log.warn("Caution: Theme not correctly configured"); |
| 315 | |
|
| 316 | 0 | } |
| 317 | 0 | } |
| 318 | |
|
| 319 | |
public static void loadDBDriver() throws IllegalAccessException, SQLException, ClassNotFoundException, InstantiationException { |
| 320 | |
|
| 321 | 0 | url = Util.getConfig().getString("jdbc.url"); |
| 322 | 0 | driver = Util.getConfig().getString("jdbc.driver"); |
| 323 | 0 | getBancoImagem().configure(url, driver); |
| 324 | 0 | } |
| 325 | |
} |