View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context;
2   
3   /*
4    * #%L
5    * Dali :: 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.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   * Action sauvegarder les contextes
39   */
40  public class SaveAction extends AbstractDaliSaveAction<ManageContextsUIModel, ManageContextsUI, ManageContextsUIHandler> {
41  
42      private List<? extends ContextDTO> contextsToSave;
43  
44      /**
45       * Constructor.
46       *
47       * @param handler Controller
48       */
49      public SaveAction(final ManageContextsUIHandler handler) {
50          super(handler, false);
51      }
52  
53      /** {@inheritDoc} */
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          // check name uniqueness in ui
67          List<String> names = Lists.newArrayList();
68          for (ContextDTO contextToSave : contextsToSave) {
69              if (names.contains(contextToSave.getName())) {
70                  // duplicate found
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          // check name uniqueness in db
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                      // duplicate found
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      /** {@inheritDoc} */
99      @Override
100     public void doAction() throws Exception {
101 
102         getContext().getContextService().saveContexts(contextsToSave);
103 
104     }
105 
106     /** {@inheritDoc} */
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     /** {@inheritDoc} */
116     @Override
117     public void postSuccessAction() {
118 
119         getUI().getManageContextsListMenuUI().getHandler().reloadComboBox();
120 
121         super.postSuccessAction();
122 
123     }
124 }