| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos; |
| 17 | |
|
| 18 | |
import com.google.common.base.Function; |
| 19 | |
import java.awt.Dimension; |
| 20 | |
import java.awt.MediaTracker; |
| 21 | |
import java.io.File; |
| 22 | |
import java.sql.SQLException; |
| 23 | |
import java.sql.Statement; |
| 24 | |
import java.util.List; |
| 25 | |
import javax.swing.ImageIcon; |
| 26 | |
import net.sf.webphotos.dao.jpa.PhotoDAO; |
| 27 | |
import net.sf.webphotos.entity.IsCredits; |
| 28 | |
import net.sf.webphotos.entity.PhotoEntity; |
| 29 | |
import net.sf.webphotos.model.CreditsVO; |
| 30 | |
import net.sf.webphotos.model.PhotoVO; |
| 31 | |
import net.sf.webphotos.util.ApplicationContextResource; |
| 32 | |
import net.sf.webphotos.util.Util; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | 0 | public class PhotoDTO extends PhotoEntity { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 0 | public static final Function<PhotoDTO, PhotoVO> FROM_PHOTODTO_PHOTOVO = new Function<PhotoDTO, PhotoVO>() { |
| 44 | |
@Override |
| 45 | |
public PhotoVO apply(PhotoDTO input) { |
| 46 | 0 | return new PhotoVO(input.getLegenda(), input.getLegenda(), new CreditsVO(input.creditoID, input.creditoNome), input.caminhoArquivo); |
| 47 | |
} |
| 48 | |
}; |
| 49 | |
|
| 50 | 0 | private int fotoID = 0; |
| 51 | 0 | private int albumID = -1; |
| 52 | 0 | private int creditoID = 0; |
| 53 | 0 | private String legenda = null; |
| 54 | 0 | private int largura = 0; |
| 55 | 0 | private int altura = 0; |
| 56 | 0 | @Deprecated |
| 57 | |
private long tamanhoBytes = 0; |
| 58 | 0 | private String creditoNome = ""; |
| 59 | 0 | private String caminhoArquivo = ""; |
| 60 | 0 | private static PhotoDAO photosDAO = (PhotoDAO) ApplicationContextResource.getBean("photosDAO"); |
| 61 | 0 | private static String[][] creditos = null; |
| 62 | |
|
| 63 | |
public PhotoDTO(PhotoVO photoVO) { |
| 64 | 0 | this(photoVO.getFotoid(), photoVO.getAlbum().getAlbumid(), photoVO.getLegenda(), photoVO.getCreditos().getCreditoid(), photoVO.getCreditos().getNome(), 0, 0, 0); |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 0 | public PhotoDTO(int ID, int albumID, String fotoLegenda, int fotoCreditoID, String fotoCreditoNome, int fotoLargura, int fotoAltura, long tamanhoBytes) { |
| 79 | 0 | this.fotoID = ID; |
| 80 | 0 | this.albumID = albumID; |
| 81 | 0 | this.legenda = fotoLegenda; |
| 82 | 0 | this.creditoID = fotoCreditoID; |
| 83 | 0 | this.creditoNome = fotoCreditoNome; |
| 84 | 0 | this.largura = fotoLargura; |
| 85 | 0 | this.altura = fotoAltura; |
| 86 | 0 | this.tamanhoBytes = tamanhoBytes; |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 0 | public PhotoDTO(String arquivo) { |
| 99 | 0 | legenda = ""; |
| 100 | 0 | caminhoArquivo = arquivo; |
| 101 | |
|
| 102 | 0 | ImageIcon a = new ImageIcon(arquivo); |
| 103 | |
|
| 104 | 0 | if (a.getImageLoadStatus() == MediaTracker.COMPLETE) { |
| 105 | 0 | largura = a.getIconWidth(); |
| 106 | 0 | altura = a.getIconHeight(); |
| 107 | 0 | tamanhoBytes = new File(arquivo).length(); |
| 108 | |
} else { |
| 109 | 0 | Util.log("[Foto.Foto]/ERRO: " + arquivo + " Não pode ser lido"); |
| 110 | |
} |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public int getFotoID() { |
| 119 | 0 | return fotoID; |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public int getCreditoID() { |
| 128 | 0 | return creditoID; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public String getCreditoNome() { |
| 137 | 0 | return creditoNome; |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
@Override |
| 146 | |
public String getLegenda() { |
| 147 | 0 | return legenda; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public int getLargura() { |
| 156 | 0 | return largura; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public int getAltura() { |
| 165 | 0 | return altura; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public Dimension getResolucao() { |
| 175 | 0 | return new Dimension(largura, altura); |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public String getCaminhoArquivo() { |
| 184 | 0 | return caminhoArquivo; |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public void setFotoID(int f) { |
| 193 | 0 | fotoID = f; |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
public void setCreditoID(int c) { |
| 202 | 0 | creditoID = c; |
| 203 | 0 | } |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public void setLegenda(String l) { |
| 211 | 0 | legenda = l; |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public void setLargura(int l) { |
| 220 | 0 | largura = l; |
| 221 | 0 | } |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
public void setAltura(int a) { |
| 229 | 0 | altura = a; |
| 230 | 0 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public void setResolucao(Dimension r) { |
| 239 | 0 | largura = r.width; |
| 240 | 0 | altura = r.height; |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public void setCreditoNome(String nome) { |
| 249 | 0 | creditoNome = nome; |
| 250 | |
|
| 251 | 0 | if (creditos != null) { |
| 252 | 0 | for (int i = 0; i < creditos.length; i++) { |
| 253 | 0 | if (creditoNome.equals(creditos[i][1])) { |
| 254 | 0 | creditoID = Integer.parseInt(creditos[i][0]); |
| 255 | 0 | break; |
| 256 | |
} |
| 257 | |
} |
| 258 | |
} |
| 259 | 0 | } |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public static String[] getCreditosArray() { |
| 269 | |
int tamanho; |
| 270 | |
|
| 271 | 0 | if (creditos == null) { |
| 272 | |
try { |
| 273 | 0 | Util.log("[Foto.getCreditosArray]/AVISO: Populando Créditos"); |
| 274 | 0 | populaCreditos(); |
| 275 | 0 | tamanho = creditos.length; |
| 276 | 0 | } catch (SQLException ex) { |
| 277 | 0 | Util.log("[Foto.getCreditosArray]/ERRO: Impossível popular Créditos - " + ex); |
| 278 | 0 | tamanho = 0; |
| 279 | 0 | } |
| 280 | |
} else { |
| 281 | 0 | tamanho = creditos.length; |
| 282 | |
} |
| 283 | |
|
| 284 | 0 | String[] nomesCreditos = new String[tamanho]; |
| 285 | 0 | for (int i = 0; i < tamanho; i++) { |
| 286 | 0 | nomesCreditos[i] = creditos[i][1]; |
| 287 | |
} |
| 288 | |
|
| 289 | 0 | return nomesCreditos; |
| 290 | |
} |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
public static int getLstCreditosIndex(String nomeCredito) { |
| 299 | 0 | for (int i = 0; i < creditos.length; i++) { |
| 300 | 0 | if (nomeCredito.compareTo(creditos[i][1]) == 0) { |
| 301 | 0 | return i; |
| 302 | |
} |
| 303 | |
} |
| 304 | |
|
| 305 | 0 | return 0; |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
public static int getLstCreditosID(String nomeCredito) { |
| 316 | 0 | for (int i = 0; i < creditos.length; i++) { |
| 317 | 0 | if (nomeCredito.equals(creditos[i][1])) { |
| 318 | 0 | return Integer.parseInt(creditos[i][0]); |
| 319 | |
} |
| 320 | |
} |
| 321 | 0 | return 0; |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
public static void populaCreditos() throws SQLException { |
| 331 | 0 | String sql = Util.getConfig().getString("sql7"); |
| 332 | |
|
| 333 | 0 | List<Object[]> tableData = photosDAO.findByNativeQuery(sql); |
| 334 | |
|
| 335 | 0 | creditos = new String[tableData.size()][2]; |
| 336 | |
|
| 337 | 0 | int ct = 0; |
| 338 | 0 | for (Object[] objects : tableData) { |
| 339 | 0 | creditos[ct][0] = objects[0].toString(); |
| 340 | 0 | creditos[ct][1] = objects[1].toString(); |
| 341 | 0 | ct++; |
| 342 | |
} |
| 343 | |
|
| 344 | 0 | } |
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
@Override |
| 352 | |
public String toString() { |
| 353 | 0 | return "fotoID: " + fotoID + "\ncreditoID: " + creditoID + "\ncreditoNome: " + creditoNome + "\nlargura: " + largura + "\naltura: " + altura + "\narquivo: " + caminhoArquivo + "\nlegenda: " + legenda; |
| 354 | |
} |
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
public void resetCaminhoArquivo() { |
| 360 | 0 | caminhoArquivo = ""; |
| 361 | 0 | } |
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
public void atualizaFoto() throws Exception { |
| 372 | 0 | int ultimoFotoID = -1; |
| 373 | |
String sql; |
| 374 | 0 | Statement st = null; |
| 375 | |
|
| 376 | |
|
| 377 | 0 | if (caminhoArquivo.length() > 0) { |
| 378 | |
try { |
| 379 | 0 | sql = "select max(fotoID) from fotos"; |
| 380 | 0 | Integer tableData = (Integer) photosDAO.createNativeQuery(sql).getSingleResult(); |
| 381 | |
|
| 382 | 0 | ultimoFotoID = tableData.intValue(); |
| 383 | 0 | } catch (Exception ex) { |
| 384 | 0 | Util.log("[Foto.atualizaFoto]/ERRO: " + ex); |
| 385 | 0 | } |
| 386 | 0 | setFotoID(ultimoFotoID); |
| 387 | |
|
| 388 | |
|
| 389 | |
try { |
| 390 | 0 | PhotoVO photoVO = new PhotoVO(fotoID, albumID, legenda, creditoID, largura, altura); |
| 391 | 0 | photosDAO.save(photoVO); |
| 392 | 0 | } catch (Exception e) { |
| 393 | 0 | Util.log("[Foto.atualizaFoto]/ERRO: " + e); |
| 394 | 0 | throw e; |
| 395 | 0 | } |
| 396 | |
|
| 397 | |
|
| 398 | |
} else { |
| 399 | |
try { |
| 400 | 0 | PhotoVO photoVO = photosDAO.findBy(fotoID); |
| 401 | 0 | photoVO.setLegenda(legenda); |
| 402 | 0 | photosDAO.save(photoVO); |
| 403 | 0 | } catch (Exception e) { |
| 404 | 0 | Util.log("[Foto.atualizaFoto]/ERRO: " + e); |
| 405 | 0 | throw e; |
| 406 | 0 | } |
| 407 | |
} |
| 408 | |
|
| 409 | |
try { |
| 410 | 0 | st.close(); |
| 411 | 0 | } catch (Exception e) { |
| 412 | 0 | } |
| 413 | 0 | } |
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
public int getAlbumID() { |
| 421 | 0 | return albumID; |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
public void setAlbumID(int albumID) { |
| 430 | 0 | this.albumID = albumID; |
| 431 | 0 | } |
| 432 | |
|
| 433 | |
@Override |
| 434 | |
public String getKey() { |
| 435 | 0 | return this.caminhoArquivo; |
| 436 | |
} |
| 437 | |
|
| 438 | |
@Override |
| 439 | |
public IsCredits getCreditos() { |
| 440 | 0 | return new CreditsVO(creditoID, creditoNome); |
| 441 | |
} |
| 442 | |
} |