View Javadoc
1   package fr.ifremer.dali.ui.swing.content.home;
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.survey.SurveyDTO;
28  import fr.ifremer.dali.service.control.ControlRuleMessagesBean;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
30  import fr.ifremer.quadrige3.core.dao.technical.Dates;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.util.Collection;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * Save action.
39   */
40  public class SaveAction extends AbstractDaliSaveAction<HomeUIModel, HomeUI, HomeUIHandler> {
41  
42      private Collection<? extends SurveyDTO> surveysToSave;
43  
44      private ControlRuleMessagesBean controlMessages;
45  
46      private boolean controlSilently = false;
47  
48      private boolean showControlIfSuccess = false;
49  
50      private boolean updateControlDateWhenControlSucceed = false;
51  
52      /**
53       * <p>Constructor for SaveAction.</p>
54       *
55       * @param handler a {@link fr.ifremer.dali.ui.swing.content.home.HomeUIHandler} object.
56       */
57      public SaveAction(final HomeUIHandler handler) {
58          super(handler, false);
59      }
60  
61      /**
62       * <p>Setter for the field <code>surveysToSave</code>.</p>
63       *
64       * @param surveysToSave a {@link java.util.Collection} object.
65       */
66      public void setSurveysToSave(Collection<? extends SurveyDTO> surveysToSave) {
67          this.surveysToSave = surveysToSave;
68      }
69  
70      /**
71       * <p>Setter for the field <code>controlSilently</code>.</p>
72       *
73       * @param controlSilently a boolean.
74       */
75      public void setControlSilently(boolean controlSilently) {
76          this.controlSilently = controlSilently;
77      }
78  
79      /**
80       * <p>Setter for the field <code>showControlIfSuccess</code>.</p>
81       *
82       * @param showControlIfSuccess a boolean.
83       */
84      public void setShowControlIfSuccess(boolean showControlIfSuccess) {
85          this.showControlIfSuccess = showControlIfSuccess;
86      }
87  
88      /**
89       * <p>Setter for the field <code>updateControlDateWhenControlSucceed</code>.</p>
90       *
91       * @param updateControlDateWhenControlSucceed a boolean.
92       */
93      public void setUpdateControlDateWhenControlSucceed(boolean updateControlDateWhenControlSucceed) {
94          this.updateControlDateWhenControlSucceed = updateControlDateWhenControlSucceed;
95      }
96  
97      /**
98       * <p>Getter for the field <code>controlMessages</code>.</p>
99       *
100      * @return a {@link fr.ifremer.dali.service.control.ControlRuleMessagesBean} object.
101      */
102     public ControlRuleMessagesBean getControlMessages() {
103         return controlMessages;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public boolean prepareAction() throws Exception {
111         if (!super.prepareAction()) {
112             return false;
113         }
114         // Model is valid
115         if (!getModel().isValid()) {
116 
117             // Remove error
118             displayErrorMessage(t("dali.action.save.errors.title"), t("dali.action.save.errors.remove"));
119 
120             // Stop saving
121             return false;
122         }
123 
124         // Selected surveys
125         if (surveysToSave == null) {
126             surveysToSave = getUI().getSurveysTableUI().getModel().getRows();
127         }
128 
129         if (CollectionUtils.isEmpty(surveysToSave)) {
130             return false;
131         }
132 
133         // Additional control before save
134         for (SurveyDTO survey : surveysToSave) {
135             if (survey.isDirty()) {
136 
137                 // Check existing surveys on same date, same location in table
138                 // Test removed (Mantis #42609)
139 //                boolean skipSearchInDb = false;
140 //                for (SurveyDTO otherSurvey : surveysToSave) {
141 //                    if (survey == otherSurvey) continue;
142 //                    if (survey.getDate().equals(otherSurvey.getDate())
143 //                            && survey.getLocation().equals(otherSurvey.getLocation())) {
144 //                        if (getContext().getDialogHelper().showConfirmDialog(
145 //                                t("dali.action.save.observation.alreadyExistsOnLocationAndDate", DaliBeans.toString(otherSurvey)),
146 //                                t("dali.action.save.observations"),
147 //                                JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
148 //                            return false;
149 //                        } else {
150 //                            skipSearchInDb = true;
151 //                        }
152 //                    }
153 //                }
154 
155                 // Check existing surveys on same date, same location in database
156                 // Test removed (Mantis #42609)
157 //                if (!skipSearchInDb) {
158 //                    SurveyFilterDTO surveyFilter = DaliBeanFactory.newSurveyFilterDTO();
159 //                    surveyFilter.setLocationId(survey.getLocation().getId());
160 //                    surveyFilter.setDate1(survey.getDate());
161 //                    surveyFilter.setSearchDateId(SearchDateValues.EQUALS.ordinal());
162 //                    List<SurveyDTO> existingSurveys = getContext().getObservationService().getSurveys(surveyFilter);
163 //                    if (CollectionUtils.isNotEmpty(existingSurveys)) {
164 //                        for (SurveyDTO existingSurvey : existingSurveys) {
165 //                            if (existingSurvey.getId().equals(survey.getId())) continue;
166 //                            if (getContext().getDialogHelper().showConfirmDialog(
167 //                                    t("dali.action.save.observation.alreadyExistsOnLocationAndDate", DaliBeans.toString(existingSurvey)),
168 //                                    t("dali.action.save.observations"),
169 //                                    JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
170 //                                return false;
171 //                            }
172 //                        }
173 //                    }
174 //                }
175 
176                 // Check survey date within campaign dates
177                 if (survey.getCampaign() != null) {
178                     if (survey.getDate().isBefore(Dates.convertToLocalDate(survey.getCampaign().getStartDate(), getConfig().getDbTimezone())) // TODO migrate campaign dates to LocalDate ?
179                         || (survey.getCampaign().getEndDate() != null && survey.getDate().isAfter(Dates.convertToLocalDate(survey.getCampaign().getEndDate(), getConfig().getDbTimezone())))) {
180                         // The save is invalid if the survey is outside campaign dates
181                         getContext().getDialogHelper().showErrorDialog(
182                             t("dali.action.save.observation.notInCampaign", DaliBeans.toString(survey), DaliBeans.toString(survey.getCampaign())),
183                             t("dali.action.save.observations"));
184                         return false;
185                     }
186                 }
187             }
188         }
189 
190         return true;
191     }
192 
193     /**
194      * {@inheritDoc}
195      */
196     @Override
197     public void doAction() throws Exception {
198 
199         createProgressionUIModel();
200 
201         // Save observations
202         getContext().getObservationService().saveSurveys(surveysToSave, getProgressionUIModel());
203 
204         // control them
205         controlMessages = getContext().getRulesControlService().controlSurveys(surveysToSave,
206             updateControlDateWhenControlSucceed,
207             true, /*Reset control date, when failed */
208             getProgressionUIModel());
209 
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public void postSuccessAction() {
217         super.postSuccessAction();
218 
219         sendMessage(t("dali.action.save.observations.success", surveysToSave.size()));
220 
221         // reset model modify state only if all surveys in table have been saved
222         if (surveysToSave.size() == getModel().getSurveysTableUIModel().getRowCount()) {
223             getModel().getSurveysTableUIModel().setModify(false);
224             getModel().getOperationsTableUIModel().setModify(false);
225             getModel().setModify(false);
226         }
227 
228         // update selected ids (Mantis #52824) - rollback from c1920adcbaf623511b4e44399b0b5cffc2e23868
229         if (getModel().getSelectedSurvey() != null) {
230             getContext().setSelectedSurveyId(getModel().getSelectedSurvey().getId());
231         }
232 
233         // Don't refresh nested UI if save action is called from a change screen action (Mantis #52661)
234         if (!isFromChangeScreenAction()) {
235 
236             getUI().getOperationsTableUI().getHandler().loadOperations(getModel().getSelectedSurvey());
237 
238             if (HomeUIHandler.SURVEYS_MAP_CARD.equals(getUI().getBottomPanelLayout().getSelected())) {
239                 getUI().getSurveysMapUI().getHandler().buildMapPositions();
240             }
241         }
242 
243         // If error messages
244         if (!controlSilently) {
245             getUI().getSurveysTableUI().getHandler().ensureColumnsWithErrorAreVisible(surveysToSave);
246             if (isFromChangeScreenAction()) {
247                 setAllowChangeScreen(showControlResultAndAskContinue(controlMessages));
248             } else {
249                 showControlResult(controlMessages, showControlIfSuccess);
250             }
251         }
252 
253     }
254 
255     /**
256      * {@inheritDoc}
257      */
258     @Override
259     protected void releaseAction() {
260         super.releaseAction();
261 
262         surveysToSave = null;
263         controlSilently = false;
264         showControlIfSuccess = false;
265         updateControlDateWhenControlSucceed = false;
266     }
267 }