| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos; |
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.sql.SQLException; |
| 20 | |
import java.sql.Statement; |
| 21 | |
import java.text.SimpleDateFormat; |
| 22 | |
import java.util.HashSet; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.Set; |
| 26 | |
import javax.swing.JOptionPane; |
| 27 | |
import net.sf.webphotos.dao.jpa.AlbumDAO; |
| 28 | |
import net.sf.webphotos.entity.IsPhoto; |
| 29 | |
import net.sf.webphotos.model.AlbumVO; |
| 30 | |
import net.sf.webphotos.model.PhotoVO; |
| 31 | |
import net.sf.webphotos.util.ApplicationContextResource; |
| 32 | |
import net.sf.webphotos.util.Util; |
| 33 | |
import net.sf.webphotos.util.legacy.CacheFTP; |
| 34 | |
import org.apache.log4j.Logger; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class Album { |
| 44 | |
|
| 45 | 0 | private static final Album instanciaAlbum = new Album(); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | private int albumID = 0; |
| 50 | 0 | private int categoriaID = 0; |
| 51 | 0 | private int usuarioID = 0; |
| 52 | 0 | private String nmAlbum = null; |
| 53 | 0 | private String descricao = null; |
| 54 | 0 | private String dtInsercao = null; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 0 | private Set<PhotoVO> fotos = new HashSet<PhotoVO>(); |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | private Set<PhotoDTO> fotosNovas = new HashSet<PhotoDTO>(); |
| 63 | 0 | private String[][] categorias = null; |
| 64 | 0 | private Logger log = Logger.getLogger(this.getClass().getName()); |
| 65 | 0 | private static AlbumDAO albunsDAO = (AlbumDAO) ApplicationContextResource.getBean("albunsDAO"); |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 0 | private Album() { |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public static Album getAlbum() { |
| 79 | 0 | return instanciaAlbum; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public void setAlbumID(int aID) { |
| 88 | 0 | albumID = aID; |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public void setUsuarioID(int uID) { |
| 97 | 0 | usuarioID = uID; |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public void setCategoriaID(int cID) { |
| 106 | 0 | categoriaID = cID; |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public void setNmAlbum(String nm) { |
| 115 | 0 | nmAlbum = nm; |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public void setDescricao(String d) { |
| 124 | 0 | descricao = d; |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public void setDtInsercao(String dt) { |
| 133 | 0 | dtInsercao = dt; |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public int getAlbumID() { |
| 142 | 0 | return albumID; |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
public int getUsuarioID() { |
| 151 | 0 | return usuarioID; |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public int getCategoriaID() { |
| 160 | 0 | return categoriaID; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public String getNmAlbum() { |
| 169 | 0 | return nmAlbum; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public String getDescricao() { |
| 178 | 0 | return descricao; |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public String getDtInsercao() { |
| 187 | 0 | return dtInsercao; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public PhotoDTO getFoto(int fotoID) { |
| 198 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 199 | |
|
| 200 | 0 | while (iter.hasNext()) { |
| 201 | 0 | PhotoVO f = iter.next(); |
| 202 | 0 | if (f.getFotoid() == fotoID) { |
| 203 | 0 | return new PhotoDTO(f); |
| 204 | |
} |
| 205 | 0 | } |
| 206 | 0 | return null; |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public PhotoDTO getFoto(String caminho) { |
| 217 | 0 | Iterator<PhotoDTO> iter = fotosNovas.iterator(); |
| 218 | |
|
| 219 | 0 | while (iter.hasNext()) { |
| 220 | 0 | PhotoDTO f = iter.next(); |
| 221 | 0 | if (caminho.equals(f.getCaminhoArquivo())) { |
| 222 | 0 | return f; |
| 223 | |
} |
| 224 | 0 | } |
| 225 | 0 | return null; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
public PhotoDTO[] getFotos() { |
| 234 | 0 | final Set<IsPhoto> allPhotos = getAllPhotos(); |
| 235 | 0 | return allPhotos.toArray(new PhotoDTO[allPhotos.size()]); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public Object[][] getFotosArray() { |
| 245 | 0 | Set<IsPhoto> joined = getAllPhotos(); |
| 246 | |
|
| 247 | 0 | Object[][] resultado = new Object[joined.size()][3]; |
| 248 | 0 | Iterator<IsPhoto> iter = joined.iterator(); |
| 249 | 0 | int ct = 0; |
| 250 | |
|
| 251 | 0 | while (iter.hasNext()) { |
| 252 | 0 | IsPhoto photo = iter.next(); |
| 253 | 0 | resultado[ct][0] = photo.getKey(); |
| 254 | 0 | resultado[ct][1] = photo.getLegenda(); |
| 255 | 0 | resultado[ct][2] = photo.getCreditos().getNome(); |
| 256 | 0 | ct++; |
| 257 | 0 | } |
| 258 | 0 | return resultado; |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public String[] getFotosColunas() { |
| 268 | 0 | return new String[]{"ID", "Legenda", "Credito"}; |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public String[] getCategoriasArray() { |
| 276 | 0 | return getCategoriasArray(Boolean.FALSE); |
| 277 | |
} |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public String[] getCategoriasArray(Boolean force) { |
| 287 | |
int tamanho; |
| 288 | 0 | if (categorias == null || force == true) { |
| 289 | |
try { |
| 290 | 0 | log.info("Populando Categorias"); |
| 291 | 0 | populaCategorias(); |
| 292 | 0 | tamanho = categorias.length; |
| 293 | 0 | } catch (SQLException ex) { |
| 294 | 0 | log.error("Impossível popular categorias", ex); |
| 295 | 0 | tamanho = 0; |
| 296 | 0 | } |
| 297 | |
} else { |
| 298 | 0 | tamanho = categorias.length; |
| 299 | |
} |
| 300 | |
|
| 301 | 0 | String[] nomesCategorias = new String[tamanho]; |
| 302 | 0 | for (int i = 0; i < tamanho; i++) { |
| 303 | 0 | nomesCategorias[i] = categorias[i][1]; |
| 304 | |
} |
| 305 | |
|
| 306 | 0 | return nomesCategorias; |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
public int getLstCategoriasIndex(String nomeCategoria) { |
| 318 | 0 | for (int i = 0; i < categorias.length; i++) { |
| 319 | 0 | if (nomeCategoria.equals(categorias[i][1])) { |
| 320 | 0 | return i; |
| 321 | |
} |
| 322 | |
} |
| 323 | 0 | return 0; |
| 324 | |
} |
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
public int getLstCategoriasIndex(int categoriaID) { |
| 335 | 0 | for (int i = 0; i < categorias.length; i++) { |
| 336 | 0 | if (categoriaID == Integer.parseInt(categorias[i][0])) { |
| 337 | 0 | return i; |
| 338 | |
} |
| 339 | |
} |
| 340 | 0 | return 0; |
| 341 | |
} |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public int getLstCategoriasID(String nomeCategoria) { |
| 350 | 0 | for (int i = 0; i < categorias.length; i++) { |
| 351 | 0 | if (nomeCategoria.equals(categorias[i][1])) { |
| 352 | 0 | return Integer.parseInt(categorias[i][0]); |
| 353 | |
} |
| 354 | |
} |
| 355 | 0 | return -1; |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
public void clear() { |
| 363 | 0 | albumID = 0; |
| 364 | 0 | usuarioID = 0; |
| 365 | 0 | categoriaID = 0; |
| 366 | 0 | nmAlbum = ""; |
| 367 | 0 | descricao = ""; |
| 368 | 0 | dtInsercao = ""; |
| 369 | 0 | if (fotos != null) { |
| 370 | 0 | fotos.clear(); |
| 371 | |
} |
| 372 | 0 | } |
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
public void loadAlbum(int aID) { |
| 383 | |
|
| 384 | 0 | fotos.clear(); |
| 385 | |
|
| 386 | 0 | AlbumVO album = ((AlbumDAO) ApplicationContextResource.getBean("albunsDAO")).findBy(aID); |
| 387 | |
|
| 388 | 0 | if (album != null) { |
| 389 | 0 | albumID = aID; |
| 390 | 0 | categoriaID = album.getCategoriasVO().getCategoriaID(); |
| 391 | 0 | usuarioID = 0; |
| 392 | 0 | nmAlbum = album.getNmalbum(); |
| 393 | 0 | descricao = album.getDescricao(); |
| 394 | 0 | dtInsercao = new SimpleDateFormat("dd/MM/yyyy").format(album.getDtInsercao()); |
| 395 | |
} |
| 396 | |
|
| 397 | |
try { |
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | 0 | fotos = album.getPhotos(); |
| 403 | |
|
| 404 | 0 | } catch (Exception e) { |
| 405 | 0 | log.error("Ocorreu um erro durante a leitura do álbum no banco de dados", e); |
| 406 | 0 | int selecao = JOptionPane.showConfirmDialog(null, |
| 407 | |
"ERRO durante leitura do álbum no banco de dados.\n\nTentar Novamente?", |
| 408 | |
"Aviso!", |
| 409 | |
JOptionPane.YES_NO_OPTION, |
| 410 | |
JOptionPane.WARNING_MESSAGE); |
| 411 | 0 | if (selecao == JOptionPane.YES_OPTION) { |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | 0 | loadAlbum(aID); |
| 416 | |
|
| 417 | |
} else { |
| 418 | 0 | log.error("Ocorreu um erro inexperado durante a leitura do álbum", e); |
| 419 | 0 | JOptionPane.showMessageDialog(null, "ERRO inexperado durante leitura do álbum - " + e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE); |
| 420 | 0 | throw new RuntimeException(e); |
| 421 | |
} |
| 422 | 0 | } |
| 423 | 0 | } |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
public void excluirAlbuns(int[] albunsID) { |
| 433 | 0 | Statement st = null; |
| 434 | 0 | boolean sucesso = true; |
| 435 | |
|
| 436 | |
|
| 437 | 0 | for (int i = 0; i < albunsID.length; i++) { |
| 438 | 0 | CacheFTP.getCache().addCommand(CacheFTP.DELETE, albunsID[i], 0); |
| 439 | |
} |
| 440 | |
|
| 441 | |
|
| 442 | |
try { |
| 443 | 0 | for (int i = 0; i < albunsID.length; i++) { |
| 444 | 0 | albunsDAO.remove(albunsDAO.findBy(i)); |
| 445 | |
} |
| 446 | 0 | log.info("Exclusão dos álbuns e fotos no banco de dados efetuada com sucesso !"); |
| 447 | 0 | } catch (Exception e) { |
| 448 | 0 | log.error("Houve um erro na exclusao de albuns no banco de dados", e); |
| 449 | 0 | sucesso = false; |
| 450 | |
} finally { |
| 451 | 0 | try { |
| 452 | 0 | if (st != null) { |
| 453 | 0 | st.close(); |
| 454 | |
} |
| 455 | 0 | } catch (Exception e) { |
| 456 | 0 | log.debug("Can't Close Statement", e); |
| 457 | 0 | } |
| 458 | 0 | } |
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | 0 | for (int i = 0; i < albunsID.length; i++) { |
| 463 | 0 | File diretorio = new File(BancoImagem.getLocalPath(albunsID[i])); |
| 464 | 0 | if (diretorio.isDirectory() == true) { |
| 465 | |
|
| 466 | 0 | String[] arquivos = diretorio.list(); |
| 467 | 0 | if (arquivos.length > 0) { |
| 468 | |
|
| 469 | 0 | for (int j = 0; j < arquivos.length; j++) { |
| 470 | 0 | File arquivo = new File(BancoImagem.getLocalPath(albunsID[i]) + File.separator + arquivos[j]); |
| 471 | 0 | if (arquivo.delete() == true) { |
| 472 | 0 | log.info(BancoImagem.getLocalPath(albunsID[i]) + File.separator + arquivos[j] + " excluído com sucesso"); |
| 473 | |
} else { |
| 474 | 0 | log.error("Erro na exclusão de " + BancoImagem.getLocalPath(albunsID[i]) + File.separator + arquivos[j]); |
| 475 | 0 | sucesso = false; |
| 476 | |
} |
| 477 | |
} |
| 478 | |
} |
| 479 | 0 | if (diretorio.delete() == true) { |
| 480 | 0 | log.info(BancoImagem.getLocalPath(albunsID[i]) + " excluído com sucesso"); |
| 481 | |
} else { |
| 482 | 0 | log.error("Erro na exclusão de " + BancoImagem.getLocalPath(albunsID[i])); |
| 483 | 0 | sucesso = false; |
| 484 | |
} |
| 485 | |
} |
| 486 | |
} |
| 487 | 0 | if (sucesso) { |
| 488 | 0 | log.info("Álbum(ns) excluído(s) com sucesso."); |
| 489 | |
} else { |
| 490 | 0 | log.warn("Finalizando com erros na exclusão."); |
| 491 | |
} |
| 492 | 0 | } |
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
public void excluirFotos(String[] nomes) { |
| 503 | |
String nome; |
| 504 | |
PhotoVO foto; |
| 505 | |
|
| 506 | 0 | for (int i = 0; i < nomes.length; i++) { |
| 507 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 508 | 0 | nome = nomes[i]; |
| 509 | |
|
| 510 | 0 | while (iter.hasNext()) { |
| 511 | 0 | foto = iter.next(); |
| 512 | 0 | if (nome.equals(foto.getCaminhoArquivo())) { |
| 513 | 0 | iter.remove(); |
| 514 | 0 | break; |
| 515 | |
} |
| 516 | |
} |
| 517 | |
} |
| 518 | 0 | } |
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
public void excluirFotos(int[] fotosID) { |
| 527 | 0 | Statement st = null; |
| 528 | 0 | boolean sucesso = true; |
| 529 | 0 | int aID = getAlbum().albumID; |
| 530 | 0 | AlbumVO albumVO = albunsDAO.findBy(aID); |
| 531 | |
String sql; |
| 532 | |
|
| 533 | |
|
| 534 | 0 | for (int i = 0; i < fotosID.length; i++) { |
| 535 | 0 | CacheFTP.getCache().addCommand(CacheFTP.DELETE, aID, fotosID[i]); |
| 536 | |
} |
| 537 | |
|
| 538 | |
|
| 539 | |
try { |
| 540 | 0 | for (int i = 0; i < fotosID.length; i++) { |
| 541 | 0 | albumVO.getPhotos().remove(albumVO.getPhotoBy(fotosID[i])); |
| 542 | |
} |
| 543 | 0 | albunsDAO.getEntityManager().merge(albumVO); |
| 544 | 0 | } catch (Exception e) { |
| 545 | 0 | log.error("Erro na exclusão no banco de dados ", e); |
| 546 | 0 | sucesso = false; |
| 547 | |
} finally { |
| 548 | 0 | try { |
| 549 | 0 | if (st != null) { |
| 550 | 0 | st.close(); |
| 551 | |
} |
| 552 | 0 | } catch (Exception e) { |
| 553 | 0 | log.warn("Can't close statement", e); |
| 554 | 0 | } |
| 555 | 0 | } |
| 556 | |
|
| 557 | |
|
| 558 | 0 | String[] prefixos = {"", "_a", "_b", "_c", "_d"}; |
| 559 | |
String nomeArquivo; |
| 560 | |
boolean encontrou; |
| 561 | |
|
| 562 | 0 | for (int i = 0; i < fotosID.length; i++) { |
| 563 | |
|
| 564 | 0 | encontrou = false; |
| 565 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 566 | 0 | while (iter.hasNext() && !encontrou) { |
| 567 | 0 | PhotoVO f = iter.next(); |
| 568 | 0 | if (f.getFotoid() == fotosID[i]) { |
| 569 | 0 | encontrou = true; |
| 570 | 0 | iter.remove(); |
| 571 | |
} |
| 572 | 0 | } |
| 573 | |
|
| 574 | 0 | for (int j = 0; j < prefixos.length; j++) { |
| 575 | 0 | nomeArquivo = BancoImagem.getLocalPath(aID) + File.separator + prefixos[j] + fotosID[i] + ".jpg"; |
| 576 | 0 | File arqFoto = new File(nomeArquivo); |
| 577 | |
|
| 578 | 0 | if (arqFoto.isFile()) { |
| 579 | 0 | if (arqFoto.delete()) { |
| 580 | 0 | log.info(nomeArquivo + " excluído com sucesso"); |
| 581 | |
} else { |
| 582 | 0 | log.error(nomeArquivo + " não pode ser excluído "); |
| 583 | 0 | sucesso = false; |
| 584 | |
} |
| 585 | |
} else { |
| 586 | 0 | log.warn(nomeArquivo + " não é arquivo ou não existe"); |
| 587 | 0 | sucesso = false; |
| 588 | |
} |
| 589 | |
} |
| 590 | |
} |
| 591 | |
|
| 592 | |
|
| 593 | 0 | if (Util.getConfig().getBoolean("autoTransferir")) { |
| 594 | 0 | Thread t = new Thread(new net.sf.webphotos.gui.util.FtpClient()); |
| 595 | 0 | t.start(); |
| 596 | |
} |
| 597 | |
|
| 598 | |
|
| 599 | 0 | if (sucesso) { |
| 600 | 0 | log.info("Exclusão de fotos finalizada com sucesso."); |
| 601 | |
} else { |
| 602 | 0 | log.warn("Exclusão de fotos finalizada com erros."); |
| 603 | |
} |
| 604 | 0 | } |
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
public void adicionarFotos(File[] f) { |
| 613 | 0 | if (f.length == 0) { |
| 614 | 0 | return; |
| 615 | |
} |
| 616 | |
|
| 617 | 0 | for (int i = 0; i < f.length; i++) { |
| 618 | 0 | File novaFoto = f[i]; |
| 619 | 0 | fotosNovas.add(new PhotoDTO(novaFoto.getAbsolutePath())); |
| 620 | |
} |
| 621 | 0 | } |
| 622 | |
|
| 623 | |
|
| 624 | |
|
| 625 | |
private void populaCategorias() throws SQLException { |
| 626 | 0 | String sql = Util.getConfig().getString("sql2"); |
| 627 | |
|
| 628 | 0 | List<Object[]> tableData = albunsDAO.findByNativeQuery(sql); |
| 629 | |
|
| 630 | 0 | categorias = new String[tableData.size()][2]; |
| 631 | |
|
| 632 | 0 | int ct = 0; |
| 633 | 0 | for (Object[] objects : tableData) { |
| 634 | 0 | categorias[ct][0] = objects[0].toString(); |
| 635 | 0 | categorias[ct][1] = objects[1].toString(); |
| 636 | 0 | ct++; |
| 637 | |
} |
| 638 | 0 | } |
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
public String getCategoria(int categoriaID) { |
| 648 | |
int tamanho; |
| 649 | 0 | if (categorias == null) { |
| 650 | |
try { |
| 651 | 0 | populaCategorias(); |
| 652 | 0 | tamanho = categorias.length; |
| 653 | 0 | } catch (SQLException ex) { |
| 654 | 0 | tamanho = 0; |
| 655 | 0 | } |
| 656 | |
} else { |
| 657 | 0 | tamanho = categorias.length; |
| 658 | |
} |
| 659 | |
|
| 660 | 0 | for (int i = 0; i < tamanho; i++) { |
| 661 | 0 | if (categorias[i][0].equals(Integer.toString(categoriaID))) { |
| 662 | 0 | return categorias[i][1]; |
| 663 | |
} |
| 664 | |
} |
| 665 | 0 | return "categoria nao encontrada"; |
| 666 | |
} |
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | |
@Override |
| 674 | |
public String toString() { |
| 675 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 676 | |
|
| 677 | 0 | String msg = "--------------------------------------" |
| 678 | |
+ "\nalbumID : " + albumID |
| 679 | |
+ "\nusuarioID : " + usuarioID |
| 680 | |
+ "\ncategoriaID: " + categoriaID |
| 681 | |
+ "\nAlbum : " + nmAlbum |
| 682 | |
+ "\nDescricao : " + descricao |
| 683 | |
+ "\ndtInsercao : " + dtInsercao |
| 684 | |
+ "\nnum.fotos : " + fotos.size() |
| 685 | |
+ "\n--------------------------------------"; |
| 686 | |
|
| 687 | 0 | while (iter.hasNext()) { |
| 688 | 0 | PhotoVO f = iter.next(); |
| 689 | 0 | msg = msg + "\n" + f.toString() + "\n--------------------------------------"; |
| 690 | 0 | } |
| 691 | |
|
| 692 | 0 | return msg; |
| 693 | |
} |
| 694 | |
|
| 695 | |
|
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
public String toXML() { |
| 701 | |
|
| 702 | 0 | String r = "<?xml version=\"1.0\" encoding=\"ISO8859-1\"?>" |
| 703 | |
+ "\n<album id=\"" + albumID + "\">" |
| 704 | |
+ "\n\t<titulo>" + nmAlbum + "</titulo>" |
| 705 | |
+ "\n\t<categoria id=\"" + categoriaID + "\">" + getCategoria(categoriaID) + "</categoria>" |
| 706 | |
+ "\n\t<descricao>" + descricao + "</descricao>" |
| 707 | |
+ "\n\t<data>" + dtInsercao + "</data>" |
| 708 | |
+ "\n" |
| 709 | |
+ "\n\t<fotos>"; |
| 710 | |
|
| 711 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 712 | 0 | while (iter.hasNext()) { |
| 713 | 0 | PhotoVO f = iter.next(); |
| 714 | 0 | r += "\n\t\t<foto id=\"" + f.getFotoid() + "\">" |
| 715 | |
+ "\n\t\t\t<legenda>" + f.getLegenda() + "</legenda>" |
| 716 | |
+ "\n\t\t\t<credito>" + f.getCreditos().getNome() + "</credito>" |
| 717 | |
+ "\n\t\t\t<altura>" + f.getAltura() + "</altura>" |
| 718 | |
+ "\n\t\t\t<largura>" + f.getLargura() + "</largura>" |
| 719 | |
+ "\n\t\t</foto>"; |
| 720 | 0 | } |
| 721 | |
|
| 722 | 0 | r += "\n\t</fotos>" |
| 723 | |
+ "\n</album>\n"; |
| 724 | |
|
| 725 | 0 | return r; |
| 726 | |
|
| 727 | |
} |
| 728 | |
|
| 729 | |
|
| 730 | |
|
| 731 | |
|
| 732 | |
|
| 733 | |
|
| 734 | |
public String toJavaScript() { |
| 735 | |
|
| 736 | 0 | String r = "albumID=" + albumID + ";\n" |
| 737 | |
+ "categoria='" + getCategoria(categoriaID) + "';\n" |
| 738 | |
+ "titulo=" + Util.stringToHtm(nmAlbum) + ";\n" |
| 739 | |
+ "data='" + dtInsercao + "';\n" |
| 740 | |
+ "descricao=" + Util.stringToHtm(descricao) + ";\n\n" |
| 741 | |
+ "fotos = new Array ("; |
| 742 | |
|
| 743 | 0 | Iterator<PhotoVO> iter = fotos.iterator(); |
| 744 | 0 | String cc = ""; |
| 745 | |
|
| 746 | 0 | while (iter.hasNext()) { |
| 747 | 0 | PhotoVO f = iter.next(); |
| 748 | 0 | r += cc + "\n\tnew Foto(" + f.getFotoid() + "," + Util.stringToHtm(f.getLegenda()) + ",'" + f.getCreditos().getNome() + "')"; |
| 749 | 0 | cc = ","; |
| 750 | 0 | } |
| 751 | |
|
| 752 | 0 | r += "\n\t);\n"; |
| 753 | |
|
| 754 | 0 | return r; |
| 755 | |
|
| 756 | |
} |
| 757 | |
|
| 758 | |
@Override |
| 759 | |
public Object clone() throws CloneNotSupportedException { |
| 760 | 0 | throw new CloneNotSupportedException("Singleton Object"); |
| 761 | |
} |
| 762 | |
|
| 763 | |
private Set<IsPhoto> getAllPhotos() { |
| 764 | 0 | Set<IsPhoto> joined = new HashSet<IsPhoto>(); |
| 765 | 0 | joined.addAll(fotos); |
| 766 | 0 | joined.addAll(fotosNovas); |
| 767 | 0 | return joined; |
| 768 | |
} |
| 769 | |
} |