View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import com.google.common.collect.Lists;
27  import fr.ifremer.tutti.persistence.entities.referential.Species;
28  import fr.ifremer.tutti.service.pupitri.PupitriExportService;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
30  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
32  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
33  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import java.io.File;
38  import java.util.List;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * To export protocol species.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 1.0
47   */
48  public class ExportPupitriAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
49  
50      /** Logger. */
51      private static final Log log =
52              LogFactory.getLog(ExportPupitriAction.class);
53  
54      private File file;
55  
56      public ExportPupitriAction(EditProtocolUIHandler handler) {
57          super(handler, true);
58      }
59  
60      @Override
61      public boolean prepareAction() throws Exception {
62  
63          boolean doAction = super.prepareAction();
64  
65          if (doAction) {
66  
67              // choose file to export
68              file = saveFile(
69                     getModel().getName() + "-pupitri",
70                      "csv",
71                      t("tutti.editProtocol.title.choose.speciesExportFile"),
72                      t("tutti.editProtocol.action.exportProtocolSpeciesFile")
73              );
74              doAction = file != null;
75          }
76          return doAction;
77      }
78  
79      @Override
80      public void releaseAction() {
81          file = null;
82          super.releaseAction();
83      }
84  
85      @Override
86      public void doAction() throws Exception {
87          Preconditions.checkNotNull(file);
88          if (log.isInfoEnabled()) {
89              log.info("Will export protocol species to file: " + file);
90          }
91  
92          EditProtocolUIModel model = getModel();
93  
94          // build species protocol to export
95  
96          List<Species> speciesList = Lists.newArrayList();
97          model.getSpeciesRow().stream().filter(AbstractTuttiBeanUIModel::isValid).forEach(row -> {
98              Species species = row.getSpecies();
99              species.setSurveyCode(row.getSpeciesSurveyCode());
100             speciesList.add(species);
101         });
102 
103         // do Export
104         PupitriExportService service = getContext().getPupitriExportService();
105         service.exportSpecies(speciesList, file);
106 
107     }
108 
109     @Override
110     public void postSuccessAction() {
111         super.postSuccessAction();
112         sendMessage(t("tutti.flash.info.species.exported.from.protocol",
113                       file));
114     }
115 }