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.protocol.SpeciesProtocol;
28  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
30  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
32  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
33  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
34  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import java.io.File;
39  import java.util.List;
40  import java.util.stream.Collectors;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * To export protocol benthos.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 1.2
49   */
50  public class ExportProtocolBenthosAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
51  
52      /** Logger. */
53      private static final Log log =
54              LogFactory.getLog(ExportProtocolBenthosAction.class);
55  
56      private File file;
57  
58      public ExportProtocolBenthosAction(EditProtocolUIHandler handler) {
59          super(handler, true);
60      }
61  
62      @Override
63      public boolean prepareAction() throws Exception {
64  
65          boolean doAction = super.prepareAction();
66  
67          if (doAction) {
68  
69              // choose file to export
70              file = saveFile(
71                      getModel().getName() + "-benthos",
72                      "csv",
73                      t("tutti.editProtocol.title.choose.benthosExportFile"),
74                      t("tutti.editProtocol.action.exportProtocolBenthosFile"),
75                      "^.*\\.csv", t("tutti.common.file.csv")
76              );
77              doAction = file != null;
78          }
79          return doAction;
80      }
81  
82      @Override
83      public void releaseAction() {
84          file = null;
85          super.releaseAction();
86      }
87  
88      @Override
89      public void doAction() throws Exception {
90          Preconditions.checkNotNull(file);
91          if (log.isInfoEnabled()) {
92              log.info("Will export protocol benthos to file: " + file);
93          }
94  
95          EditProtocolUIModel model = getModel();
96  
97          // build benthos protocol to export
98  
99          List<SpeciesProtocol> protocols = Lists.newArrayList();
100         protocols.addAll(model.getBenthosRow()
101                               .stream()
102                               .filter(AbstractTuttiBeanUIModel::isValid)
103                               .map(EditProtocolSpeciesRowModel::toEntity)
104                               .collect(Collectors.toList()));
105 
106         // import
107         ProtocolImportExportService service =
108                 getContext().getTuttiProtocolImportExportService();
109 
110         service.exportProtocolBenthos(file,
111                                       protocols,
112                                       model.getAllCaracteristic(),
113                                       model.getAllReferentSpeciesByTaxonId());
114 
115         sendMessage(t("tutti.flash.info.species.exported.from.protocol",
116                       file));
117     }
118 }