View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.referential.replace.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Preconditions;
28  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
29  import fr.ifremer.tutti.service.PersistenceService;
30  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
31  import fr.ifremer.tutti.ui.swing.content.referential.ManageTemporaryReferentialUI;
32  import fr.ifremer.tutti.ui.swing.content.referential.replace.AbstractReplaceTemporaryUIModel;
33  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
34  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * Created on 7/6/14.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 3.6
45   */
46  public abstract class AbstractReplaceTemporaryUIAction<E extends TuttiReferentialEntity, M extends AbstractReplaceTemporaryUIModel<E>, UI extends TuttiUI<M, ?>, H extends AbstractTuttiUIHandler<M, UI>> extends LongActionSupport<M, UI, H> {
47  
48      /** Logger. */
49      private static final Log log =
50              LogFactory.getLog(AbstractReplaceTemporaryUIAction.class);
51  
52      protected E source;
53  
54      protected E target;
55  
56      protected Boolean delete;
57  
58      protected abstract String getEntityLabel();
59  
60      protected abstract void replaceReferentialEntity(PersistenceService persistenceService, E source, E target, boolean delete);
61  
62      protected abstract void updateNumberOfTemporaryEntities(ManageTemporaryReferentialUI mainUi);
63  
64      protected AbstractReplaceTemporaryUIAction(H handler) {
65          super(handler, true);
66      }
67  
68      @Override
69      public boolean prepareAction() throws Exception {
70  
71  
72          boolean doAction = super.prepareAction();
73  
74          if (doAction) {
75  
76              M model = getModel();
77              doAction = model.isValid();
78  
79              if (doAction) {
80                  source = model.getSelectedSource();
81                  target = model.getSelectedTarget();
82                  delete = model.isDelete();
83              }
84          }
85  
86          getUI().getHandler().onCloseUI();
87  
88          return doAction;
89      }
90  
91      @Override
92      public void doAction() throws Exception {
93  
94          Preconditions.checkNotNull(source);
95          Preconditions.checkNotNull(target);
96          Preconditions.checkNotNull(delete);
97  
98          String entityLabel = getEntityLabel();
99  
100         if (log.isInfoEnabled()) {
101             log.info(String.format("Will replace %s temporary: %s by: %s",
102                                    entityLabel, source.getName(), target.getName()));
103         }
104 
105         replaceReferentialEntity(getContext().getPersistenceService(), source, target, delete);
106 
107     }
108 
109     @Override
110     public void releaseAction() {
111         source = target = null;
112         delete = null;
113         super.releaseAction();
114     }
115 
116 
117     @Override
118     public void postSuccessAction() {
119         super.postSuccessAction();
120 
121         if (delete) {
122 
123             ManageTemporaryReferentialUI mainUi = getUI().getContextValue(ManageTemporaryReferentialUI.class, "owner");
124 
125             updateNumberOfTemporaryEntities(mainUi);
126             sendMessage(t("tutti.replaceTemporaryAndDelete.done", getEntityLabel(), decorate(source), decorate(target)));
127         } else {
128             sendMessage(t("tutti.replaceTemporary.done", getEntityLabel(), decorate(source), decorate(target)));
129         }
130 
131     }
132 
133 
134     protected void reloadCruise() {
135         if (getDataContext().isCruiseFilled()) {
136             getDataContext().reloadCruise();
137         }
138     }
139 
140     protected void reloadFishingOperation() {
141         if (getDataContext().isFishingOperationFilled()) {
142             getDataContext().reloadFishingOperation();
143         }
144     }
145 
146 }