View Javadoc
1   package fr.ifremer.dali.ui.swing.content.home.survey;
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.ui.swing.action.AbstractDaliAction;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import java.util.Collection;
33  import java.util.List;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * Action permettant de supprimer une observation dans le tableau des observations de l ecran d accueil.
39   */
40  public class DeleteSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
41  
42      /**
43       * Loggeur.
44       */
45      private static final Log LOG = LogFactory.getLog(DeleteSurveyAction.class);
46  
47      private Collection<? extends SurveyDTO> surveysToDelete;
48  
49      /**
50       * Constructor.
51       *
52       * @param handler Handler
53       */
54      public DeleteSurveyAction(final SurveysTableUIHandler handler) {
55          super(handler, false);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public boolean prepareAction() throws Exception {
61          if (!super.prepareAction()) {
62              return false;
63          }
64  
65          if (getModel().getSelectedRows().isEmpty()) {
66              LOG.warn("Aucune observations de selectionne");
67              return false;
68          }
69  
70          surveysToDelete = getModel().getSelectedRows();
71  
72          // Check sampling operations exists
73          boolean hasSamplingOperation = false;
74          for (SurveyDTO survey : surveysToDelete) {
75  
76              if (DaliBeans.isSurveyValidated(survey)) {
77                  displayErrorMessage(t("dali.action.delete.survey.titre"), t("dali.action.delete.survey.error.isValidated"));
78                  return false;
79              }
80  
81              if (!survey.isSamplingOperationsLoaded()) {
82                  getContext().getObservationService().loadSamplingOperationsFromSurvey(survey, false);
83              }
84              if (!survey.isSamplingOperationsEmpty()) {
85                  hasSamplingOperation = true;
86              }
87          }
88  
89          // Demande de confirmation avant la suppression
90          if (hasSamplingOperation) {
91              return askBeforeDelete(t("dali.action.delete.survey.titre"), t("dali.action.delete.survey.withSamplingOperation.message"));
92          } else {
93              return askBeforeDelete(t("dali.action.delete.survey.titre"), t("dali.action.delete.survey.message"));
94          }
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public void doAction() throws Exception {
100 
101         // Selected observation ID
102         final List<Integer> idObservationToDelete = DaliBeans.collectIds(surveysToDelete);
103 
104         // Remove observations service
105         getContext().getObservationService().deleteSurveys(idObservationToDelete);
106 
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public void postSuccessAction() {
112 
113         getModel().getMainUIModel().setSelectedSurvey(null);
114 
115         // Remove form model
116         getModel().deleteSelectedRows();
117 
118         super.postSuccessAction();
119     }
120 }