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 fr.ifremer.tutti.persistence.entities.protocol.CalcifiedPiecesSamplingDefinition;
27  import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol;
28  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29  import fr.ifremer.tutti.persistence.entities.referential.Species;
30  import fr.ifremer.tutti.service.DecoratorService;
31  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
32  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
33  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
34  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorRowModel;
35  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUI;
36  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUIHandler;
37  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import java.io.File;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Set;
47  import java.util.function.Function;
48  import java.util.stream.Collectors;
49  
50  import static org.nuiton.i18n.I18n.t;
51  
52  /**
53   * To import protocol cps.
54   *
55   * @author Kevin Morin (Code Lutin)
56   * @since 4.5
57   */
58  public class ImportProtocolCpsAction extends LongActionSupport<EditProtocolUIModel, CalcifiedPiecesSamplingEditorUI, CalcifiedPiecesSamplingEditorUIHandler> {
59  
60      /** Logger. */
61      private static final Log log = LogFactory.getLog(ImportProtocolCpsAction.class);
62  
63      private File file;
64  
65      private TuttiProtocol protocol;
66  
67      private Set<Species> notImportedSpecies;
68  
69      public ImportProtocolCpsAction(CalcifiedPiecesSamplingEditorUIHandler handler) {
70          super(handler, false);
71      }
72  
73      @Override
74      public boolean prepareAction() throws Exception {
75  
76          boolean doAction = super.prepareAction();
77  
78          if (doAction) {
79  
80              // choose file to import
81              file = chooseFile(
82                      t("tutti.editProtocol.title.choose.cpsImportFile"),
83                      t("tutti.editProtocol.action.importProtocolCpsFile"),
84                      "^.*\\.csv", t("tutti.common.file.csv")
85              );
86  
87              doAction = file != null;
88          }
89          return doAction;
90      }
91  
92      @Override
93      public void doAction() throws Exception {
94          Preconditions.checkNotNull(file);
95          if (log.isInfoEnabled()) {
96              log.info("Will import protocol cps file: " + file);
97          }
98  
99          EditProtocolUIModel model = getModel();
100 
101         // bind to a protocol
102         protocol = model.toEntity();
103 
104         // import
105         ProtocolImportExportService service =
106                 getContext().getTuttiProtocolImportExportService();
107 
108        notImportedSpecies = service.importCalcifiedPiecesSamplings(file,
109                                                                    protocol,
110                                                                    model.getAllReferentSpeciesByTaxonId());
111 
112     }
113 
114     @Override
115     public void postSuccessAction() {
116         super.postSuccessAction();
117 
118         // add all the cps rows
119         Collection<SpeciesProtocol> allSpeciesProtocol = new ArrayList<>();
120         if (!protocol.isSpeciesEmpty()) {
121             allSpeciesProtocol.addAll(protocol.getSpecies());
122         }
123         if (!protocol.isBenthosEmpty()) {
124             allSpeciesProtocol.addAll(protocol.getBenthos());
125         }
126 
127         Collection<EditProtocolSpeciesRowModel> speciesRows = new ArrayList<>();
128         speciesRows.addAll(getModel().getSpeciesRow());
129         speciesRows.addAll(getModel().getBenthosRow());
130         Map<Integer, EditProtocolSpeciesRowModel> rowsByRefTaxId = speciesRows.stream()
131                 .collect(Collectors.toMap(EditProtocolSpeciesRowModel::getSpeciesReferenceTaxonId,
132                                           Function.identity()));
133 
134         List<CalcifiedPiecesSamplingEditorRowModel> cpsRows = new ArrayList<>();
135 
136         allSpeciesProtocol.forEach(speciesProtocol -> {
137 
138             Collection<CalcifiedPiecesSamplingDefinition> cpsDefs = speciesProtocol.getCalcifiedPiecesSamplingDefinition();
139             if (cpsDefs != null) {
140                 cpsDefs.forEach(def -> {
141                     CalcifiedPiecesSamplingEditorRowModel cpsRow = new CalcifiedPiecesSamplingEditorRowModel();
142                     cpsRow.fromEntity(def);
143                     cpsRow.setProtocolSpecies(rowsByRefTaxId.get(speciesProtocol.getSpeciesReferenceTaxonId()));
144                     cpsRow.setValid(true);
145                     cpsRows.add(cpsRow);
146                 });
147             }
148         });
149 
150         getModel().setCpsRows(cpsRows);
151 
152         if (log.isInfoEnabled()) {
153             log.info("not imported cps : " + notImportedSpecies.size() + "\n" + notImportedSpecies);
154         }
155 
156         sendMessage(t("tutti.flash.info.cps.imported.in.protocol", file));
157 
158         if (!notImportedSpecies.isEmpty()) {
159             StringBuilder sb = new StringBuilder();
160             for (Species sp : notImportedSpecies) {
161                 sb.append("<li>").append(decorate(sp, DecoratorService.FROM_PROTOCOL)).append("</li>");
162             }
163             displayWarningMessage(
164                     t("tutti.editProtocol.action.importProtocolCps.speciesNotImported.title"),
165                     "<html><body>" +
166                     t("tutti.editProtocol.action.importProtocolSpecies.speciesNotImported", sb.toString()) +
167                     "</body></html>"
168             );
169         }
170     }
171 
172     @Override
173     public void releaseAction() {
174         file = null;
175         protocol = null;
176         notImportedSpecies = null;
177         super.releaseAction();
178     }
179 }