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 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
39
40 public class DeleteSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
41
42
43
44
45 private static final Log LOG = LogFactory.getLog(DeleteSurveyAction.class);
46
47 private Collection<? extends SurveyDTO> surveysToDelete;
48
49
50
51
52
53
54 public DeleteSurveyAction(final SurveysTableUIHandler handler) {
55 super(handler, false);
56 }
57
58
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
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
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
98 @Override
99 public void doAction() throws Exception {
100
101
102 final List<Integer> idObservationToDelete = DaliBeans.collectIds(surveysToDelete);
103
104
105 getContext().getObservationService().deleteSurveys(idObservationToDelete);
106
107 }
108
109
110 @Override
111 public void postSuccessAction() {
112
113 getModel().getMainUIModel().setSelectedSurvey(null);
114
115
116 getModel().deleteSelectedRows();
117
118 super.postSuccessAction();
119 }
120 }