View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.contextslist;
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 fr.ifremer.dali.dto.configuration.context.ContextDTO;
27  import fr.ifremer.dali.service.administration.context.ContextService;
28  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
29  import fr.ifremer.quadrige3.core.dao.technical.Times;
30  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.io.File;
34  import java.util.List;
35  import java.util.stream.Collectors;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  
40  /**
41   * <p>ExportContextAction class.</p>
42   *
43   */
44  public class ExportContextAction extends AbstractDaliAction<ManageContextsListTableUIModel, ManageContextsListTableUI, ManageContextsListTableUIHandler> {
45  
46      private List<ContextDTO> contextsToExport;
47      private File targetDirectory;
48      private File exportFile;
49      
50  	/**
51  	 * <p>Constructor for ExportContextAction.</p>
52  	 *
53  	 * @param handler a {@link ManageContextsListTableUIHandler} object.
54  	 */
55  	public ExportContextAction(ManageContextsListTableUIHandler handler) {
56  		super(handler, false);
57  	}
58      
59      /** {@inheritDoc} */
60      @Override
61      public boolean prepareAction() throws Exception {
62          contextsToExport = null;
63          targetDirectory = null;
64          exportFile = null;
65          
66          if (super.prepareAction()) {
67              // Choose directory
68              targetDirectory = chooseDirectory(
69                      t("dali.action.context.export.title"),
70                      t("dali.action.common.chooseDirectory.buttonLabel"));
71  
72              contextsToExport = getModel().getSelectedRows().stream().map(AbstractBeanUIModel::toBean).collect(Collectors.toList());
73          }
74          return (targetDirectory != null && CollectionUtils.isNotEmpty(contextsToExport));
75      }
76  
77  	/** {@inheritDoc} */
78  	@Override
79  	public void doAction() throws Exception {
80          // create new filename
81          String fileName = String.format(ContextService.EXPORT_FILE_FORMAT, Times.getFileSuffix());
82          exportFile = new File(targetDirectory, fileName);
83  
84          // Export context
85          getContext().getContextService().exportContexts(contextsToExport, exportFile);
86  	}
87      
88      /** {@inheritDoc} */
89      @Override
90      public void postSuccessAction() {
91          // display success message
92          displayInfoMessage(t("dali.common.success"), t("dali.action.context.export.success", exportFile.getAbsolutePath()));
93      }
94  }