Project

General

Profile

Download (26.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.store;
11

    
12
import java.util.EnumSet;
13

    
14
import javax.management.relation.Role;
15

    
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.jobs.Job;
18
import org.eclipse.swt.widgets.Display;
19
import org.slf4j.Marker;
20
import org.springframework.core.io.ClassPathResource;
21
import org.springframework.core.io.Resource;
22
import org.springframework.security.access.PermissionEvaluator;
23
import org.springframework.security.authentication.AuthenticationManager;
24
import org.springframework.security.core.Authentication;
25
import org.springframework.security.core.GrantedAuthority;
26
import org.springframework.security.core.context.SecurityContext;
27
import org.springframework.security.core.context.SecurityContextHolder;
28

    
29
import eu.etaxonomy.cdm.api.application.CdmApplicationException;
30
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
31
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
32
import eu.etaxonomy.cdm.api.application.ICdmRepository;
33
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.service.IAgentService;
36
import eu.etaxonomy.cdm.api.service.IAnnotationService;
37
import eu.etaxonomy.cdm.api.service.IClassificationService;
38
import eu.etaxonomy.cdm.api.service.ICollectionService;
39
import eu.etaxonomy.cdm.api.service.ICommonService;
40
import eu.etaxonomy.cdm.api.service.IDescriptionService;
41
import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
42
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
43
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
44
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
45
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
46
import eu.etaxonomy.cdm.api.service.IGroupService;
47
import eu.etaxonomy.cdm.api.service.IMarkerService;
48
import eu.etaxonomy.cdm.api.service.IMediaService;
49
import eu.etaxonomy.cdm.api.service.INameService;
50
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
51
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
52
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
53
import eu.etaxonomy.cdm.api.service.IReferenceService;
54
import eu.etaxonomy.cdm.api.service.IRegistrationService;
55
import eu.etaxonomy.cdm.api.service.IRightsService;
56
import eu.etaxonomy.cdm.api.service.IService;
57
import eu.etaxonomy.cdm.api.service.ITaxonService;
58
import eu.etaxonomy.cdm.api.service.ITermService;
59
import eu.etaxonomy.cdm.api.service.IUserService;
60
import eu.etaxonomy.cdm.api.service.IVocabularyService;
61
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
62
import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
63
import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
64
import eu.etaxonomy.cdm.config.ICdmSource;
65
import eu.etaxonomy.cdm.database.DbSchemaValidation;
66
import eu.etaxonomy.cdm.model.agent.AgentBase;
67
import eu.etaxonomy.cdm.model.common.Annotation;
68
import eu.etaxonomy.cdm.model.common.CdmBase;
69
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
70
import eu.etaxonomy.cdm.model.common.Group;
71
import eu.etaxonomy.cdm.model.common.Language;
72
import eu.etaxonomy.cdm.model.common.TermVocabulary;
73
import eu.etaxonomy.cdm.model.description.DescriptionBase;
74
import eu.etaxonomy.cdm.model.description.FeatureNode;
75
import eu.etaxonomy.cdm.model.description.FeatureTree;
76
import eu.etaxonomy.cdm.model.description.PolytomousKey;
77
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
78
import eu.etaxonomy.cdm.model.description.WorkingSet;
79
import eu.etaxonomy.cdm.model.media.Media;
80
import eu.etaxonomy.cdm.model.media.Rights;
81
import eu.etaxonomy.cdm.model.molecular.Amplification;
82
import eu.etaxonomy.cdm.model.molecular.Primer;
83
import eu.etaxonomy.cdm.model.name.Registration;
84
import eu.etaxonomy.cdm.model.name.TaxonName;
85
import eu.etaxonomy.cdm.model.occurrence.Collection;
86
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
87
import eu.etaxonomy.cdm.model.reference.Reference;
88
import eu.etaxonomy.cdm.model.taxon.Classification;
89
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
90
import eu.etaxonomy.cdm.model.validation.EntityConstraintViolation;
91
import eu.etaxonomy.cdm.model.validation.EntityValidation;
92
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
93
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
94
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
95
import eu.etaxonomy.taxeditor.io.ExportManager;
96
import eu.etaxonomy.taxeditor.io.ImportManager;
97
import eu.etaxonomy.taxeditor.model.MessagingUtils;
98
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
99
import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
100
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
101
import eu.etaxonomy.taxeditor.session.mock.MockCdmEntitySessionManager;
102
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
103
import eu.etaxonomy.taxeditor.ui.dialog.RemotingLoginDialog;
104
import eu.etaxonomy.taxeditor.util.ProgressMonitorClientManager;
105
import eu.etaxonomy.taxeditor.webapp.CDMServer;
106

    
107
/**
108
 * This implementation of ICdmDataRepository depends on hibernate sessions to
109
 * store the data correctly for the current session. No state is held in this
110
 * class.
111
 *
112
 * Only methods that either get or manipulate data are exposed here. So this
113
 * class acts as a facade for the methods in cdmlib-service.
114
 *
115
 * @author n.hoffmann
116
 * @created 17.03.2009
117
 * @version 1.0
118
 */
119
public class CdmStore {
120

    
121
    private static final Resource DEFAULT_APPLICATION_CONTEXT = new ClassPathResource(
122
            "/eu/etaxonomy/cdm/editorApplicationContext.xml",
123
            TaxeditorStorePlugin.class);
124
    private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.VALIDATE;
125

    
126
    private static CdmStore instance;
127

    
128
    //private final ICdmRepository applicationConfiguration;
129

    
130
    private static ContextManager contextManager = new ContextManager();
131

    
132
    private static LoginManager loginManager = new LoginManager();
133

    
134
    private static TermManager termManager = new TermManager();
135

    
136
    private static SearchManager searchManager = new SearchManager();
137

    
138
    private static EditorManager editorManager = new EditorManager();
139

    
140
    private static UseObjectStore useObjectInitializer = new UseObjectStore();
141

    
142
    private static ProgressMonitorClientManager progressMonitorClientManager = new ProgressMonitorClientManager();
143

    
144
    private static CdmStoreConnector job;
145

    
146
    private static CDMServer managedServer;
147

    
148
    private Language language;
149

    
150
    private ICdmSource cdmSource;
151

    
152
    private boolean isConnected;
153

    
154

    
155

    
156
    /**
157
     * <p>
158
     * getDefault
159
     * </p>
160
     *
161
     * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
162
     */
163
    protected static CdmStore getDefault(boolean connecting){
164
        if (instance != null && instance.isConnected) {
165
            return instance;
166
        } else{// if (instance == null || !instance.isConnected) {
167
            if (connecting){
168
                MessagingUtils.dataSourceNotAvailableWarningDialog(instance);
169
            }else{
170
                MessagingUtils.noDataSourceWarningDialog(instance);
171
            }
172

    
173

    
174
            return null;
175
        }
176
    }
177
    /**
178
     * <p>
179
     * getDefault
180
     * </p>
181
     *
182
     * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
183
     */
184
    protected static CdmStore getDefault(){
185
       return getDefault(false);
186
    }
187

    
188
    /**
189
     * Initialize the with the last edited datasource
190
     */
191
    public static void connect() {
192

    
193
        ICdmSource cdmSource;
194
        try {
195

    
196
            cdmSource = CdmDataSourceRepository.getCurrentCdmSource();
197
            connect(cdmSource);
198
        } catch (Exception e) {
199
            MessagingUtils.messageDialog("Connection to CDM Source Failed", CdmStore.class, "Could not connect to target CDM Source", e);
200
        }
201

    
202

    
203
    }
204

    
205
    /**
206
     * Initialize with a specific datasource
207
     *
208
     * @param datasource
209
     *            a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
210
     */
211
    public static void connect(ICdmSource cdmSource) {
212
        connect(cdmSource, DEFAULT_DB_SCHEMA_VALIDATION,
213
                DEFAULT_APPLICATION_CONTEXT);
214
    }
215

    
216
    public static void connect(ICdmSource cdmSource, RemotingLoginDialog loginDialog) {
217
        connect(cdmSource,
218
                DEFAULT_DB_SCHEMA_VALIDATION,
219
                DEFAULT_APPLICATION_CONTEXT,
220
                loginDialog);
221
    }
222

    
223
    /**
224
     * Initialize and provide
225
     *
226
     * @param datasource
227
     * @param dbSchemaValidation
228
     * @param applicationContextBean
229
     */
230
    private static void connect(final ICdmSource cdmSource,
231
            final DbSchemaValidation dbSchemaValidation,
232
            final Resource applicationContextBean) {
233

    
234
        MessagingUtils.info("Connecting to datasource: " + cdmSource);
235

    
236
        job = new CdmStoreConnector(Display.getDefault(), cdmSource,
237
                dbSchemaValidation, applicationContextBean);
238
        job.setUser(true);
239
        job.setPriority(Job.BUILD);
240
        job.schedule();
241

    
242
    }
243

    
244
    private static void connect(final ICdmSource cdmSource,
245
            final DbSchemaValidation dbSchemaValidation,
246
            final Resource applicationContextBean,
247
            RemotingLoginDialog remotingLoginDialog) {
248
        RemotingLoginDialog loginDialog = remotingLoginDialog;
249
        if(isActive()) {
250
            // before we connect we clear the entity caches and the sessions
251
            CdmRemoteCacheManager.removeEntityCaches();
252
            if(getCurrentSessionManager(true) != null) {
253
                getCurrentSessionManager(true).disposeAll();
254
            }
255
        }
256
        MessagingUtils.info("Connecting to datasource: " + cdmSource);
257

    
258
        job = new CdmStoreConnector(Display.getDefault(),
259
                cdmSource,
260
                dbSchemaValidation,
261
                applicationContextBean);
262
        job.start(loginDialog);
263

    
264
    }
265

    
266
    public static boolean isConnecting() {
267
        return job != null && job.getState() == Job.RUNNING;
268
    }
269

    
270
    /**
271
     * Closes the current application context
272
     *
273
     * @param monitor
274
     *            a {@link org.eclipse.core.runtime.IProgressMonitor} object.
275
     */
276
    public static void close(final IProgressMonitor monitor) {
277
        Display.getDefault().asyncExec(new Runnable() {
278

    
279
            @Override
280
            public void run() {
281
                getContextManager().notifyContextAboutToStop(monitor);
282
                if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
283
                    getContextManager().notifyContextStop(monitor);
284
                    instance.close();
285
                }
286
            }
287
        });
288
    }
289

    
290
    public static void close(IProgressMonitor monitor, boolean async) {
291
        if(async) {
292
            close(monitor);
293
        } else {
294
            getContextManager().notifyContextAboutToStop(monitor);
295
            if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
296
                getContextManager().notifyContextStop(monitor);
297
                instance.close();
298
            }
299
        }
300

    
301
    }
