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