1 package fr.ifremer.dali.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.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
44
45 public class ImportAction extends AbstractDaliAction<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 @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
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
83 return false;
84 }
85 }
86
87
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
103 newPhoto.setPath(null);
104 newPhoto.setFullPath(tempFile.getAbsolutePath());
105
106
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
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
142 @Override
143 protected void releaseAction() {
144 super.releaseAction();
145
146 originalImages = null;
147 newPhotos = null;
148 getModel().setLoading(false);
149 }
150 }