Project

General

Profile

Download (26.2 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
import javax.print.attribute.standard.Media;
16

    
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
import org.springframework.security.core.userdetails.User;
29

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

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

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

    
123
    private static CdmStore instance;
124

    
125
    //private final ICdmRepository applicationConfiguration;
126

    
127
    private static ContextManager contextManager = new ContextManager();
128

    
129
    private static LoginManager loginManager = new LoginManager();
130

    
131
    private static TermManager termManager = new TermManager();
132

    
133
    private static SearchManager searchManager = new SearchManager();
134

    
135
    private static EditorManager editorManager = new EditorManager();
136

    
137
    private static UseObjectStore useObjectInitializer = new UseObjectStore();
138

    
139
    private static ProgressMonitorClientManager progressMonitorClientManager = new ProgressMonitorClientManager();
140

    
141
    private static CdmStoreConnector job;
142

    
143
    private static CDMServer managedServer;
144

    
145
    private Language language;
146

    
147
    private ICdmSource cdmSource;
148

    
149
    private boolean isConnected;
150

    
151

    
152

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

    
170

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

    
185
    /**
186
     * Initialize the with the last edited datasource
187
     */
188
    public static void connect() {
189

    
190
        ICdmSource cdmSource;
191
        try {
192

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

    
199

    
200
    }
201

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

    
213
    public static void connect(ICdmSource cdmSource, RemotingLoginDialog loginDialog) {
214
        connect(cdmSource,
215
                DEFAULT_DB_SCHEMA_VALIDATION,
216
                DEFAULT_APPLICATION_CONTEXT,
217
                loginDialog);
218
    }
219

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

    
231
        MessagingUtils.info("Connecting to datasource: " + cdmSource);
232

    
233
        job = new CdmStoreConnector(Display.getDefault(), cdmSource,
234
                dbSchemaValidation, applicationContextBean);
235
        job.setUser(true);
236
        job.setPriority(Job.BUILD);
237
        job.schedule();
238

    
239
    }
240

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

    
255
        job = new CdmStoreConnector(Display.getDefault(),
256
                cdmSource,
257
                dbSchemaValidation,
258
                applicationContextBean);
259
        job.start(loginDialog);
260

    
261
    }
262

    
263
    public static boolean isConnecting() {
264
        return job != null && job.getState() == Job.RUNNING;
265
    }
266

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

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

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

    
298
    }
299
    private void close() {
300
        isConnected = false;
301
        cdmSource = null;
302
        CdmApplicationState.dispose();
303
    }
304

    
305
    static void setInstance(ICdmRepository applicationController,
306
            ICdmSource cdmSource) {
307
        instance = new CdmStore(applicationController, cdmSource);
308
        CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
309

    
310
    }
311

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

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

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

    
350
    /*
351
     * CONVERSATIONS
352
     */
353

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

    
372
    //FIXME:Remoting should be removed after moving completely to remoting
373
    private MockCdmEntitySessionManager mockCdmEntitySessionManager;
374

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

    
391

    
392
    public static  ICdmEntitySessionManager getCurrentSessionManager(boolean connecting) {
393
        if (getDefault(connecting) != null) {
394
            return getDefault(connecting).getSessionManager();
395
        }
396
        return null;
397

    
398
    }
399

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

    
417
        return service;
418
    }
419

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

    
427
    }
428

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

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

    
454

    
455
    /*
456
     * SECURITY RELATED CONVENIENCE METHODS
457
     */
458

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

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

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

    
507
    public static Authentication getCurrentAuthentiation() {
508
        SecurityContext context = SecurityContextHolder.getContext();
509
        return context.getAuthentication();
510
    }
511

    
512
    /*
513
     * LANGUAGE
514
     */
515

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

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

    
540
    /**
541
     * @return the language
542
     */
543
    private Language getLanguage() {
544
        return language;
545
    }
546

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

    
555
    /*
556
     * LOGIN
557
     */
558

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

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

    
581
    public static TermManager getTermManager() {
582
        return termManager;
583
    }
584

    
585
    public static SearchManager getSearchManager() {
586
        return searchManager;
587
    }
588

    
589
    public static EditorManager getEditorManager() {
590
        return editorManager;
591
    }
592

    
593
    public static ProgressMonitorClientManager getProgressMonitorClientManager() {
594
        return progressMonitorClientManager;
595
    }
596

    
597
    /*
598
     * IMPORT/EXPORT FACTORIES
599
     */
600

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

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

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

    
632
    public static ICdmSource getActiveCdmSource() {
633
        if (isActive()) {
634
            return instance.getCdmSource();
635
        }
636
        return null;
637
    }
638

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

    
654
    /**
655
     * @return
656
     */
657
    private ICdmSource getCdmSource() {
658
        return cdmSource;
659
    }
660

    
661

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

    
756
    public static void setManagedServer(CDMServer server) {
757
        managedServer = server;
758
    }
759

    
760
    public static CDMServer getManagedServer() {
761
        return managedServer;
762
    }
763

    
764
}
(3-3/14)