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 com.google.common.collect.Lists;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28  import fr.ifremer.dali.dto.DaliBeans;
29  import fr.ifremer.dali.dto.data.survey.SurveyDTO;
30  import fr.ifremer.dali.dto.enums.SynchronizationStatusValues;
31  import fr.ifremer.dali.service.DaliBusinessException;
32  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
33  import fr.ifremer.dali.ui.swing.content.home.HomeUI;
34  import fr.ifremer.dali.ui.swing.content.home.HomeUIModel;
35  import fr.ifremer.dali.ui.swing.content.home.SaveAction;
36  import fr.ifremer.dali.ui.swing.content.home.survey.validate.ValidateSurveyUI;
37  import org.apache.commons.collections4.CollectionUtils;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import java.util.Collection;
42  import java.util.List;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * Action permettant de changer l'état d'une observation dans le tableau des
48   * observations de l ecran d accueil.
49   */
50  public class ValidateSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
51  
52      private Collection<? extends SurveyDTO> selectedSurveys;
53  
54      private List<SurveyDTO> controlledSurveys;
55  
56      private String validationComment;
57  
58      private boolean isControlError;
59  
60      /**
61       * Loggeur.
62       */
63      private static final Log LOG = LogFactory.getLog(ValidateSurveyAction.class);
64  
65      /**
66       * Constructor.
67       *
68       * @param handler Controleur
69       */
70      public ValidateSurveyAction(final SurveysTableUIHandler handler) {
71          super(handler, false);
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public boolean prepareAction() throws Exception {
77          if (!super.prepareAction()) {
78              return false;
79          }
80  
81          if (getModel().getSelectedRows().isEmpty()) {
82              LOG.warn("Aucune Observation de selectionne");
83              return false;
84          }
85  
86          if (!getContext().getDataContext().isRecorderValidator()) {
87              displayErrorMessage(t("dali.action.validate.survey.title"), t("dali.action.validate.survey.error.forbidden"));
88              return false;
89          }
90  
91          selectedSurveys = getModel().getSelectedRows();
92          controlledSurveys = Lists.newArrayList();
93  
94          // check if selected surveys can be validated
95          for (SurveyDTO survey : selectedSurveys) {
96  
97              // On ne peut pas valider une observation qui a déja été validée
98              if (survey.getValidationDate() != null) {
99                  displayWarningMessage(t("dali.action.validate.survey.title"), t("dali.action.validate.survey.error.alreadyValid"));
100                 return false;
101             }
102 
103         }
104 
105         // Demande de confirmation avant la validation
106         ValidateSurveyUI validateSurveyUI = new ValidateSurveyUI(getContext());
107         validateSurveyUI.getModel().setUnValidation(false);
108         getHandler().openDialog(validateSurveyUI);
109 
110         if (validateSurveyUI.getModel().isValid()) {
111             validationComment = validateSurveyUI.getModel().getComment();
112             return true;
113         }
114 
115         return false;
116 
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public void doAction() throws Exception {
122         boolean isModifyModelState = getModel().isModify();
123 
124         List<SurveyDTO> surveysToControl = Lists.newArrayList();
125         for (SurveyDTO survey : selectedSurveys) {
126             if (survey.isDirty() || survey.getControlDate() == null) {
127                 surveysToControl.add(survey);
128             }
129         }
130 
131         if (!surveysToControl.isEmpty()) {
132             // Must save and control dirty or uncontrolled surveys
133             SaveAction saveAction = getContext().getActionFactory().createLogicAction(getUI().getParentContainer(HomeUI.class).getHandler(), SaveAction.class);
134             saveAction.setSurveysToSave(surveysToControl);
135             saveAction.setControlSilently(true);
136             saveAction.setShowControlIfSuccess(false);
137             saveAction.setUpdateControlDateWhenControlSucceed(true);
138             if (!saveAction.prepareAction()) {
139                 // return silently when save action can not be prepared
140                 return;
141             }
142             getContext().getActionEngine().runInternalAction(saveAction);
143             isControlError = saveAction.getControlMessages().isErrorMessages();
144         }
145 
146         // Check dirty state to continue validation & Filter controlled surveys
147         controlledSurveys = Lists.newArrayList();
148         for (SurveyDTO survey : selectedSurveys) {
149             if (survey.isDirty()) {
150                 // something goes wrong when save or control
151                 throw new DaliBusinessException("something goes wrong when save or control");
152             }
153             if (survey.getControlDate() != null) {
154                 controlledSurveys.add(survey);
155             }
156         }
157 
158         // Validate controlled surveys only
159         getContext().getObservationService().validateSurveys(controlledSurveys, validationComment, createProgressionUIModel());
160 
161         getModel().setModify(isModifyModelState);
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public void postSuccessAction() {
167 
168         // force reloading operations
169         getModel().getMainUIModel().firePropertyChanged(HomeUIModel.PROPERTY_SELECTED_SURVEY, null, null);
170 
171         // Show warning when not all selected surveys have been validated
172         if (isControlError) {
173             displayWarningMessage(t("dali.action.validate.survey.title"), t("dali.action.validate.survey.warning.notControlled"));
174         }
175 
176         // if controlledSurveys is not empty: they have been validated
177         List<SurveyDTO> notReadyToSynchronizeSurveys = Lists.newArrayList();
178         for (SurveyDTO validatedSurvey : controlledSurveys) {
179 
180             // check the synchronization status
181             if (!SynchronizationStatusValues.READY_TO_SYNCHRONIZE.equals(validatedSurvey.getSynchronizationStatus())) {
182                 notReadyToSynchronizeSurveys.add(validatedSurvey);
183             }
184         }
185 
186         // show warning message if some validated surveys are not ready to synchronize
187         if (CollectionUtils.isNotEmpty(notReadyToSynchronizeSurveys)) {
188 
189             List<String> list = DaliBeans.transformCollection(notReadyToSynchronizeSurveys, DaliBeans::toString);
190             getContext().getDialogHelper().showWarningDialog(
191                     t("dali.action.validate.survey.warning.notReadyToSynchronize.message"),
192                     ApplicationUIUtil.getHtmlString(list),
193                     t("dali.action.validate.survey.warning.notReadyToSynchronize.help"),
194                     t("dali.action.validate.survey.title")
195             );
196         }
197 
198         super.postSuccessAction();
199     }
200 
201     /** {@inheritDoc} */
202     @Override
203     protected void releaseAction() {
204 
205         isControlError = false;
206         controlledSurveys = Lists.newArrayList();
207 
208         super.releaseAction();
209     }
210 }