302
    private void close() {
303
        isConnected = false;
304
        cdmSource = null;
305
        CdmApplicationState.dispose();
306
    }
307

    
308
    static void setInstance(ICdmRepository applicationController,
309
            ICdmSource cdmSource) {
310
        instance = new CdmStore(applicationController, cdmSource);
311
        CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
312

    
313
    }
314

    
315
    private CdmStore(ICdmRepository repository,
316
            ICdmSource cdmSource) {
317
        CdmApplicationState.setCurrentAppConfig(repository);
318
        CdmApplicationState.setCurrentDataChangeService(new CdmUIDataChangeService());
319
        this.cdmSource = cdmSource;
320
        isConnected = true;
321
    }
322

    
323
    /**
324
     * All calls to the datastore require
325
     *
326
     * @return
327
     */
328
    private ICdmRepository getApplicationConfiguration() {
329
        try {
330
            return CdmApplicationState.getCurrentAppConfig();
331
        } catch (Exception e) {
332
            MessagingUtils.error(CdmStore.class, e);
333
        }
334
        return null;
335
    }
336

    
337
    /**
338
     * <p>
339
     * getCurrentApplicationController
340
     * </p>
341
     *
342
     * @return a
343
     *         {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
344
     *         object.
345
     */
346
    public static ICdmRepository getCurrentApplicationConfiguration() {
347
        if (getDefault() != null) {
348
            return getDefault().getApplicationConfiguration();
349
        }
350
        return null;
351
    }
