1 package fr.ifremer.reefdb.ui.swing.content.observation.shared;
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 com.google.common.collect.Lists;
27 import fr.ifremer.quadrige3.core.dao.technical.factorization.pmfm.AllowedQualitativeValuesMap;
28 import fr.ifremer.quadrige3.ui.core.dto.CommentAware;
29 import fr.ifremer.quadrige3.ui.swing.table.ColumnIdentifier;
30 import fr.ifremer.reefdb.dto.ErrorAware;
31 import fr.ifremer.reefdb.dto.ErrorDTO;
32 import fr.ifremer.reefdb.dto.ReefDbBeans;
33 import fr.ifremer.reefdb.dto.data.measurement.MeasurementAware;
34 import fr.ifremer.reefdb.dto.data.measurement.MeasurementDTO;
35 import fr.ifremer.reefdb.dto.referential.DepartmentDTO;
36 import fr.ifremer.reefdb.dto.referential.TaxonDTO;
37 import fr.ifremer.reefdb.dto.referential.TaxonGroupDTO;
38 import fr.ifremer.reefdb.dto.referential.pmfm.PmfmDTO;
39 import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbRowUIModel;
40
41 import java.util.*;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
44
45
46
47
48 public abstract class AbstractMeasurementsGroupedRowModel<B extends MeasurementDTO, R extends AbstractMeasurementsGroupedRowModel<B, R>>
49 extends AbstractReefDbRowUIModel<B, R> implements CommentAware, MeasurementAware, ErrorAware {
50
51 public static String PROPERTY_INDIVIDUAL_ID = "individualId";
52 public static String PROPERTY_TAXON = "taxon";
53 public static String PROPERTY_INPUT_TAXON_ID = "inputTaxonId";
54 public static String PROPERTY_INPUT_TAXON_NAME = "inputTaxonName";
55 public static String PROPERTY_TAXON_GROUP = "taxonGroup";
56 public static String PROPERTY_COMMENT = "comment";
57 public static String PROPERTY_ANALYST = "analyst";
58 public static String PROPERTY_INDIVIDUAL_PMFMS = "individualPmfms";
59 public static String PROPERTY_INDIVIDUAL_MEASUREMENTS = "individualMeasurements";
60
61 private Integer individualId;
62 private TaxonDTO taxon;
63 private Integer inputTaxonId;
64 private String inputTaxonName;
65 private TaxonGroupDTO taxonGroup;
66
67 private String comment;
68 private DepartmentDTO analyst;
69 private List<PmfmDTO> individualPmfms;
70 private List<MeasurementDTO> individualMeasurements;
71 private final List<ErrorDTO> errors;
72 private final boolean readOnly;
73
74 private final AllowedQualitativeValuesMap allowedQualitativeValuesMap;
75 private final List<ErrorDTO> preconditionErrors;
76
77
78 private List<ColumnIdentifier<? extends R>> multipleValuesOnIdentifier;
79 private List<Integer> multipleValuesOnPmfmIds;
80
81
82
83
84 public AbstractMeasurementsGroupedRowModel(boolean readOnly) {
85 super(null, null);
86 this.individualMeasurements = new ArrayList<>();
87 this.errors = new ArrayList<>();
88 this.preconditionErrors = new ArrayList<>();
89 this.allowedQualitativeValuesMap = new AllowedQualitativeValuesMap();
90 this.multipleValuesOnIdentifier = new ArrayList<>();
91 this.multipleValuesOnPmfmIds = new ArrayList<>();
92 this.readOnly = readOnly;
93 }
94
95
96
97
98 @Override
99 public boolean isEditable() {
100 return !readOnly && super.isEditable();
101 }
102
103
104
105
106 @Override
107 protected B newBean() {
108 return null;
109 }
110
111
112
113
114
115
116 public Integer getIndividualId() {
117 return individualId;
118 }
119
120
121
122
123
124
125 public void setIndividualId(Integer individualId) {
126 this.individualId = individualId;
127 }
128
129
130
131
132
133
134 public TaxonGroupDTO getTaxonGroup() {
135 return taxonGroup;
136 }
137
138
139
140
141
142
143 public void setTaxonGroup(TaxonGroupDTO taxonGroup) {
144 TaxonGroupDTO oldValue = this.taxonGroup;
145 this.taxonGroup = taxonGroup;
146 firePropertyChange(PROPERTY_TAXON_GROUP, oldValue, taxonGroup);
147 }
148
149
150
151
152
153
154 public TaxonDTO getTaxon() {
155 return taxon;
156 }
157
158
159
160
161
162
163 public void setTaxon(TaxonDTO taxon) {
164 TaxonDTO oldValue = this.taxon;
165 this.taxon = taxon;
166 firePropertyChange(PROPERTY_TAXON, oldValue, taxon);
167 }
168
169 public Integer getInputTaxonId() {
170 return inputTaxonId;
171 }
172
173 public void setInputTaxonId(Integer inputTaxonId) {
174 Integer oldValue = this.inputTaxonId;
175 this.inputTaxonId = inputTaxonId;
176 firePropertyChange(PROPERTY_INPUT_TAXON_ID, oldValue, inputTaxonId);
177 }
178
179 public String getInputTaxonName() {
180 return inputTaxonName;
181 }
182
183 public void setInputTaxonName(String inputTaxonName) {
184 String oldValue = this.inputTaxonName;
185 this.inputTaxonName = inputTaxonName;
186 firePropertyChange(PROPERTY_INPUT_TAXON_NAME, oldValue, inputTaxonName);
187 }
188
189
190
191
192 @Override
193 public String getComment() {
194 return comment;
195 }
196
197
198
199
200 @Override
201 public void setComment(String comment) {
202 String oldValue = this.comment;
203 this.comment = comment;
204 firePropertyChange(PROPERTY_COMMENT, oldValue, comment);
205 }
206
207
208
209
210
211
212 public DepartmentDTO getAnalyst() {
213 return analyst;
214 }
215
216
217
218
219
220
221 public void setAnalyst(DepartmentDTO analyst) {
222 DepartmentDTO oldValue = this.analyst;
223 this.analyst = analyst;
224 firePropertyChange(PROPERTY_ANALYST, oldValue, analyst);
225 }
226
227
228
229
230 @Override
231 public List<PmfmDTO> getPmfms() {
232 return getIndividualPmfms();
233 }
234
235
236
237
238 @Override
239 public List<MeasurementDTO> getMeasurements() {
240 return getIndividualMeasurements();
241 }
242
243
244
245
246 @Override
247 public List<PmfmDTO> getIndividualPmfms() {
248 return individualPmfms;
249 }
250
251
252
253
254
255
256 public void setIndividualPmfms(List<PmfmDTO> individualPmfms) {
257 List<PmfmDTO> oldValue = this.individualPmfms;
258 this.individualPmfms = individualPmfms;
259 firePropertyChange(PROPERTY_INDIVIDUAL_PMFMS, oldValue, individualPmfms);
260 }
261
262
263
264
265 @Override
266 public List<MeasurementDTO> getIndividualMeasurements() {
267 return individualMeasurements;
268 }
269
270
271
272
273
274
275 public void setIndividualMeasurements(List<MeasurementDTO> individualMeasurements) {
276 List<MeasurementDTO> oldValue = this.individualMeasurements;
277 this.individualMeasurements = individualMeasurements;
278 firePropertyChange(PROPERTY_INDIVIDUAL_MEASUREMENTS, oldValue, individualMeasurements);
279 }
280
281 public AllowedQualitativeValuesMap getAllowedQualitativeValuesMap() {
282 return allowedQualitativeValuesMap;
283 }
284
285
286
287
288 @Override
289 public Collection<ErrorDTO> getErrors() {
290 return errors;
291 }
292
293 public List<ErrorDTO> getPreconditionErrors() {
294 return preconditionErrors;
295 }
296
297 public List<ColumnIdentifier<? extends R>> getMultipleValuesOnIdentifier() {
298 return multipleValuesOnIdentifier;
299 }
300
301 public void addMultipleValuesOnIdentifier(ColumnIdentifier<? extends R> identifier) {
302 multipleValuesOnIdentifier.add(identifier);
303 }
304
305 public List<Integer> getMultipleValuesOnPmfmIds() {
306 return multipleValuesOnPmfmIds;
307 }
308
309 public void addMultipleValuesOnPmfmId(Integer pmfmId) {
310 multipleValuesOnPmfmIds.add(pmfmId);
311 }
312
313 public boolean isSameRow(AbstractMeasurementsGroupedRowModel<?, ?> that) {
314 return Objects.equals(this.getTaxonGroup(), that.getTaxonGroup())
315 && Objects.equals(this.getTaxon(), that.getTaxon());
316 }
317
318 public List<String> getDefaultProperties() {
319 return Lists.newArrayList(PROPERTY_TAXON_GROUP, PROPERTY_TAXON);
320 }
321
322 public boolean hasTaxonInformation() {
323 return getTaxonGroup() != null || getTaxon() != null;
324 }
325
326 public boolean hasNonEmptyMeasurements() {
327 return getIndividualMeasurements().stream().anyMatch(measurement -> !ReefDbBeans.isMeasurementEmpty(measurement));
328 }
329
330
331
332
333
334
335
336 public String rowWithMeasurementsHashCode() {
337 return rowWithMeasurementsHashCode(x -> true);
338 }
339
340
341
342
343
344
345 public String rowWithMeasurementsHashCode(Collection<PmfmDTO> filterPmfms) {
346 return rowWithMeasurementsHashCode(measurement -> filterPmfms.contains(measurement.getPmfm()));
347 }
348
349
350
351
352
353
354 private String rowWithMeasurementsHashCode(Predicate<MeasurementDTO> filterPredicate) {
355 StringBuilder sb = new StringBuilder();
356 sb.append(getTaxonGroup() != null ? getTaxonGroup().getId() : null).append('|');
357 sb.append(getTaxon() != null ? getTaxon().getId() : null).append('|');
358 if (getIndividualMeasurements() != null) {
359 sb.append(
360 getIndividualMeasurements().stream()
361 .filter(filterPredicate)
362 .filter(measurement -> !ReefDbBeans.isMeasurementEmpty(measurement))
363 .sorted(Comparator.comparing(o -> o.getPmfm().getId()))
364 .map(measurement -> measurement.getPmfm().getId().toString() + ':' +
365 (measurement.getPmfm().getParameter().isQualitative()
366 ? String.valueOf(measurement.getQualitativeValue() != null ? measurement.getQualitativeValue().getId() : null)
367 : String.valueOf(measurement.getNumericalValue())))
368 .collect(Collectors.joining("|"))
369 );
370 } else {
371 sb.append('N');
372 }
373 return sb.toString();
374 }
375 }