View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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.quadrige3.core.dao.technical.Assert;
27  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
28  import fr.ifremer.quadrige3.core.security.AuthenticationInfo;
29  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
30  import fr.ifremer.quadrige3.synchro.service.client.SynchroRestClientService;
31  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
32  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionVO;
33  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.nuiton.i18n.I18n;
37  
38  /**
39   * <p>ExportSynchroGetStatusAction class.</p>
40   *
41   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
42   */
43  public class ExportSynchroGetStatusAction extends AbstractSynchroAction {
44  
45      private static final Log log = LogFactory.getLog(ExportSynchroGetStatusAction.class);
46  
47      /**
48       * <p>Constructor for ExportSynchroStartAction.</p>
49       *
50       * @param handler a {@link SynchroUIHandler} object.
51       */
52      public ExportSynchroGetStatusAction(SynchroUIHandler handler) {
53          super(handler);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public boolean initAction() {
59          super.initAction();
60  
61          getHandler().showProgressCard();
62  
63          return true;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public void doAction() throws Exception {
69  
70          Assert.notNull(getModel().getSynchroExportContext());
71          Assert.notBlank(getModel().getSynchroExportContext().getJobId());
72  
73          if (log.isDebugEnabled()) {
74              log.debug(String.format("Try to retrieve export status from synchronization site : %s", getBaseUri()));
75          }
76  
77          // No authentication (=Anonymous) if user is not authenticate or as local user (mantis #26658)
78          AuthenticationInfo authenticationInfo = !getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()
79                  ? null
80                  : getContext().getAuthenticationInfo();
81  
82          SynchroRestClientService service = ClientServiceLocator.instance().getSynchroRestClientService();
83  
84          SynchroProgressionStatus serverExportStatus;
85          SynchroProgressionVO serverExportResult = service.getExportLastStatus(
86                  authenticationInfo,
87                  getModel().getSynchroExportContext().getJobId(),
88                  getModel().getProgressionModel());
89          serverExportStatus = SynchroProgressionStatus.valueOf(serverExportResult.getStatus());
90  
91          // If failed, throw an exception with the message from ProgressionUIModel
92          if (serverExportStatus == SynchroProgressionStatus.FAILED) {
93              throw new SynchroException(I18n.t("quadrige3.error.synchro.status", getModel().getProgressionModel().getMessage()));
94          }
95  
96          // If still running (not stopped elsewhere, e.g. in SynchroStopAction): update UI model
97          else if (getModel().getStatus() == SynchroProgressionStatus.RUNNING) {
98              getModel().setStatus(serverExportStatus);
99          }
100 
101         // save status into the synchro ui context
102         else {
103             getModel().setStatus(serverExportStatus);
104         }
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public void doneAction() {
110 
111         getModel().saveExportContext();
112 
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public void failedAction(Throwable ex) {
118         //  super.failedAction(ex); don't use abstract failedAction method when this action used in ApplicationUIAction
119         throw new QuadrigeTechnicalException(I18n.t("quadrige3.error.execute.action"), ex);
120 
121     }
122 
123 }