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.ui.swing.action.AbstractMainUIAction;
32  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
33  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
34  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroDirection;
35  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
36  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
37  import org.nuiton.i18n.I18n;
38  
39  /**
40   * Import action doing the full logic of a worker process if synchronization using server option is true,
41   * or a direct access to central data base if this option is false
42   *
43   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
44   */
45  public class ImportReferentialSynchroAtOnceAction extends AbstractMainUIAction {
46  
47      private boolean forceClearUpdateDateCache = false;
48  
49      /**
50       * <p>Constructor for ImportReferentialSynchroAtOnceAction.</p>
51       *
52       * @param handler a {@link AbstractMainUIHandler} object.
53       */
54      public ImportReferentialSynchroAtOnceAction(AbstractMainUIHandler handler) {
55          super(handler, true);
56      }
57  
58      public boolean isForceClearUpdateDateCache() {
59          return forceClearUpdateDateCache;
60      }
61  
62      public void setForceClearUpdateDateCache(boolean forceClearUpdateDateCache) {
63          this.forceClearUpdateDateCache = forceClearUpdateDateCache;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public void doAction() throws Exception {
69  
70          // Force updateDt cache to be clear, before check is need update
71          if (forceClearUpdateDateCache && getConfig().isSynchronizationUsingServer()) {
72              clearReferentialUpdateDateCacheOnServer();
73          }
74  
75          // Check if a referential import is need
76          ImportSynchroCheckAction checkAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroCheckAction.class);
77          checkAction.setCheckReferentialOnly(true);
78          checkAction.executeAndWait();
79          boolean needReferentialImport = getSynchroUIContext().getSynchroImportContext().isWithReferential();
80  
81          // If do not need to import referential, then exit
82          // else run an import (see mantis #25665)
83          if (!needReferentialImport) {
84              return;
85          }
86  
87          // Make sure the direction is correct (wee be restore at method end)
88          SynchroDirection previousDirection = getSynchroUIContext().getDirection();
89          getSynchroUIContext().setDirection(SynchroDirection.IMPORT);
90  
91          // only use REST HTTP synchro server if specified
92          if (getConfig().isSynchronizationUsingServer()) {
93  
94              ProgressionUIModel previousProgressionModel = getProgressionUIModel();
95              setProgressionUIModel(getSynchroUIContext().getProgressionModel());
96              getProgressionUIModel().setTotal(50);
97  
98              // launch synchro on server
99              AbstractSynchroAction action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroStartAction.class);
100             action.executeAndWait();
101 
102             // download
103             action = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroDownloadAction.class);
104             action.executeAndWait();
105 
106             // apply the import (without confirmation)
107             ImportSynchroApplyAction importAction = getActionFactory().createLogicAction(handler, ImportSynchroApplyAction.class);
108             importAction.setSilent(true);
109             importAction.setSkipScreenReload(true);
110             if (importAction.prepareAction()) {
111                 getActionEngine().runInternalAction(importAction);
112             }
113 
114             setProgressionUIModel(previousProgressionModel);
115         } else { // synchronize without using synchro server
116 
117             ImportSynchroDirectAction importAction = getActionFactory().createLogicAction(getSynchroHandler(), ImportSynchroDirectAction.class);
118             if (importAction.prepareAction()) {
119                 getActionEngine().runInternalAction(importAction);
120             }
121         }
122 
123         // Restore the previous direction
124         getSynchroUIContext().setDirection(previousDirection);
125     }
126 
127     private SynchroUIContext getSynchroUIContext() {
128         return getContext().getSynchroContext();
129     }
130 
131     private SynchroUIHandler getSynchroHandler() {
132         return getContext().getSynchroHandler();
133     }
134 
135     private void clearReferentialUpdateDateCacheOnServer() {
136         // Force updateDt cache to be clear - mantis #39353
137         SynchroRestClientService service = ClientServiceLocator.instance().getSynchroRestClientService();
138         try {
139 
140             // No authentication (=Anonymous) if user is not authenticate or as local user (mantis #26658)
141             AuthenticationInfo authenticationInfo = !getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()
142                     ? null
143                     : getContext().getAuthenticationInfo();
144 
145             // Check context
146             service.clearReferentialUpdateDateCache(authenticationInfo);
147 
148         } catch (QuadrigeTechnicalException e) {
149             throw new SynchroException(I18n.t("quadrige3.error.synchro.connect", e.getMessage()), e);
150         }
151     }
152 }