View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.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.base.Function;
27  import com.google.common.collect.Lists;
28  import fr.ifremer.reefdb.dto.ReefDbBeans;
29  import fr.ifremer.reefdb.dto.referential.pmfm.PmfmDTO;
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 javax.annotation.Nullable;
35  import java.util.List;
36  import java.util.Set;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * Action permettant de delete un quadruplet local
42   */
43  public class DeletePmfmAction extends AbstractReefDbAction<PmfmsLocalUIModel, PmfmsLocalUI, PmfmsLocalUIHandler> {
44  
45      private Set<? extends PmfmDTO> toDelete;
46      boolean deleteOk = false;
47  
48      /**
49       * Constructor.
50       *
51       * @param handler Le controleur
52       */
53      public DeletePmfmAction(final PmfmsLocalUIHandler handler) {
54          super(handler, false);
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public boolean prepareAction() throws Exception {
60  
61          toDelete = getModel().getSelectedRows();
62          List<String> names = ReefDbBeans.transformCollection(toDelete, new Function<PmfmDTO, String>() {
63              @Nullable
64              @Override
65              public String apply(PmfmDTO input) {
66                  return decorate(input);
67              }
68          });
69          return super.prepareAction()
70                  && CollectionUtils.isNotEmpty(toDelete)
71                  && askBeforeDeleteMany(
72                  t("reefdb.action.delete.pmfm.title"),
73                  t("reefdb.action.delete.pmfm.message"),
74                  names);
75  
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public void doAction() {
81  
82          // check usage
83          List<String> used = Lists.newArrayList();
84          for (PmfmDTO pmfm : toDelete) {
85              if (pmfm.getId() != null) {
86                  if (getContext().getReferentialService().isPmfmUsedInData(pmfm.getId())) {
87                      used.add(decorate(pmfm));
88                  }
89              }
90          }
91          if (!used.isEmpty()) {
92              getContext().getDialogHelper().showErrorDialog(
93                      t("reefdb.action.delete.referential.used.title"),
94                      ReefDbUIs.getHtmlString(used),
95                      t("reefdb.action.delete.referential.used.data.message"));
96              return;
97          }
98  
99          for (PmfmDTO pmfm : toDelete) {
100             if (pmfm.getId() != null) {
101                 if (getContext().getReferentialService().isPmfmUsedInProgram(pmfm.getId())) {
102                     used.add(decorate(pmfm));
103                 }
104             }
105         }
106         if (!used.isEmpty()) {
107             getContext().getDialogHelper().showErrorDialog(
108                     t("reefdb.action.delete.referential.used.title"),
109                     ReefDbUIs.getHtmlString(used),
110                     t("reefdb.action.delete.referential.used.strategy.message"));
111             return;
112         }
113 
114         // apply delete
115         getContext().getReferentialService().deletePmfms(ReefDbBeans.collectIds(toDelete));
116         deleteOk = true;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public void postSuccessAction() {
122 
123         if (deleteOk) {
124             getModel().deleteSelectedRows();
125         }
126 
127         super.postSuccessAction();
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     protected void releaseAction() {
133         deleteOk = false;
134         super.releaseAction();
135     }
136 }