View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
26  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
27  import fr.ifremer.tutti.service.PersistenceService;
28  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
30  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
32  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Saves a protocol
40   *
41   * @author Kevin Morin - kmorin@codelutin.com
42   * @since 1.0
43   */
44  public class SaveProtocolAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
45  
46      /** Logger. */
47      private static final Log log = LogFactory.getLog(SaveProtocolAction.class);
48  
49      public SaveProtocolAction(EditProtocolUIHandler handler) {
50          super(handler, true);
51      }
52  
53      /**
54       * If the event source is a TuttiScreen, then the screen changes to the source.
55       * Otherwise, the screen changes to the home.
56       */
57      @Override
58      public void doAction() throws Exception {
59          EditProtocolUIModel model = getModel();
60          TuttiUIContext context = getContext();
61  
62          PersistenceService persistenceService = context.getPersistenceService();
63  
64          TuttiProtocol bean = model.toEntity();
65  
66          if (log.isInfoEnabled()) {
67              log.info("bean zones " + bean.getZone());
68          }
69  
70          if (log.isDebugEnabled()) {
71              log.debug("protocol id to save: " + bean.getId());
72          }
73  
74          TuttiProtocol saved;
75  
76          if (TuttiEntities.isNew(bean)) {
77  
78              saved = persistenceService.createProtocol(bean);
79              model.setId(saved.getId());
80              sendMessage(t("tutti.flash.info.protocolCreated", bean.getName()));
81          } else {
82              saved = persistenceService.saveProtocol(bean);
83              sendMessage(t("tutti.flash.info.protocolSaved", bean.getName()));
84          }
85  
86          context.setProtocolId(saved.getId());
87  
88          getDataContext().closeCruiseCache();
89  
90          model.setModify(false);
91      }
92  
93      @Override
94      public void postSuccessAction() {
95          getContext().getMainUI().getHandler().setBodyTitle(EditProtocolUIHandler.getTitle(true));
96          getUI().getSaveWarningContainer().setVisible(false);
97  
98      }
99  }