1 package fr.ifremer.reefdb.ui.swing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
138
139
140
141 public class ReefDbUIContext extends ApplicationUIContext implements JAXXHelpUIHandler, BeanPropertyChangeListener {
142
143
144 public static final String PROPERTY_APPLICATION_CONTEXT = "applicationContext";
145
146 public static final String PROPERTY_VALIDATION_CONTEXT = "validationContext";
147
148 public static final String PROPERTY_LAST_OBSERVATION_ID = "lastObservationId";
149
150 public static final String PROPERTY_SELECTED_CONTEXT_ID = "selectedContextId";
151
152 public static final String PROPERTY_AUTHENTICATION_LABEL = "authenticationLabel";
153
154 public static final String PROPERTY_AUTHENTICATION_TOOLTIPTEXT = "authenticationToolTipText";
155
156 public static final String PROPERTY_SELECTED_SURVEY_ID = "selectedSurveyId";
157
158
159
160
161 private static final Log LOG = LogFactory.getLog(ReefDbUIContext.class);
162
163
164
165
166
167
168 protected final Set<UIMessageNotifier> messageNotifiers;
169
170
171
172 protected final ReefDbDataContext dataContext;
173
174
175
176 private final Map<String, Icon> objectStatusIconMap = Maps.newHashMap();
177
178
179
180
181
182 protected ReefDbHelpBroker helpBroker;
183 private Properties helpMapping;
184
185
186
187
188
189
190 private String validationContext;
191
192
193
194 private SurveyFilterDTO surveyFilter;
195
196 private String authenticationLabel;
197 private String authenticationToolTipText;
198
199
200
201
202 private Integer selectedSurveyId;
203
204
205
206 private Integer selectedSamplingOperationId;
207
208
209
210 private String selectedProgramCode;
211
212
213
214 private Integer selectedLocationId;
215
216
217
218 private Integer selectedContextId;
219
220 private boolean preventNextImportSynchroCheckAction;
221
222
223
224
225
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
252
253
254
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
265
266
267
268 public String getValidationContext() {
269 return validationContext;
270 }
271
272
273
274
275
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
285
286
287
288
289 public void init(String i18nBundleName) {
290
291 super.init(i18nBundleName);
292
293
294
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
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
325 checkDbExists();
326
327 }
328
329 private void initAuthentication() {
330
331
332 dataContext.addPropertyChangeListener(ReefDbDataContext.PROPERTY_RECORDER_PERSON_ID, evt -> {
333
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
347 setAuthenticated(false);
348
349 }
350
351
352
353
354
355
356
357
358
359
360 public DecoratorService getDecoratorService() {
361 return ReefDbServiceLocator.instance().getDecoratorService();
362 }
363
364
365
366
367
368
369 public SystemService getSystemService() {
370 return ReefDbServiceLocator.instance().getSystemService();
371 }
372
373
374
375
376
377
378 public ReferentialService getReferentialService() {
379 return ReefDbServiceLocator.instance().getReferentialService();
380 }
381
382
383
384
385
386
387 public ObservationService getObservationService() {
388 return ReefDbServiceLocator.instance().getObservationService();
389 }
390
391
392
393
394
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
406
407
408
409 public PersistenceService getPersistenceService() {
410 return ReefDbServiceLocator.instance().getPersistenceService();
411 }
412
413
414
415
416
417
418 public UserService getUserService() {
419 return ReefDbServiceLocator.instance().getUserService();
420 }
421
422
423
424
425
426
427 public SynchroHistoryService getSynchroHistoryService() {
428 return ReefDbServiceLocator.instance().getSynchroHistoryService();
429 }
430
431
432
433
434
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
446
447
448
449 public ContextService getContextService() {
450 return ReefDbServiceLocator.instance().getContextService();
451 }
452
453
454
455
456
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
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
480
481 private void checkDbExists() {
482
483
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
495 setDbExist(true);
496 setPersistenceLoaded(true);
497 }
498 }
499
500
501
502
503 public void closePersistenceService() {
504 closePersistenceService(false, false);
505 }
506
507
508
509
510
511
512
513 public void closePersistenceService(boolean compact, boolean keepAuthenticationProperties) {
514
515 if (isPersistenceLoaded() && compact) {
516 getPersistenceService().compactDb();
517 }
518
519
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
529 setPersistenceLoaded(false);
530
531
532 if (keepAuthenticationProperties) {
533 clearDbContext(true);
534 } else {
535 clearDbContext(false);
536 }
537
538 }
539
540
541
542
543
544
545 public void openPersistenceService(boolean clearCache) {
546
547
548 getPersistenceService();
549
550
551 setPersistenceLoaded(true);
552
553
554 if (clearCache || isDbJustInstalled() || isDbJustImportedFromFile()) {
555 clearCaches();
556 }
557
558
559 tryReAuthenticate();
560 }
561
562
563
564
565
566
567 public void checkDbContext(ProgressionUIModel progressionModel) {
568
569
570 {
571
572 if (isDbJustImportedFromFile()) {
573
574
575 setDbJustImportedFromFile(false);
576
577
578
579 }
580
581
582 if (isDbJustInstalled()) {
583
584 setDbJustInstalled(false);
585 }
586
587 }
588
589
590 {
591 getPersistenceService().loadDefaultCaches(progressionModel);
592 }
593
594
595 save();
596
597
598
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
609
610 @Override
611 public void clearCaches() {
612 super.clearCaches();
613
614
615 dataContext.resetLocalCache();
616 }
617
618
619
620
621 public void clearDbContext() {
622
623 if (isPersistenceLoaded()) {
624 tryReAuthenticate();
625 clearDbContext(isAuthenticated());
626 } else {
627 clearDbContext(false);
628 }
629 }
630
631
632
633
634
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
643 setAuthenticated(false);
644 } else {
645 dataContext.clearContextKeepRecorderPerson();
646
647 setAuthenticated(true);
648 }
649 setDbJustInstalled(false);
650 setDbJustImportedFromFile(false);
651 }
652
653
654
655
656
657
658 public Integer getLastObservationId() {
659 return getConfiguration().getLastObservationId();
660 }
661
662
663
664
665
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
675
676
677
678
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
733
734
735
736
737
738
739 public ReefDbDataContext getDataContext() {
740 return dataContext;
741 }
742
743
744
745
746
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
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
804 @Override
805 public String getI18nPrefix() {
806 return "reefdb.property.";
807 }
808
809
810 @Override
811 public String getDateFormat() {
812 return getConfiguration().getDateFormat();
813 }
814
815
816 @Override
817 public boolean isActionInProgress(ApplicationUIAction applicationUIAction) {
818
819
820 return false;
821 }
822
823
824 @Override
825 public void setActionInProgress(ApplicationUIAction applicationUIAction, boolean b) {
826
827
828 }
829
830
831 @Override
832 public ReefDbConfiguration getConfiguration() {
833 return (ReefDbConfiguration) super.getConfiguration();
834 }
835
836
837
838
839
840
841
842
843
844
845 public final String getAuthenticationLabel() {
846 return authenticationLabel;
847 }
848
849
850
851
852
853
854 public final void setAuthenticationLabel(String authenticationLabel) {
855 this.authenticationLabel = authenticationLabel;
856 firePropertyChange(PROPERTY_AUTHENTICATION_LABEL, null, authenticationLabel);
857 }
858
859
860
861
862
863
864 public final String getAuthenticationToolTipText() {
865 return authenticationToolTipText;
866 }
867
868
869
870
871
872
873 public final void setAuthenticationToolTipText(String authenticationTooTipText) {
874 this.authenticationToolTipText = authenticationTooTipText;
875 firePropertyChange(PROPERTY_AUTHENTICATION_TOOLTIPTEXT, null, authenticationTooTipText);
876 }
877
878
879
880
881
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
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
914
915
916
917 public boolean isAuthenticatedAsLocalUser() {
918 return isAuthenticated() && SecurityContextHelper.getQuadrigeUser().isLocal();
919 }
920
921
922
923
924
925
926 public boolean isAuthenticatedAsAdmin() {
927 return SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.LOCAL_ADMIN);
928 }
929
930
931
932
933
934
935
936
937
938
939
940
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
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
1050 @Override
1051 public ApplicationActionUI getExistingActionUI() {
1052 while (getActionUI() == null) {
1053
1054 try {
1055 Thread.sleep(50);
1056 } catch (InterruptedException e) {
1057
1058 }
1059 }
1060 return getActionUI();
1061 }
1062
1063
1064 @Override
1065 public Component getBodyUI() {
1066 return getMainUI() == null ? null : getMainUI().getBody();
1067 }
1068
1069
1070 @Override
1071 public Component getStatusUI() {
1072 return getMainUI() == null ? null : getMainUI().getStatus();
1073 }
1074
1075
1076 @Override
1077 public Color getColorBlockingLayer() {
1078 return getConfiguration().getColorBlockingLayer();
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 public final void addMessageNotifier(UIMessageNotifier messageNotifier) {
1090 this.messageNotifiers.add(messageNotifier);
1091 }
1092
1093
1094
1095
1096
1097
1098 public final void removeMessageNotifier(UIMessageNotifier messageNotifier) {
1099 this.messageNotifiers.remove(messageNotifier);
1100 }
1101
1102
1103 @Override
1104 public void showInformationMessage(String message) {
1105 for (UIMessageNotifier messageNotifier : messageNotifiers) {
1106 messageNotifier.showInformationMessage(message);
1107 }
1108 }
1109
1110
1111
1112
1113
1114
1115 public Integer getSelectedSurveyId() {
1116 return selectedSurveyId;
1117 }
1118
1119
1120
1121
1122
1123
1124 public void setSelectedSurveyId(Integer selectedSurveyId) {
1125 this.selectedSurveyId = selectedSurveyId;
1126 firePropertyChange(PROPERTY_SELECTED_SURVEY_ID, null, selectedSurveyId);
1127 }
1128
1129
1130
1131
1132
1133
1134 public Integer getSelectedSamplingOperationId() {
1135 return selectedSamplingOperationId;
1136 }
1137
1138
1139
1140
1141
1142
1143 public void setSelectedSamplingOperationId(Integer selectedSamplingOperationId) {
1144 this.selectedSamplingOperationId = selectedSamplingOperationId;
1145 }
1146
1147
1148
1149
1150
1151
1152 public SurveyFilterDTO getSurveyFilter() {
1153 return surveyFilter;
1154 }
1155
1156
1157
1158
1159
1160
1161 public void setSurveyFilter(SurveyFilterDTO surveyFilter) {
1162 this.surveyFilter = surveyFilter;
1163 }
1164
1165
1166
1167
1168
1169
1170 public void clearObservationPrelevementsIds() {
1171 setSelectedSurveyId(null);
1172 setSelectedSamplingOperationId(null);
1173 setSurveyFilter(null);
1174 }
1175
1176
1177
1178
1179
1180
1181 public String getSelectedProgramCode() {
1182 return selectedProgramCode;
1183 }
1184
1185
1186
1187
1188
1189
1190 public void setSelectedProgramCode(String code) {
1191 this.selectedProgramCode = code;
1192 }
1193
1194
1195
1196
1197
1198
1199 public Integer getSelectedLocationId() {
1200 return selectedLocationId;
1201 }
1202
1203
1204
1205
1206
1207
1208 public void setSelectedLocationId(Integer id) {
1209 this.selectedLocationId = id;
1210 }
1211
1212
1213
1214
1215 public void clearProgrammeStrategieIds() {
1216 setSelectedProgramCode(null);
1217 setSelectedLocationId(null);
1218 }
1219
1220
1221
1222
1223
1224
1225 public Integer getSelectedContextId() {
1226 return selectedContextId;
1227 }
1228
1229
1230
1231
1232
1233
1234 public ContextDTO getSelectedContext() {
1235 if (selectedContextId == null) {
1236
1237
1238 if (getConfiguration().getLastContextId() != null) {
1239 setSelectedContext(getContextService().getContext(getConfiguration().getLastContextId()));
1240 }
1241 }
1242 return dataContext.getContext();
1243 }
1244
1245
1246
1247
1248
1249
1250 public void setSelectedContext(ContextDTO selectedContext) {
1251 Integer contextId = selectedContext == null ? null : selectedContext.getId();
1252 this.selectedContextId = contextId;
1253
1254
1255 getConfiguration().setLastContextId(contextId);
1256
1257
1258 dataContext.setContext(selectedContext);
1259 firePropertyChange(PROPERTY_SELECTED_CONTEXT_ID, null, contextId);
1260
1261
1262 getMainUI().getHandler().changeTitle();
1263 }
1264
1265
1266
1267
1268
1269
1270
1271 public boolean isDataReadyToSynchronize() {
1272 return isSynchroEnabled() && getObservationService().isDataReadyToSynchronize();
1273 }
1274
1275
1276
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 }