1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.department.local.replace;
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 fr.ifremer.reefdb.dto.referential.DepartmentDTO;
27 import fr.ifremer.reefdb.service.referential.ReferentialService;
28 import fr.ifremer.reefdb.ui.swing.content.manage.referential.department.ManageDepartmentsUI;
29 import fr.ifremer.reefdb.ui.swing.content.manage.referential.replace.AbstractReplaceAction;
30
31 import javax.swing.*;
32
33 import static org.nuiton.i18n.I18n.t;
34
35
36
37
38
39
40 public class ReplaceDepartmentAction extends AbstractReplaceAction<DepartmentDTO, ReplaceDepartmentUIModel, ReplaceDepartmentUI, ReplaceDepartmentUIHandler> {
41
42
43
44
45
46
47 public ReplaceDepartmentAction(ReplaceDepartmentUIHandler handler) {
48 super(handler);
49 }
50
51
52 @Override
53 protected String getReferentialLabel() {
54 return t("reefdb.property.department");
55 }
56
57
58 @Override
59 protected boolean prepareReplaceReferential(ReferentialService service, DepartmentDTO source, DepartmentDTO target) {
60
61 if (service.isDepartmentUsedInProgram(source.getId())) {
62 String decoratedSource = decorate(source);
63 if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.programStrategy.message", decoratedSource, decoratedSource),
64 getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
65 return false;
66 }
67 setDelete(false);
68 }
69
70 if (service.isDepartmentUsedInRules(source.getId())) {
71 String decoratedSource = decorate(source);
72 if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.controlRules.message", decoratedSource, decoratedSource),
73 getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
74 return false;
75 }
76 setDelete(false);
77 }
78
79 if (service.isDepartmentUsedInValidatedData(source.getId())) {
80 String decoratedSource = decorate(source);
81 if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.validatedData.message", decoratedSource, decoratedSource),
82 getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
83 return false;
84 }
85 setDelete(false);
86 }
87
88 return true;
89 }
90
91
92 @Override
93 protected void replaceReferential(ReferentialService service, DepartmentDTO source, DepartmentDTO target, boolean delete) {
94
95 service.replaceDepartment(source, target, delete);
96 }
97
98
99 @Override
100 protected void resetCaches() {
101 }
102
103
104 @Override
105 public void postSuccessAction() {
106 super.postSuccessAction();
107
108 final ManageDepartmentsUI departmentsLocalUI = (ManageDepartmentsUI) getContext().getMainUI().getHandler().getCurrentBody();
109
110 if (departmentsLocalUI != null) {
111 SwingUtilities.invokeLater(() -> getActionEngine().runAction(departmentsLocalUI.getManageDepartmentsLocalUI().getDepartmentMenuUI().getSearchButton()));
112 }
113
114 }
115 }