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  
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>ExportSynchroStartAction class.</p>
40   *
41   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
42   */
43  public class ExportSynchroStartAction extends AbstractSynchroAction {
44  
45      private static final Log log = LogFactory.getLog(ExportSynchroStartAction.class);
46  
47      /**
48       * <p>Constructor for ExportSynchroStartAction.</p>
49       *
50       * @param handler a {@link SynchroUIHandler} object.
51       */
52      public ExportSynchroStartAction(SynchroUIHandler handler) {
53          super(handler);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public boolean initAction() {
59          super.initAction();
60  
61          getModel().getProgressionModel().clear();
62          getModel().getProgressionModel().setTotal(100);
63          getHandler().showProgressCard();
64  
65          return true;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public void doAction() {
71  
72          if (log.isDebugEnabled()) {
73              log.debug(String.format("try to connect to synchronization site : %s", getBaseUri()));
74          }
75  
76          boolean silentIfErrorOrNoData = getModel().getExportJobId() != null;
77  
78          // No authentication (=Anonymous) if user is not authenticate or as local user (mantis #26658)
79          AuthenticationInfo authenticationInfo = !getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()
80                  ? null
81                  : getContext().getAuthenticationInfo();
82  
83          SynchroRestClientService service = ClientServiceLocator.instance().getSynchroRestClientService();
84  
85          SynchroProgressionVO serverExportResult = service.startExport(
86                  authenticationInfo,
87                  getModel().getSynchroExportContext(),
88                  getModel().getProgressionModel());
89  
90          SynchroProgressionStatus serverExportStatus = SynchroProgressionStatus.valueOf(serverExportResult.getStatus());
91  
92          if (log.isTraceEnabled()) {
93              log.trace(String.format("serverExportStatus : %s | model status : %s ", serverExportStatus.name(), getModel().getStatus().name()));
94          }
95  
96          // If error or no data, but silent mode, do nothing (see mantis #24916)
97          if (silentIfErrorOrNoData
98                  && serverExportStatus == SynchroProgressionStatus.FAILED) {
99  
100             // Reset the import context
101             getModel().resetExportContext();
102             getModel().saveExportContext();
103         }
104 
105         // If failed, throw an exception with the message from ProgressionUIModel
106         else if (serverExportStatus == SynchroProgressionStatus.FAILED) {
107             throw new SynchroException(I18n.t("quadrige3.error.synchro.status", getModel().getProgressionModel().getMessage()));
108         }
109 
110         // If still running (not stopped elsewhere, e.g. in SynchroStopAction): update UI model
111         else if (getModel().getStatus() == SynchroProgressionStatus.RUNNING) {
112             getModel().setStatus(serverExportStatus);
113             getModel().setExportJobId(serverExportResult.getJobId());
114         }
115 
116         // save status into the synchro ui context
117         else {
118             getModel().setStatus(serverExportStatus);
119         }
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public void doneAction() {
125 
126         getModel().saveExportContext();
127 
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public void failedAction(Throwable ex) {
133         //        super.failedAction(ex); don't use abstract failedAction method when this action used in ApplicationUIAction
134         throw new QuadrigeTechnicalException(I18n.t("quadrige3.error.execute.action"), ex);
135 
136     }
137 
138 }