View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.observation.photo;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Download action.
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       * Constructor.
52       *
53       * @param handler Controller
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       * {@inheritDoc}
74       */
75      @Override
76      public boolean prepareAction() throws Exception {
77          return super.prepareAction() && !getToDownload().isEmpty() && getToDownload().stream().anyMatch(PhotosTableRowModel::isFileDownloadable);
78      }
79  
80      /**
81       * {@inheritDoc}
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      * {@inheritDoc}
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         // update buttons states
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 }