1 package fr.ifremer.reefdb.ui.swing.content.manage.program.programs;
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.reefdb.dto.ReefDbBeans;
28 import fr.ifremer.reefdb.dto.configuration.programStrategy.ProgramDTO;
29 import fr.ifremer.reefdb.service.administration.program.ProgramStrategyService;
30 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31 import fr.ifremer.reefdb.ui.swing.content.manage.program.ProgramsUI;
32 import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
33
34 import java.util.List;
35
36 import static org.nuiton.i18n.I18n.t;
37
38
39
40
41 public class DeleteProgramAction extends AbstractReefDbAction<ProgramsTableUIModel, ProgramsTableUI, ProgramsTableUIHandler> {
42
43 List<String> programCodes;
44
45
46
47
48
49
50 public DeleteProgramAction(final ProgramsTableUIHandler handler) {
51 super(handler, false);
52 }
53
54
55 @Override
56 public boolean prepareAction() throws Exception {
57 if (!super.prepareAction()) {
58 return false;
59 }
60
61 if (getModel().getSelectedRows().isEmpty()) {
62 return false;
63 }
64
65 programCodes = Lists.newArrayList();
66
67
68 for (ProgramDTO program : getModel().getSelectedRows()) {
69 programCodes.add(program.getCode());
70 if (!ReefDbBeans.isLocalStatus(program.getStatus())) {
71 displayErrorMessage(
72 t("reefdb.action.delete.program.title"),
73 t("reefdb.action.delete.program.error.national.message"));
74 return false;
75 }
76 }
77
78
79 return askBeforeDelete(t("reefdb.action.delete.program.title"), t("reefdb.action.delete.program.message"));
80 }
81
82
83 @Override
84 public void doAction() {
85
86
87 for (String programCode : programCodes) {
88
89 Long surveyCount = getContext().getObservationService().countSurveysWithProgramAndLocations(programCode, null);
90 if (surveyCount > 0) {
91 getContext().getDialogHelper().showErrorDialog(
92 surveyCount == 1
93 ? t("reefdb.action.delete.program.used.data.message", programCode)
94 : t("reefdb.action.delete.program.used.data.many.message", surveyCount, programCode),
95 t("reefdb.action.delete.program.title"));
96 return;
97
98 }
99 }
100
101
102 ProgramStrategyService service = getContext().getProgramStrategyService();
103 List<String> used = Lists.newArrayList();
104 for (String programCode : programCodes) {
105 if (service.isProgramUsedByRuleList(programCode)) {
106 used.add(programCode);
107 }
108 }
109 if (!used.isEmpty()) {
110 getContext().getDialogHelper().showErrorDialog(
111 t("reefdb.action.delete.referential.used.title"),
112 ReefDbUIs.getHtmlString(used),
113 t("reefdb.action.delete.referential.used.rule.message"));
114 return;
115
116 }
117 for (String programCode : programCodes) {
118 if (service.isProgramUsedByFilter(programCode)) {
119 used.add(programCode);
120 }
121 }
122 if (!used.isEmpty()) {
123 getContext().getDialogHelper().showErrorDialog(
124 t("reefdb.action.delete.referential.used.title"),
125 ReefDbUIs.getHtmlString(used),
126 t("reefdb.action.delete.referential.used.filter.message"));
127 return;
128
129 }
130 for (String programCode : programCodes) {
131 if (service.isProgramUsedByExtraction(programCode)) {
132 used.add(programCode);
133 }
134 }
135 if (!used.isEmpty()) {
136 getContext().getDialogHelper().showErrorDialog(
137 t("reefdb.action.delete.referential.used.title"),
138 ReefDbUIs.getHtmlString(used),
139 t("reefdb.action.delete.referential.used.extraction.message"));
140 return;
141
142 }
143
144
145 service.deletePrograms(programCodes);
146
147
148 getModel().deleteSelectedRows();
149
150 }
151
152
153 @Override
154 public void postSuccessAction() {
155 super.postSuccessAction();
156
157 final ProgramsUI adminProgrammeUI = getHandler().getProgramsUI();
158
159
160 adminProgrammeUI.getStrategiesTableUI().getHandler().clearTable();
161 adminProgrammeUI.getLocationsTableUI().getHandler().clearTable();
162 adminProgrammeUI.getPmfmsTableUI().getHandler().clearTable();
163
164
165 adminProgrammeUI.getMenuUI().getProgramMnemonicCombo().setData(getContext().getProgramStrategyService().getWritablePrograms());
166 adminProgrammeUI.getMenuUI().getProgramMnemonicCombo().setSelectedItem(null);
167 adminProgrammeUI.getMenuUI().getProgramCodeCombo().setData(getContext().getProgramStrategyService().getWritablePrograms());
168 adminProgrammeUI.getMenuUI().getProgramCodeCombo().setSelectedItem(null);
169 }
170 }