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.data.survey.SurveyDTO;
27  import fr.ifremer.dali.dto.enums.SynchronizationStatusValues;
28  import fr.ifremer.dali.service.DaliServiceLocator;
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.SearchAction;
32  import fr.ifremer.dali.ui.swing.content.home.survey.validate.ValidateSurveyUI;
33  import fr.ifremer.quadrige3.synchro.vo.SynchroImportContextVO;
34  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportDataSynchroAtOnceAction;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import javax.swing.JOptionPane;
39  import java.util.Collection;
40  import java.util.Set;
41  import java.util.stream.Collectors;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Action permettant de changer l'état d'une observation dans le tableau des
47   * observations de l ecran d accueil.
48   */
49  public class UnvalidateSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
50  
51      private enum Action {
52          UNVALIDATE,
53          IMPORT_DATA
54      }
55  
56      private Action action;
57  
58      private Collection<? extends SurveyDTO> surveyRows;
59  
60      private String unvalidationComment;
61  
62      /**
63       * Loggeur.
64       */
65      private static final Log LOG = LogFactory.getLog(UnvalidateSurveyAction.class);
66  
67      /**
68       * Constructor.
69       *
70       * @param handler Controleur
71       */
72      public UnvalidateSurveyAction(final SurveysTableUIHandler handler) {
73          super(handler, false);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public boolean prepareAction() throws Exception {
81  
82          // Default Action
83          action = Action.UNVALIDATE;
84  
85          if (!super.prepareAction()) {
86              return false;
87          }
88  
89          if (getModel().getSelectedRows().isEmpty()) {
90              LOG.warn("Aucune Observation de selectionne");
91              return false;
92          }
93  
94          if (!getContext().getDataContext().isRecorderValidator()) {
95              displayErrorMessage(t("dali.action.unvalidate.survey.title"), t("dali.action.unvalidate.survey.error.forbidden"));
96              return false;
97          }
98  
99          surveyRows = getModel().getSelectedRows();
100 
101         // check if selected surveys can be devalidated
102         boolean isSynchronized = false;
103         boolean isQualified = false;
104         for (SurveyDTO survey : surveyRows) {
105 
106             //On ne peut pas de-valider une observation qui n'a jamais été validée
107             if (survey.getValidationDate() == null) {
108                 displayWarningMessage(t("dali.action.unvalidate.survey.title"), t("dali.action.unvalidate.survey.error.notValid"));
109                 return false;
110             }
111 
112             if (SynchronizationStatusValues.SYNCHRONIZED.equals(survey.getSynchronizationStatus())) {
113                 isSynchronized = true;
114             }
115 
116             if (survey.getQualificationDate() != null) {
117                 isQualified = true;
118             }
119         }
120 
121         if (isSynchronized) {
122             displayWarningMessage(t("dali.action.unvalidate.survey.title"), t("dali.action.unvalidate.survey.warning.isSynchronized"));
123         }
124         if (isQualified) {
125 
126             if (!getContext().getDataContext().isRecorderQualifier()) {
127                 // if user is not qualifier, he cant unvalidate qualified survey
128                 displayWarningMessage(t("dali.action.unvalidate.survey.title"), t("dali.action.unvalidate.survey.error.isQualified"));
129                 return false;
130             }
131 
132             displayWarningMessage(t("dali.action.unvalidate.survey.title"), t("dali.action.unvalidate.survey.warning.isQualified"));
133         }
134 
135         // Check data updates if server available
136         try {
137             boolean doUpdate = checkDataUpdate();
138             if (doUpdate) {
139                 action = Action.IMPORT_DATA;
140             }
141         } catch (Exception e) {
142             LOG.warn("Connection to synchronization server failed, data updates ignored");
143         }
144 
145         if (action == Action.UNVALIDATE) {
146             // Ask confirmation with mandatory comment
147             ValidateSurveyUI validateSurveyUI = new ValidateSurveyUI(getContext());
148             validateSurveyUI.getModel().setUnValidation(true);
149             getHandler().openDialog(validateSurveyUI);
150 
151             if (validateSurveyUI.getModel().isValid()) {
152                 unvalidationComment = validateSurveyUI.getModel().getComment();
153             } else {
154                 // Confirmation rejected
155                 return false;
156             }
157         }
158 
159         return true;
160 
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public void doAction() throws Exception {
168 
169         if (action == Action.UNVALIDATE) {
170 
171             boolean isModifyModelState = getModel().isModify();
172 
173             // Unvalidate
174             getContext().getObservationService().unvalidateSurveys(surveyRows, unvalidationComment, createProgressionUIModel());
175 
176             getModel().setModify(isModifyModelState);
177 
178         } else if (action == Action.IMPORT_DATA) {
179 
180             doImportData();
181             LOG.info("Data have been imported, stop unvalidation action");
182 
183         }
184     }
185 
186     private boolean checkDataUpdate() {
187 
188         Set<String> programCodes = surveyRows.stream().map(survey -> survey.getProgram().getCode()).collect(Collectors.toSet());
189 
190         getContext().getSynchroContext().setImportDataProgramCodes(programCodes);
191 
192         SynchroImportContextVO contextResult = DaliServiceLocator.instance().getSynchroRestClientService().checkImportDataContext(
193             getContext().getAuthenticationInfo(),
194             getContext().getSynchroContext().getSynchroImportContext()
195         );
196 
197         if (contextResult.isWithData()) {
198 
199             // ask use to update to start import data
200             return getContext().getDialogHelper().showConfirmDialog(
201                 t("dali.action.unvalidate.survey.updateAvailable"),
202                 t("dali.action.unvalidate.survey.title"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
203 
204         }
205 
206         return false;
207     }
208 
209     private void doImportData() {
210 
211         // Keep old action description
212         String oldDescription = getActionDescription();
213 
214         // Create import data action
215         ImportDataSynchroAtOnceAction importAction = getContext().getActionFactory().createLogicAction(getContext().getMainUI().getHandler(), ImportDataSynchroAtOnceAction.class);
216 
217         // override action description
218         forceActionDescription(t("dali.action.synchro.import.data"));
219 
220         // run internally
221         getContext().getActionEngine().runInternalAction(importAction);
222 
223         // Restore description
224         forceActionDescription(oldDescription);
225 
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public void postSuccessAction() {
233 
234         if (action == Action.IMPORT_DATA) {
235             // run search to update surveys list
236             SearchAction searchAction = getActionFactory().createLogicAction(getHandler().getHomeUIHandler(), SearchAction.class);
237             getActionEngine().runInternalAction(searchAction);
238         }
239 
240         // force reloading operations
241         getModel().getMainUIModel().firePropertyChanged(HomeUIModel.PROPERTY_SELECTED_SURVEY, null, null);
242 
243         super.postSuccessAction();
244         // do something ?
245     }
246 
247 }