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