View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.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.MethodDTO;
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 method local
40   */
41  public class DeleteMethodAction extends AbstractReefDbAction<ManageMethodsLocalUIModel, ManageMethodsLocalUI, ManageMethodsLocalUIHandler> {
42  
43      private Set<? extends MethodDTO> toDelete;
44      private boolean deleteOk = false;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controleur
50       */
51      public DeleteMethodAction(final ManageMethodsLocalUIHandler 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, MethodDTO.PROPERTY_NAME);
61          return super.prepareAction()
62                  && CollectionUtils.isNotEmpty(toDelete)
63                  && askBeforeDeleteMany(
64                  t("reefdb.action.delete.method.title"),
65                  t("reefdb.action.delete.method.message"),
66                  names);
67  
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void doAction() {
73          // check usage
74          List<String> used = Lists.newArrayList();
75          for (MethodDTO method : toDelete) {
76              if (method.getId() != null) {
77                  if (getContext().getReferentialService().isMethodUsedInReferential(method.getId())) {
78                      used.add(method.getName());
79                  }
80              }
81          }
82          if (!used.isEmpty()) {
83              getContext().getDialogHelper().showErrorDialog(
84                      t("reefdb.action.delete.referential.used.title"),
85                      ReefDbUIs.getHtmlString(used),
86                      t("reefdb.action.delete.referential.used.referential.message"));
87              return;
88          }
89  
90          for (MethodDTO method : toDelete) {
91              if (method.getId() != null) {
92                  if (getContext().getReferentialService().isMethodUsedInRules(method.getId())) {
93                      used.add(method.getName());
94                  }
95              }
96          }
97          if (!used.isEmpty()) {
98              getContext().getDialogHelper().showErrorDialog(
99                      t("reefdb.action.delete.referential.used.title"),
100                     ReefDbUIs.getHtmlString(used),
101                     t("reefdb.action.delete.referential.used.rule.message"));
102             return;
103         }
104 
105         // apply delete
106         getContext().getReferentialService().deleteMethods(ReefDbBeans.collectIds(toDelete));
107         deleteOk = true;
108 
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 }