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.security.SecurityContextHelper;
28  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
29  import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
30  import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroClientExportToFileResult;
31  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
32  import fr.ifremer.quadrige3.ui.swing.action.AbstractReloadCurrentScreenAction;
33  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
34  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
35  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
36  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.nuiton.util.DateUtil;
40  
41  import java.io.File;
42  import java.util.Date;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * <p>ExportAllReferentialToFileAction class.</p>
48   *
49   * @author benoit.lavenier@e-is.pro
50   * @since 1.0
51   */
52  public class ExportReferentialToFileAction extends AbstractReloadCurrentScreenAction {
53  
54      private static final Log log = LogFactory.getLog(ExportReferentialToFileAction.class);
55  
56      private boolean hasReferential = false;
57  
58      protected File file;
59  
60      /**
61       * <p>Constructor for ExportAllReferentialToFileAction.</p>
62       *
63       * @param handler a {@link AbstractMainUIHandler} object.
64       */
65      public ExportReferentialToFileAction(AbstractMainUIHandler handler) {
66          super(handler, true);
67          setActionDescription(t("quadrige3.action.synchro.exportReferentialToFile.title"));
68      }
69  
70      /**
71       * <p>Setter for the field <code>file</code>.</p>
72       *
73       * @param file a {@link File} object.
74       */
75      public void setFile(File file) {
76          this.file = file;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public boolean prepareAction() throws Exception {
82          file = null;
83          boolean doAction = super.prepareAction();
84          if (!doAction) {
85              return false;
86          }
87  
88          String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
89  
90          // ask user file where to export db
91          file = saveFile(
92                  getConfig().getSynchroZipFilePrefix() + date,
93                  "zip",
94                  t("quadrige3.synchro.export.choose.exportReferentialToFile.title"),
95                  t("quadrige3.synchro.export.choose.exportReferentialToFile.buttonLabel"),
96                  "^.*\\.zip", t("quadrige3.common.file.zip")
97          );
98          doAction = file != null;
99  
100         return doAction;
101     }
102 
103 
104     private SynchroUIContext getSynchroUIContext() {
105         return getContext().getSynchroContext();
106     }
107 
108     private SynchroUIHandler getSynchroHandler() {
109         return getContext().getSynchroHandler();
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public void doActionBeforeReload() {
115         hasReferential = false;
116 
117         SynchroClientService synchroService = ClientServiceLocator.instance().getSynchroClientService();
118 
119         getSynchroUIContext().saveExportContext();
120 
121         ProgressionUIModel mainProgressionModel = createProgressionUIModel(100);
122         getSynchroUIContext().setStatus(SynchroProgressionStatus.RUNNING);
123         getSynchroUIContext().saveExportContext();
124 
125         // build temp database and export local to temp
126         getProgressionUIModel().setMessage(t("quadrige3.synchro.progress.export"));
127 
128         // export directory is set by the synchronization service
129         SynchroClientExportToFileResult exportResult =  synchroService.exportAllReferentialToFile(
130                 SecurityContextHelper.getQuadrigeUserId(),
131                 file,
132                 getProgressionUIModel(),
133                 100);
134 
135         // If no data to export, stop here
136         hasReferential = exportResult.getReferentialResult() != null && exportResult.getReferentialResult().getTotalTreated() > 0;
137         if (!hasReferential) {
138             showNoReferentialMessage();
139             return;
140         }
141 
142         getSynchroUIContext().setExportFileName(file.getName());
143         getSynchroUIContext().saveExportContext();
144 
145         // delegate progression model from SynchroUIContext
146         getProgressionUIModel().setTotal(100);
147         setProgressionUIModel(getSynchroUIContext().getProgressionModel());
148 
149         // restore action progression model
150         setProgressionUIModel(mainProgressionModel);
151 
152         // Skip screen reloading if not necessary
153         setSkipScreenReload(!hasReferential);
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     public void postSuccessAction() {
159 
160         super.postSuccessAction();
161         if (log.isInfoEnabled()) {
162             log.info("Synchronization export success");
163         }
164 
165         getSynchroUIContext().resetExportContext();
166         getSynchroUIContext().saveExportContext();
167 
168         // do not display if no data found
169         if (!hasReferential) {
170             getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.noReferential"), false);
171         }
172         else {
173             getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.success"));
174         }
175     }
176 
177     /** {@inheritDoc} */
178     @Override
179     public void postFailedAction(Throwable error) {
180         super.postFailedAction(error);
181 
182         if (error != null) {
183             log.error(t("quadrige3.action.synchro.export.failed.log", error.getMessage()));
184         }
185         else if(getSynchroUIContext().getProgressionModel() != null
186                 && getSynchroUIContext().getProgressionModel().getMessage() != null) {
187 
188             String serverMessage = getSynchroUIContext().getProgressionModel().getMessage();
189             log.error(t("quadrige3.action.synchro.export.failed.server.log", serverMessage));
190         }
191         else {
192             log.error(t("quadrige3.action.synchro.export.failed"));
193         }
194 
195         getSynchroUIContext().resetExportContext();
196         getSynchroUIContext().saveExportContext();
197 
198         getContext().getSynchroHandler().report(t("quadrige3.action.synchro.export.failed"));
199     }
200 
201     /* -- Internal methods -- */
202 
203     private void showNoReferentialMessage() {
204         getContext().getDialogHelper().showMessageDialog(t("quadrige3.action.synchro.export.noReferential"),
205                 t("quadrige3.action.synchro.export.result.title"));
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     protected void releaseAction() {
211         super.releaseAction();
212         file = null;
213         hasReferential = false;
214     }
215 }