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