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.ui.swing.action.AbstractMainUIAction;
28  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
29  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
30  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroDirection;
31  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
32  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
33  
34  /**
35   * Import action doing the full logic of a worker process if synchronization using server option is true,
36   * or a direct access to central data base if this option is false
37   *
38   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
39   */
40  public class ImportDataSynchroAtOnceAction extends AbstractMainUIAction {
41  
42      /**
43       * <p>Constructor for ImportReferentialSynchroAtOnceAction.</p>
44       *
45       * @param handler a {@link AbstractMainUIHandler} object.
46       */
47      public ImportDataSynchroAtOnceAction(AbstractMainUIHandler handler) {
48          super(handler, true);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public void doAction() throws Exception {
54  
55          // Check if a referential import is need
56          ImportSynchroCheckAction checkAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroCheckAction.class);
57          checkAction.executeAndWait();
58          boolean needDataImport = getSynchroUIContext().getSynchroImportContext().isWithData();
59  
60          // If do not need to import data, then exit
61          // else run an import (see mantis #25665)
62          if (!needDataImport) {
63              return;
64          }
65  
66          // Make sure the direction is correct (wee be restore at method end)
67          SynchroDirection previousDirection = getSynchroUIContext().getDirection();
68          getSynchroUIContext().setDirection(SynchroDirection.IMPORT);
69  
70          // only use REST HTTP synchro server if specified
71          if (getConfig().isSynchronizationUsingServer()) {
72  
73              ProgressionUIModel previousProgressionModel = getProgressionUIModel();
74              setProgressionUIModel(getSynchroUIContext().getProgressionModel());
75              getProgressionUIModel().setTotal(50);
76  
77              // launch synchro on server
78              AbstractSynchroAction action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroStartAction.class);
79              action.executeAndWait();
80  
81              // download
82              action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroDownloadAction.class);
83              action.executeAndWait();
84  
85              // apply the import (without confirmation)
86              ImportSynchroApplyAction importAction = getActionFactory().createLogicAction(handler, ImportSynchroApplyAction.class);
87              importAction.setSilent(true);
88              importAction.setSkipScreenReload(true);
89              if (importAction.prepareAction()) {
90                  getActionEngine().runInternalAction(importAction);
91              }
92  
93              setProgressionUIModel(previousProgressionModel);
94          } else { // synchronize without using synchro server
95  
96              ImportSynchroDirectAction importAction = getActionFactory().createLogicAction(getSynchroHandler(), ImportSynchroDirectAction.class);
97              if (importAction.prepareAction()) {
98                  getActionEngine().runInternalAction(importAction);
99              }
100         }
101 
102         // Restore the previous direction
103         getSynchroUIContext().setDirection(previousDirection);
104     }
105 
106     private SynchroUIContext getSynchroUIContext() {
107         return getContext().getSynchroContext();
108     }
109 
110     private SynchroUIHandler getSynchroHandler() {
111         return getContext().getSynchroHandler();
112     }
113 
114 }