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 fr.ifremer.quadrige3.core.dao.technical.Files;
27  import fr.ifremer.quadrige3.core.dao.technical.Images;
28  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
29  import fr.ifremer.reefdb.dto.data.photo.PhotoDTO;
30  import fr.ifremer.reefdb.dto.data.sampling.SamplingOperationDTO;
31  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
32  import org.apache.commons.collections4.CollectionUtils;
33  
34  import java.io.File;
35  import java.util.ArrayList;
36  import java.util.Date;
37  import java.util.List;
38  import java.util.Objects;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Import action.
44   */
45  public class ImportAction extends AbstractReefDbAction<PhotosTabUIModel, PhotosTabUI, PhotosTabUIHandler> {
46  
47      private List<File> originalImages;
48      private List<PhotoDTO> newPhotos;
49  
50      /**
51       * Constructor.
52       *
53       * @param handler Controller
54       */
55      public ImportAction(PhotosTabUIHandler handler) {
56          super(handler, false);
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public boolean prepareAction() throws Exception {
64          if (!super.prepareAction()) {
65              return false;
66          }
67  
68          originalImages = chooseFiles(
69              t("reefdb.action.photo.import.chooseFile.title"),
70              t("reefdb.action.photo.import.chooseFile.buttonLabel"),
71              "(.+(\\.(?i)(" + Images.AVAILABLE_EXTENSION + "))$)",
72              t("reefdb.action.photo.import.chooseFile.filterDescription", Images.AVAILABLE_EXTENSION_LIST.toString().toUpperCase()));
73  
74          if (CollectionUtils.isNotEmpty(originalImages)) {
75  
76              // get file size
77              long maxSize = getConfig().getSynchroPhotoMaxSize();
78              if (maxSize > 0 && originalImages.stream().anyMatch(file -> file.length() > maxSize)) {
79                  displayErrorMessage(getActionDescription(),
80                      t("reefdb.action.photo.import.fileTooLarge.message", maxSize, Files.byteCountToDisplaySize(maxSize)));
81                  return false;
82              }
83  
84              return true;
85  
86          } else {
87  
88              // no selection
89              return false;
90          }
91  
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void doAction() {
99  
100         getModel().setLoading(true);
101         newPhotos = new ArrayList<>();
102 
103         originalImages.forEach(originalImage -> {
104 
105             File tempFile = Images.importAndPrepareImageFile(originalImage, getConfig().getTempDirectory());
106 
107             PhotoDTO newPhoto = ReefDbBeanFactory.newPhotoDTO();
108             newPhoto.setDirty(true);
109             newPhoto.setDate(new Date());
110             newPhoto.setName(originalImage.getName());
111             // set path file to null
112             newPhoto.setPath(null);
113             newPhoto.setFullPath(tempFile.getAbsolutePath());
114 
115             // set sampling operation
116             if (getContext().getSelectedSamplingOperationId() != null) {
117                 for (SamplingOperationDTO samplingOperation : getModel().getObservationModel().getSamplingOperations()) {
118                     if (Objects.equals(samplingOperation.getId(), getContext().getSelectedSamplingOperationId())) {
119                         newPhoto.setSamplingOperation(samplingOperation);
120                         break;
121                     }
122                 }
123             }
124 
125             newPhotos.add(newPhoto);
126 
127         });
128 
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public void postSuccessAction() {
136         super.postSuccessAction();
137 
138         if (CollectionUtils.isNotEmpty(newPhotos)) {
139 
140             PhotosTableRowModel firstRow = null;
141             for (PhotoDTO newPhoto : newPhotos) {
142                 PhotosTableRowModel newRow = getModel().addNewRow(newPhoto);
143                 if (firstRow == null)
144                     firstRow = newRow;
145             }
146             if (firstRow != null)
147                 getHandler().setFocusOnCell(firstRow);
148 
149             getModel().setModify(true);
150         }
151 
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     protected void releaseAction() {
159         super.releaseAction();
160 
161         originalImages = null;
162         newPhotos = null;
163         getModel().setLoading(false);
164     }
165 }