1 package fr.ifremer.reefdb.ui.swing.content.manage.context;
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.ImmutableList;
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Maps;
29 import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
30 import fr.ifremer.reefdb.dto.configuration.context.ContextDTO;
31 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbSaveAction;
32
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.nuiton.i18n.I18n.t;
37
38
39
40
41 public class SaveAction extends AbstractReefDbSaveAction<ManageContextsUIModel, ManageContextsUI, ManageContextsUIHandler> {
42
43 List<? extends ContextDTO> contextsToSave;
44
45
46
47
48
49
50 public SaveAction(final ManageContextsUIHandler handler) {
51 super(handler, false);
52 }
53
54
55 @Override
56 public boolean prepareAction() throws Exception {
57
58 if (!super.prepareAction() || !getModel().isModify() || !getModel().isValid()) {
59 return false;
60 }
61
62 contextsToSave = getUI().getManageContextsListUI().getModel().getRows();
63 if (contextsToSave.isEmpty()) {
64 return false;
65 }
66
67
68 List<String> names = Lists.newArrayList();
69 for (ContextDTO contextToSave : contextsToSave) {
70 if (names.contains(contextToSave.getName())) {
71
72 getContext().getDialogHelper().showErrorDialog(t("reefdb.error.alreadyExists.label.ui", contextToSave.getName()));
73 return false;
74 } else {
75 names.add(contextToSave.getName());
76 }
77 }
78
79
80 List<ContextDTO> allContexts = getContext().getContextService().getAllContexts();
81 Map<String, Integer> contextIdsByNames = Maps.newHashMap();
82 for (ContextDTO context : allContexts) {
83 contextIdsByNames.put(context.getName(), context.getId());
84 }
85 for (ContextDTO contextToSave : contextsToSave) {
86 if (contextToSave.isDirty()) {
87 Integer existingId = contextIdsByNames.get(contextToSave.getName());
88 if (existingId != null && !existingId.equals(contextToSave.getId())) {
89
90 getContext().getDialogHelper().showErrorDialog(t("reefdb.error.alreadyExists.label.db", contextToSave.getName()));
91 return false;
92 }
93 }
94 }
95
96 return true;
97 }
98
99
100 @Override
101 public void doAction() {
102
103 getContext().getContextService().saveContexts(contextsToSave);
104
105 }
106
107 @Override
108 protected List<AbstractBeanUIModel> getModelsToModify() {
109 return ImmutableList.of(
110 getModel().getContextListModel(),
111 getModel().getFilterListModel()
112 );
113 }
114
115
116 @Override
117 public void postSuccessAction() {
118
119 getUI().getManageContextsListMenuUI().getHandler().reloadComboBox();
120
121 super.postSuccessAction();
122
123 }
124 }