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.CaracteristicMappingRow;
27  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
28  import fr.ifremer.tutti.service.protocol.ProtocolCaracteristicsImportExportService;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolCaracteristicsRowModel;
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.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.Collections;
39  import java.util.List;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * To import protocol caracteristics.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 1.0
48   */
49  public class ImportProtocolCaracteristicAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
50  
51      /** Logger. */
52      private static final Log log =
53              LogFactory.getLog(ImportProtocolCaracteristicAction.class);
54  
55      private File file;
56      protected TuttiProtocol protocol;
57  
58      public ImportProtocolCaracteristicAction(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 import
70              file = chooseFile(
71                      t("tutti.editProtocol.title.choose.caracteristicImportFile"),
72                      t("tutti.editProtocol.action.importProtocolCaracteristicFile"),
73                      "^.*\\.csv", t("tutti.common.file.csv")
74              );
75  
76              doAction = file != null;
77          }
78          return doAction;
79      }
80  
81      @Override
82      public void releaseAction() {
83          file = null;
84          super.releaseAction();
85      }
86  
87      @Override
88      public void doAction() throws Exception {
89          Preconditions.checkNotNull(file);
90          if (log.isInfoEnabled()) {
91              log.info("Will import protocol caracteristic file: " + file);
92          }
93  
94          EditProtocolUIModel model = getModel();
95  
96          // bind to a protocol
97          protocol = model.toEntity();
98  
99          ProtocolCaracteristicsImportExportService service = getContext().getProtocolCaracteristicsImportExportService();
100 
101         service.importProtocolCaracteristic(file, protocol, model.getAllCaracteristic());
102 
103     }
104 
105     @Override
106     public void postSuccessAction() {
107 
108         super.postSuccessAction();
109 
110         handler.addDoubleListListeners();
111 
112         try {
113             // rebind to model
114             getModel().fromEntity(protocol);
115         } finally {
116             handler.removeDoubleListListeners();
117         }
118 
119         List<CaracteristicMappingRow> caracteristicMapping = protocol.getCaracteristicMapping();
120         List<EditProtocolCaracteristicsRowModel> caracteristicsRowModels = getHandler().toProtocolCaracteristicRows(caracteristicMapping);
121         getModel().setCaracteristicMappingRows(Collections.emptyList());
122         getModel().setCaracteristicMappingRows(caracteristicsRowModels);
123         getHandler().getCaracteristicMappingTableModel().setRows(caracteristicsRowModels);
124 
125         sendMessage(t("tutti.flash.info.caracteristic.imported.in.protocol", file));
126 
127     }
128 
129 }