1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.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.data.sampling.SamplingOperationDTO;
29 import fr.ifremer.reefdb.dto.referential.SamplingEquipmentDTO;
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 java.util.List;
35 import java.util.Set;
36
37 import static org.nuiton.i18n.I18n.t;
38
39
40
41
42 public class DeleteSamplingEquipmentAction extends AbstractReefDbAction<SamplingEquipmentsLocalUIModel, SamplingEquipmentsLocalUI, SamplingEquipmentsLocalUIHandler> {
43
44 Set<? extends SamplingEquipmentDTO> toDelete;
45 boolean deleteOk = false;
46
47
48
49
50
51
52 public DeleteSamplingEquipmentAction(final SamplingEquipmentsLocalUIHandler handler) {
53 super(handler, false);
54 }
55
56
57 @Override
58 public boolean prepareAction() throws Exception {
59
60 toDelete = getModel().getSelectedRows();
61 List<String> names = ReefDbBeans.collectProperties(toDelete, SamplingOperationDTO.PROPERTY_NAME);
62 return super.prepareAction()
63 && CollectionUtils.isNotEmpty(toDelete)
64 && askBeforeDeleteMany(
65 t("reefdb.action.delete.samplingEquipment.title"),
66 t("reefdb.action.delete.samplingEquipment.message"),
67 names);
68 }
69
70
71 @Override
72 public void doAction() {
73
74
75 List<String> used = Lists.newArrayList();
76 for (SamplingEquipmentDTO samplingEquipment : toDelete) {
77 if (samplingEquipment.getId() != null) {
78 if (getContext().getReferentialService().isSamplingEquipmentUsedInData(samplingEquipment.getId())) {
79 used.add(samplingEquipment.getName());
80 }
81 }
82 }
83
84 if (used.isEmpty()) {
85 getContext().getReferentialService().deleteSamplingEquipments(ReefDbBeans.collectIds(toDelete));
86 deleteOk = true;
87 } else {
88 getContext().getDialogHelper().showErrorDialog(
89 t("reefdb.action.delete.referential.used.title"),
90 ReefDbUIs.getHtmlString(used),
91 t("reefdb.action.delete.referential.used.data.message"));
92 }
93 }
94
95
96 @Override
97 public void postSuccessAction() {
98
99 if (deleteOk) {
100 getModel().deleteSelectedRows();
101 getUI().getSamplingEquipmentsLocalMenuUI().getHandler().reloadComboBox();
102 }
103
104 super.postSuccessAction();
105 }
106
107
108 @Override
109 protected void releaseAction() {
110 deleteOk = false;
111 super.releaseAction();
112 }
113 }