352

    
353
    /*
354
     * CONVERSATIONS
355
     */
356

    
357
    /**
358
     * Creates a new conversation, binds resources to the conversation and start
359
     * a transaction for this conversation.
360
     *
361
     * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
362
     *         object.
363
     */
364
    public static ConversationHolder createConversation() {
365
        ConversationHolder conversation = getCurrentApplicationConfiguration()
366
                .NewConversation();
367
        try {
368
            conversation.startTransaction();
369
        }catch(Exception e){
370
            MessagingUtils.messageDialog("No database connection", CdmStore.class, "No database connection available", e);
371
        }
372
        return conversation;
373
    }
374

    
375
    //FIXME:Remoting should be removed after moving completely to remoting
376
    private MockCdmEntitySessionManager mockCdmEntitySessionManager;
377

    
378
    private ICdmEntitySessionManager getSessionManager() {
379
        //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
380
        //               bad hack which should be finally removed
381
        if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController) {
382
            return ((CdmApplicationRemoteController)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
383
        } else {
384
            if(mockCdmEntitySessionManager == null) {
385
                mockCdmEntitySessionManager = new MockCdmEntitySessionManager();
386
            }
387
            return mockCdmEntitySessionManager;
388
        }
389
    }
390
    public static  ICdmEntitySessionManager getCurrentSessionManager() {
391
        return getCurrentSessionManager(false);
392
    }
