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 fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol;
27  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
28  import fr.ifremer.tutti.persistence.entities.referential.Species;
29  import fr.ifremer.tutti.service.DecoratorService;
30  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
31  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
32  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
33  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
34  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
35  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import java.io.File;
40  import java.util.List;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * To import protocol benthos.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 1.0
49   */
50  public class ImportProtocolBenthosAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
51  
52      /** Logger. */
53      private static final Log log =
54              LogFactory.getLog(ImportProtocolBenthosAction.class);
55  
56      private File file;
57  
58      private TuttiProtocol protocol;
59  
60      private List<Species> notImportedBenthos;
61  
62      public ImportProtocolBenthosAction(EditProtocolUIHandler handler) {
63          super(handler, false);
64      }
65  
66      @Override
67      public boolean prepareAction() throws Exception {
68  
69          boolean doAction = super.prepareAction();
70  
71          if (doAction) {
72  
73              // choose file to import
74              file = chooseFile(
75                      t("tutti.editProtocol.title.choose.benthosImportFile"),
76                      t("tutti.editProtocol.action.importProtocolBenthosFile"),
77                      "^.*\\.csv", t("tutti.common.file.csv")
78              );
79  
80              doAction = file != null;
81          }
82          return doAction;
83      }
84  
85      @Override
86      public void doAction() throws Exception {
87          Preconditions.checkNotNull(file);
88          if (log.isInfoEnabled()) {
89              log.info("Will import protocol benthos file: " + file);
90          }
91  
92          EditProtocolUIModel model = getModel();
93  
94          // bind to a protocol
95          protocol = model.toEntity();
96  
97          // import
98          ProtocolImportExportService service =
99                  getContext().getTuttiProtocolImportExportService();
100 
101         notImportedBenthos = service.importProtocolBenthos(file,
102                                                            protocol,
103                                                            model.getAllCaracteristic(),
104                                                            model.getAllReferentSpeciesByTaxonId());
105     }
106 
107     @Override
108     public void postSuccessAction() {
109         super.postSuccessAction();
110 
111         // build rows from imported+merged protocol
112         // (will also remove all synonyms of species referent used)
113         List<EditProtocolSpeciesRowModel> rows =
114                 handler.toSpeciesRows(protocol.getBenthos());
115 
116         // update species comboBox
117         getUI().getSpeciesComboBox().getHandler().reset();
118 
119         // update benthos comboBox
120         getUI().getBenthosComboBox().getHandler().reset();
121 
122         // update rows in model
123         getModel().setBenthosRow(rows);
124 
125         getHandler().getBenthosTableModel().setRows(rows);
126 
127         int nbSynonym = 0;
128         for (SpeciesProtocol speciesProtocol : protocol.getBenthos()) {
129             if (!speciesProtocol.isMadeFromAReferentTaxon()) {
130                 nbSynonym++;
131             }
132         }
133 
134         String message;
135         switch (nbSynonym) {
136             case 0:
137                 message = t("tutti.flash.info.benthos.imported.in.protocol",
138                             file);
139                 break;
140 
141             case 1:
142                 message = t("tutti.flash.info.benthos.imported.in.protocol.oneReplaced",
143                             file);
144                 break;
145 
146             default:
147                 message = t("tutti.flash.info.benthos.imported.in.protocol.severalReplaced",
148                             file, nbSynonym);
149         }
150         sendMessage(message);
151 
152         if (!notImportedBenthos.isEmpty()) {
153             StringBuilder sb = new StringBuilder();
154             for (Species sp : notImportedBenthos) {
155                 sb.append("<li>").append(decorate(sp, DecoratorService.FROM_PROTOCOL)).append("</li>");
156             }
157             displayWarningMessage(
158                     t("tutti.editProtocol.action.importProtocolBenthos.speciesInSpecies.title"),
159                     "<html><body>" +
160                     t("tutti.editProtocol.action.importProtocolBenthos.speciesInSpecies", sb.toString()) +
161                     "</body></html>"
162             );
163         }
164     }
165 
166     @Override
167     public void releaseAction() {
168         file = null;
169         protocol = null;
170         notImportedBenthos = null;
171         super.releaseAction();
172     }
173 }