View Javadoc
1   package fr.ifremer.dali.ui.swing.content.observation.photo;
2   
3   /*
4    * #%L
5    * Dali :: 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.dali.dto.DaliBeanFactory;
27  import fr.ifremer.dali.dto.data.photo.PhotoDTO;
28  import fr.ifremer.dali.dto.data.sampling.SamplingOperationDTO;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
30  import fr.ifremer.quadrige3.core.dao.technical.Files;
31  import fr.ifremer.quadrige3.core.dao.technical.Images;
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 AbstractDaliAction<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      /** {@inheritDoc} */
60      @Override
61      public boolean prepareAction() throws Exception {
62          if (!super.prepareAction()) {
63              return false;
64          }
65  
66          originalImages = chooseFiles(
67                  t("dali.action.photo.import.chooseFile.title"),
68                  t("dali.action.photo.import.chooseFile.buttonLabel"),
69                  "(.+(\\.(?i)(" + Images.AVAILABLE_EXTENSION + "))$)",
70                  t("dali.action.photo.import.chooseFile.filterDescription", Images.AVAILABLE_EXTENSION_LIST.toString().toUpperCase()));
71  
72          if (CollectionUtils.isNotEmpty(originalImages)) {
73              // get file size
74              long maxSize = getConfig().getSynchroPhotoMaxSize();
75              if (maxSize > 0 && originalImages.stream().anyMatch(file -> file.length() > maxSize)) {
76                  displayErrorMessage(getActionDescription(),
77                          t("dali.action.photo.import.fileTooLarge.message", maxSize, Files.byteCountToDisplaySize(maxSize)));
78                  return false;
79              }
80              return true;
81          } else {
82              // no selection
83              return false;
84          }
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public void doAction() throws Exception {
90  
91          getModel().setLoading(true);
92          newPhotos = new ArrayList<>();
93  
94          originalImages.forEach(originalImage -> {
95  
96              File tempFile = Images.importAndPrepareImageFile(originalImage, getConfig().getTempDirectory());
97  
98              PhotoDTO newPhoto = DaliBeanFactory.newPhotoDTO();
99              newPhoto.setDirty(true);
100             newPhoto.setDate(new Date());
101             newPhoto.setName(originalImage.getName());
102             // set path file to null
103             newPhoto.setPath(null);
104             newPhoto.setFullPath(tempFile.getAbsolutePath());
105 
106             // set sampling operation
107             if (getContext().getSelectedSamplingOperationId() != null) {
108                 for (SamplingOperationDTO samplingOperation : getModel().getObservationModel().getSamplingOperations()) {
109                     if (Objects.equals(samplingOperation.getId(), getContext().getSelectedSamplingOperationId())) {
110                         newPhoto.setSamplingOperation(samplingOperation);
111                         break;
112                     }
113                 }
114             }
115 
116             newPhotos.add(newPhoto);
117         });
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public void postSuccessAction() {
123         super.postSuccessAction();
124 
125         if (CollectionUtils.isNotEmpty(newPhotos)) {
126 
127             PhotosTableRowModel firstRow = null;
128             for (PhotoDTO newPhoto : newPhotos) {
129                 PhotosTableRowModel newRow = getModel().addNewRow(newPhoto);
130                 if (firstRow == null)
131                     firstRow = newRow;
132             }
133             if (firstRow != null)
134                 getHandler().setFocusOnCell(firstRow);
135 
136             getModel().setModify(true);
137         }
138 
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     protected void releaseAction() {
144         super.releaseAction();
145 
146         originalImages = null;
147         newPhotos = null;
148         getModel().setLoading(false);
149     }
150 }