View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.observation.shared;
2   
3   /*-
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2019 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 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   * @author peck7 on 08/01/2019.
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      // used with multi edit model
78      private List<ColumnIdentifier<? extends R>> multipleValuesOnIdentifier;
79      private List<Integer> multipleValuesOnPmfmIds;
80  
81      /**
82       * <p>Constructor for AbstractReefDbRowUIModel.</p>
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       * {@inheritDoc}
97       */
98      @Override
99      public boolean isEditable() {
100         return !readOnly && super.isEditable();
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     protected B newBean() {
108         return null;
109     }
110 
111     /**
112      * <p>Getter for the field <code>individualId</code>.</p>
113      *
114      * @return the indivualId
115      */
116     public Integer getIndividualId() {
117         return individualId;
118     }
119 
120     /**
121      * <p>Setter for the field <code>individualId</code>.</p>
122      *
123      * @param individualId the indivualId to set
124      */
125     public void setIndividualId(Integer individualId) {
126         this.individualId = individualId;
127     }
128 
129     /**
130      * <p>Getter for the field <code>taxonGroup</code>.</p>
131      *
132      * @return the groupTaxon
133      */
134     public TaxonGroupDTO getTaxonGroup() {
135         return taxonGroup;
136     }
137 
138     /**
139      * <p>Setter for the field <code>taxonGroup</code>.</p>
140      *
141      * @param taxonGroup the taxonGroup to set
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      * <p>Getter for the field <code>taxon</code>.</p>
151      *
152      * @return a {@link fr.ifremer.reefdb.dto.referential.TaxonDTO} object.
153      */
154     public TaxonDTO getTaxon() {
155         return taxon;
156     }
157 
158     /**
159      * <p>Setter for the field <code>taxon</code>.</p>
160      *
161      * @param taxon a {@link fr.ifremer.reefdb.dto.referential.TaxonDTO} object.
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      * {@inheritDoc}
191      */
192     @Override
193     public String getComment() {
194         return comment;
195     }
196 
197     /**
198      * {@inheritDoc}
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      * <p>Getter for the field <code>analyst</code>.</p>
209      *
210      * @return the analyst
211      */
212     public DepartmentDTO getAnalyst() {
213         return analyst;
214     }
215 
216     /**
217      * <p>Setter for the field <code>analyst</code>.</p>
218      *
219      * @param analyst the analyst to set
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      * {@inheritDoc}
229      */
230     @Override
231     public List<PmfmDTO> getPmfms() {
232         return getIndividualPmfms();
233     }
234 
235     /**
236      * {@inheritDoc}
237      */
238     @Override
239     public List<MeasurementDTO> getMeasurements() {
240         return getIndividualMeasurements();
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public List<PmfmDTO> getIndividualPmfms() {
248         return individualPmfms;
249     }
250 
251     /**
252      * <p>Setter for the field <code>individualPmfms</code>.</p>
253      *
254      * @param individualPmfms a {@link java.util.List} object.
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      * {@inheritDoc}
264      */
265     @Override
266     public List<MeasurementDTO> getIndividualMeasurements() {
267         return individualMeasurements;
268     }
269 
270     /**
271      * <p>Setter for the field <code>individualMeasurements</code>.</p>
272      *
273      * @param individualMeasurements a {@link java.util.List} object.
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      * {@inheritDoc}
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      * Return a unique hashCode for the row
333      *
334      * @return hashCode
335      */
336     public String rowWithMeasurementsHashCode() {
337         return rowWithMeasurementsHashCode(x -> true);
338     }
339 
340     /**
341      * Return a unique hashCode for the row
342      *
343      * @return hashCode
344      */
345     public String rowWithMeasurementsHashCode(Collection<PmfmDTO> filterPmfms) {
346         return rowWithMeasurementsHashCode(measurement -> filterPmfms.contains(measurement.getPmfm()));
347     }
348 
349     /**
350      * Return a unique hashCode for the row with specified filter
351      *
352      * @return hashCode
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 }