View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.department.local.replace;
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 fr.ifremer.reefdb.dto.referential.DepartmentDTO;
27  import fr.ifremer.reefdb.service.referential.ReferentialService;
28  import fr.ifremer.reefdb.ui.swing.content.manage.referential.department.ManageDepartmentsUI;
29  import fr.ifremer.reefdb.ui.swing.content.manage.referential.replace.AbstractReplaceAction;
30  
31  import javax.swing.*;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Created on 7/6/14.
37   *
38   * @since 3.6
39   */
40  public class ReplaceDepartmentAction extends AbstractReplaceAction<DepartmentDTO, ReplaceDepartmentUIModel, ReplaceDepartmentUI, ReplaceDepartmentUIHandler> {
41  
42      /**
43       * <p>Constructor for ReplaceDepartmentAction.</p>
44       *
45       * @param handler a {@link fr.ifremer.reefdb.ui.swing.content.manage.referential.department.local.replace.ReplaceDepartmentUIHandler} object.
46       */
47      public ReplaceDepartmentAction(ReplaceDepartmentUIHandler handler) {
48          super(handler);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      protected String getReferentialLabel() {
54          return t("reefdb.property.department");
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      protected boolean prepareReplaceReferential(ReferentialService service, DepartmentDTO source, DepartmentDTO target) {
60  
61          if (service.isDepartmentUsedInProgram(source.getId())) {
62              String decoratedSource = decorate(source);
63              if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.programStrategy.message", decoratedSource, decoratedSource),
64                      getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
65                  return false;
66              }
67              setDelete(false);
68          }
69  
70          if (service.isDepartmentUsedInRules(source.getId())) {
71              String decoratedSource = decorate(source);
72              if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.controlRules.message", decoratedSource, decoratedSource),
73                      getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
74                  return false;
75              }
76              setDelete(false);
77          }
78  
79          if (service.isDepartmentUsedInValidatedData(source.getId())) {
80              String decoratedSource = decorate(source);
81              if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.validatedData.message", decoratedSource, decoratedSource),
82                      getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
83                  return false;
84              }
85              setDelete(false);
86          }
87  
88          return true;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      protected void replaceReferential(ReferentialService service, DepartmentDTO source, DepartmentDTO target, boolean delete) {
94  
95          service.replaceDepartment(source, target, delete);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     protected void resetCaches() {
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public void postSuccessAction() {
106         super.postSuccessAction();
107 
108         final ManageDepartmentsUI departmentsLocalUI = (ManageDepartmentsUI) getContext().getMainUI().getHandler().getCurrentBody();
109         // reload table
110         if (departmentsLocalUI != null) {
111             SwingUtilities.invokeLater(() -> getActionEngine().runAction(departmentsLocalUI.getManageDepartmentsLocalUI().getDepartmentMenuUI().getSearchButton()));
112         }
113 
114     }
115 }