View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.genericformat.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 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.Program;
28  import fr.ifremer.tutti.service.genericformat.GenericFormatImportConfiguration;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatImportService;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatValidateFileResult;
31  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
32  import fr.ifremer.tutti.ui.swing.content.genericformat.GenericFormatImportUI;
33  import fr.ifremer.tutti.ui.swing.content.genericformat.GenericFormatImportUIHandler;
34  import fr.ifremer.tutti.ui.swing.content.genericformat.GenericFormatImportUIModel;
35  import jaxx.runtime.SwingUtil;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import javax.swing.Icon;
40  import javax.swing.JLabel;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Created on 2/24/15.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 3.14
49   */
50  public class GenericFormatValidateAction extends LongActionSupport<GenericFormatImportUIModel, GenericFormatImportUI, GenericFormatImportUIHandler> {
51  
52      /** Logger. */
53      private static final Log log = LogFactory.getLog(GenericFormatValidateAction.class);
54  
55      private GenericFormatValidateFileResult validateFileResult;
56  
57      public GenericFormatValidateAction(GenericFormatImportUIHandler handler) {
58          super(handler, false);
59      }
60  
61      @Override
62      public boolean prepareAction() throws Exception {
63  
64          boolean doAction = super.prepareAction();
65  
66          if (doAction) {
67  
68              doAction = getModel().isCanValidate();
69  
70          }
71  
72          if (doAction) {
73  
74              // Remove any previous validate result
75              updateResult(null);
76  
77          }
78  
79          return doAction;
80  
81      }
82  
83      @Override
84      public void doAction() throws Exception {
85  
86          getModel().setValidateReportFile(getConfig().newTempFile("genericFormatValidateReport", ".pdf"));
87  
88          int maximumRowsInErrorsPerFile = getConfig().getGenericFormatImportMaximumRowsInErrorsPerFile();
89          GenericFormatImportConfiguration configuration = getModel().toValidateImportFileConfiguration(maximumRowsInErrorsPerFile);
90  
91          GenericFormatImportService service = getContext().getGenericFormatImportService();
92  
93          int nbSteps = service.getValidateImportFileNbSteps(configuration);
94  
95          if (log.isInfoEnabled()) {
96              log.info("validate import file nb steps: " + nbSteps);
97          }
98          createProgressionModelIfRequired(nbSteps);
99  
100         Program program = getModel().getProgram();
101 
102         if (log.isInfoEnabled()) {
103             log.info("Validate generic format import file for program: " + program.getName() + " from file: " + configuration.getImportFile());
104         }
105 
106         validateFileResult = service.validateImportFile(configuration, getProgressionModel());
107 
108     }
109 
110     @Override
111     public void postSuccessAction() {
112 
113         updateResult(validateFileResult);
114 
115     }
116 
117     @Override
118     protected void releaseAction() {
119         validateFileResult = null;
120         super.releaseAction();
121     }
122 
123     protected void updateResult(GenericFormatValidateFileResult result) {
124 
125         getModel().setValidateResult(result);
126 
127         if (result != null) {
128 
129             Icon icon;
130             String text;
131             String tip;
132             if (result.isValid()) {
133 
134                 icon = SwingUtil.createActionIcon("accept");
135                 text = t("tutti.genericFormat.validate.success");
136                 tip = t("tutti.genericFormat.validate.success.tip");
137 
138             } else {
139 
140                 icon = SwingUtil.createActionIcon("cancel");
141                 text = t("tutti.genericFormat.validate.error");
142                 tip = t("tutti.genericFormat.validate.error.tip");
143 
144             }
145 
146             JLabel resultText = getUI().getValidateResultText();
147             resultText.setIcon(icon);
148             resultText.setText(text);
149             resultText.setToolTipText(tip);
150 
151         }
152 
153     }
154 
155 }