View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.dali.dto.DaliBeans;
25  import fr.ifremer.dali.dto.enums.ExtractionFilterTypeValues;
26  import fr.ifremer.dali.dto.enums.ExtractionOutputType;
27  import fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
28  import fr.ifremer.dali.service.DaliServiceLocator;
29  import fr.ifremer.dali.ui.swing.action.AbstractCheckModelAction;
30  import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
31  import fr.ifremer.dali.ui.swing.content.extraction.ExtractionUI;
32  import fr.ifremer.dali.ui.swing.content.extraction.SaveAction;
33  import fr.ifremer.quadrige3.core.dao.technical.Assert;
34  import fr.ifremer.quadrige3.synchro.vo.SynchroImportContextVO;
35  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportDataSynchroAtOnceAction;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
39  
40  import javax.swing.JOptionPane;
41  import java.io.File;
42  import java.util.LinkedHashSet;
43  import java.util.List;
44  
45  import static org.nuiton.i18n.I18n.t;
46  
47  /**
48   * @author peck7 on 08/02/2018.
49   */
50  public abstract class AbstractExtractAction extends AbstractCheckModelAction<ExtractionsTableUIModel, ExtractionsTableUI, ExtractionsTableUIHandler> {
51  
52      private static final Log LOG = LogFactory.getLog(AbstractExtractAction.class);
53  
54      private File outputFile;
55  
56      /**
57       * Constructor.
58       *
59       * @param handler Handler
60       */
61      protected AbstractExtractAction(ExtractionsTableUIHandler handler) {
62          super(handler, true);
63      }
64  
65      /**
66       * <p>getOutputType.</p>
67       *
68       * @return a {@link fr.ifremer.dali.dto.enums.ExtractionOutputType} object.
69       */
70      protected abstract ExtractionOutputType getOutputType();
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected Class<? extends AbstractDaliSaveAction> getSaveActionClass() {
77          return SaveAction.class;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected boolean isModelModify() {
85          return getModel().getExtractionUIModel().isModify();
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      protected void setModelModify(boolean modelModify) {
93          getModel().getExtractionUIModel().setModify(modelModify);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     protected boolean isModelValid() {
101         return getModel().getExtractionUIModel().isValid();
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
109         final ExtractionUI extractionUI = getUI().getParentContainer(ExtractionUI.class);
110         return extractionUI.getHandler();
111     }
112 
113     protected ExtractionDTO getSelectedExtraction() {
114         return getModel().getSelectedRows().size() != 1 ? null : getModel().getSelectedRows().iterator().next();
115     }
116 
117     protected File getOutputFile() {
118         return outputFile;
119     }
120 
121     protected void setOutputFile(File outputFile) {
122         this.outputFile = outputFile;
123     }
124 
125     @Override
126     public boolean prepareAction() throws Exception {
127         if (!super.prepareAction()) {
128             return false;
129         }
130 
131         if (getSelectedExtraction() == null) {
132             LOG.warn("no selected extraction");
133             return false;
134         }
135 
136         if (getOutputType() == null) {
137             LOG.error("no ExtractionOutputType");
138             return false;
139         }
140 
141         return true;
142     }
143 
144     @Override
145     public void doAction() throws Exception {
146 
147         // Check data updates if server available
148         try {
149             checkDataUpdate();
150         } catch (Exception e) {
151             LOG.warn("Connection to synchronization server failed, data updates ignored");
152         }
153 
154     }
155 
156     private void checkDataUpdate() {
157         List<String> programs = DaliBeans.getFilterElementsIds(getSelectedExtraction(), ExtractionFilterTypeValues.PROGRAM);
158         Assert.notEmpty(programs);
159         getContext().getSynchroContext().setImportDataProgramCodes(new LinkedHashSet<>(programs));
160 
161         SynchroImportContextVO contextResult = DaliServiceLocator.instance().getSynchroRestClientService().checkImportDataContext(
162                 getContext().getAuthenticationInfo(),
163                 getContext().getSynchroContext().getSynchroImportContext()
164         );
165 
166         if (contextResult.isWithData()) {
167 
168             // ask use to update to start import data
169             if (getContext().getDialogHelper().showConfirmDialog(
170                     t("dali.action.extract.updateAvailable"),
171                     t("dali.action.extract.title", getOutputType().getLabel()), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
172 
173                 doImportData();
174             }
175 
176         }
177 
178     }
179 
180     private void doImportData() {
181 
182         // Keep old action description
183         String oldDescription = getActionDescription();
184 
185         // Create import data action
186         ImportDataSynchroAtOnceAction importAction = getContext().getActionFactory().createLogicAction(getContext().getMainUI().getHandler(), ImportDataSynchroAtOnceAction.class);
187 
188         // override action description
189         forceActionDescription(t("dali.action.synchro.import.data"));
190 
191         // run internally
192         getContext().getActionEngine().runInternalAction(importAction);
193 
194         // Restore description
195         forceActionDescription(oldDescription);
196 
197     }
198 
199     protected void performExtraction() {
200         getContext().getExtractionPerformService().performExtraction(getSelectedExtraction(), getOutputType(), getOutputFile(), createProgressionUIModel());
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     protected void releaseAction() {
208         outputFile = null;
209         super.releaseAction();
210     }
211 }