View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.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.quadrige3.core.dao.technical.Assert;
27  import fr.ifremer.quadrige3.ui.core.dto.CodeOnly;
28  import fr.ifremer.quadrige3.ui.core.dto.referential.BaseReferentialDTO;
29  import fr.ifremer.reefdb.service.referential.ReferentialService;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
31  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
32  import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import java.util.Objects;
37  
38  import static org.nuiton.i18n.I18n.n;
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * Created on 7/6/14.
43   *
44   * @param <E>  type of bean implementing StatusDTO
45   * @param <M>
46   * @param <UI>
47   * @param <H>
48   * @since 3.6
49   */
50  public abstract class AbstractReplaceAction<
51          E extends BaseReferentialDTO,
52          M extends AbstractReplaceUIModel<E>,
53          UI extends ReefDbUI<M, ?>,
54          H extends AbstractReefDbUIHandler<M, UI>> extends AbstractReefDbAction<M, UI, H> {
55  
56      static {
57          n("reefdb.replaceTemporaryAndDelete.done");
58          n("reefdb.replaceTemporary.done");
59      }
60  
61      /**
62       * Logger.
63       */
64      private static final Log log =
65              LogFactory.getLog(AbstractReplaceAction.class);
66  
67      /**
68       * <p>getReferentialLabel.</p>
69       *
70       * @return a {@link java.lang.String} object.
71       */
72      protected abstract String getReferentialLabel();
73  
74      private E source;
75  
76      private E target;
77  
78      private boolean delete;
79  
80      private boolean replaced;
81  
82      /**
83       * <p>prepareReplaceReferential.</p>
84       *
85       * @param service a {@link fr.ifremer.reefdb.service.referential.ReferentialService} object.
86       * @param source a E object.
87       * @param target a E object.
88       * @return a boolean.
89       */
90      protected abstract boolean prepareReplaceReferential(ReferentialService service, E source, E target);
91  
92      /**
93       * <p>replaceReferential.</p>
94       *
95       * @param service a {@link fr.ifremer.reefdb.service.referential.ReferentialService} object.
96       * @param source a E object.
97       * @param target a E object.
98       * @param delete a boolean.
99       */
100     protected abstract void replaceReferential(ReferentialService service, E source, E target, boolean delete);
101 
102     /**
103      * <p>resetCaches.</p>
104      */
105     protected abstract void resetCaches();
106 
107     /**
108      * <p>Constructor for AbstractReplaceAction.</p>
109      *
110      * @param handler a H object.
111      */
112     protected AbstractReplaceAction(H handler) {
113         super(handler, false);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public boolean prepareAction() throws Exception {
119 
120         replaced = false;
121         boolean doAction = super.prepareAction();
122 
123         if (doAction) {
124 
125             M model = getModel();
126             doAction = model.isValid();
127 
128             if (doAction) {
129                 source = model.getSelectedSource();
130                 target = model.getSelectedTarget();
131                 delete = model.isDelete();
132             }
133 
134             if (Objects.equals(source, target)) {
135                 displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.sameReferential"));
136                 doAction = false;
137             }
138 
139             // except for some DTO, test if source and target have ids.
140             if (!(source instanceof CodeOnly)) {
141 
142                 if (source.getId() == null) {
143                     displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.source.invalid"));
144                     doAction = false;
145                 }
146 
147                 if (target.getId() == null) {
148                     displayErrorMessage(getReplaceUI().getTitle(), t("reefdb.replaceReferential.error.target.invalid"));
149                     doAction = false;
150                 }
151             }
152         }
153 
154         return doAction;
155     }
156 
157     /**
158      * <p>Getter for the field <code>source</code>.</p>
159      *
160      * @return a E object.
161      */
162     public E getSource() {
163         return source;
164     }
165 
166     /**
167      * <p>Getter for the field <code>target</code>.</p>
168      *
169      * @return a E object.
170      */
171     public E getTarget() {
172         return target;
173     }
174 
175     /**
176      * <p>isDelete.</p>
177      *
178      * @return a boolean.
179      */
180     public boolean isDelete() {
181         return delete;
182     }
183 
184     /**
185      * <p>Setter for the field <code>delete</code>.</p>
186      *
187      * @param delete a boolean.
188      */
189     public void setDelete(boolean delete) {
190         this.delete = delete;
191     }
192 
193     /**
194      * <p>getReplaceUI.</p>
195      *
196      * @return a {@link fr.ifremer.reefdb.ui.swing.content.manage.referential.replace.AbstractReplaceUI} object.
197      */
198     protected AbstractReplaceUI getReplaceUI() {
199         return (AbstractReplaceUI) getUI();
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     protected String decorate(Object object) {
205         return super.decorate(object, getModel().getDecoratorContext());
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     public void doAction() {
211 
212         Assert.notNull(source);
213         Assert.notNull(target);
214 
215         if (!prepareReplaceReferential(getContext().getReferentialService(), source, target)) {
216             return;
217         }
218 
219         if (log.isInfoEnabled()) {
220             log.info(String.format("Will replace %s temporary: %s by: %s",
221                     getReferentialLabel(), decorate(source), decorate(target)));
222         }
223 
224         replaceReferential(getContext().getReferentialService(), source, target, delete);
225         replaced = true;
226 
227         // reset cache
228         resetCaches();
229 
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     public void releaseAction() {
235         source = target = null;
236         delete = replaced = false;
237         super.releaseAction();
238     }
239 
240     /** {@inheritDoc} */
241     @Override
242     public void postSuccessAction() {
243         super.postSuccessAction();
244 
245         getUI().getHandler().onCloseUI();
246 
247         if (replaced) {
248             displayInfoMessage(getReplaceUI().getTitle(),
249                     t(delete ? "reefdb.replaceTemporaryAndDelete.done" : "reefdb.replaceTemporary.done", getReferentialLabel(), decorate(source), decorate(target)));
250         }
251     }
252 
253 }