View Javadoc
1   package fr.ifremer.dali.ui.swing.action;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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 com.google.common.collect.Sets;
27  import fr.ifremer.dali.dto.DaliBeans;
28  import fr.ifremer.dali.dto.SearchDateDTO;
29  import fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
30  import fr.ifremer.dali.service.DaliServiceLocator;
31  import fr.ifremer.dali.service.StatusFilter;
32  import fr.ifremer.dali.ui.swing.content.DaliMainUIHandler;
33  import fr.ifremer.dali.ui.swing.content.synchro.program.ProgramSelectUI;
34  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
35  import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
36  import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroClientExportToFileResult;
37  import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
38  import fr.ifremer.quadrige3.ui.swing.action.AbstractReloadCurrentScreenAction;
39  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
40  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
41  import org.apache.commons.collections4.CollectionUtils;
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  import org.nuiton.util.DateUtil;
45  
46  import java.awt.Dimension;
47  import java.io.File;
48  import java.util.Date;
49  import java.util.List;
50  import java.util.Set;
51  
52  import static org.nuiton.i18n.I18n.t;
53  
54  /**
55   * <p>ExportToFileSynchroAction class.</p>
56   *
57   * @author benoit.lavenier@e-is.pro
58   * @since 1.0
59   */
60  public class ExportToFileSynchroAction extends AbstractReloadCurrentScreenAction {
61  
62      private static final Log log = LogFactory.getLog(ExportToFileSynchroAction.class);
63      protected File file;
64      private Set<String> programCodes;
65      private boolean dirtyOnly;
66      private SearchDateDTO searchDate;
67      private Date startDate;
68      private Date endDate;
69      private boolean hasData = false;
70  
71      /**
72       * <p>Constructor for ExportToFileSynchroAction.</p>
73       *
74       * @param handler a {@link DaliMainUIHandler} object.
75       */
76      public ExportToFileSynchroAction(DaliMainUIHandler handler) {
77          super(handler, true);
78          setActionDescription(t("dali.action.synchro.exportToFile.title"));
79      }
80  
81      /**
82       * <p>Setter for the field <code>file</code>.</p>
83       *
84       * @param file a {@link java.io.File} object.
85       */
86      public void setFile(File file) {
87          this.file = file;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public boolean prepareAction() throws Exception {
93          file = null;
94          boolean doAction = super.prepareAction();
95          if (!doAction) {
96              return false;
97          }
98  
99          // Ask user to select program to import
100         programCodes = getSynchroUIContext().getExportDataProgramCodes();
101         {
102             ProgramSelectUI programSelectUI = new ProgramSelectUI((ApplicationUI) getUI(), StatusFilter.ALL, programCodes, true, false);
103             handler.openDialog(programSelectUI, t("dali.action.synchro.export.dataProgramCodes.title"), new Dimension(800, 400));
104 
105             List<ProgramDTO> programs = programSelectUI.getModel().getSelectedPrograms();
106 
107             // If no programs selected (or user cancelled): exit
108             if (CollectionUtils.isEmpty(programs)) {
109                 return false;
110             }
111 
112             // Get selected programs as code list
113             programCodes = Sets.newHashSet(DaliBeans.collectProperties(programs, ProgramDTO.PROPERTY_CODE));
114 
115             // Get export options
116             dirtyOnly = programSelectUI.getModel().isDirtyOnly();
117             searchDate = programSelectUI.getModel().getSearchDate();
118             startDate = programSelectUI.getModel().getStartDate();
119             endDate = programSelectUI.getModel().getEndDate();
120         }
121 
122         // ask user file where to export db
123         String date = DateUtil.formatDate(new Date(), "yyy-MM-dd");
124         file = saveFile(
125                 getConfig().getSynchroZipFilePrefix() + date,
126                 "zip",
127                 t("dali.synchro.export.choose.exportDataToFile.title"),
128                 t("dali.synchro.export.choose.exportDataToFile.buttonLabel"),
129                 "^.*\\.zip", t("dali.common.file.zip")
130         );
131         doAction = file != null;
132 
133         return doAction;
134     }
135 
136     private SynchroUIContext getSynchroUIContext() {
137         return getContext().getSynchroContext();
138     }
139 
140     private SynchroUIHandler getSynchroHandler() {
141         return getContext().getSynchroHandler();
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public void doActionBeforeReload() {
147         hasData = false;
148 
149         SynchroClientService synchroService = DaliServiceLocator.instance().getSynchroClientService();
150         int userId = SecurityContextHelper.getQuadrigeUserId();
151 
152         createProgressionUIModel(100);
153 
154         // Save the UI context
155         getSynchroUIContext().setExportDataProgramCodes(programCodes);
156         getSynchroUIContext().saveExportContext();
157 
158         // build temp database and export local to temp
159         getProgressionUIModel().setMessage(t("quadrige3.synchro.progress.export"));
160 
161         // export directory is set by the synchronization service
162         SynchroClientExportToFileResult exportResult = synchroService.exportToFile(userId,
163                 file,
164                 programCodes,
165                 dirtyOnly,
166                 false,
167                 DaliBeans.getDateOperator(searchDate),
168                 startDate,
169                 endDate,
170                 getProgressionUIModel(), 100);
171 
172         // If no data to export, stop here
173         hasData = exportResult.getDataResult().getTotalTreated() > 0;
174         if (!hasData) {
175             showNoDataMessage();
176             return;
177         }
178 
179         // delegate progression model from SynchroUIContext
180         getProgressionUIModel().setTotal(100);
181 
182         // restore action progression model
183         setProgressionUIModel(getProgressionUIModel());
184 
185         // do NOT reload screen if no data
186         setSkipScreenReload(!hasData);
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public void postSuccessAction() {
192 
193         super.postSuccessAction();
194         if (log.isInfoEnabled()) {
195             log.info(t("dali.action.synchro.export.success"));
196         }
197 
198         // do not display if no data found
199         if (!hasData) {
200             getContext().getSynchroHandler().report(t("dali.action.synchro.export.noData"), false);
201         } else {
202             getContext().getSynchroHandler().report(t("dali.action.synchro.export.success"));
203         }
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     public void postFailedAction(Throwable error) {
209         super.postFailedAction(error);
210 
211         if (error != null) {
212             log.error(t("dali.action.synchro.export.failed.log", error.getMessage()));
213         }
214 
215         else if (getSynchroUIContext().getProgressionModel() != null
216                 && getSynchroUIContext().getProgressionModel().getMessage() != null) {
217 
218             String serverMessage = getSynchroUIContext().getProgressionModel().getMessage();
219             log.error(t("dali.action.synchro.export.failed.server.log", serverMessage));
220 
221         }
222 
223         else {
224             log.error(t("dali.action.synchro.export.failed"));
225         }
226 
227         getContext().getSynchroHandler().report(t("dali.action.synchro.export.failed"));
228     }
229 
230     /* -- Internal methods -- */
231 
232     private void showNoDataMessage() {
233         getContext().getDialogHelper().showMessageDialog(t("dali.action.synchro.export.noData"), t("dali.action.synchro.export.result.title"));
234     }
235 
236     /** {@inheritDoc} */
237     @Override
238     protected void releaseAction() {
239         super.releaseAction();
240         file = null;
241         hasData = false;
242     }
243 }