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 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
44
45 public class ImportAction extends AbstractReefDbAction<PhotosTabUIModel, PhotosTabUI, PhotosTabUIHandler> {
46
47 private List<File> originalImages;
48 private List<PhotoDTO> newPhotos;
49
50
51
52
53
54
55 public ImportAction(PhotosTabUIHandler handler) {
56 super(handler, false);
57 }
58
59
60
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
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
89 return false;
90 }
91
92 }
93
94
95
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
112 newPhoto.setPath(null);
113 newPhoto.setFullPath(tempFile.getAbsolutePath());
114
115
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
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
156
157 @Override
158 protected void releaseAction() {
159 super.releaseAction();
160
161 originalImages = null;
162 newPhotos = null;
163 getModel().setLoading(false);
164 }
165 }