1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.local;
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.referential.pmfm.MethodDTO;
29 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
30 import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
31 import org.apache.commons.collections4.CollectionUtils;
32
33 import java.util.List;
34 import java.util.Set;
35
36 import static org.nuiton.i18n.I18n.t;
37
38
39
40
41 public class DeleteMethodAction extends AbstractReefDbAction<ManageMethodsLocalUIModel, ManageMethodsLocalUI, ManageMethodsLocalUIHandler> {
42
43 private Set<? extends MethodDTO> toDelete;
44 private boolean deleteOk = false;
45
46
47
48
49
50
51 public DeleteMethodAction(final ManageMethodsLocalUIHandler handler) {
52 super(handler, false);
53 }
54
55
56 @Override
57 public boolean prepareAction() throws Exception {
58
59 toDelete = getModel().getSelectedRows();
60 List<String> names = ReefDbBeans.collectProperties(toDelete, MethodDTO.PROPERTY_NAME);
61 return super.prepareAction()
62 && CollectionUtils.isNotEmpty(toDelete)
63 && askBeforeDeleteMany(
64 t("reefdb.action.delete.method.title"),
65 t("reefdb.action.delete.method.message"),
66 names);
67
68 }
69
70
71 @Override
72 public void doAction() {
73
74 List<String> used = Lists.newArrayList();
75 for (MethodDTO method : toDelete) {
76 if (method.getId() != null) {
77 if (getContext().getReferentialService().isMethodUsedInReferential(method.getId())) {
78 used.add(method.getName());
79 }
80 }
81 }
82 if (!used.isEmpty()) {
83 getContext().getDialogHelper().showErrorDialog(
84 t("reefdb.action.delete.referential.used.title"),
85 ReefDbUIs.getHtmlString(used),
86 t("reefdb.action.delete.referential.used.referential.message"));
87 return;
88 }
89
90 for (MethodDTO method : toDelete) {
91 if (method.getId() != null) {
92 if (getContext().getReferentialService().isMethodUsedInRules(method.getId())) {
93 used.add(method.getName());
94 }
95 }
96 }
97 if (!used.isEmpty()) {
98 getContext().getDialogHelper().showErrorDialog(
99 t("reefdb.action.delete.referential.used.title"),
100 ReefDbUIs.getHtmlString(used),
101 t("reefdb.action.delete.referential.used.rule.message"));
102 return;
103 }
104
105
106 getContext().getReferentialService().deleteMethods(ReefDbBeans.collectIds(toDelete));
107 deleteOk = true;
108
109 }
110
111
112 @Override
113 public void postSuccessAction() {
114
115 if (deleteOk) {
116 getModel().deleteSelectedRows();
117 getUI().getMenuUI().getHandler().reloadComboBox();
118 }
119
120 super.postSuccessAction();
121 }
122
123
124 @Override
125 protected void releaseAction() {
126 deleteOk = false;
127 super.releaseAction();
128 }
129 }