1 package fr.ifremer.dali.ui.swing.content.home.survey;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
48
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
62
63 private static final Log LOG = LogFactory.getLog(ValidateSurveyAction.class);
64
65
66
67
68
69
70 public ValidateSurveyAction(final SurveysTableUIHandler handler) {
71 super(handler, false);
72 }
73
74
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
95 for (SurveyDTO survey : selectedSurveys) {
96
97
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
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
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
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
140 return;
141 }
142 getContext().getActionEngine().runInternalAction(saveAction);
143 isControlError = saveAction.getControlMessages().isErrorMessages();
144 }
145
146
147 controlledSurveys = Lists.newArrayList();
148 for (SurveyDTO survey : selectedSurveys) {
149 if (survey.isDirty()) {
150
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
159 getContext().getObservationService().validateSurveys(controlledSurveys, validationComment, createProgressionUIModel());
160
161 getModel().setModify(isModifyModelState);
162 }
163
164
165 @Override
166 public void postSuccessAction() {
167
168
169 getModel().getMainUIModel().firePropertyChanged(HomeUIModel.PROPERTY_SELECTED_SURVEY, null, null);
170
171
172 if (isControlError) {
173 displayWarningMessage(t("dali.action.validate.survey.title"), t("dali.action.validate.survey.warning.notControlled"));
174 }
175
176
177 List<SurveyDTO> notReadyToSynchronizeSurveys = Lists.newArrayList();
178 for (SurveyDTO validatedSurvey : controlledSurveys) {
179
180
181 if (!SynchronizationStatusValues.READY_TO_SYNCHRONIZE.equals(validatedSurvey.getSynchronizationStatus())) {
182 notReadyToSynchronizeSurveys.add(validatedSurvey);
183 }
184 }
185
186
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
202 @Override
203 protected void releaseAction() {
204
205 isControlError = false;
206 controlledSurveys = Lists.newArrayList();
207
208 super.releaseAction();
209 }
210 }