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