View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.fraction.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.referential.pmfm.FractionDTO;
29  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
30  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.util.List;
34  import java.util.Set;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Action permettant de delete un fraction local
40   */
41  public class DeleteFractionAction extends AbstractReefDbAction<ManageFractionsLocalUIModel, ManageFractionsLocalUI, ManageFractionsLocalUIHandler> {
42  
43      private Set<? extends FractionDTO> toDelete;
44      private boolean deleteOk = false;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controleur
50       */
51      public DeleteFractionAction(final ManageFractionsLocalUIHandler handler) {
52          super(handler, false);
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean prepareAction() throws Exception {
58  
59          toDelete = getModel().getSelectedRows();
60          List<String> names = ReefDbBeans.collectProperties(toDelete, FractionDTO.PROPERTY_NAME);
61          return super.prepareAction()
62                  && CollectionUtils.isNotEmpty(toDelete)
63                  && askBeforeDeleteMany(
64                  t("reefdb.action.delete.fraction.title"),
65                  t("reefdb.action.delete.fraction.message"),
66                  names);
67  
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void doAction() {
73  
74          // check usage
75          List<String> used = Lists.newArrayList();
76          for (FractionDTO fraction : toDelete) {
77              if (fraction.getId() != null) {
78                  if (getContext().getReferentialService().isFractionUsedInReferential(fraction.getId())) {
79                      used.add(fraction.getName());
80                  }
81              }
82          }
83          if (!used.isEmpty()) {
84              getContext().getDialogHelper().showErrorDialog(
85                      t("reefdb.action.delete.referential.used.title"),
86                      ReefDbUIs.getHtmlString(used),
87                      t("reefdb.action.delete.referential.used.referential.message"));
88              return;
89          }
90  
91          for (FractionDTO fraction : toDelete) {
92              if (fraction.getId() != null) {
93                  if (getContext().getReferentialService().isFractionUsedInRules(fraction.getId())) {
94                      used.add(fraction.getName());
95                  }
96              }
97          }
98          if (!used.isEmpty()) {
99              getContext().getDialogHelper().showErrorDialog(
100                     t("reefdb.action.delete.referential.used.title"),
101                     ReefDbUIs.getHtmlString(used),
102                     t("reefdb.action.delete.referential.used.rule.message"));
103             return;
104         }
105 
106         // apply delete
107         getContext().getReferentialService().deleteFractions(ReefDbBeans.collectIds(toDelete));
108         deleteOk = true;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public void postSuccessAction() {
114 
115         if (deleteOk) {
116             getModel().deleteSelectedRows();
117             getUI().getMenuUI().getHandler().reloadComboBox();
118         }
119 
120         super.postSuccessAction();
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     protected void releaseAction() {
126         deleteOk = false;
127         super.releaseAction();
128     }
129 }