View Javadoc
1   package fr.ifremer.dali.ui.swing.action;
2   
3   /*
4    * #%L
5    * Dali :: 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 com.google.common.collect.Sets;
27  import fr.ifremer.dali.dto.DaliBeans;
28  import fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
29  import fr.ifremer.dali.service.StatusFilter;
30  import fr.ifremer.dali.ui.swing.DaliUIContext;
31  import fr.ifremer.dali.ui.swing.content.DaliMainUIHandler;
32  import fr.ifremer.dali.ui.swing.content.synchro.program.ProgramSelectUI;
33  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
34  import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
35  import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
36  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroDirection;
37  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIContext;
38  import fr.ifremer.quadrige3.ui.swing.synchro.SynchroUIHandler;
39  import fr.ifremer.quadrige3.ui.swing.synchro.action.AbstractSynchroAction;
40  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
41  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroDirectAction;
42  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroStartAction;
43  import org.apache.commons.collections4.CollectionUtils;
44  
45  import java.awt.Dimension;
46  import java.util.List;
47  import java.util.Set;
48  
49  import static org.nuiton.i18n.I18n.t;
50  
51  /**
52   * Main import synchro action
53   * <p>
54   * Created by Ludovic on 18/05/2015.
55   */
56  public class ImportSynchroAction extends AbstractMainUIAction {
57  
58      private AbstractSynchroAction nextSynchroAction;
59      private boolean referentialOnly = false;
60      private boolean silentIfNoUpdate = false;
61      private boolean silentIfUpdate = false;
62  
63      /**
64       * <p>Constructor for ImportSynchroAction.</p>
65       *
66       * @param handler a {@link DaliMainUIHandler} object.
67       */
68      public ImportSynchroAction(DaliMainUIHandler handler) {
69          super(handler, false);
70      }
71  
72      @Override
73      public DaliUIContext getContext() {
74          return (DaliUIContext) super.getContext();
75      }
76  
77      private SynchroUIContext getSynchroUIContext() {
78          return getContext().getSynchroContext();
79      }
80  
81      private SynchroUIHandler getSynchroHandler() {
82          return getContext().getSynchroHandler();
83      }
84  
85      /**
86       * <p>Setter for the field <code>referentialOnly</code>.</p>
87       *
88       * @param referentialOnly a boolean.
89       */
90      public void setReferentialOnly(boolean referentialOnly) {
91          this.referentialOnly = referentialOnly;
92      }
93  
94      /**
95       * <p>Setter for the field <code>silentIfNoUpdate</code>.</p>
96       *
97       * @param silentIfNoUpdate a boolean.
98       */
99      public void setSilentIfNoUpdate(boolean silentIfNoUpdate) {
100         this.silentIfNoUpdate = silentIfNoUpdate;
101     }
102 
103     public void setSilentIfUpdate(boolean silentIfUpdate) {
104         this.silentIfUpdate = silentIfUpdate;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public boolean prepareAction() throws Exception {
112 
113         if (!getContext().isSynchroEnabled() && getConfig().isSynchronizationUsingServer()) {
114             getContext().getErrorHelper().showWarningDialog(t("dali.synchro.unavailable"));
115             return false;
116         }
117 
118         // Local user could not import with data
119         if (!referentialOnly && getContext().isAuthenticatedAsLocalUser()) {
120             getContext().getErrorHelper().showWarningDialog(t("dali.error.authenticate.accessDenied"));
121             return false;
122         }
123 
124         return super.prepareAction();
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public void doAction() throws Exception {
132         // test actual progression
133         getSynchroUIContext().getProgressionModel().clear();
134         getSynchroUIContext().setDirection(SynchroDirection.IMPORT);
135         getSynchroUIContext().loadImportContext();
136         boolean isSynchronizationUsingServer = getConfig().isSynchronizationUsingServer();
137 
138         // If last synchronization has failedAction
139         if (getSynchroUIContext().getStatus() == SynchroProgressionStatus.FAILED) {
140             getSynchroHandler().report(t("quadrige3.synchro.report.failed"));
141             return;
142         }
143 
144         // If synchronisation file is already here, apply it
145         if (isSynchronizationUsingServer && getSynchroUIContext().getStatus() == SynchroProgressionStatus.SUCCESS && getSynchroUIContext().getWorkingDirectory() != null) {
146             // already downloaded
147             getSynchroHandler().showValidApplyPopup();
148             return;
149         }
150 
151         // If last status was RUNNING
152         // AND there is a jobId (mantis #24916)
153         if (isSynchronizationUsingServer
154                 && getSynchroUIContext().isRunningStatus()) {
155             if (getSynchroUIContext().getImportJobId() != null) {
156                 nextSynchroAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroStartAction.class);
157                 return;
158             } else {
159                 // This should never append, but reset some attributes anyway (NOT a complete reset, to avoid Direction reset...)
160                 getSynchroUIContext().setImportJobId(null);
161                 getSynchroUIContext().setStatus(SynchroProgressionStatus.NOT_STARTED);
162             }
163         }
164 
165         // auto select referential AND data updates (only if not referential only)
166         getSynchroUIContext().setImportReferential(true);
167         getSynchroUIContext().setImportData(!referentialOnly);
168 
169         // Ask user to select program to import
170         if (!referentialOnly) {
171 
172             // Get from server if user has no access rights, only import referential
173             if (isSynchronizationUsingServer
174                     && !getContext().getProgramStrategyService().hasRemoteAccessRightOnProgram(getContext().getAuthenticationInfo())) {
175                 referentialOnly = true;
176             } else {
177 
178                 // Ask for programs
179                 Set<String> programCodes = getSynchroUIContext().getImportDataProgramCodes();
180                 ProgramSelectUI programSelectUI = new ProgramSelectUI((ApplicationUI) getUI(), StatusFilter.ACTIVE, programCodes, false, true);
181 
182                 handler.openDialog(programSelectUI, t("dali.action.synchro.import.programCodes.title"), new Dimension(800, 400));
183 
184                 List<ProgramDTO> programs = programSelectUI.getModel().getSelectedPrograms();
185 
186                 // If no programs selected (or user cancelled): only import referential
187                 if (CollectionUtils.isEmpty(programs)) {
188                     getSynchroUIContext().setImportData(false);
189                     referentialOnly = true;
190                 }
191 
192                 // Transform selected programs into a code list
193                 else {
194                     programCodes = Sets.newHashSet(DaliBeans.<String, ProgramDTO>collectProperties(programs, ProgramDTO.PROPERTY_CODE));
195                     getSynchroUIContext().setImportDataProgramCodes(programCodes);
196                 }
197 
198                 // get photo option and set it in context
199                 getSynchroUIContext().setImportPhoto(programSelectUI.getModel().isEnablePhoto());
200 
201             }
202         }
203 
204         getSynchroUIContext().saveImportContext();
205 
206         // if user is admin/local admin or validator, ask dates (if enable in config)
207         if (!referentialOnly && getConfig().isSynchronizationPeriodSelectionEnable()) {
208 
209             // Show dates period popup
210             getSynchroHandler().showDateChoicePopup();
211             return;
212         }
213 
214         // If using synchro from server
215         if (isSynchronizationUsingServer) {
216             // launch synchro
217             ImportSynchroCheckAction checkAction = getContext().getActionFactory().createNonBlockingUIAction(getSynchroHandler(), ImportSynchroCheckAction.class);
218             checkAction.setCheckReferentialOnly(referentialOnly);
219             checkAction.setSilentIfNoUpdate(silentIfNoUpdate);
220             checkAction.setSilentIfUpdate(silentIfUpdate);
221             nextSynchroAction = checkAction;
222         }
223 
224         // run apply direct synchro action
225         else {
226 
227             ImportSynchroDirectAction importAction = getActionFactory().createLogicAction(getSynchroHandler(), ImportSynchroDirectAction.class);
228             if (importAction.prepareAction()) {
229                 getActionEngine().runInternalAction(importAction);
230             }
231         }
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public void postSuccessAction() {
239         super.postSuccessAction();
240 
241         getSynchroUIContext().saveImportContext();
242 
243         if (nextSynchroAction != null && getConfig().isSynchronizationUsingServer()) {
244             nextSynchroAction.execute();
245         }
246 
247     }
248 
249     /**
250      * {@inheritDoc}
251      */
252     @Override
253     protected void releaseAction() {
254         super.releaseAction();
255         referentialOnly = false;
256         silentIfNoUpdate = false;
257         silentIfUpdate = false;
258         nextSynchroAction = null;
259     }
260 }