View Javadoc
1   package fr.ifremer.dali.ui.swing.action;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2014 - 2017 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.dali.ui.swing.util.DaliUI;
25  import fr.ifremer.quadrige3.core.exception.BadUpdateDtException;
26  import fr.ifremer.quadrige3.core.exception.Exceptions;
27  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
28  import fr.ifremer.quadrige3.core.exception.SaveForbiddenException;
29  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
30  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
31  import fr.ifremer.quadrige3.ui.swing.DialogHelper;
32  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import javax.swing.JOptionPane;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * @author peck7 on 04/07/2017.
42   */
43  public abstract class AbstractDaliRemoteSaveAction<M extends AbstractBeanUIModel, UI extends DaliUI<M, ?>, H extends AbstractUIHandler<M, UI>>
44          extends AbstractDaliSaveAction<M, UI, H> {
45  
46      private static final Log LOG = LogFactory.getLog(AbstractDaliRemoteSaveAction.class);
47  
48      private boolean saveAborted = false;
49  
50      protected abstract void doSave();
51  
52      /**
53       * <p>Constructor for AbstractAction.</p>
54       *
55       * @param handler  a H object.
56       * @param hideBody a boolean.
57       */
58      public AbstractDaliRemoteSaveAction(H handler, boolean hideBody) {
59          super(handler, hideBody);
60      }
61  
62      public boolean isSaveAborted() {
63          return saveAborted;
64      }
65  
66      @Override
67      public boolean prepareAction() throws Exception {
68          if (!super.prepareAction()) {
69              return false;
70          }
71  
72          // if the screen can be saved and the screen is valid
73          if (!getModel().isModify()) {
74              return false;
75          }
76          if (!getModel().isValid()) {
77              displayErrorMessage(t("dali.action.save.errors.title"), t("dali.action.save.errors.remove"));
78              return false;
79          }
80          return true;
81  
82      }
83  
84      @Override
85      public void doAction() throws Exception {
86  
87          // Check server connection (Mantis #47532)
88          try {
89              getContext().getProgramStrategyService().getRemoteProgramsByUser(getContext().getAuthenticationInfo());
90          } catch (QuadrigeTechnicalException ex) {
91              LOG.warn("Connection to synchronization server failed", ex);
92              displayWarningMessage(t("dali.action.save.errors.title"), t("dali.error.synchro.serverUnavailable"));
93  
94              saveAborted = true;
95              return;
96          }
97  
98          // Perform save action and reload
99          try {
100 
101             doSave();
102 
103             reload();
104 
105         } catch (Throwable throwable) {
106 
107             // If a concurrent save occurs, ask user to import referential
108             if (Exceptions.hasCause(throwable, BadUpdateDtException.class)) {
109                 LOG.warn(throwable.getMessage(), throwable);
110                 boolean confirmImportReferential = getContext().getDialogHelper().showOptionDialog(null,
111                         ApplicationUIUtil.getHtmlString(t("dali.action.common.import.referential.confirm")),
112                         t("dali.action.save.errors.title"),
113                         JOptionPane.ERROR_MESSAGE,
114                         DialogHelper.CUSTOM_OPTION,
115                         t("dali.common.import"),
116                         t("dali.common.cancel")
117                 ) == JOptionPane.OK_OPTION;
118 
119                 if (confirmImportReferential) {
120 
121                     // Import referential
122                     doImportReferential();
123 
124                     // then reload
125                     reload();
126 
127                 } else {
128 
129                     // Abort save, don't reload
130                     saveAborted = true;
131                 }
132 
133             } else if (Exceptions.hasCause(throwable, SaveForbiddenException.class)) {
134                 SaveForbiddenException exception = (SaveForbiddenException) Exceptions.getCause(throwable, SaveForbiddenException.class);
135 
136                 onSaveForbiddenException(exception);
137 
138                 saveAborted = true;
139 
140             } else
141                 // throw other exceptions
142                 throw throwable;
143 
144         }
145 
146     }
147 
148     protected void reload() {
149         // nothing by default
150     }
151 
152     protected void onSaveForbiddenException(SaveForbiddenException exception) {
153         // nothing by default
154     }
155 
156     @Override
157     public void postSuccessAction() {
158         if (isSaveAborted()) {
159             // no reload if aborted
160             return;
161         }
162 
163         super.postSuccessAction();
164     }
165 
166     @Override
167     protected void releaseAction() {
168 
169         saveAborted = false;
170 
171         super.releaseAction();
172     }
173 }