1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.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.base.Function;
27 import com.google.common.collect.Lists;
28 import fr.ifremer.reefdb.dto.ReefDbBeans;
29 import fr.ifremer.reefdb.dto.referential.pmfm.PmfmDTO;
30 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31 import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
32 import org.apache.commons.collections4.CollectionUtils;
33
34 import javax.annotation.Nullable;
35 import java.util.List;
36 import java.util.Set;
37
38 import static org.nuiton.i18n.I18n.t;
39
40
41
42
43 public class DeletePmfmAction extends AbstractReefDbAction<PmfmsLocalUIModel, PmfmsLocalUI, PmfmsLocalUIHandler> {
44
45 private Set<? extends PmfmDTO> toDelete;
46 boolean deleteOk = false;
47
48
49
50
51
52
53 public DeletePmfmAction(final PmfmsLocalUIHandler handler) {
54 super(handler, false);
55 }
56
57
58 @Override
59 public boolean prepareAction() throws Exception {
60
61 toDelete = getModel().getSelectedRows();
62 List<String> names = ReefDbBeans.transformCollection(toDelete, new Function<PmfmDTO, String>() {
63 @Nullable
64 @Override
65 public String apply(PmfmDTO input) {
66 return decorate(input);
67 }
68 });
69 return super.prepareAction()
70 && CollectionUtils.isNotEmpty(toDelete)
71 && askBeforeDeleteMany(
72 t("reefdb.action.delete.pmfm.title"),
73 t("reefdb.action.delete.pmfm.message"),
74 names);
75
76 }
77
78
79 @Override
80 public void doAction() {
81
82
83 List<String> used = Lists.newArrayList();
84 for (PmfmDTO pmfm : toDelete) {
85 if (pmfm.getId() != null) {
86 if (getContext().getReferentialService().isPmfmUsedInData(pmfm.getId())) {
87 used.add(decorate(pmfm));
88 }
89 }
90 }
91 if (!used.isEmpty()) {
92 getContext().getDialogHelper().showErrorDialog(
93 t("reefdb.action.delete.referential.used.title"),
94 ReefDbUIs.getHtmlString(used),
95 t("reefdb.action.delete.referential.used.data.message"));
96 return;
97 }
98
99 for (PmfmDTO pmfm : toDelete) {
100 if (pmfm.getId() != null) {
101 if (getContext().getReferentialService().isPmfmUsedInProgram(pmfm.getId())) {
102 used.add(decorate(pmfm));
103 }
104 }
105 }
106 if (!used.isEmpty()) {
107 getContext().getDialogHelper().showErrorDialog(
108 t("reefdb.action.delete.referential.used.title"),
109 ReefDbUIs.getHtmlString(used),
110 t("reefdb.action.delete.referential.used.strategy.message"));
111 return;
112 }
113
114
115 getContext().getReferentialService().deletePmfms(ReefDbBeans.collectIds(toDelete));
116 deleteOk = true;
117 }
118
119
120 @Override
121 public void postSuccessAction() {
122
123 if (deleteOk) {
124 getModel().deleteSelectedRows();
125 }
126
127 super.postSuccessAction();
128 }
129
130
131 @Override
132 protected void releaseAction() {
133 deleteOk = false;
134 super.releaseAction();
135 }
136 }