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