393

    
394

    
395
    public static  ICdmEntitySessionManager getCurrentSessionManager(boolean connecting) {
396
        if (getDefault(connecting) != null) {
397
            return getDefault(connecting).getSessionManager();
398
        }
399
        return null;
400

    
401
    }
402

    
403
    /**
404
     * Generic method that will scan the getters of {@link ICdmRepository} for the given service
405
     * interface. If a matching getter is found the according service implementation is returned by
406
     * invoking the getter otherwise the method returns <code>null</code>.
407
     *
408
     * @param <T>
409
     * @param serviceClass
410
     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
411
     */
412
    public static <T extends IService> T getService(Class<T> serviceClass) {
413
        T service = null;
414
        try {
415
            service = CdmApplicationState.getService(serviceClass);
416
        } catch (CdmApplicationException cae) {
417
            MessagingUtils.error(CdmStore.class, cae);
418
        }
419

    
420
        return service;
421
    }
422

    
423
    /**
424
     * @see #getService(Class)
425
     * As ICommonService is not extending IService we need a specific request here
426
     */
427
    public static ICommonService getCommonService() {
428
        return CdmApplicationState.getCommonService();
429

    
430
    }
431

    
432
    /**
433
     * <p>
434
     * getAuthenticationManager
435
     * </p>
436
     *
437
     * @return a
438
     *         {@link org.springframework.security.authentication.ProviderManager}
439
     *         object.
440
     */
441
    public static AuthenticationManager getAuthenticationManager() {
442
        return getCurrentApplicationConfiguration().getAuthenticationManager();
443
    }
444

    
445
    /**
446
     * <p>
447
     * getAuthenticationManager
448
     * </p>
449
     *
450
     * @return a
451
     *         {@link ICdmPermissionEvaluator} object.
452
     */
453
    public static ICdmPermissionEvaluator getPermissionEvaluator() {
454
        return getCurrentApplicationConfiguration().getPermissionEvaluator();
455
    }
456

    
457

    
458
    /*
459
     * SECURITY RELATED CONVENIENCE METHODS
460
     */
461

    
462
    /**
463
     * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
464
     *
465
     * @param targetDomainObject
466
     * @param permission
467
     * @return
468
     */
469
    public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject, EnumSet<CRUD> permission){
470
        //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
471
        SecurityContext context = SecurityContextHolder.getContext();
472
        PermissionEvaluator pe = getPermissionEvaluator();
473
        boolean hasPermission = false;
474
        try {
475
            hasPermission = getPermissionEvaluator().hasPermission(context.getAuthentication(), targetDomainObject,
476
                    permission);
477
        } catch (org.springframework.security.access.AccessDeniedException e) {
478
            /* IGNORE */
479
        }
480
        return hasPermission;
481
    }
482

    
483
    /**
484
     * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
485
     *
486
     * @param targetDomainObject
487
     * @param permission
488
     * @return
489
     */
490
    public static boolean currentAuthentiationHasPermission(Class<? extends CdmBase> targetType, EnumSet<CRUD> permission){
491
        boolean hasPermission = false;
492
        try {
493
            hasPermission = getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType.getName(), permission);
494
        } catch (org.springframework.security.access.AccessDeniedException e) {
495
            /* IGNORE */
496
        }
497
        return hasPermission;
498
    }
