1 package fr.ifremer.reefdb.ui.swing.content.observation.photo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.collect.ImmutableList;
27 import fr.ifremer.quadrige3.core.service.http.HttpNotFoundException;
28 import fr.ifremer.reefdb.service.ReefDbBusinessException;
29 import fr.ifremer.reefdb.service.ReefDbServiceLocator;
30 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Optional;
37 import java.util.stream.Collectors;
38
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44 public class DownloadAction extends AbstractReefDbAction<PhotosTabUIModel, PhotosTabUI, PhotosTabUIHandler> {
45
46 private static final Log LOG = LogFactory.getLog(DownloadAction.class);
47 private boolean allDownloaded;
48 private Collection<PhotosTableRowModel> toDownload;
49
50
51
52
53
54
55 public DownloadAction(PhotosTabUIHandler handler) {
56 super(handler, false);
57 setActionDescription(t("reefdb.photo.download.tip"));
58 }
59
60 public Collection<PhotosTableRowModel> getToDownload() {
61 return Optional.ofNullable(toDownload).orElse(getModel().getSelectedRows());
62 }
63
64 public void setToDownload(PhotosTableRowModel toDownload) {
65 this.toDownload = ImmutableList.of(toDownload);
66 }
67
68 public void setToDownload(Collection<PhotosTableRowModel> toDownload) {
69 this.toDownload = toDownload;
70 }
71
72
73
74
75 @Override
76 public boolean prepareAction() throws Exception {
77 return super.prepareAction() && !getToDownload().isEmpty() && getToDownload().stream().anyMatch(PhotosTableRowModel::isFileDownloadable);
78 }
79
80
81
82
83 @Override
84 public void doAction() throws Exception {
85
86 allDownloaded = true;
87 createProgressionUIModel();
88 List<PhotosTableRowModel> downloadablePhotos = getToDownload().stream().filter(PhotosTableRowModel::isFileDownloadable).collect(Collectors.toList());
89 int nbPhotos = downloadablePhotos.size();
90 int nPhoto = 0;
91
92 for (PhotosTableRowModel photo: downloadablePhotos) {
93
94 try {
95 getProgressionUIModel().setMessage(String.format("%s / %s", ++nPhoto, nbPhotos));
96 boolean downloaded = ReefDbServiceLocator.instance().getSynchroRestClientService().downloadPhoto(
97 getContext().getAuthenticationInfo(),
98 photo.getRemoteId(),
99 photo.getFullPath(),
100 getProgressionUIModel()
101 );
102 if (!downloaded) {
103 LOG.warn(String.format("the photo %s was not download, but without exception", photo.getName()));
104 }
105 allDownloaded &= downloaded;
106
107 } catch (HttpNotFoundException e) {
108 throw new ReefDbBusinessException(t("reefdb.photo.download.notFound", photo.getName()));
109 }
110
111 }
112 }
113
114
115
116
117 @Override
118 public void postSuccessAction() {
119
120 if (!allDownloaded)
121 getContext().getDialogHelper().showErrorDialog(t("reefdb.photo.download.error"));
122
123 getHandler().updatePhotoViewerContent(false);
124 getUI().invalidate();
125 getUI().repaint();
126
127 getUI().processDataBinding(PhotosTabUI.BINDING_EXPORT_PHOTO_BUTTON_ENABLED);
128 getUI().processDataBinding(PhotosTabUI.BINDING_DOWNLOAD_PHOTO_BUTTON_ENABLED);
129
130 super.postSuccessAction();
131 }
132
133 }