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