499

    
500
    public static boolean currentAuthentiationHasOneOfRoles(Role ... roles){
501
        boolean hasPermission = false;
502
        try {
503
            hasPermission =  getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles);
504
        } catch (org.springframework.security.access.AccessDeniedException e) {
505
            /* IGNORE */
506
        }
507
        return hasPermission;
508
    }
509

    
510
    public static Authentication getCurrentAuthentiation() {
511
        SecurityContext context = SecurityContextHolder.getContext();
512
        return context.getAuthentication();
513
    }
514

    
515
    /*
516
     * LANGUAGE
517
     */
518

    
519
    /**
520
     * Provides access to the global default language set in the application preferences.
521
     *
522
     * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
523
     */
524
    public static Language getDefaultLanguage() {
525
        if (getDefault().getLanguage() == null) {
526
            getDefault().setLanguage(PreferencesUtil.getGlobalLanguage());
527
        }
528
        return getDefault().getLanguage();
529
    }
530

    
531
    /**
532
     * <p>
533
     * setDefaultLanguage
534
     * </p>
535
     *
536
     * @param language
537
     *            a {@link eu.etaxonomy.cdm.model.common.Language} object.
538
     */
539
    public static void setDefaultLanguage(Language language) {
540
        getDefault().setLanguage(language);
541
    }
542

    
543
    /**
544
     * @return the language
545
     */
546
    private Language getLanguage() {
547
        return language;
548
    }
549

    
550
    /**
551
     * @param language
552
     *            the language to set
553
     */
554
    private void setLanguage(Language language) {
555
        this.language = language;
556
    }
557

    
558
    /*
559
     * LOGIN
560
     */
561

    
562
    /**
563
     * <p>
564
     * Getter for the field <code>loginManager</code>.
565
     * </p>
566
     *
567
     * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
568
     */
569
    public static LoginManager getLoginManager() {
570
        return loginManager;
571
    }
572

    
573
    /**
574
     * <p>
575
     * Getter for the field <code>contextManager</code>.
576
     * </p>
577
     *
578
     * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
579
     */
580
    public static ContextManager getContextManager() {
581
        return contextManager;
582
    }
583

    
584
    public static TermManager getTermManager() {
585
        return termManager;
586
    }
587

    
588
    public static SearchManager getSearchManager() {
589
        return searchManager;
590
    }
591

    
592
    public static EditorManager getEditorManager() {
593
        return editorManager;
594
    }
595

    
596
    public static ProgressMonitorClientManager getProgressMonitorClientManager() {
597
        return progressMonitorClientManager;
598
    }
599

    
600
    /*
601
     * IMPORT/EXPORT FACTORIES
602
     */
603

    
604
    /**
605
     * <p>
606
     * Getter for the field <code>importHandler</code>.
607
     * </p>
608
     *
609
     * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
610
     */
611
    public static ImportManager getImportManager() {
612
        return ImportManager.NewInstance(getCurrentApplicationConfiguration());
613
    }
614

    
615
    /**
616
     * <p>
617
     * Getter for the field <code>exportHandler</code>.
618
     * </p>
619
     *
620
     * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
621
     */
622
    public static ExportManager getExportManager() {
623
        return ExportManager.NewInstance(getCurrentApplicationConfiguration());
624
    }
625

    
626
    /**
627
     * Whether this CdmStore is currently connected to a datasource
628
     *
629
     * @return a boolean.
630
     */
631
    public static boolean isActive() {
632
        return instance != null && instance.isConnected;
633
    }
634

    
635
    public static ICdmSource getActiveCdmSource() {
636
        if (isActive()) {
637
            return instance.getCdmSource();
638
        }
639
        return null;
640
    }
641

    
642
    /**
643
     * <p>
644
     * getDataSource
645
     * </p>
646
     *
647
     * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
648
     * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
649
     */
650
    //	public static ICdmDataSource getDataSource() {
651
    //		if (isActive()) {
652
    //			return (ICdmDataSource)instance.getCdmSource();
653
    //		}
654
    //		return null;
655
    //	}
656

    
657
    /**
658
     * @return
659
     */
660
    private ICdmSource getCdmSource() {
661
        return cdmSource;
662
    }
