View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
28  import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
29  import fr.ifremer.dali.ui.swing.content.extraction.list.ExtractionsRowModel;
30  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.util.Collection;
34  import java.util.List;
35  import java.util.stream.Collectors;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Save action.
41   */
42  public class SaveAction extends AbstractDaliSaveAction<ExtractionUIModel, ExtractionUI, ExtractionUIHandler> {
43  
44      private Collection<? extends ExtractionDTO> extractionsToSave;
45  
46      /**
47       * <p>Constructor for SaveAction.</p>
48       *
49       * @param handler a {@link fr.ifremer.dali.ui.swing.content.extraction.ExtractionUIHandler} object.
50       */
51      public SaveAction(final ExtractionUIHandler handler) {
52          super(handler, false);
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean prepareAction() throws Exception {
58          if (!super.prepareAction()) {
59              return false;
60          }
61          // Model is valid
62          if (!getModel().isValid()) {
63  
64              // Remove error
65              displayErrorMessage(t("dali.action.save.errors.title"), t("dali.action.save.errors.remove"));
66  
67              // Stop saving
68              return false;
69          }
70  
71          // Selected extractions
72          extractionsToSave = getUI().getExtractionsTable().getModel().getRows().stream().filter(ExtractionsRowModel::isDirty).collect(Collectors.toList());
73  
74          return !CollectionUtils.isEmpty(extractionsToSave);
75  
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public void doAction() throws Exception {
81  
82          // Save observations
83          getContext().getExtractionService().saveExtractions(extractionsToSave);
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      protected List<AbstractBeanUIModel> getModelsToModify() {
89          List<AbstractBeanUIModel> models = Lists.newArrayList();
90          models.add(getModel().getExtractionsTableUIModel());
91          models.add(getModel().getExtractionFiltersUIModel());
92          models.add(getModel().getExtractionConfigUIModel());
93          return models;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void postSuccessAction() {
99  
100         getHandler().reloadComboBox();
101 
102         sendMessage(t("dali.action.save.extractions.success", extractionsToSave.size()));
103 
104         super.postSuccessAction();
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     protected void releaseAction() {
110         super.releaseAction();
111 
112         extractionsToSave = null;
113     }
114 }