View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.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.ArrayListMultimap;
27  import com.google.common.collect.Multimap;
28  import fr.ifremer.tutti.persistence.entities.protocol.CalcifiedPiecesSamplingDefinition;
29  import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol;
30  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
32  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorRowModel;
33  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUI;
34  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUIHandler;
35  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import java.io.File;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * To export protocol cps.
45   *
46   * @author Kevin Morin (Code Lutin)
47   * @since 4.5
48   */
49  public class ExportProtocolCpsAction extends LongActionSupport<EditProtocolUIModel,
50                                                                 CalcifiedPiecesSamplingEditorUI,
51                                                                 CalcifiedPiecesSamplingEditorUIHandler> {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(ExportProtocolCpsAction.class);
55  
56      private File file;
57  
58      public ExportProtocolCpsAction(CalcifiedPiecesSamplingEditorUIHandler 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() + "-cps",
72                      "csv",
73                      t("tutti.editProtocol.title.choose.cpsExportFile"),
74                      t("tutti.editProtocol.action.exportProtocolCpsFile"),
75                      "^.*\\.csv", t("tutti.common.file.csv")
76              );
77              doAction = file != null;
78          }
79          return doAction;
80      }
81  
82      @Override
83      public void doAction() throws Exception {
84          Preconditions.checkNotNull(file);
85          if (log.isInfoEnabled()) {
86              log.info("Will export protocol cps to file: " + file);
87          }
88  
89          EditProtocolUIModel model = getModel();
90  
91          // build species protocol to export
92  
93          Multimap<SpeciesProtocol, CalcifiedPiecesSamplingDefinition> cps = ArrayListMultimap.create();
94          for (CalcifiedPiecesSamplingEditorRowModel row : model.getCpsRows()) {
95              cps.put(row.getProtocolSpecies().toEntity(), row.toEntity());
96          }
97  
98          ProtocolImportExportService service = getContext().getTuttiProtocolImportExportService();
99  
100         service.exportCalcifiedPiecesSamplings(file, cps, getModel().getAllReferentSpeciesByTaxonId());
101 
102     }
103 
104     @Override
105     public void postSuccessAction() {
106         super.postSuccessAction();
107         sendMessage(t("tutti.flash.info.cps.exported.from.protocol", file));
108     }
109 
110     @Override
111     public void releaseAction() {
112         file = null;
113         super.releaseAction();
114     }
115 }