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.security.AuthenticationInfo;
27  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
28  import fr.ifremer.quadrige3.synchro.service.client.SynchroRestClientService;
29  import fr.ifremer.quadrige3.synchro.vo.SynchroImportContextVO;
30  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
31  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionVO;
32  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.i18n.I18n;
36  
37  import java.beans.PropertyChangeListener;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * <p>ImportSynchroStartAction class.</p>
43   *
44   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
45   */
46  public class ImportSynchroStartAction extends AbstractSynchroAction {
47  
48      private static final Log log = LogFactory.getLog(ImportSynchroStartAction.class);
49  
50      private boolean silentIfNoData = false;
51  
52      /**
53       * <p>Constructor for ImportSynchroStartAction.</p>
54       *
55       * @param handler a {@link SynchroUIHandler} object.
56       */
57      public ImportSynchroStartAction(SynchroUIHandler handler) {
58          super(handler);
59      }
60  
61      public void setSilentIfNoData(boolean silentIfNoData) {
62          this.silentIfNoData = silentIfNoData;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public boolean initAction() {
70          super.initAction();
71  
72          getModel().getProgressionModel().clear();
73          getModel().getProgressionModel().setTotal(100);
74          getHandler().showProgressCard();
75  
76          return true;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void doAction() throws Exception {
84  
85          if (log.isDebugEnabled()) {
86              log.debug(String.format("try to connect to synchronization site : %s", getBaseUri()));
87          }
88  
89          boolean silentIfErrorOrNoData = getModel().getImportJobId() != null;
90  
91          // Set model as RUNNING
92          getModel().setStatus(SynchroProgressionStatus.RUNNING);
93  
94          SynchroRestClientService service = ClientServiceLocator.instance().getSynchroRestClientService();
95  
96          // No authentication (=Anonymous) if user is not authenticate or as local user (mantis #26658)
97          AuthenticationInfo authenticationInfo = !getContext().isAuthenticated() || getContext().isAuthenticatedAsLocalUser()
98                  ? null
99                  : getContext().getAuthenticationInfo();
100 
101         // Add a property change listener in SynchroImportContext to retrieve jobId from the service
102         PropertyChangeListener listener = evt -> {
103             if (SynchroImportContextVO.PROPERTY_JOB_ID.equals(evt.getPropertyName())) {
104                 // Save import context to file when the jobId is set
105                 getModel().saveImportContext();
106             }
107         };
108         getModel().getSynchroImportContext().addPropertyChangeListener(listener);
109 
110         // Start import
111         SynchroProgressionVO serverImportResult = service.startImport(
112                 authenticationInfo,
113                 getModel().getSynchroImportContext(),
114                 getModel().getProgressionModel());
115 
116         SynchroProgressionStatus serverImportStatus = SynchroProgressionStatus.valueOf(serverImportResult.getStatus());
117 
118         // Can remove the listener
119         getModel().getSynchroImportContext().removePropertyChangeListener(listener);
120 
121         // If error or no data, but silent mode, do nothing (see mantis #24916)
122         if ((silentIfNoData && serverImportStatus == SynchroProgressionStatus.NO_DATA)
123                 ||
124                 (silentIfErrorOrNoData
125                         && (serverImportStatus == SynchroProgressionStatus.FAILED
126                         || serverImportStatus == SynchroProgressionStatus.NO_DATA))) {
127 
128             // Reset the import context
129             getModel().resetImportContext();
130             getModel().saveImportContext();
131         }
132 
133         // If failed, throw an exception with the message from ProgressionUIModel
134         else if (serverImportStatus == SynchroProgressionStatus.FAILED) {
135             throw new SynchroException(I18n.t("quadrige3.error.synchro.status", getModel().getProgressionModel().getMessage()));
136         }
137 
138         // If no data : display message
139         else if (serverImportStatus == SynchroProgressionStatus.NO_DATA) {
140             throw new SynchroException(I18n.t("quadrige3.error.synchro.import.noData"));
141         }
142 
143         // If still running (not stopped elsewhere, e.g. in SynchroStopAction): update UI model
144         else if (getModel().getStatus() == SynchroProgressionStatus.RUNNING) {
145             getModel().setStatus(serverImportStatus);
146             getModel().setImportJobId(serverImportResult.getJobId());
147             getModel().setTransferFilename(serverImportResult.getFileName());
148         }
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public void doneAction() {
156 
157         getModel().saveImportContext();
158 
159         // if action launched with lock, don't chain action
160         if (isWait()) {
161             return;
162         }
163 
164         SynchroProgressionStatus serverImportStatus = getModel().getStatus();
165 
166         if (serverImportStatus == SynchroProgressionStatus.SUCCESS) {
167             // download
168             ImportSynchroDownloadAction action
169                     = getContext().getActionFactory().createNonBlockingUIAction(handler, ImportSynchroDownloadAction.class);
170             action.execute();
171         }
172 
173         // If NOT_STARTED (could append) if job was previously launched
174         // (see mantis #24916 - Server error "Invalid Context")
175         else if (serverImportStatus == SynchroProgressionStatus.NOT_STARTED) {
176             // reset UI to default state
177             getUI().getHandler().report(t("quadrige3.synchro.report.idle"), false);
178         }
179     }
180 
181     @Override
182     public void disposeAction() {
183         super.disposeAction();
184         silentIfNoData = false;
185     }
186 }