View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.fishing.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperations;
29  import fr.ifremer.tutti.service.operationimport.FishingOperationImportService;
30  import fr.ifremer.tutti.service.operationimport.ImportFromColumnFileFishingOperationNotFoundException;
31  import fr.ifremer.tutti.service.operationimport.ImportFromColumnFileInvalidRowException;
32  import fr.ifremer.tutti.service.operationimport.ImportFromColumnFileMissingHeaderException;
33  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
34  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUI;
35  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIHandler;
36  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIModel;
37  import fr.ifremer.tutti.ui.swing.content.protocol.actions.LoadProtocolImportColumnsAction;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.nuiton.csv.AbstractImportErrorInfo;
41  import org.nuiton.jaxx.application.ApplicationBusinessException;
42  import org.nuiton.util.beans.Binder;
43  import org.nuiton.util.beans.BinderFactory;
44  
45  import java.io.File;
46  import java.io.IOException;
47  
48  import static org.nuiton.i18n.I18n.t;
49  
50  /**
51   * @author Kevin Morin (Code Lutin)
52   * @since 3.10
53   */
54  public class ImportFishingOperationCaracteristicsFromColumnFileAction extends LongActionSupport<EditFishingOperationUIModel, EditFishingOperationUI, EditFishingOperationUIHandler> {
55  
56      private static final Log log = LogFactory.getLog(LoadProtocolImportColumnsAction.class);
57  
58      /**
59       * File to import.
60       */
61      protected File importFile;
62  
63      /**
64       * Result fishing operation after import to merge back to ui model.
65       */
66      protected FishingOperation fishingOperationToMerge;
67  
68      public ImportFishingOperationCaracteristicsFromColumnFileAction(EditFishingOperationUIHandler handler) {
69          super(handler, true);
70      }
71  
72      @Override
73      public boolean prepareAction() throws Exception {
74          boolean result = super.prepareAction() && getModel().isImportFromColumnFileEnabled();
75  
76          if (result) {
77              importFile = chooseFile(t("tutti.editFishingOperation.action.title.choose.importColumnsFile"),
78                                      t("tutti.editFishingOperation.action.chooseColumnsFile.import"),
79                                      "^.*\\.csv", t("tutti.common.file.csv"));
80  
81              result = importFile != null;
82          }
83  
84          return result;
85      }
86  
87      @Override
88      public void doAction() throws ImportFromColumnFileInvalidRowException, ImportFromColumnFileFishingOperationNotFoundException, ImportFromColumnFileMissingHeaderException, IOException {
89  
90          EditFishingOperationUIModel model = getModel();
91  
92          // Create a copy of the ui model to use in the import
93          // this bean will be merged back after import into ui model
94          // use the binder, cause EditFishingOperationUIModel.newEntity() returns always the same instance
95          fishingOperationToMerge = FishingOperations.newFishingOperation();
96          Binder<EditFishingOperationUIModel, FishingOperation> toBeanBinder =
97                  BinderFactory.newBinder(EditFishingOperationUIModel.class, FishingOperation.class);
98          toBeanBinder.copy(model, fishingOperationToMerge);
99  
100         FishingOperationImportService importService = getContext().getFishingOperationImportService();
101 
102         String errorMessage = null;
103         try {
104             importService.importCaracteristicsFromColumnFile(importFile, fishingOperationToMerge);
105 
106         } catch (ImportFromColumnFileInvalidRowException error) {
107             // matching row is not valid
108 
109             errorMessage = "";
110             for (AbstractImportErrorInfo<FishingOperation> errorInfo : error.getErrors()) {
111                 Throwable errorInfoCause = errorInfo.getCause();
112                 if (log.isErrorEnabled()) {
113                     log.error(errorInfoCause.getLocalizedMessage());
114                 }
115                 errorMessage += "<li>" + errorInfoCause.getLocalizedMessage().replaceAll("\\s+", " ") + "</li>";
116             }
117 
118         } catch (ImportFromColumnFileFishingOperationNotFoundException error) {
119             // no matching fishing operation
120             errorMessage = "<li>" + t("tutti.editFishingOperation.action.importColumns.operationNotFound") + "</li>";
121 
122         } catch (ImportFromColumnFileMissingHeaderException error) {
123             errorMessage = t("tutti.editFishingOperation.action.importColumns.missingHeader", error.getImportColumn());
124         }
125 
126         if (errorMessage != null) {
127             throw new ApplicationBusinessException(t("tutti.editFishingOperation.action.importColumns.error", errorMessage));
128         }
129     }
130 
131     @Override
132     public void postSuccessAction() {
133 
134         super.postSuccessAction();
135 
136         // Merge result to ui model
137         EditFishingOperationUI ui = getUI();
138         EditFishingOperationUIModel model = getModel();
139 
140         model.fromEntity(fishingOperationToMerge);
141 
142         //FIXME Find out why ?
143         //these 2 do not refresh correctly
144         ui.getRecorderPersonList().getHandler().setSelected(fishingOperationToMerge.getRecorderPerson());
145         ui.getSecondaryVesselList().getHandler().setSelected(fishingOperationToMerge.getSecondaryVessel());
146 
147         ui.getGearUseFeatureTabContent().getHandler().mergeCaracteristics(fishingOperationToMerge);
148         ui.getVesselUseFeatureTabContent().getHandler().mergeCaracteristics(fishingOperationToMerge);
149 
150         sendMessage(t("tutti.editFishingOperation.action.importColumns.success"));
151 
152     }
153 
154 }