663

    
664

    
665
    public static <T extends CdmBase> IService<T> getService(T cdmBase){
666
        IService<T> service = null;
667
        if(cdmBase!=null){
668
            //get corresponding service
669
            if(cdmBase.isInstanceOf(Reference.class)){
670
                service = (IService<T>) getService(IReferenceService.class);
671
            }
672
            else if (cdmBase.isInstanceOf(AgentBase.class)){
673
                service = (IService<T>) getService(IAgentService.class);
674
            }
675
            else if (cdmBase instanceof TaxonName) {
676
                service = (IService<T>) getService(INameService.class);
677
            }
678
            else if (cdmBase instanceof TaxonBase) {
679
                service = (IService<T>) getService(ITaxonService.class);
680
            }
681
            else if (cdmBase instanceof SpecimenOrObservationBase) {
682
                service = (IService<T>) getService(IOccurrenceService.class);
683
            }
684
            else if (cdmBase instanceof Media) {
685
                service = (IService<T>) getService(IMediaService.class);
686
            }
687
            else if (cdmBase instanceof Collection) {
688
                service = (IService<T>) getService(ICollectionService.class);
689
            }
690
            else if (cdmBase instanceof eu.etaxonomy.cdm.model.common.User) {
691
                service = (IService<T>) getService(IUserService.class);
692
            }
693
            else if (cdmBase instanceof Group) {
694
            	service = (IService<T>) getService(IGroupService.class);
695
            }
696
            else if (cdmBase instanceof WorkingSet) {
697
            	service = (IService<T>) getService(IWorkingSetService.class);
698
            }
699
            else if (cdmBase instanceof TermVocabulary<?>) {
700
            	service = (IService<T>) getService(IVocabularyService.class);
701
            }
702
            else if (cdmBase instanceof DefinedTermBase<?>) {
703
            	service = (IService<T>) getService(ITermService.class);
704
            }
705
            else if (cdmBase instanceof Primer) {
706
                service = (IService<T>) getService(IPrimerService.class);
707
            }
708
            else if (cdmBase instanceof Amplification) {
709
                service = (IService<T>) getService(IAmplificationService.class);
710
            }
711
            else if (cdmBase instanceof PolytomousKey) {
712
                service = (IService<T>) getService(IPolytomousKeyService.class);
713
            }
714
            else if (cdmBase instanceof PolytomousKeyNode) {
715
                service = (IService<T>) getService(IPolytomousKeyNodeService.class);
716
            }
717
            else if (cdmBase instanceof Annotation) {
718
                service = (IService<T>) getService(IAnnotationService.class);
719
            }
720
            else if (cdmBase instanceof Classification) {
721
                service = (IService<T>) getService(IClassificationService.class);
722
            }
723
            else if (cdmBase instanceof DescriptionBase<?>) {
724
                service = (IService<T>) getService(IDescriptionService.class);
725
            }
726
            else if (cdmBase instanceof EntityConstraintViolation) {
727
                service = (IService<T>) getService(IEntityConstraintViolationService.class);
728
            }
729
            else if (cdmBase instanceof EntityValidation) {
730
                service = (IService<T>) getService(IEntityValidationService.class);
731
            }
732
            else if (cdmBase instanceof FeatureNode) {
733
                service = (IService<T>) getService(IFeatureNodeService.class);
734
            }
735
            else if (cdmBase instanceof FeatureTree) {
736
                service = (IService<T>) getService(IFeatureTreeService.class);
737
            }
738
            else if (cdmBase instanceof GrantedAuthority) {
739
                service = (IService<T>) getService(IGrantedAuthorityService.class);
740
            }
741
            else if (cdmBase instanceof Marker) {
742
                service = (IService<T>) getService(IMarkerService.class);
743
            }
744
            else if (cdmBase instanceof Registration) {
745
                service = (IService<T>) getService(IRegistrationService.class);
746
            }
747
            else if (cdmBase instanceof Rights) {
748
                service = (IService<T>) getService(IRightsService.class);
749
            }
750
            else{
751
            	String message = "Service for entity of class %s not yet implemented in TaxEditor";
752
            	message = String.format(message, cdmBase.getClass().getSimpleName());
753
            	throw new RuntimeException(message);
754
            }
755
        }
756
        return service;
757
    }
758

    
759
    public static void setManagedServer(CDMServer server) {
760
        managedServer = server;
761
    }
762

    
763
    public static CDMServer getManagedServer() {
764
        return managedServer;
765
    }
766

    
767
}
(3-3/14)