View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.program;
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 fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
27  import fr.ifremer.dali.ui.swing.action.AbstractDaliRemoteSaveAction;
28  import fr.ifremer.dali.ui.swing.content.manage.program.menu.SearchAction;
29  import fr.ifremer.dali.ui.swing.content.manage.program.programs.ProgramsTableRowModel;
30  import fr.ifremer.quadrige3.core.exception.SaveForbiddenException;
31  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
32  import org.apache.commons.collections4.CollectionUtils;
33  
34  import java.util.List;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Action save a program.
40   */
41  public class SaveAction extends AbstractDaliRemoteSaveAction<ProgramsUIModel, ProgramsUI, ProgramsUIHandler> {
42  
43      private List<ProgramsTableRowModel> programsToSave;
44  
45      private List<ProgramDTO> reloadedPrograms;
46  
47      /**
48       * constructor.
49       *
50       * @param handler Controller
51       */
52      public SaveAction(final ProgramsUIHandler handler) {
53          super(handler, false);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public boolean prepareAction() throws Exception {
61  
62          if (!super.prepareAction()) {
63              return false;
64          }
65  
66          // Displayed programs
67          programsToSave = getModel().getProgramsUIModel().getRows();
68  
69          return true;
70      }
71  
72      @Override
73      protected void doSave() {
74  
75          getContext().getProgramStrategyService().savePrograms(getContext().getAuthenticationInfo(), programsToSave);
76  
77      }
78  
79      @Override
80      protected void reload() {
81  
82          reloadedPrograms = getContext().getProgramStrategyService().getWritablePrograms();
83  
84      }
85  
86      @Override
87      protected void onSaveForbiddenException(SaveForbiddenException exception) {
88  
89          if (exception.getType() == SaveForbiddenException.Type.PERMISSION) {
90  
91              if (CollectionUtils.isNotEmpty(exception.getObjectIds())) {
92                  getContext().getDialogHelper().showErrorDialog(
93                          t("dali.action.save.programs.forbidden.topMessage"),
94                          ApplicationUIUtil.getHtmlString(exception.getObjectIds()),
95                          t("dali.action.save.programs.forbidden.bottomMessage"),
96                          t("dali.action.save.errors.title"));
97              } else {
98                  getContext().getDialogHelper().showErrorDialog(
99                          t("dali.action.save.programs.forbidden.message"),
100                         t("dali.action.save.errors.title"));
101             }
102 
103         } else if (exception.getType() == SaveForbiddenException.Type.ATTACHED_DATA) {
104 
105             if (CollectionUtils.isNotEmpty(exception.getObjectIds())) {
106                 getContext().getDialogHelper().showErrorDialog(
107                         t("dali.action.save.programs.forbidden.attachedData.topMessage"),
108                         ApplicationUIUtil.getHtmlString(exception.getObjectIds()),
109                         t("dali.action.save.programs.forbidden.attachedData.bottomMessage"),
110                         t("dali.action.save.errors.title"));
111             } else {
112                 getContext().getDialogHelper().showErrorDialog(
113                         t("dali.action.save.programs.forbidden.attachedData.message"),
114                         t("dali.action.save.errors.title"));
115             }
116 
117         } else {
118 
119             // if no information on Type, throw it as is
120             throw exception;
121         }
122 
123     }
124 
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void postSuccessAction() {
131         super.postSuccessAction();
132 
133         // don't finish action if remote save is aborted (Mantis #48235)
134         if (isSaveAborted())
135             return;
136 
137         // Reload list with saved data
138         getUI().getMenuUI().getProgramMnemonicCombo().setData(reloadedPrograms);
139         getUI().getMenuUI().getProgramCodeCombo().setData(reloadedPrograms);
140 
141         getModel().setModify(false);
142 
143         // Don't search again here because if this action comes from a CheckModelAction, the search will be performed by it (Mantis #46633)
144         if (!isFromCheckModelAction()) {
145             getActionEngine().runInternalAction(getUI().getMenuUI().getHandler(), SearchAction.class);
146 
147             // if selected program, reload strategies
148             getModel().getProgramsUIModel().fireReselect();
149         }
150 
151     }
152 
153     @Override
154     protected void releaseAction() {
155         super.releaseAction();
156 
157         programsToSave = null;
158         reloadedPrograms = null;
159     }
160 
161 }