View Javadoc
1   package fr.ifremer.dali.ui.swing;
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.Lists;
27  import com.google.common.collect.Maps;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.dali.config.DaliConfiguration;
30  import fr.ifremer.dali.config.DaliConfigurationOption;
31  import fr.ifremer.dali.decorator.DecoratorService;
32  import fr.ifremer.dali.dto.configuration.context.ContextDTO;
33  import fr.ifremer.dali.dto.data.survey.SurveyFilterDTO;
34  import fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
35  import fr.ifremer.dali.service.DaliDataContext;
36  import fr.ifremer.dali.service.DaliServiceLocator;
37  import fr.ifremer.dali.service.administration.campaign.CampaignService;
38  import fr.ifremer.dali.service.administration.context.ContextService;
39  import fr.ifremer.dali.service.administration.program.ProgramStrategyService;
40  import fr.ifremer.dali.service.administration.user.UserService;
41  import fr.ifremer.dali.service.control.ControlRuleService;
42  import fr.ifremer.dali.service.control.RuleListService;
43  import fr.ifremer.dali.service.extraction.ExtractionPerformService;
44  import fr.ifremer.dali.service.extraction.ExtractionService;
45  import fr.ifremer.dali.service.observation.ObservationService;
46  import fr.ifremer.dali.service.persistence.PersistenceService;
47  import fr.ifremer.dali.service.referential.ReferentialService;
48  import fr.ifremer.dali.service.system.SystemService;
49  import fr.ifremer.dali.ui.swing.action.ImportReferentialSynchroAction;
50  import fr.ifremer.dali.ui.swing.content.DaliMainUI;
51  import fr.ifremer.dali.ui.swing.content.config.DaliConfigUI;
52  import fr.ifremer.dali.ui.swing.content.extraction.ExtractionUI;
53  import fr.ifremer.dali.ui.swing.content.home.HomeUI;
54  import fr.ifremer.dali.ui.swing.content.manage.campaign.CampaignsUI;
55  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUI;
56  import fr.ifremer.dali.ui.swing.content.manage.filter.campaign.FilterCampaignUI;
57  import fr.ifremer.dali.ui.swing.content.manage.filter.department.FilterDepartmentUI;
58  import fr.ifremer.dali.ui.swing.content.manage.filter.equipment.FilterEquipmentUI;
59  import fr.ifremer.dali.ui.swing.content.manage.filter.instrument.FilterInstrumentUI;
60  import fr.ifremer.dali.ui.swing.content.manage.filter.location.FilterLocationUI;
61  import fr.ifremer.dali.ui.swing.content.manage.filter.pmfm.FilterPmfmUI;
62  import fr.ifremer.dali.ui.swing.content.manage.filter.program.FilterProgramUI;
63  import fr.ifremer.dali.ui.swing.content.manage.filter.taxon.FilterTaxonUI;
64  import fr.ifremer.dali.ui.swing.content.manage.filter.taxongroup.FilterTaxonGroupUI;
65  import fr.ifremer.dali.ui.swing.content.manage.filter.user.FilterUserUI;
66  import fr.ifremer.dali.ui.swing.content.manage.program.ProgramsUI;
67  import fr.ifremer.dali.ui.swing.content.manage.program.strategiesByLocation.StrategiesLieuxUI;
68  import fr.ifremer.dali.ui.swing.content.manage.referential.analysisinstruments.ReferentialAnalysisInstrumentsUI;
69  import fr.ifremer.dali.ui.swing.content.manage.referential.department.ManageDepartmentsUI;
70  import fr.ifremer.dali.ui.swing.content.manage.referential.location.ManageLocationUI;
71  import fr.ifremer.dali.ui.swing.content.manage.referential.pmfm.ManagePmfmsUI;
72  import fr.ifremer.dali.ui.swing.content.manage.referential.pmfm.fraction.ManageFractionsUI;
73  import fr.ifremer.dali.ui.swing.content.manage.referential.pmfm.matrix.ManageMatricesUI;
74  import fr.ifremer.dali.ui.swing.content.manage.referential.pmfm.method.ManageMethodsUI;
75  import fr.ifremer.dali.ui.swing.content.manage.referential.pmfm.parameter.ManageParametersUI;
76  import fr.ifremer.dali.ui.swing.content.manage.referential.samplingequipment.ManageSamplingEquipmentsUI;
77  import fr.ifremer.dali.ui.swing.content.manage.referential.taxon.ManageTaxonsUI;
78  import fr.ifremer.dali.ui.swing.content.manage.referential.taxongroup.ManageTaxonGroupUI;
79  import fr.ifremer.dali.ui.swing.content.manage.referential.unit.ReferentialUnitsUI;
80  import fr.ifremer.dali.ui.swing.content.manage.referential.user.ManageUsersUI;
81  import fr.ifremer.dali.ui.swing.content.manage.rule.RulesUI;
82  import fr.ifremer.dali.ui.swing.content.observation.ObservationUI;
83  import fr.ifremer.dali.ui.swing.content.welcome.WelcomeUI;
84  import fr.ifremer.dali.ui.swing.util.tab.DaliTabIndexes;
85  import fr.ifremer.dali.ui.swing.util.table.state.DaliTableSessionState;
86  import fr.ifremer.quadrige3.core.dao.technical.Assert;
87  import fr.ifremer.quadrige3.core.security.QuadrigeUserDetails;
88  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
89  import fr.ifremer.quadrige3.synchro.service.client.SynchroHistoryService;
90  import fr.ifremer.quadrige3.ui.swing.*;
91  import fr.ifremer.quadrige3.ui.swing.component.OverlayIcon;
92  import fr.ifremer.quadrige3.ui.swing.content.db.DbManagerUI;
93  import fr.ifremer.quadrige3.ui.swing.model.BeanPropertyChangeListener;
94  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
95  import fr.ifremer.quadrige3.ui.swing.synchro.log.SynchroLogUI;
96  import fr.ifremer.quadrige3.ui.swing.table.FixedSwingTable;
97  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
98  import fr.ifremer.quadrige3.ui.swing.table.state.FixedSwingTableSessionState;
99  import jaxx.runtime.JAXXContext;
100 import jaxx.runtime.SwingUtil;
101 import jaxx.runtime.swing.editor.bean.BeanDoubleList;
102 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
103 import jaxx.runtime.swing.help.JAXXHelpBroker;
104 import jaxx.runtime.swing.help.JAXXHelpUIHandler;
105 import jaxx.runtime.swing.session.BeanDoubleListState;
106 import jaxx.runtime.swing.session.BeanFilterableComboBoxState;
107 import jaxx.runtime.swing.session.State;
108 import org.apache.commons.collections4.CollectionUtils;
109 import org.apache.commons.io.FileUtils;
110 import org.apache.commons.io.IOUtils;
111 import org.apache.commons.lang3.StringUtils;
112 import org.apache.commons.logging.Log;
113 import org.apache.commons.logging.LogFactory;
114 import org.nuiton.jaxx.application.ApplicationBusinessException;
115 import org.nuiton.jaxx.application.ApplicationIOUtil;
116 import org.nuiton.jaxx.application.listener.PropagatePropertyChangeListener;
117 import org.nuiton.jaxx.application.swing.action.ApplicationActionUI;
118 import org.nuiton.jaxx.application.swing.action.ApplicationUIAction;
119 import org.springframework.security.core.GrantedAuthority;
120 
121 import javax.swing.Icon;
122 import javax.swing.ImageIcon;
123 import java.awt.Color;
124 import java.awt.Component;
125 import java.io.File;
126 import java.io.InputStream;
127 import java.net.URI;
128 import java.util.List;
129 import java.util.Map;
130 import java.util.Properties;
131 import java.util.Set;
132 
133 import static org.nuiton.i18n.I18n.t;
134 
135 /**
136  * <p>DaliUIContext class.</p>
137  *
138  * @author Lionel Touseau <lionel.touseau@e-is.pro>
139  */
140 public class DaliUIContext extends ApplicationUIContext implements JAXXHelpUIHandler, BeanPropertyChangeListener {
141 
142     /**
143      * Constant <code>PROPERTY_VALIDATION_CONTEXT="validationContext"</code>
144      */
145     public static final String PROPERTY_VALIDATION_CONTEXT = "validationContext";
146     /**
147      * Constant <code>PROPERTY_LAST_OBSERVATION_ID="lastObservationId"</code>
148      */
149     public static final String PROPERTY_LAST_OBSERVATION_ID = "lastObservationId";
150     /**
151      * Constant <code>PROPERTY_SELECTED_CONTEXT_ID="selectedContextId"</code>
152      */
153     public static final String PROPERTY_SELECTED_CONTEXT_ID = "selectedContextId";
154     /**
155      * Constant <code>PROPERTY_AUTHENTICATION_LABEL="authenticationLabel"</code>
156      */
157     public static final String PROPERTY_AUTHENTICATION_LABEL = "authenticationLabel";
158     /**
159      * Constant <code>PROPERTY_AUTHENTICATION_TOOLTIPTEXT="authenticationToolTipText"</code>
160      */
161     public static final String PROPERTY_AUTHENTICATION_TOOLTIPTEXT = "authenticationToolTipText";
162     /**
163      * Constant <code>PROPERTY_SELECTED_SURVEY_ID="selectedSurveyId"</code>
164      */
165     public static final String PROPERTY_SELECTED_SURVEY_ID = "selectedSurveyId";
166     /**
167      * Logger.
168      */
169     private static final Log LOG = LogFactory.getLog(DaliUIContext.class);
170 
171     /**
172      * Message notifiers.
173      *
174      * @since 1.0
175      */
176     protected final Set<UIMessageNotifier> messageNotifiers;
177     /**
178      * Shared data context.
179      */
180     protected final DaliDataContext dataContext;
181     /**
182      * Map used to cache icons
183      */
184     private final Map<String, Icon> objectStatusIconMap = Maps.newHashMap();
185     /**
186      * Dali help broker.
187      *
188      * @since 1.0
189      */
190     protected DaliHelpBroker helpBroker;
191     private Properties helpMapping;
192     /**
193      * Validation context (used by fishingOperation screens).
194      *
195      * @since 1.0
196      */
197     private String validationContext;
198     /**
199      * Les parametres de recherche pour l'acceuil
200      */
201     private SurveyFilterDTO surveyFilter;
202 
203     /**
204      * Authentication information
205      */
206     private String authenticationLabel;
207     private String authenticationToolTipText;
208 
209     /**
210      * The selected survey id in home
211      */
212     private Integer selectedSurveyId;
213     /**
214      * Identifiant du prelevement selectionne.
215      */
216     private Integer selectedSamplingOperationId;
217     /**
218      * Identifiant du programme selectionne dans la configuration des programmes et strategie.
219      */
220     private String selectProgramCode;
221     /**
222      * Identifiant du lieu selectionne dans la configuration des programmes et strategie.
223      */
224     private Integer selectedLocationId;
225     /**
226      * Szlected context.
227      */
228     private Integer selectedContextId;
229 
230     private boolean preventNextImportSynchroCheckAction;
231 
232     /**
233      * Constructor.
234      *
235      * @param config Configuration
236      */
237     protected DaliUIContext(DaliConfiguration config) {
238         super(config);
239 
240         Map<Class, State> additionalStates = Maps.newHashMap();
241         additionalStates.put(BeanFilterableComboBox.class, new BeanFilterableComboBoxState());
242         additionalStates.put(BeanDoubleList.class, new BeanDoubleListState());
243         additionalStates.put(SwingTable.class, new DaliTableSessionState());
244         // Add also state for FixedSwingTable (Mantis #51311)
245         additionalStates.put(FixedSwingTable.class, new FixedSwingTableSessionState());
246         setSwingSession(SwingSession.newSwingSession(config.getUIConfigFile(), false, additionalStates));
247         this.dataContext = DaliServiceLocator.instance().getDataContext();
248         PropagatePropertyChangeListener.listenAndPropagateAll(dataContext, this);
249         UIMessageNotifier logMessageNotifier = message -> {
250             if (StringUtils.isNotBlank(message) && LOG.isDebugEnabled()) {
251                 LOG.debug(ApplicationUIUtil.removeHtmlTags(message));
252             }
253         };
254         this.messageNotifiers = Sets.newHashSet();
255         addMessageNotifier(logMessageNotifier);
256 
257         PROPERTIES_TO_SAVE.add(PROPERTY_SELECTED_CONTEXT_ID);
258         PROPERTIES_TO_SAVE.add(PROPERTY_LAST_OBSERVATION_ID);
259 
260     }
261 
262     /**
263      * <p>newContext.</p>
264      *
265      * @param config a {@link DaliConfiguration} object.
266      * @return a {@link DaliUIContext} object.
267      */
268     public static DaliUIContext newContext(DaliConfiguration config) {
269         Assert.notNull(config);
270         Assert.state(getInstance() == null, "Application context was already opened!");
271         ApplicationUIContext.setInstance(new DaliUIContext(config));
272         return (DaliUIContext) ApplicationUIContext.getInstance();
273     }
274 
275     /**
276      * <p>Getter for the field <code>validationContext</code>.</p>
277      *
278      * @return a {@link java.lang.String} object.
279      */
280     public String getValidationContext() {
281         return validationContext;
282     }
283 
284     /**
285      * <p>Setter for the field <code>validationContext</code>.</p>
286      *
287      * @param validationContext a {@link java.lang.String} object.
288      */
289     public void setValidationContext(String validationContext) {
290         Object oldValue = getValidationContext();
291         this.validationContext = validationContext;
292         firePropertyChange(PROPERTY_VALIDATION_CONTEXT, oldValue, validationContext);
293     }
294 
295     // ------------------------------------------------------------------------//
296     // -- Open / close methods --//
297     // ------------------------------------------------------------------------//
298 
299     /**
300      * <p>init.</p>
301      *
302      * @param i18nBundleName i18n bundle name
303      */
304     @Override
305     public void init(String i18nBundleName) {
306 
307         super.init(i18nBundleName);
308 
309         // --------------------------------------------------------------------//
310         // init help
311         // --------------------------------------------------------------------//
312         File helpDirectory = getConfiguration().getHelpDirectory();
313 
314         if (!getConfiguration().isFullLaunchMode() && (helpDirectory == null || !helpDirectory.exists())) {
315             helpDirectory = new File(getConfiguration().getDataDirectory(), "help");
316         }
317 
318         if (LOG.isDebugEnabled()) {
319             LOG.debug("Help directory: " + helpDirectory);
320         }
321         ApplicationIOUtil.forceMkdir(helpDirectory, t("dali.help.mkDir.error", helpDirectory));
322 
323         // load help mapping
324         String mappingProperties = "/dali-help-" + getLocale().getLanguage() + ".properties"; // TODO what happens if locale changes
325         try {
326 
327             InputStream resourceAsStream = getClass().getResourceAsStream(mappingProperties);
328             helpMapping = new Properties();
329             helpMapping.load(resourceAsStream);
330 
331         } catch (Exception eee) {
332             LOG.error("Failed to load help mapping file at '" + mappingProperties + "'", eee);
333         }
334         if (LOG.isInfoEnabled()) {
335             LOG.info(String.format("Starts help with locale [%s] at [%s]", getLocale(), helpDirectory));
336         }
337 
338         initAuthentication();
339 
340         // check database
341         checkDbExists();
342 
343     }
344 
345     private void initAuthentication() {
346 
347         // reset authentication user
348         setAuthenticated(false);
349 
350     }
351 
352     /**
353      * Do some auto save actions at application close (Mantis #52386)
354      */
355     @Override
356     public void close() {
357 
358         // Don't try to close if already closed
359         if (isClosed()) {
360             return;
361         }
362 
363         // Save context and extractions only if persistence is loaded (Mantis #52727)
364         // and if user is authenticated
365         if (isPersistenceLoaded() && isAuthenticated()) {
366 
367             if (LOG.isInfoEnabled()) {
368                 LOG.info("Saving Contexts and Filters ...");
369             }
370 
371             {
372                 // Load all context
373                 List<ContextDTO> contexts = getContextService().getAllContexts();
374                 // create new filename
375                 File exportFile = new File(getConfiguration().getContextDirectory(), String.format(ContextService.EXPORT_FILE_FORMAT, "autosave"));
376                 // Export context
377                 getContextService().exportContexts(contexts, exportFile);
378                 if (LOG.isInfoEnabled()) {
379                     LOG.info(String.format("Auto-save all contexts in %s", exportFile.getAbsolutePath()));
380                 }
381             }
382 
383             {
384                 // Load all extractions (with filters and configs)
385                 List<ExtractionDTO> extractions = getExtractionService().getExtractions(null, null);
386                 File extractionDirectory = getConfiguration().getExtractionDirectory();
387                 extractions.forEach(extraction -> {
388                     File exportFile = new File(
389                         extractionDirectory,
390                         String.format("%s-%s-autosave.%s", t("dali.service.extraction.file.prefix"), extraction.getName(), getConfiguration().getExtractionFileExtension())
391                     );
392                     getExtractionService().exportExtraction(extraction, exportFile);
393                     if (LOG.isInfoEnabled()) {
394                         LOG.info(String.format("Auto-save extraction in %s", exportFile.getAbsolutePath()));
395                     }
396                 });
397             }
398         }
399 
400         super.close();
401     }
402 
403     // ------------------------------------------------------------------------//
404     // -- Services methods --//
405     // ------------------------------------------------------------------------//
406 
407     /**
408      * <p>Getter for the field <code>decoratorService</code>.</p>
409      *
410      * @return a {@link fr.ifremer.dali.decorator.DecoratorService} object.
411      */
412     public DecoratorService getDecoratorService() {
413         return dataContext.getDecoratorService();
414     }
415 
416     /**
417      * <p>getSystemService.</p>
418      *
419      * @return a {@link fr.ifremer.dali.service.system.SystemService} object.
420      */
421     public SystemService getSystemService() {
422         return dataContext.getSystemService();
423     }
424 
425     /**
426      * <p>getReferentialService.</p>
427      *
428      * @return a {@link fr.ifremer.dali.service.referential.ReferentialService} object.
429      */
430     public ReferentialService getReferentialService() {
431         return dataContext.getReferentialService();
432     }
433 
434     /**
435      * <p>getObservationService.</p>
436      *
437      * @return a {@link fr.ifremer.dali.service.observation.ObservationService} object.
438      */
439     public ObservationService getObservationService() {
440         return dataContext.getObservationService();
441     }
442 
443     /**
444      * <p>getExtractionService.</p>
445      *
446      * @return a {@link fr.ifremer.dali.service.extraction.ExtractionService} object.
447      */
448     public ExtractionService getExtractionService() {
449         return dataContext.getExtractionService();
450     }
451 
452     /**
453      * <p>getExtractionPerformService.</p>
454      *
455      * @return a {@link fr.ifremer.dali.service.extraction.ExtractionPerformService} object.
456      */
457     public ExtractionPerformService getExtractionPerformService() {
458         return dataContext.getExtractionPerformService();
459     }
460 
461     /**
462      * <p>getPersistenceService.</p>
463      *
464      * @return a {@link fr.ifremer.dali.service.persistence.PersistenceService} object.
465      */
466     public PersistenceService getPersistenceService() {
467         return dataContext.getPersistenceService();
468     }
469 
470     /**
471      * <p>getUserService.</p>
472      *
473      * @return a {@link fr.ifremer.dali.service.administration.user.UserService} object.
474      */
475     public UserService getUserService() {
476         return dataContext.getUserService();
477     }
478 
479     /**
480      * <p>getSynchroHistoryService.</p>
481      *
482      * @return a {@link fr.ifremer.quadrige3.synchro.service.client.SynchroHistoryService} object.
483      */
484     public SynchroHistoryService getSynchroHistoryService() {
485         return dataContext.getSynchroHistoryService();
486     }
487 
488     /**
489      * <p>getProgramStrategyService.</p>
490      *
491      * @return a {@link fr.ifremer.dali.service.administration.program.ProgramStrategyService} object.
492      */
493     public ProgramStrategyService getProgramStrategyService() {
494         return dataContext.getProgramStrategyService();
495     }
496 
497     /**
498      * <p>getContextService.</p>
499      *
500      * @return a {@link fr.ifremer.dali.service.administration.context.ContextService} object.
501      */
502     public ContextService getContextService() {
503         return dataContext.getContextService();
504     }
505 
506     /**
507      * <p>getRulesControlService.</p>
508      *
509      * @return a {@link ControlRuleService} object.
510      */
511     public ControlRuleService getRulesControlService() {
512         return dataContext.getRulesControlService();
513     }
514 
515     public RuleListService getRuleListService() {
516         return dataContext.getRuleListService();
517     }
518 
519     public CampaignService getCampaignService() {
520         return dataContext.getCampaignService();
521     }
522 
523     @Override
524     protected boolean doUpdates() {
525         boolean needRestart = super.doUpdates();
526 
527         // check dali.persistence.db.timezone option
528         if (StringUtils.isBlank(getConfiguration().getApplicationConfig().getOption(DaliConfigurationOption.DB_TIMEZONE.getKey()))) {
529             getDialogHelper().showWarningDialog(t("dali.config.option.dbTimezone.missing"), t("quadrige3.error.business.warning"));
530         }
531 
532         return needRestart;
533     }
534 
535     // ------------------------------------------------------------------------//
536     // -- Db methods --//
537     // ------------------------------------------------------------------------//
538     private void checkDbExists() {
539 
540         // TODO EIS remove this test on 'dali.persistence.enable' (when mock is removed)
541         String enablePersistenceProperty = System.getProperty("dali.persistence.enable");
542         boolean enablePersistence = !"false".equals(enablePersistenceProperty);
543         if (enablePersistence) {
544             setDbExist(getConfiguration().isDbExists());
545 
546             if (!isDbExist()) {
547                 setPersistenceLoaded(false);
548             }
549         } else {
550 
551             // mock is used, so simulate db is open
552             setDbExist(true);
553             setPersistenceLoaded(true);
554         }
555     }
556 
557     /**
558      * <p>closePersistenceService.</p>
559      */
560     public void closePersistenceService() {
561         closePersistenceService(false, false);
562     }
563 
564     /**
565      * Close services and database
566      *
567      * @param compact                      if true, the database is compact
568      * @param keepAuthenticationProperties if true, the authentication is kept if possible
569      */
570     public void closePersistenceService(boolean compact, boolean keepAuthenticationProperties) {
571 
572         if (isPersistenceLoaded() && compact) {
573             getPersistenceService().compactDb();
574         }
575 
576         // close Sprint context
577         if (LOG.isDebugEnabled()) {
578             LOG.debug("closing Spring context");
579         }
580         IOUtils.closeQuietly(DaliServiceLocator.instance());
581         if (LOG.isDebugEnabled()) {
582             LOG.debug("Spring context closed");
583         }
584 
585         // DB is unloaded
586         setPersistenceLoaded(false);
587 
588         // clear db context
589         if (keepAuthenticationProperties) {
590             clearDbContext(true);
591         } else {
592             clearDbContext(false);
593         }
594 
595     }
596 
597     /**
598      * <p>openPersistenceService.</p>
599      *
600      * @param clearCache a boolean.
601      */
602     public void openPersistenceService(boolean clearCache) {
603 
604         // open and load the persistence layer
605         getPersistenceService();
606 
607         // mark the persistence layer as loaded
608         setPersistenceLoaded(true);
609 
610         // clear caches if forced OR if a post import action is needed
611         if (clearCache || isDbJustInstalled() || isDbJustImportedFromFile()) {
612             clearAllCaches();
613         }
614 
615         // Try to re authenticate
616         tryReAuthenticate();
617     }
618 
619     /**
620      * <p>checkDbContext.</p>
621      *
622      * @param progressionModel a {@link ProgressionUIModel} object.
623      */
624     public void checkDbContext(ProgressionUIModel progressionModel) {
625 
626         // Make post DB open tasks
627         {
628             // If just imported
629             if (isDbJustImportedFromFile()) {
630 
631                 // Reset flags
632                 setDbJustImportedFromFile(false);
633 
634                 // Do tasks after imported/installed DB
635                 // example : denormalize some data...
636             }
637 
638             // If just installed
639             if (isDbJustInstalled()) {
640                 // Reset flags
641                 setDbJustInstalled(false);
642 
643                 // Clear all caches
644                 clearAllCaches();
645             }
646 
647         }
648 
649         // Load default caches
650         try {
651             getPersistenceService().loadDefaultCaches(progressionModel);
652         } catch (Exception e) {
653             // log but continue
654             LOG.error("Something goes wrong with cache loading", e);
655         }
656 
657         // save config
658         save();
659 
660         // ------------------------------------------------------------------ //
661         // --- Check referential updates                                      //
662         // ------------------------------------------------------------------ //
663         if (isSynchroEnabled()) {
664             ImportReferentialSynchroAction importSynchroAction = getActionFactory().createLogicAction(getMainUI().getHandler(), ImportReferentialSynchroAction.class);
665             importSynchroAction.setSilentIfNoUpdate(true);
666             getActionEngine().runFullInternalAction(importSynchroAction);
667         }
668     }
669 
670     /**
671      * Clear all caches
672      */
673     public void clearAllCaches() {
674 
675         if (isPersistenceLoaded()) {
676 
677             // clear DB cache
678             getPersistenceService().clearAllCaches();
679 
680             // clear local cache
681             dataContext.resetLocalCache();
682         }
683     }
684 
685     /**
686      * <p>clearAuthenticationCache.</p>
687      *
688      * update some cache or other things depending on authenticated user
689      */
690     protected void clearAuthenticationCache() {
691     }
692 
693     /**
694      * Will clean the context, but re-authenticate the user if already authenticated
695      */
696     @Override
697     public void clearDbContext() {
698 
699         if (isPersistenceLoaded()) {
700             tryReAuthenticate();
701             clearDbContext(isAuthenticated());
702         } else {
703             clearDbContext(false);
704         }
705     }
706 
707     /**
708      * Will clean the context (never attempt to authenticate, but can keep authentication properties)
709      *
710      * @param keepAuthentication a boolean.
711      */
712     protected void clearDbContext(boolean keepAuthentication) {
713         if (LOG.isDebugEnabled()) {
714             LOG.debug(String.format("Clear DB context (keep authentication=%1$s)", keepAuthentication));
715         }
716         if (!keepAuthentication || !isAuthenticated()) {
717             dataContext.clearContext();
718             // Force logged out, because the Person object could have changed in the PERSON table
719             setAuthenticated(false);
720         } else {
721             dataContext.clearContextKeepRecorderPerson();
722             // Force logged in, because the Person object could have changed in the PERSON table
723             setAuthenticated(true);
724         }
725         setDbJustInstalled(false);
726         setDbJustImportedFromFile(false);
727     }
728 
729     /**
730      * <p>getLastSurveyId.</p>
731      *
732      * @return a {@link java.lang.Integer} object.
733      */
734     public Integer getLastObservationId() {
735         return getConfiguration().getLastSurveyId();
736     }
737 
738     /**
739      * <p>setLastSurveyId.</p>
740      *
741      * @param lastObservationId a {@link java.lang.Integer} object.
742      */
743     public void setLastObservationId(Integer lastObservationId) {
744         Integer oldValue = getLastObservationId();
745         getConfiguration().setLastSurveyId(lastObservationId);
746         firePropertyChange(PROPERTY_LAST_OBSERVATION_ID, oldValue, lastObservationId);
747     }
748 
749     /**
750      * <p>getObjectStatusIcon.</p>
751      *
752      * @param objectType a {@link java.lang.String} object.
753      * @param statusCode a {@link java.lang.String} object.
754      * @return a {@link javax.swing.Icon} object.
755      */
756     public Icon getObjectStatusIcon(String objectType, String statusCode) {
757         if (StringUtils.isBlank(objectType)) {
758             return null;
759         }
760 
761         Icon icon = objectStatusIconMap.get(statusCode);
762 
763         if (icon == null) {
764 
765             OverlayIcon newIcon = new OverlayIcon(SwingUtil.createActionIcon(objectType));
766             newIcon.setOverlay((ImageIcon) getStatusOverlayIcon(statusCode));
767 
768             objectStatusIconMap.put(objectType + StringUtils.defaultString(statusCode), newIcon);
769             return newIcon;
770         }
771 
772         return icon;
773     }
774 
775     private Icon getStatusOverlayIcon(String code) {
776         String overlayName = null;
777         if (getConfiguration().getEnableStatusCode().equals(code)) {
778             overlayName = "overlay-enable";
779         } else if (getConfiguration().getTemporaryStatusCode().equals(code)) {
780             overlayName = "overlay-temporary";
781         } else if (getConfiguration().getDisableStatusCode().equals(code)) {
782             overlayName = "overlay-disable";
783         } else if (getConfiguration().getDeletedStatusCode().equals(code)) {
784             overlayName = "overlay-deleted";
785         }
786         if (overlayName != null) {
787             return SwingUtil.createActionIcon(overlayName);
788         }
789         return null;
790     }
791 
792     private Icon getSynchronizationStatusOverlayIcon(String code) {
793         String overlayName = null;
794         if (getConfiguration().getDirtySynchronizationStatusCode().equals(code)) {
795             overlayName = "overlay-dirty";
796         } else if (getConfiguration().getReadySynchronizationStatusCode().equals(code)) {
797             overlayName = "overlay-waiting";
798         } else if (getConfiguration().getSynchronizedSynchronizationStatusCode().equals(code)) {
799             overlayName = "overlay-enable";
800         }
801         if (overlayName != null) {
802             return SwingUtil.createActionIcon(overlayName);
803         }
804         return null;
805     }
806 
807     // ------------------------------------------------------------------------//
808     // -- DataContext methods --//
809     // ------------------------------------------------------------------------//
810 
811     /**
812      * <p>Getter for the field <code>dataContext</code>.</p>
813      *
814      * @return a {@link DaliDataContext} object.
815      */
816     public DaliDataContext getDataContext() {
817         return dataContext;
818     }
819 
820     // ------------------------------------------------------------------------//
821     // -- Help methods --//
822     // ------------------------------------------------------------------------//
823 
824     /**
825      * {@inheritDoc}
826      */
827     @Override
828     public void showHelp(final JAXXContext context, final JAXXHelpBroker broker, final String helpId) {
829 
830         String helpIdTemp = helpId;
831         if (helpIdTemp == null) {
832             helpIdTemp = broker.getDefaultID();
833         }
834 
835         if (LOG.isInfoEnabled()) {
836             LOG.info("show help " + helpIdTemp);
837         }
838 
839         String value = (String) helpMapping.get(helpIdTemp);
840 
841         if (value == null) {
842             throw new ApplicationBusinessException(t("dali.context.helpPage.notFound", helpIdTemp));
843         }
844 
845         String helpDirectory = getConfiguration().getHelpResourceWithLocale(value);
846         boolean withFragment = helpDirectory.contains("#");
847 
848         String fragment = null;
849         if (withFragment) {
850             helpDirectory = StringUtils.substringBefore(helpDirectory, "#");
851             fragment = StringUtils.substringAfter(helpDirectory, "#");
852         }
853 
854         URI resolvedUri = new File(helpDirectory).toURI();
855         try {
856 
857             if (withFragment) {
858                 resolvedUri = new URI(resolvedUri.toString() + "#" + fragment);
859             }
860             if (LOG.isInfoEnabled()) {
861                 LOG.info("help uri = " + resolvedUri);
862             }
863             ApplicationUIUtil.openLink(resolvedUri);
864         } catch (Exception e) {
865             LOG.error(e.getMessage());
866             throw new ApplicationBusinessException(t("dali.context.helpPage.notFound", resolvedUri));
867         }
868     }
869 
870     /**
871      * {@inheritDoc}
872      */
873     @Override
874     public void firePropertyChanged(String propertyName, Object oldValue, Object newValue) {
875         firePropertyChange(propertyName, oldValue, newValue);
876     }
877 
878     @Override
879     public void fireIndexedPropertyChanged(String propertyName, int index, Object oldValue, Object newValue) {
880         fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
881     }
882 
883     /**
884      * {@inheritDoc}
885      */
886     @Override
887     public String getI18nPrefix() {
888         return "dali.property.";
889     }
890 
891     /**
892      * {@inheritDoc}
893      */
894     @Override
895     public String getDateFormat() {
896         return getConfiguration().getDateFormat();
897     }
898 
899     /**
900      * {@inheritDoc}
901      */
902     @Override
903     public boolean isActionInProgress(ApplicationUIAction applicationUIAction) {
904 
905         // TODO maintain a map of running uiaction
906         return false;
907     }
908 
909     /**
910      * {@inheritDoc}
911      */
912     @Override
913     public void setActionInProgress(ApplicationUIAction applicationUIAction, boolean b) {
914         // TODO maintain a map of running uiaction
915 
916     }
917 
918     //------------------------------------------------------------------------//
919     //-- Authentication methods                                             --//
920     //------------------------------------------------------------------------//
921 
922     /**
923      * <p>Getter for the field <code>authenticationLabel</code>.</p>
924      *
925      * @return a {@link java.lang.String} object.
926      */
927     @Override
928     public final String getAuthenticationLabel() {
929         return authenticationLabel;
930     }
931 
932     /**
933      * <p>Setter for the field <code>authenticationLabel</code>.</p>
934      *
935      * @param authenticationLabel a {@link java.lang.String} object.
936      */
937     public final void setAuthenticationLabel(String authenticationLabel) {
938         this.authenticationLabel = authenticationLabel;
939         firePropertyChange(PROPERTY_AUTHENTICATION_LABEL, null, authenticationLabel);
940     }
941 
942     /**
943      * <p>Getter for the field <code>authenticationToolTipText</code>.</p>
944      *
945      * @return a {@link java.lang.String} object.
946      */
947     public final String getAuthenticationToolTipText() {
948         return authenticationToolTipText;
949     }
950 
951     /**
952      * <p>Setter for the field <code>authenticationToolTipText</code>.</p>
953      *
954      * @param authenticationTooTipText a {@link java.lang.String} object.
955      */
956     public final void setAuthenticationToolTipText(String authenticationTooTipText) {
957         this.authenticationToolTipText = authenticationTooTipText;
958         firePropertyChange(PROPERTY_AUTHENTICATION_TOOLTIPTEXT, null, authenticationTooTipText);
959     }
960 
961     /**
962      * <p>Setter for the field <code>authenticated</code>.</p>
963      *
964      * @param authenticated a boolean.
965      */
966     @Override
967     public final void setAuthenticated(boolean authenticated) {
968         super.setAuthenticated(authenticated);
969         if (authenticated) {
970             QuadrigeUserDetails user = SecurityContextHelper.getQuadrigeUser();
971             if (LOG.isInfoEnabled()) {
972                 LOG.info("the authenticated user is " + user);
973             }
974             dataContext.setRecorderPersonId(user.getUserId());
975             setAuthenticationLabel(t("dali.status.authentication.label.user", user.getUsername()));
976 
977             // authorities
978             String userToolTip = null;
979             if (CollectionUtils.isNotEmpty(user.getAuthorities())) {
980                 List<String> authorities = Lists.newArrayList();
981                 for (GrantedAuthority authority : user.getAuthorities()) {
982                     authorities.add(String.valueOf(authority));
983                 }
984                 userToolTip = ApplicationUIUtil.getHtmlString(t("dali.status.authentication.label.authorities"), authorities);
985             }
986             setAuthenticationToolTipText(userToolTip);
987 
988             // update some cache or other things depending on authenticated user
989             clearAuthenticationCache();
990 
991         } else {
992             SecurityContextHelper.clear();
993             dataContext.setRecorderPersonId(null);
994             setAuthenticationLabel(t("dali.status.authentication.label.none"));
995             setAuthenticationToolTipText(null);
996         }
997     }
998 
999     /**
1000      * Say if the user is a local user
1001      *
1002      * @return a boolean.
1003      */
1004     public boolean isAuthenticatedAsLocalUser() {
1005         return isAuthenticated() && SecurityContextHelper.getQuadrigeUser().isLocal();
1006     }
1007 
1008     // ------------------------------------------------------------------------//
1009     // -- UI methods --//
1010     // ------------------------------------------------------------------------//
1011 
1012     /**
1013      * Get the UI instance corresponding to the requested screen
1014      *
1015      * @param screen the requested screen
1016      * @return the UI instance
1017      */
1018     @Override
1019     public ApplicationUI<?, ?> getApplicationUI(Screen screen) {
1020 
1021         if (DaliScreen.HOME.equals(screen)) {
1022             return new WelcomeUI(getMainUI());
1023 
1024         } else if (DaliScreen.OBSERVATION.equals(screen)) {
1025             return new HomeUI(getMainUI());
1026 
1027         } else if (DaliScreen.MANAGE_DB.equals(screen)) {
1028             DbManagerUI dbManagerUI = new DbManagerUI(getMainUI());
1029             // Hide export all referential (Mantis #40201)
1030             dbManagerUI.getExportAllReferentialToFileButton().setVisible(false);
1031             return dbManagerUI;
1032 
1033         } else if (DaliScreen.OBSERVATION_GENERAL.equals(screen)) {
1034             return new ObservationUI(getMainUI(), DaliTabIndexes.OBSERVATION_GENERAL);
1035 
1036         } else if (DaliScreen.OPERATION_MEASUREMENTS.equals(screen)) {
1037             return new ObservationUI(getMainUI(), DaliTabIndexes.OPERATION_MEASUREMENTS);
1038 
1039         } else if (DaliScreen.PHOTOS.equals(screen)) {
1040             return new ObservationUI(getMainUI(), DaliTabIndexes.PHOTOS);
1041 
1042         } else if (DaliScreen.CONFIGURATION.equals(screen)) {
1043             return new DaliConfigUI(getMainUI());
1044 
1045         } else if (DaliScreen.CONTEXT.equals(screen)) {
1046             return new ManageContextsUI(getMainUI());
1047 
1048         } else if (DaliScreen.FILTER_LOCATION.equals(screen)) {
1049             return new FilterLocationUI(getMainUI());
1050 
1051         } else if (DaliScreen.FILTER_PROGRAM.equals(screen)) {
1052             return new FilterProgramUI(getMainUI());
1053 
1054         } else if (DaliScreen.FILTER_CAMPAIGN.equals(screen)) {
1055             return new FilterCampaignUI(getMainUI());
1056 
1057         } else if (DaliScreen.FILTER_DEPARTMENT.equals(screen)) {
1058             return new FilterDepartmentUI(getMainUI());
1059 
1060         } else if (DaliScreen.FILTER_ANALYSIS_INSTRUMENT.equals(screen)) {
1061             return new FilterInstrumentUI(getMainUI());
1062 
1063         } else if (DaliScreen.FILTER_SAMPLING_EQUIPMENT.equals(screen)) {
1064             return new FilterEquipmentUI(getMainUI());
1065 
1066         } else if (DaliScreen.FILTER_PMFM.equals(screen)) {
1067             return new FilterPmfmUI(getMainUI());
1068 
1069         } else if (DaliScreen.FILTER_TAXON.equals(screen)) {
1070             return new FilterTaxonUI(getMainUI());
1071 
1072         } else if (DaliScreen.FILTER_TAXON_GROUP.equals(screen)) {
1073             return new FilterTaxonGroupUI(getMainUI());
1074 
1075         } else if (DaliScreen.FILTER_USER.equals(screen)) {
1076             return new FilterUserUI(getMainUI());
1077 
1078         } else if (DaliScreen.LOCATION.equals(screen)) {
1079             return new ManageLocationUI(getMainUI());
1080 
1081         } else if (DaliScreen.STRATEGY_LOCATION.equals(screen)) {
1082             return new StrategiesLieuxUI(getMainUI());
1083 
1084         } else if (DaliScreen.PROGRAM.equals(screen)) {
1085             return new ProgramsUI(getMainUI());
1086 
1087         } else if (DaliScreen.CAMPAIGNS.equals(screen)) {
1088             return new CampaignsUI(getMainUI());
1089 
1090         } else if (DaliScreen.RULE_LIST.equals(screen)) {
1091             return new RulesUI(getMainUI());
1092 
1093         } else if (DaliScreen.TAXON_GROUP.equals(screen)) {
1094             return new ManageTaxonGroupUI(getMainUI());
1095 
1096         } else if (DaliScreen.TAXON.equals(screen)) {
1097             return new ManageTaxonsUI(getMainUI());
1098 
1099         } else if (DaliScreen.USER.equals(screen)) {
1100             return new ManageUsersUI(getMainUI());
1101 
1102         } else if (DaliScreen.DEPARTMENT.equals(screen)) {
1103             return new ManageDepartmentsUI(getMainUI());
1104 
1105         } else if (DaliScreen.PARAMETER.equals(screen)) {
1106             return new ManageParametersUI(getMainUI());
1107 
1108         } else if (DaliScreen.METHOD.equals(screen)) {
1109             return new ManageMethodsUI(getMainUI());
1110 
1111         } else if (DaliScreen.FRACTION.equals(screen)) {
1112             return new ManageFractionsUI(getMainUI());
1113 
1114         } else if (DaliScreen.MATRIX.equals(screen)) {
1115             return new ManageMatricesUI(getMainUI());
1116 
1117         } else if (DaliScreen.PMFM.equals(screen)) {
1118             return new ManagePmfmsUI(getMainUI());
1119 
1120         } else if (DaliScreen.UNIT.equals(screen)) {
1121             return new ReferentialUnitsUI(getMainUI());
1122 
1123         } else if (DaliScreen.SAMPLING_EQUIPMENT.equals(screen)) {
1124             return new ManageSamplingEquipmentsUI(getMainUI());
1125 
1126         } else if (DaliScreen.ANALYSIS_INSTRUMENT.equals(screen)) {
1127             return new ReferentialAnalysisInstrumentsUI(getMainUI());
1128 
1129         } else if (DaliScreen.SYNCHRO_LOG.equals(screen)) {
1130             return new SynchroLogUI(getMainUI());
1131 
1132         } else if (DaliScreen.EXTRACTION.equals(screen)) {
1133             return new ExtractionUI(getMainUI());
1134 
1135         }
1136 
1137         return null;
1138     }
1139 
1140     @Override
1141     public String getSelectedScreenTitle() {
1142         String title;
1143 
1144         if (isPersistenceLoaded()) {
1145             if (getSelectedContext() == null) {
1146                 title = t("dali.main.title.noContext");
1147             } else {
1148                 title = getSelectedContext().getName();
1149             }
1150         } else {
1151 
1152             // no db loaded
1153             title = t("dali.main.title.noDb");
1154         }
1155         return title;
1156 
1157     }
1158 
1159     /**
1160      * {@inheritDoc}
1161      */
1162     @Override
1163     public DaliMainUI getMainUI() {
1164         return (DaliMainUI) super.getMainUI();
1165     }
1166 
1167     /**
1168      * {@inheritDoc}
1169      */
1170     @Override
1171     public ApplicationActionUI getExistingActionUI() {
1172         while (getActionUI() == null) {
1173 
1174             try {
1175                 Thread.sleep(50);
1176             } catch (InterruptedException e) {
1177                 // ignore this one
1178             }
1179         }
1180         return getActionUI();
1181     }
1182 
1183     /**
1184      * {@inheritDoc}
1185      */
1186     @Override
1187     public Component getBodyUI() {
1188         return getMainUI() == null ? null : getMainUI().getBody();
1189     }
1190 
1191     /**
1192      * {@inheritDoc}
1193      */
1194     @Override
1195     public Component getStatusUI() {
1196         return getMainUI() == null ? null : getMainUI().getStatus();
1197     }
1198 
1199     /**
1200      * {@inheritDoc}
1201      */
1202     @Override
1203     public Color getColorBlockingLayer() {
1204         return getConfiguration().getColorBlockingLayer();
1205     }
1206 
1207     // ------------------------------------------------------------------------//
1208     // -- UIMessageNotifier methods --//
1209     // ------------------------------------------------------------------------//
1210 
1211     /**
1212      * <p>addMessageNotifier.</p>
1213      *
1214      * @param messageNotifier a {@link UIMessageNotifier} object.
1215      */
1216     public final void addMessageNotifier(UIMessageNotifier messageNotifier) {
1217         this.messageNotifiers.add(messageNotifier);
1218     }
1219 
1220     /**
1221      * <p>removeMessageNotifier.</p>
1222      *
1223      * @param messageNotifier a {@link UIMessageNotifier} object.
1224      */
1225     public final void removeMessageNotifier(UIMessageNotifier messageNotifier) {
1226         this.messageNotifiers.remove(messageNotifier);
1227     }
1228 
1229     /**
1230      * {@inheritDoc}
1231      */
1232     @Override
1233     public void showInformationMessage(String message) {
1234         for (UIMessageNotifier messageNotifier : messageNotifiers) {
1235             messageNotifier.showInformationMessage(message);
1236         }
1237     }
1238 
1239     // ------------------------------------------------------------------------//
1240     // -- Config methods --//
1241     // ------------------------------------------------------------------------//
1242 
1243     /**
1244      * <p>Getter for the field <code>config</code>.</p>
1245      *
1246      * @return a {@link DaliConfiguration} object.
1247      */
1248     @Override
1249     public DaliConfiguration getConfiguration() {
1250         return (DaliConfiguration) super.getConfiguration();
1251     }
1252 
1253     /**
1254      * <p>Getter for the field <code>selectedSurveyId</code>.</p>
1255      *
1256      * @return a {@link java.lang.Integer} object.
1257      */
1258     public Integer getSelectedSurveyId() {
1259         return selectedSurveyId;
1260     }
1261 
1262     /**
1263      * <p>Setter for the field <code>selectedSurveyId</code>.</p>
1264      *
1265      * @param selectedSurveyId a {@link java.lang.Integer} object.
1266      */
1267     public void setSelectedSurveyId(Integer selectedSurveyId) {
1268         this.selectedSurveyId = selectedSurveyId;
1269         firePropertyChange(PROPERTY_SELECTED_SURVEY_ID, null, selectedSurveyId);
1270     }
1271 
1272     /**
1273      * <p>Getter for the field <code>selectedSamplingOperationId</code>.</p>
1274      *
1275      * @return a {@link java.lang.Integer} object.
1276      */
1277     public Integer getSelectedSamplingOperationId() {
1278         return selectedSamplingOperationId;
1279     }
1280 
1281     /**
1282      * <p>Setter for the field <code>selectedSamplingOperationId</code>.</p>
1283      *
1284      * @param selectedSamplingOperationId a {@link java.lang.Integer} object.
1285      */
1286     public void setSelectedSamplingOperationId(Integer selectedSamplingOperationId) {
1287         this.selectedSamplingOperationId = selectedSamplingOperationId;
1288     }
1289 
1290     /**
1291      * <p>Getter for the field <code>surveyFilter</code>.</p>
1292      *
1293      * @return a {@link fr.ifremer.dali.dto.data.survey.SurveyFilterDTO} object.
1294      */
1295     public SurveyFilterDTO getSurveyFilter() {
1296         return surveyFilter;
1297     }
1298 
1299     /**
1300      * <p>Setter for the field <code>surveyFilter</code>.</p>
1301      *
1302      * @param surveyFilter a {@link fr.ifremer.dali.dto.data.survey.SurveyFilterDTO} object.
1303      */
1304     public void setSurveyFilter(SurveyFilterDTO surveyFilter) {
1305         this.surveyFilter = surveyFilter;
1306     }
1307 
1308     /**
1309      * Clear observations & prelevements IDs.
1310      * <p/>
1311      * TODO vraiment utile ?
1312      */
1313     public void clearObservationIds() {
1314         setSelectedSurveyId(null);
1315         setSelectedSamplingOperationId(null);
1316         setSurveyFilter(null);
1317     }
1318 
1319     /**
1320      * <p>Getter for the field <code>selectProgramCode</code>.</p>
1321      *
1322      * @return a {@link java.lang.String} object.
1323      */
1324     public String getSelectProgramCode() {
1325         return selectProgramCode;
1326     }
1327 
1328     /**
1329      * <p>setSelectedProgramCode.</p>
1330      *
1331      * @param code a {@link java.lang.String} object.
1332      */
1333     public void setSelectedProgramCode(String code) {
1334         this.selectProgramCode = code;
1335     }
1336 
1337     /**
1338      * <p>Getter for the field <code>selectedLocationId</code>.</p>
1339      *
1340      * @return a {@link java.lang.Integer} object.
1341      */
1342     public Integer getSelectedLocationId() {
1343         return selectedLocationId;
1344     }
1345 
1346     /**
1347      * <p>Setter for the field <code>selectedLocationId</code>.</p>
1348      *
1349      * @param id a {@link java.lang.Integer} object.
1350      */
1351     public void setSelectedLocationId(Integer id) {
1352         this.selectedLocationId = id;
1353     }
1354 
1355     /**
1356      * Clear programme & strategie IDs.
1357      */
1358     public void clearProgrammeStrategieIds() {
1359         setSelectedProgramCode(null);
1360         setSelectedLocationId(null);
1361     }
1362 
1363     /**
1364      * <p>Getter for the field <code>selectedContextId</code>.</p>
1365      *
1366      * @return a {@link java.lang.Integer} object.
1367      */
1368     public Integer getSelectedContextId() {
1369         return selectedContextId;
1370     }
1371 
1372     /**
1373      * <p>getSelectedContext.</p>
1374      *
1375      * @return a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
1376      */
1377     public ContextDTO getSelectedContext() {
1378         if (selectedContextId == null) {
1379 
1380             // load last context from configuration
1381             if (getConfiguration().getLastContextId() != null) {
1382                 setSelectedContext(getContextService().getContext(getConfiguration().getLastContextId()));
1383             }
1384         }
1385         return dataContext.getContext();
1386     }
1387 
1388     /**
1389      * <p>setSelectedContext.</p>
1390      *
1391      * @param selectedContext a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
1392      */
1393     public void setSelectedContext(ContextDTO selectedContext) {
1394         Integer contextId = selectedContext == null ? null : selectedContext.getId();
1395         this.selectedContextId = contextId;
1396 
1397         // set to configuration to hold last value
1398         getConfiguration().setLastContextId(contextId);
1399 
1400         // set to data context (for service use)
1401         dataContext.setContext(selectedContext);
1402         firePropertyChange(PROPERTY_SELECTED_CONTEXT_ID, null, contextId);
1403 
1404         // Change title to reflect context change (Mantis #49551)
1405         getMainUI().getHandler().changeTitle();
1406     }
1407 
1408     /**
1409      * delete the content of synchro directory for all user
1410      */
1411     public void deleteAllSynchroContext() {
1412 
1413         File synchroDir = getConfiguration().getSynchronizationDirectory();
1414         if (synchroDir != null && synchroDir.exists()) {
1415             FileUtils.deleteQuietly(synchroDir);
1416         }
1417     }
1418 
1419     public void preventNextImportSynchroCheckAction() {
1420         this.preventNextImportSynchroCheckAction = true;
1421     }
1422 
1423     public boolean isNextImportSynchroCheckActionPrevented() {
1424         try {
1425             return preventNextImportSynchroCheckAction;
1426         } finally {
1427             preventNextImportSynchroCheckAction = false;
1428         }
1429     }
1430 
1431 }