View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.program.programs;
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.configuration.programStrategy.ProgramDTO;
29  import fr.ifremer.reefdb.service.administration.program.ProgramStrategyService;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31  import fr.ifremer.reefdb.ui.swing.content.manage.program.ProgramsUI;
32  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
33  
34  import java.util.List;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Action permettant de supprimer un programmes.
40   */
41  public class DeleteProgramAction extends AbstractReefDbAction<ProgramsTableUIModel, ProgramsTableUI, ProgramsTableUIHandler> {
42  
43      List<String> programCodes;
44  
45      /**
46       * Constructor.
47       *
48       * @param handler Le controleur
49       */
50      public DeleteProgramAction(final ProgramsTableUIHandler handler) {
51          super(handler, false);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public boolean prepareAction() throws Exception {
57          if (!super.prepareAction()) {
58              return false;
59          }
60  
61          if (getModel().getSelectedRows().isEmpty()) {
62              return false;
63          }
64  
65          programCodes = Lists.newArrayList();
66  
67          // Vérification de la présence que de programme locaux
68          for (ProgramDTO program : getModel().getSelectedRows()) {
69              programCodes.add(program.getCode());
70              if (!ReefDbBeans.isLocalStatus(program.getStatus())) {
71                  displayErrorMessage(
72                          t("reefdb.action.delete.program.title"),
73                          t("reefdb.action.delete.program.error.national.message"));
74                  return false;
75              }
76          }
77  
78          // Demande de confirmation avant la suppression
79          return askBeforeDelete(t("reefdb.action.delete.program.title"), t("reefdb.action.delete.program.message"));
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public void doAction() {
85  
86          // check program usage in data
87          for (String programCode : programCodes) {
88  
89              Long surveyCount = getContext().getObservationService().countSurveysWithProgramAndLocations(programCode, null);
90              if (surveyCount > 0) {
91                  getContext().getDialogHelper().showErrorDialog(
92                          surveyCount == 1
93                                  ? t("reefdb.action.delete.program.used.data.message", programCode)
94                                  : t("reefdb.action.delete.program.used.data.many.message", surveyCount, programCode),
95                          t("reefdb.action.delete.program.title"));
96                  return;
97  
98              }
99          }
100 
101         // check usage in other tables
102         ProgramStrategyService service = getContext().getProgramStrategyService();
103         List<String> used = Lists.newArrayList();
104         for (String programCode : programCodes) {
105             if (service.isProgramUsedByRuleList(programCode)) {
106                 used.add(programCode);
107             }
108         }
109         if (!used.isEmpty()) {
110             getContext().getDialogHelper().showErrorDialog(
111                     t("reefdb.action.delete.referential.used.title"),
112                     ReefDbUIs.getHtmlString(used),
113                     t("reefdb.action.delete.referential.used.rule.message"));
114             return;
115 
116         }
117         for (String programCode : programCodes) {
118             if (service.isProgramUsedByFilter(programCode)) {
119                 used.add(programCode);
120             }
121         }
122         if (!used.isEmpty()) {
123             getContext().getDialogHelper().showErrorDialog(
124                     t("reefdb.action.delete.referential.used.title"),
125                     ReefDbUIs.getHtmlString(used),
126                     t("reefdb.action.delete.referential.used.filter.message"));
127             return;
128 
129         }
130         for (String programCode : programCodes) {
131             if (service.isProgramUsedByExtraction(programCode)) {
132                 used.add(programCode);
133             }
134         }
135         if (!used.isEmpty()) {
136             getContext().getDialogHelper().showErrorDialog(
137                     t("reefdb.action.delete.referential.used.title"),
138                     ReefDbUIs.getHtmlString(used),
139                     t("reefdb.action.delete.referential.used.extraction.message"));
140             return;
141 
142         }
143 
144         // Suppression des programmes
145         service.deletePrograms(programCodes);
146 
147         // Suppression des lignes
148         getModel().deleteSelectedRows();
149 
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public void postSuccessAction() {
155         super.postSuccessAction();
156 
157         final ProgramsUI adminProgrammeUI = getHandler().getProgramsUI();
158 
159         // reset other tables
160         adminProgrammeUI.getStrategiesTableUI().getHandler().clearTable();
161         adminProgrammeUI.getLocationsTableUI().getHandler().clearTable();
162         adminProgrammeUI.getPmfmsTableUI().getHandler().clearTable();
163 
164         // Reload list without deleted data
165         adminProgrammeUI.getMenuUI().getProgramMnemonicCombo().setData(getContext().getProgramStrategyService().getWritablePrograms());
166         adminProgrammeUI.getMenuUI().getProgramMnemonicCombo().setSelectedItem(null);
167         adminProgrammeUI.getMenuUI().getProgramCodeCombo().setData(getContext().getProgramStrategyService().getWritablePrograms());
168         adminProgrammeUI.getMenuUI().getProgramCodeCombo().setSelectedItem(null);
169     }
170 }