View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.local;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.reefdb.dto.ReefDbBeans;
28  import fr.ifremer.reefdb.dto.data.sampling.SamplingOperationDTO;
29  import fr.ifremer.reefdb.dto.referential.SamplingEquipmentDTO;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
32  import org.apache.commons.collections4.CollectionUtils;
33  
34  import java.util.List;
35  import java.util.Set;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Action permettant de delete un enginPrelevement local
41   */
42  public class DeleteSamplingEquipmentAction extends AbstractReefDbAction<SamplingEquipmentsLocalUIModel, SamplingEquipmentsLocalUI, SamplingEquipmentsLocalUIHandler> {
43  
44      Set<? extends SamplingEquipmentDTO> toDelete;
45      boolean deleteOk = false;
46  
47      /**
48       * Constructor.
49       *
50       * @param handler Le controleur
51       */
52      public DeleteSamplingEquipmentAction(final SamplingEquipmentsLocalUIHandler handler) {
53          super(handler, false);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public boolean prepareAction() throws Exception {
59  
60          toDelete = getModel().getSelectedRows();
61          List<String> names = ReefDbBeans.collectProperties(toDelete, SamplingOperationDTO.PROPERTY_NAME);
62          return super.prepareAction()
63                  && CollectionUtils.isNotEmpty(toDelete)
64                  && askBeforeDeleteMany(
65                  t("reefdb.action.delete.samplingEquipment.title"),
66                  t("reefdb.action.delete.samplingEquipment.message"),
67                  names);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void doAction() {
73  
74          // check usage
75          List<String> used = Lists.newArrayList();
76          for (SamplingEquipmentDTO samplingEquipment : toDelete) {
77              if (samplingEquipment.getId() != null) {
78                  if (getContext().getReferentialService().isSamplingEquipmentUsedInData(samplingEquipment.getId())) {
79                      used.add(samplingEquipment.getName());
80                  }
81              }
82          }
83  
84          if (used.isEmpty()) {
85              getContext().getReferentialService().deleteSamplingEquipments(ReefDbBeans.collectIds(toDelete));
86              deleteOk = true;
87          } else {
88              getContext().getDialogHelper().showErrorDialog(
89                      t("reefdb.action.delete.referential.used.title"),
90                      ReefDbUIs.getHtmlString(used),
91                      t("reefdb.action.delete.referential.used.data.message"));
92          }
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public void postSuccessAction() {
98  
99          if (deleteOk) {
100             getModel().deleteSelectedRows();
101             getUI().getSamplingEquipmentsLocalMenuUI().getHandler().reloadComboBox();
102         }
103 
104         super.postSuccessAction();
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     protected void releaseAction() {
110         deleteOk = false;
111         super.releaseAction();
112     }
113 }