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.dto.referential.QualityLevelDTO;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
30  import fr.ifremer.dali.ui.swing.content.home.HomeUIModel;
31  import fr.ifremer.dali.ui.swing.content.home.survey.qualify.QualifySurveyUI;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import java.util.Collection;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Action permettant de changer l'état d'une observation dans le tableau des
41   * observations de l ecran d accueil.
42   */
43  public class QualifySurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
44  
45      private Collection<? extends SurveyDTO> selectedSurveys;
46  
47      private String qualificationComment;
48      private QualityLevelDTO qualityLevel;
49  
50      /**
51       * Loggeur.
52       */
53      private static final Log LOG = LogFactory.getLog(QualifySurveyAction.class);
54  
55      /**
56       * Constructor.
57       *
58       * @param handler Controleur
59       */
60      public QualifySurveyAction(final SurveysTableUIHandler handler) {
61          super(handler, false);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public boolean prepareAction() throws Exception {
67          if (!super.prepareAction()) {
68              return false;
69          }
70  
71          if (getModel().getSelectedRows().isEmpty()) {
72              LOG.warn("Aucune Observation de selectionne");
73              return false;
74          }
75  
76          if (!getContext().getDataContext().isRecorderQualifier()) {
77              displayErrorMessage(t("dali.action.qualify.survey.title"), t("dali.action.qualify.survey.error.forbidden"));
78              return false;
79          }
80  
81          selectedSurveys = getModel().getSelectedRows();
82  
83          // check if selected surveys can be qualified
84          for (SurveyDTO survey : selectedSurveys) {
85  
86              // On ne peut pas valider une observation modifiée (normallement cela n'est pas possible)
87              if (survey.isDirty()) {
88                  getContext().getDialogHelper().showWarningDialog(t("dali.action.qualify.survey.error.dirty"));
89                  return false;
90              }
91  
92              // On ne peut pas qualifier une observation qui n'est pas validée
93              if (!DaliBeans.isSurveyValidated(survey)) {
94                  displayWarningMessage(t("dali.action.qualify.survey.title"), t("dali.action.qualify.survey.error.notValid"));
95                  return false;
96              }
97  
98          }
99  
100         // Demande de confirmation avant la qualification
101         QualifySurveyUI qualifySurveyUI = new QualifySurveyUI(getContext());
102         getHandler().openDialog(qualifySurveyUI);
103 
104         if (qualifySurveyUI.getModel().isValid()) {
105             qualificationComment = qualifySurveyUI.getModel().getComment();
106             qualityLevel = qualifySurveyUI.getModel().getQualityLevel();
107             return true;
108         }
109 
110         return false;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public void doAction() throws Exception {
116         boolean isModifyModelState = getModel().isModify();
117 
118         // Qualify surveys
119         getContext().getObservationService().qualifySurveys(selectedSurveys, qualificationComment, qualityLevel, createProgressionUIModel());
120 
121         getModel().setModify(isModifyModelState);
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public void postSuccessAction() {
127 
128         // force reloading operations
129         getModel().getMainUIModel().firePropertyChanged(HomeUIModel.PROPERTY_SELECTED_SURVEY, null, null);
130 
131         super.postSuccessAction();
132     }
133 }