View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
28  import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
29  import fr.ifremer.quadrige3.synchro.service.client.SynchroClientService;
30  import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroClientImportResult;
31  import fr.ifremer.quadrige3.synchro.vo.SynchroImportContextVO;
32  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
33  import fr.ifremer.quadrige3.ui.swing.Screen;
34  import fr.ifremer.quadrige3.ui.swing.action.AbstractChangeScreenAction;
35  import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
36  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
37  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
38  import fr.ifremer.quadrige3.ui.swing.synchro.resolver.SynchroRejectedRowUIResolver;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * <p>ImportSynchroDirectAction class.</p>
46   *
47   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
48   */
49  public class ImportSynchroDirectAction extends AbstractChangeScreenAction {
50  
51      private static final Log log = LogFactory.getLog(ImportSynchroDirectAction.class);
52  
53      private boolean silent = false;
54      private boolean needAuthentication = false;
55  
56      /**
57       * <p>Constructor for ImportSynchroDirectAction.</p>
58       *
59       * @param handler a {@link AbstractMainUIHandler} object.
60       */
61      public ImportSynchroDirectAction(AbstractMainUIHandler handler) {
62          super(handler, true, Screen.HOME);
63          setActionDescription(t("quadrige3.action.synchro.import.title"));
64      }
65  
66      private SynchroUIContext getSynchroUIContext() {
67          return getContext().getSynchroContext();
68      }
69  
70      private SynchroUIHandler getSynchroHandler() {
71          return getContext().getSynchroHandler();
72      }
73  
74      /**
75       * <p>Setter for the field <code>silent</code>.</p>
76       *
77       * @param silent a boolean.
78       */
79      public void setSilent(boolean silent) {
80          this.silent = silent;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public boolean prepareAction() throws Exception {
86          super.prepareAction();
87  
88          getSynchroHandler().hidePopup();
89  
90          return true;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public void doAction() throws Exception {
96  
97          createProgressionUIModel(103);
98  
99          SynchroClientService service = ClientServiceLocator.instance().getSynchroClientService();
100         SynchroImportContextVO contextVO = getSynchroUIContext().getSynchroImportContext();
101         SynchroClientImportResult result = service.importFromServerDatabase(
102                 SecurityContextHelper.getQuadrigeUserId(),
103                 contextVO,
104                 new SynchroRejectedRowUIResolver(getContext().getDialogHelper(), true /*isImport*/),
105                 getProgressionUIModel(),
106                 100);
107 
108         // reset cache
109         {
110             if (log.isInfoEnabled()) {
111                 log.info("Reset all caches.");
112             }
113             getProgressionUIModel().setMessage(t("quadrige3.synchro.progress.resetCache"));
114             getContext().reloadDbCache(getProgressionUIModel());
115         }
116 
117         if (!getContext().isAuthenticated()) {
118             // If the login has changed to another user, the re-authentication could have failed
119             // Do not try to store the import context (mantis #23535) because the user id is not known
120             needAuthentication = true;
121             if (log.isInfoEnabled()) {
122                 log.warn(t("quadrige3.action.synchro.import.success.notAuthenticated"));
123             }
124             // reset synchronization context
125             getSynchroUIContext().resetImportContext();
126             getProgressionUIModel().increments(1);
127         } else {
128             getProgressionUIModel().increments(t("quadrige3.synchro.progress.saveContext"));
129 
130             // reset synchronization context
131             getSynchroUIContext().resetImportContext();
132 
133             // Update last update date for referential (only if update done)
134             if (getSynchroUIContext().isImportReferential()) {
135                 getSynchroUIContext().setImportReferentialUpdateDate(result.getReferentialSynchronizationDate());
136             }
137 
138             // Update last update date for data (only if update done and if there are no rejects left)
139             if (getSynchroUIContext().isImportData()) {
140                 // Not need to remove a delta (already done in the server when exported data)
141                 getSynchroUIContext().setImportDataUpdateDate(result.getDataSynchronizationDate());
142             }
143             // save context with data date range
144             getSynchroUIContext().saveImportContext(true, true);
145         }
146 
147         getProgressionUIModel().increments(1);
148 
149         // if silent, stay on the current screen
150         if (!silent) {
151             super.doAction();
152         }
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public void postSuccessAction() {
158         super.postSuccessAction();
159 
160         getHandler().reloadDbManagerText();
161 
162         getSynchroHandler().report(needAuthentication
163                 ? t("quadrige3.action.synchro.import.success.notAuthenticated")
164                 : t("quadrige3.action.synchro.import.success"), !silent);
165 
166         if (log.isInfoEnabled()) {
167             log.info("Synchronization import success");
168         }
169 
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public void postFailedAction(Throwable error) {
175         if (!silent) {
176             super.postFailedAction(error);
177         }
178 
179         getSynchroHandler().report(t("quadrige3.synchro.report.failed"), !silent);
180         getSynchroUIContext().setStatus(SynchroProgressionStatus.FAILED);
181 
182         log.error("Synchronization import failed");
183     }
184 
185     @Override
186     protected void releaseAction() {
187         super.releaseAction();
188 
189         silent = false;
190         needAuthentication = false;
191     }
192 }