View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.context;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Action sauvegarder les contextes
40   */
41  public class SaveAction extends AbstractReefDbSaveAction<ManageContextsUIModel, ManageContextsUI, ManageContextsUIHandler> {
42  
43      List<? extends ContextDTO> contextsToSave;
44  
45      /**
46       * Constructor.
47       *
48       * @param handler Controller
49       */
50      public SaveAction(final ManageContextsUIHandler handler) {
51          super(handler, false);
52      }
53  
54      /** {@inheritDoc} */
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          // check name uniqueness in ui
68          List<String> names = Lists.newArrayList();
69          for (ContextDTO contextToSave : contextsToSave) {
70              if (names.contains(contextToSave.getName())) {
71                  // duplicate found
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          // check name uniqueness in db
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                      // duplicate found
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
116     @Override
117     public void postSuccessAction() {
118 
119         getUI().getManageContextsListMenuUI().getHandler().reloadComboBox();
120 
121         super.postSuccessAction();
122 
123     }
124 }