1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package net.sf.webphotos.action; |
17 | |
|
18 | |
import com.google.common.collect.Collections2; |
19 | |
import com.google.common.collect.Sets; |
20 | |
import java.awt.event.ActionEvent; |
21 | |
import java.io.*; |
22 | |
import java.nio.channels.FileChannel; |
23 | |
import java.text.ParseException; |
24 | |
import java.text.SimpleDateFormat; |
25 | |
import java.util.Collection; |
26 | |
import java.util.Date; |
27 | |
import java.util.HashSet; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.Set; |
30 | |
import java.util.logging.Level; |
31 | |
import java.util.logging.Logger; |
32 | |
import javax.sql.RowSet; |
33 | |
import javax.swing.AbstractAction; |
34 | |
import javax.swing.JButton; |
35 | |
import javax.swing.JOptionPane; |
36 | |
import javax.swing.JTable; |
37 | |
import net.sf.webphotos.Album; |
38 | |
import net.sf.webphotos.BancoImagem; |
39 | |
import net.sf.webphotos.PhotoDTO; |
40 | |
import net.sf.webphotos.dao.jpa.AlbumDAO; |
41 | |
import net.sf.webphotos.gui.PainelWebFotos; |
42 | |
import net.sf.webphotos.gui.util.TableModelAlbum; |
43 | |
import net.sf.webphotos.gui.util.TableModelFoto; |
44 | |
import net.sf.webphotos.model.AlbumVO; |
45 | |
import net.sf.webphotos.model.CategoryVO; |
46 | |
import net.sf.webphotos.model.PhotoVO; |
47 | |
import net.sf.webphotos.tools.Thumbnail; |
48 | |
import net.sf.webphotos.util.ApplicationContextResource; |
49 | |
import net.sf.webphotos.util.Util; |
50 | |
import net.sf.webphotos.util.legacy.CacheFTP; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class AcaoAlterarAlbum extends AbstractAction { |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private static final long serialVersionUID = 7297664420604720262L; |
63 | |
JButton btAlterar, btNovo; |
64 | |
JTable tbAlbuns, tbFotos; |
65 | 3 | private RowSet rowSet = BancoImagem.getRSet(); |
66 | 3 | private AlbumDAO albumDAO = (AlbumDAO) ApplicationContextResource.getBean("albunsDAO"); |
67 | |
private boolean sucesso; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 3 | public AcaoAlterarAlbum(JButton botaoNovo, JButton botaoAlterar) { |
80 | 3 | btAlterar = botaoAlterar; |
81 | 3 | btNovo = botaoNovo; |
82 | 3 | tbAlbuns = PainelWebFotos.getTbAlbuns(); |
83 | 3 | tbFotos = PainelWebFotos.getTbFotos(); |
84 | 3 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
@Override |
100 | |
public void actionPerformed(ActionEvent ev) { |
101 | |
|
102 | 6 | if (ev.getSource() == btNovo && btNovo.getActionCommand().equals(PainelWebFotos.ACAO_NOVO)) { |
103 | 3 | PainelWebFotos.botaoNovo(); |
104 | |
} |
105 | |
|
106 | |
|
107 | 3 | else if (btAlterar.getActionCommand().equals(PainelWebFotos.ACAO_CANCELAR) && ev.getSource() == btAlterar) { |
108 | 0 | PainelWebFotos.botaoCancelar(); |
109 | |
} |
110 | |
|
111 | 3 | else if ((btAlterar.getActionCommand().equals(PainelWebFotos.ACAO_ALTERAR) && ev.getSource() == btAlterar) || (btNovo.getActionCommand().equals(PainelWebFotos.ACAO_FINALIZAR) && ev.getSource() == btNovo)) { |
112 | 3 | if (!PainelWebFotos.atualizaAlbum()) { |
113 | 0 | return; |
114 | |
} |
115 | 3 | executaAlteracoes(); |
116 | 3 | if (ev.getSource() == btNovo) { |
117 | 3 | PainelWebFotos.botaoFinalizar(); |
118 | |
} |
119 | |
} |
120 | 6 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public void executaAlteracoes() { |
135 | |
|
136 | 3 | Album album = Album.getAlbum(); |
137 | 3 | PhotoDTO[] fotos = album.getFotos(); |
138 | |
PhotoDTO f; |
139 | 3 | int albumID = album.getAlbumID(); |
140 | 3 | sucesso = true; |
141 | |
|
142 | 3 | PainelWebFotos.setCursorWait(true); |
143 | |
|
144 | 3 | AlbumVO albumVO = null; |
145 | |
|
146 | |
|
147 | |
|
148 | |
try { |
149 | 3 | final HashSet<PhotoDTO> newHashSet = Sets.newHashSet(fotos); |
150 | 3 | final Collection<PhotoVO> transformedCollection = Collections2.transform(newHashSet, PhotoDTO.FROM_PHOTODTO_PHOTOVO); |
151 | 3 | final HashSet<PhotoVO> photosVO = new HashSet<PhotoVO>(transformedCollection); |
152 | 3 | albumVO = AlbumVO.builder(albumID) |
153 | |
.withAlbumName(album.getNmAlbum()) |
154 | |
.withDescription(album.getDescricao()) |
155 | |
.withCreationDate(parseDate(album.getDtInsercao())) |
156 | |
.withCategory(new CategoryVO(album.getCategoriaID(), album.getCategoria(album.getCategoriaID()))) |
157 | |
.withPhotos(photosVO) |
158 | |
.build(); |
159 | 3 | albumDAO.save(albumVO); |
160 | |
|
161 | 3 | albumID = albumVO.getAlbumid(); |
162 | 3 | album.setAlbumID(albumID); |
163 | |
|
164 | 3 | sucesso = true; |
165 | 0 | } catch (Exception ex) { |
166 | 0 | Logger.getLogger(AcaoAlterarAlbum.class.getName()).log(Level.SEVERE, null, ex); |
167 | 0 | sucesso = false; |
168 | 3 | } |
169 | |
|
170 | |
|
171 | |
|
172 | 3 | String caminhoAlbum = Util.getFolder("albunsRoot").getPath() + File.separator + albumID; |
173 | |
|
174 | |
|
175 | 3 | File diretorioAlbum = new File(caminhoAlbum); |
176 | 3 | if (!diretorioAlbum.isDirectory()) { |
177 | |
|
178 | 0 | if (!diretorioAlbum.mkdir()) { |
179 | 0 | Util.log("[AcaoAlterarAlbum.executaAlteracoes.7]/ERRO: diretorio " + caminhoAlbum + " não pode ser criado. abortando"); |
180 | 0 | return; |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
|
185 | 3 | for (Iterator<PhotoVO> it = albumVO.getPhotos().iterator(); it.hasNext();) { |
186 | 6 | PhotoVO photoVO = it.next(); |
187 | |
|
188 | 6 | if (photoVO.getCaminhoArquivo().length() > 0) { |
189 | |
try { |
190 | 6 | FileChannel canalOrigem = new FileInputStream(photoVO.getCaminhoArquivo()).getChannel(); |
191 | 6 | FileChannel canalDestino = new FileOutputStream( |
192 | |
caminhoAlbum + File.separator + photoVO.getId() + ".jpg").getChannel(); |
193 | 6 | canalDestino.transferFrom(canalOrigem, 0, canalOrigem.size()); |
194 | 0 | } catch (Exception e) { |
195 | 0 | Util.log("[AcaoAlterarAlbum.executaAlteracoes.8]/ERRO: " + e); |
196 | 0 | sucesso = false; |
197 | 6 | } |
198 | |
} |
199 | 6 | } |
200 | |
|
201 | 3 | prepareThumbsAndFTP(albumVO, caminhoAlbum); |
202 | |
|
203 | 3 | prepareExtraFiles(album, caminhoAlbum); |
204 | |
|
205 | 3 | fireChangesToGUI(fotos); |
206 | |
|
207 | 3 | dispatchAlbum(); |
208 | |
|
209 | 3 | PainelWebFotos.setCursorWait(false); |
210 | 3 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
private void prepareThumbsAndFTP(AlbumVO albumVO, String caminhoAlbum) { |
220 | 3 | Set<PhotoVO> photoVOs = albumVO.getPhotos(); |
221 | |
|
222 | 3 | for (Iterator<PhotoVO> it = photoVOs.iterator(); it.hasNext();) { |
223 | 6 | PhotoVO photoVO = it.next(); |
224 | |
|
225 | |
String caminhoArquivo; |
226 | |
|
227 | |
|
228 | 6 | if (photoVO.getCaminhoArquivo().length() > 0) { |
229 | 6 | caminhoArquivo = caminhoAlbum + File.separator + photoVO.getId() + ".jpg"; |
230 | 6 | Thumbnail.makeThumbs(caminhoArquivo); |
231 | |
|
232 | |
|
233 | 6 | CacheFTP.getCache().addCommand(CacheFTP.UPLOAD, albumVO.getAlbumid(), |
234 | |
photoVO.getId()); |
235 | |
} |
236 | 6 | } |
237 | 3 | } |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
private void prepareExtraFiles(Album album, String caminhoAlbum) { |
246 | |
|
247 | |
try { |
248 | 3 | FileWriter out = new FileWriter(caminhoAlbum + File.separator + album.getAlbumID() + ".js"); |
249 | 3 | out.write(album.toJavaScript()); |
250 | 3 | out.flush(); |
251 | 3 | out.close(); |
252 | |
|
253 | 3 | out = new FileWriter(caminhoAlbum + File.separator + album.getAlbumID() + ".xml"); |
254 | 3 | out.write(album.toXML()); |
255 | 3 | out.flush(); |
256 | 3 | out.close(); |
257 | |
|
258 | 0 | } catch (IOException ex) { |
259 | 0 | ex.printStackTrace(Util.err); |
260 | 3 | } |
261 | 3 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
private void fireChangesToGUI(PhotoDTO[] fotos) { |
269 | |
PhotoDTO f; |
270 | |
|
271 | 9 | for (int i = 0; i < fotos.length; i++) { |
272 | 6 | f = fotos[i]; |
273 | 6 | f.resetCaminhoArquivo(); |
274 | |
} |
275 | |
|
276 | |
try { |
277 | |
|
278 | 3 | TableModelFoto.getModel().update(); |
279 | 3 | TableModelFoto.getModel().fireTableDataChanged(); |
280 | 3 | TableModelAlbum.getModel().update(); |
281 | 0 | } catch (Exception ex) { |
282 | 0 | ex.printStackTrace(Util.err); |
283 | 3 | } |
284 | 3 | TableModelAlbum.getModel().fireTableDataChanged(); |
285 | 3 | ((javax.swing.table.AbstractTableModel) tbAlbuns.getModel()).fireTableDataChanged(); |
286 | 3 | ((javax.swing.table.AbstractTableModel) tbFotos.getModel()).fireTableDataChanged(); |
287 | |
|
288 | 3 | PainelWebFotos.alteracaoFinalizada(); |
289 | 3 | if (sucesso) { |
290 | 3 | Util.log("Operação finalizada com sucesso."); |
291 | |
} else { |
292 | 0 | Util.log("Operação finalizada com erros."); |
293 | |
} |
294 | 3 | } |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
private void dispatchAlbum() { |
300 | |
|
301 | |
|
302 | |
|
303 | 3 | boolean autoTransferir = Util.getConfig().getBoolean("autoTransferir"); |
304 | 3 | int retorno = 1; |
305 | 3 | if (!autoTransferir) { |
306 | 0 | retorno = JOptionPane.showConfirmDialog( |
307 | |
PainelWebFotos.getInstance(), |
308 | |
"Deseja publicar agora as fotos na internet?\n\nSE CLICAR EM \"NÃO\" SERÁ NECESSÁRIO ATIVAR A PUBLICAÇÃO MANUALMENTE!", |
309 | |
"Publicar fotos?", |
310 | |
JOptionPane.YES_NO_OPTION, |
311 | |
JOptionPane.WARNING_MESSAGE); |
312 | |
} |
313 | |
|
314 | 3 | if (retorno == 0 || autoTransferir) { |
315 | 3 | Thread t = new Thread(new net.sf.webphotos.gui.util.FtpClient()); |
316 | 3 | t.start(); |
317 | |
} |
318 | |
|
319 | 3 | } |
320 | |
|
321 | |
private Date parseDate(final String dtInsercao) throws ParseException { |
322 | 3 | SimpleDateFormat dataBR = new SimpleDateFormat("dd/MM/yy"); |
323 | 3 | Date dateParsed = dataBR.parse(dtInsercao); |
324 | 3 | return dateParsed; |
325 | |
} |
326 | |
} |