View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.filter.list;
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.Lists;
27  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
28  import fr.ifremer.dali.dto.enums.FilterTypeValues;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
30  import fr.ifremer.quadrige3.core.dao.technical.Times;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.io.File;
34  import java.util.List;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Export action.
40   */
41  public class ExportFilterAction extends AbstractDaliAction<FilterListUIModel, FilterListUI, FilterListUIHandler> {
42  
43      private static final String EXPORT_FILE_FORMAT = "dali-filters-%s-%s.dat";
44      private FilterTypeValues contextFilter;
45      private List<FilterDTO> filtersToExport;
46      private File targetDirectory;
47      private File exportFile;
48  
49      /**
50       * Constructor.
51       *
52       * @param handler Controlleur
53       */
54      public ExportFilterAction(FilterListUIHandler handler) {
55          super(handler, false);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public boolean prepareAction() throws Exception {
61          filtersToExport = null;
62          targetDirectory = null;
63          exportFile = null;
64          contextFilter = null;
65  
66          if (super.prepareAction()) {
67              // Choose directory
68              targetDirectory = chooseDirectory(
69                      t("dali.action.filter.export.title"),
70                      t("dali.action.common.chooseDirectory.buttonLabel"));
71  
72              filtersToExport = Lists.newArrayList();
73              for (FilterListRowModel row : getModel().getSelectedRows()) {
74                  filtersToExport.add(row.toBean());
75                  if (contextFilter == null) {
76                      contextFilter = FilterTypeValues.getFilterType(row.getFilterTypeId());
77                  }
78              }
79          }
80          return (targetDirectory != null && CollectionUtils.isNotEmpty(filtersToExport));
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public void doAction() throws Exception {
86  
87          // create new filename
88          String fileName = String.format(EXPORT_FILE_FORMAT, contextFilter.toString().toLowerCase(), Times.getFileSuffix());
89          exportFile = new File(targetDirectory, fileName);
90  
91          // Export filters
92          getContext().getContextService().exportFilter(filtersToExport, exportFile);
93  
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void postSuccessAction() {
99          // display success message
100         displayInfoMessage(t("dali.common.success"), t("dali.action.filter.export.success", exportFile.getAbsolutePath()));
101     }
102 
103 }