View Javadoc
1   package fr.ifremer.dali.ui.swing.content.observation;
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.DaliBeans;
27  import fr.ifremer.dali.dto.data.measurement.MeasurementDTO;
28  import fr.ifremer.dali.service.control.ControlRuleMessagesBean;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
30  import fr.ifremer.dali.ui.swing.content.observation.operation.measurement.grouped.OperationMeasurementsGroupedRowModel;
31  import fr.ifremer.quadrige3.core.dao.technical.Dates;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Save action.
37   */
38  public class SaveAction extends AbstractDaliSaveAction<ObservationUIModel, ObservationUI, ObservationUIHandler> {
39  
40      private ControlRuleMessagesBean controlMessages;
41  
42      /**
43       * Constructor.
44       *
45       * @param handler Controller
46       */
47      public SaveAction(final ObservationUIHandler handler) {
48          super(handler, false);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public boolean prepareAction() throws Exception {
54          if (!super.prepareAction()) {
55              return false;
56          }
57  
58          // check if survey is not dirty
59          if (DaliBeans.isSurveyValidated(getModel())) {
60              getContext().getDialogHelper().showErrorDialog(t("dali.action.save.observation.alreadySynchronized"), t("dali.action.save.observation"));
61              return false;
62          }
63  
64          // Check survey date within campaign dates
65          if (getModel().getCampaign() != null) {
66              if (getModel().getDate().isBefore(Dates.convertToLocalDate(getModel().getCampaign().getStartDate(), getConfig().getDbTimezone())) // TODO migrate campaign dates to LocalDate ?
67                      || (getModel().getCampaign().getEndDate() != null && getModel().getDate().isAfter(Dates.convertToLocalDate(getModel().getCampaign().getEndDate(), getConfig().getDbTimezone())))) {
68                  // The save is invalid if the survey is outside campaign dates
69                  getContext().getDialogHelper().showErrorDialog(
70                          t("dali.action.save.observation.notInCampaign", DaliBeans.toString(getModel()), DaliBeans.toString(getModel().getCampaign())),
71                          t("dali.action.save.observations"));
72                  return false;
73              }
74          }
75  
76          // check for individual measurements with taxon (or taxon group) and without measurement value
77          if (isSurveyHasEmptyTaxonMeasurements()) {
78              getContext().getDialogHelper().showErrorDialog(t("dali.action.save.observation.emptyTaxonMeasurement"), t("dali.action.save.observation"));
79              return false;
80          }
81  
82  //        // Save partial models from tabs
83  //        getUI().getSurveyDetailsTabUI().getHandler().saveActualModel();
84  //        getUI().getPhotosTabUI().getHandler().saveActualModel();
85  //
86  //        // Save and control measurements tabs (Mantis #52401)
87  //        getUI().getOperationMeasurementsTabUI().getHandler().save();
88  
89          return true;
90      }
91  
92      private boolean isSurveyHasEmptyTaxonMeasurements() {
93  
94          // check operations individual measurement rows
95          for (OperationMeasurementsGroupedRowModel row : getModel().getOperationMeasurementsTabUIModel().getGroupedTableUIModel().getRows()) {
96              if (row.getTaxonGroup() != null || row.getTaxon() != null) {
97                  boolean rowWithoutMeasurements = true;
98                  for (MeasurementDTO measurement : row.getMeasurements()) {
99                      if (!DaliBeans.isMeasurementEmpty(measurement)) {
100                         rowWithoutMeasurements = false;
101                         break;
102                     }
103                 }
104                 if (rowWithoutMeasurements) return true;
105             }
106         }
107 
108         return false;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public void doAction() throws Exception {
114 
115         // Save partial models from tabs
116         getUI().getSurveyDetailsTabUI().getHandler().saveActualModel();
117         getUI().getPhotosTabUI().getHandler().saveActualModel();
118 
119         // Save and control measurements tabs (Mantis #52401)
120         getUI().getOperationMeasurementsTabUI().getHandler().save();
121 
122         // Save observation
123         getContext().getObservationService().saveSurvey(getModel());
124 
125         // Clear some errors
126         getModel().getSurveyDetailsTabUIModel().getUngroupedTableUIModel().getRows()
127                 .forEach(surveyMeasurementsUngroupedRowModel -> surveyMeasurementsUngroupedRowModel.setErrors(null));
128 
129         // Control observation
130         controlMessages = getContext().getRulesControlService().controlSurvey(getModel(),
131                 false /*Do not set control date, if succeed */,
132                 true /*But reset control date if KO */);
133 
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public void postSuccessAction() {
139         super.postSuccessAction();
140 
141         getModel().setModify(false);
142 
143         // show error messages
144         showControlResult(controlMessages, false);
145 
146         getHandler().refreshModels();
147 
148     }
149 
150 }