2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.util
.ArrayList
;
13 import java
.util
.LinkedHashMap
;
14 import java
.util
.List
;
16 import java
.util
.SortedSet
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
23 import eu
.etaxonomy
.cdm
.api
.service
.ILocationService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.impl
.TaxonServiceConfiguratorImpl
;
30 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
31 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
32 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
33 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
35 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
36 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
37 import eu
.etaxonomy
.cdm
.model
.common
.VocabularyEnum
;
38 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
39 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
40 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
41 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
42 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
43 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.Article
;
45 import eu
.etaxonomy
.cdm
.model
.reference
.Book
;
46 import eu
.etaxonomy
.cdm
.model
.reference
.BookSection
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
48 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
51 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
52 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
55 * This implementation of ICdmDataRepository depends on hibernate sessions to store the data correctly
56 * for the current session. No state is held in this class.
58 * Only methods that either get or manipulate data are exposed here. So this class acts as a facade
59 * for the methods in cdmlib-service.
66 public class CdmStore
{
67 private static final Logger logger
= Logger
.getLogger(CdmStore
.class);
69 // FIXME change this to ClassPathResources as soon as it is included into the plugin
70 private static String DEFAULT_APPLICATION_CONTEXT
= null;
71 private static DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.UPDATE
;
73 private static CdmStore instance
;
75 private CdmApplicationController applicationController
;
77 private ConversationHolder globalReadOnlyConversation
;
79 private static DbSchemaValidation dbSchemaValidation
;
85 public static CdmStore
getDefault(){
86 return getDefault(DEFAULT_APPLICATION_CONTEXT
);
91 * @param applicationContextBean
94 public static CdmStore
getDefault(String applicationContextBean
){
96 logger
.info("Initializing application context ...");
97 ICdmDataSource cdmDatasource
= CdmDataSourceRepository
.getDefault().
98 getCurrentDataSource();
100 instance
= new CdmStore(cdmDatasource
, getDbSchemaValidation(), applicationContextBean
);
102 logger
.info("Application context initialized.");
110 private static DbSchemaValidation
getDbSchemaValidation() {
111 return (dbSchemaValidation
== null) ? DEFAULT_DB_SCHEMA_VALIDATION
: dbSchemaValidation
;
117 private CdmStore(ICdmDataSource dataSource
, DbSchemaValidation dbSchemaValidation
, String applicationContextBean
) {
118 // TODO application context bean is not honored by application controller at the moment.
119 // cdmDefaultApplicationController bean gets loaded per default always.
122 CdmStore
.DEFAULT_APPLICATION_CONTEXT
= applicationContextBean
;
123 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
125 // logger.warn("OMITTING TERM LOADING FOR DEBUGGING");
126 // applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation, true);
127 applicationController
= CdmApplicationController
.NewInstance(dataSource
, dbSchemaValidation
);
128 } catch (Exception e
) {
129 throw new RuntimeException(e
);
133 public Set
<TaxonRelationshipType
> getConceptRelationshipTypes() {
134 Set
<TaxonRelationshipType
> conceptRelationshipTypes
= getTaxonRelationshipTypes().getTerms();
135 // remove these two relations as they are considered standard taxon relations
136 conceptRelationshipTypes
.remove(TaxonRelationshipType
.MISAPPLIED_NAME_FOR());
137 conceptRelationshipTypes
.remove(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN());
139 return conceptRelationshipTypes
;
142 public ReferenceBase
<?
> getDefaultSec() {
143 // TODO why is this returning null? and of course, replace w the real deal
144 return applicationController
.getReferenceService().getReferenceByUuid(
145 UUID
.fromString("f3593c18-a8d2-4e51-bdad-0befbf8fb2d1"));
148 private TermVocabulary
<Feature
> getFeaturesInternal() {
149 return applicationController
.getTermService().getVocabulary(VocabularyEnum
.Feature
);
152 public static TermVocabulary
<Feature
> getFeatures() {
153 return getDefault().getFeaturesInternal();
156 public SortedSet
<NameRelationshipType
> getNameRelationshipTypes() {
157 return applicationController
.getTermService().getVocabulary(VocabularyEnum
.NameRelationshipType
).getTermsOrderedByLabels(Language
.DEFAULT());
160 public TermVocabulary
<NomenclaturalStatusType
> getNomenclaturalStatusTypesInternal() {
161 return applicationController
.getTermService().getVocabulary(VocabularyEnum
.NomenclaturalStatusType
);
164 public static Set
<NomenclaturalStatusType
> getNomenclaturalStatusTypes(){
166 return getDefault().getNomenclaturalStatusTypesInternal().getTerms();
169 private SortedSet
<Rank
> getRanksInternal() {
170 OrderedTermVocabulary rankVocabulary
= HibernateProxyHelper
.deproxy(applicationController
.getTermService().getVocabulary(VocabularyEnum
.Rank
), OrderedTermVocabulary
.class);
171 return rankVocabulary
.getOrderedTerms(null);
174 public static SortedSet
<Rank
> getRanks() {
175 return getDefault().getRanksInternal();
179 public List
<Taxon
> getRootTaxa() {
180 boolean onlyWithChildren
= false;
181 boolean withMisapplications
= true;
183 return applicationController
.getTaxonService().getRootTaxa(
184 getDefaultSec(), onlyWithChildren
, withMisapplications
);
187 public OrderedTermVocabulary
<TaxonRelationshipType
> getTaxonRelationshipTypes() {
188 return (OrderedTermVocabulary
<TaxonRelationshipType
>) applicationController
.getTermService().getVocabulary(VocabularyEnum
.TaxonRelationshipType
);
191 public TermVocabulary
<SpecimenTypeDesignationStatus
> getSpecimenTypeDesignationStatus() {
192 return applicationController
.getTermService().getVocabulary(VocabularyEnum
.SpecimenTypeDesignationStatus
);
196 public UUID
saveTaxon(Taxon taxon
) {
197 return applicationController
.getTaxonService().save(taxon
);
200 public void setApplicationController(
201 CdmApplicationController applicationController
) {
202 this.applicationController
= applicationController
;
205 public CdmApplicationController
getApplicationController(){
206 return applicationController
;
211 * Create a new conversation and bind resources to it
215 public static ConversationHolder
NewConversation(){
217 CdmStore store
= getDefault();
218 CdmApplicationController controller
= store
.getApplicationController();
220 ConversationHolder conversation
= controller
.NewConversation();
226 * Creates a new conversation, binds resources to the conversation and
227 * start a transaction for this conversation.
231 public static ConversationHolder
NewTransactionalConversation() {
232 ConversationHolder conversation
= NewConversation();
234 conversation
.startTransaction();
239 private ConversationHolder
getGlobalReadOnlyConversation() {
240 ConversationHolder conversation
= globalReadOnlyConversation
== null
242 : globalReadOnlyConversation
;
247 public static ConversationHolder
getGlobalConversation(){
248 return getDefault().getGlobalReadOnlyConversation();
251 public static ITaxonService
getTaxonService(){
252 return getDefault().getApplicationController().getTaxonService();
255 public static INameService
getNameService(){
256 return getDefault().getApplicationController().getNameService();
259 public static IReferenceService
getReferenceService(){
260 return getDefault().getApplicationController().getReferenceService();
263 public static ILocationService
getLocationService(){
264 return getDefault().getApplicationController().getLocationService();
271 @SuppressWarnings("unchecked")
272 public static List
<TaxonNameBase
> searchNameString(String searchText
) {
273 List
<TaxonNameBase
> resultSet
= new ArrayList
<TaxonNameBase
>();
274 resultSet
.addAll(getNameService()
275 .getNamesByName(searchText
.replace("*", "%")));
279 private List
<IdentifiableEntity
> findTaxaByName(String queryString
, boolean restrictToTaxonObjs
) {
281 ITaxonServiceConfigurator configurator
= new TaxonServiceConfiguratorImpl();
283 configurator
.setSearchString(queryString
);
284 configurator
.setDoTaxa(true);
285 configurator
.setMatchMode(MatchMode
.BEGINNING
);
286 if (restrictToTaxonObjs
) {
287 configurator
.setDoNamesWithoutTaxa(false);
288 configurator
.setDoSynonyms(false);
290 configurator
.setDoNamesWithoutTaxa(true);
291 configurator
.setDoSynonyms(true);
293 configurator
.setReferenceBase(null);
294 configurator
.setPageNumber(0);
295 // TODO currently limit results to 1000
296 configurator
.setPageSize(1000);
298 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
303 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
){
304 return getDefault().findTaxaByName(queryString
, false);
307 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
, boolean restrictToTaxonObjs
){
308 return getDefault().findTaxaByName(queryString
, restrictToTaxonObjs
);
312 * Searches for references by string. "%" is used as a wildcard.
317 public static List
<ReferenceBase
> getReferencesByTitle(String reference
) {
319 reference
= reference
.replace("*", "%");
320 List
<ReferenceBase
> resultsList
= null;
322 resultsList
= getReferenceService().getReferencesByTitle(reference
);
323 } catch (RuntimeException e
) {
324 // MessageDialog.openError(GlobalController.getShell(), "Search reference error",
325 // "Reference search returned an error. This could be a Hibernate concurrency problem. " +
326 // "Please try saving your work, then searching again.");
333 public static List
<Language
> getLanguages() {
334 ITermService termService
= getDefault().getApplicationController().getTermService();
335 ArrayList
<Language
> languages
= new ArrayList
<Language
>();
336 for (Language language
: termService
.getLanguageVocabulary()) {
337 languages
.add(language
);
340 // TODO sort languages
342 // languages.add(Language.ENGLISH());
343 // languages.add(Language.GERMAN());
352 public static Language
getDefaultLanguage(){
353 return Language
.ENGLISH();
359 private static String
getApplicationContextBean() {
360 return DEFAULT_APPLICATION_CONTEXT
;
366 public static LinkedHashMap
<Class
<?
>, String
> getReferenceTypes() {
367 LinkedHashMap
<Class
<?
>, String
> nomReferenceTypeMap
= new LinkedHashMap
<Class
<?
>, String
>();
369 // referenceTypeMap.put(BibtexReference.class, "BibTeX Reference");
370 nomReferenceTypeMap
.put(Article
.class, "Article");
371 nomReferenceTypeMap
.put(Generic
.class, "Generic");
372 nomReferenceTypeMap
.put(Book
.class, "Book");
373 nomReferenceTypeMap
.put(BookSection
.class, "Book Section");
375 return nomReferenceTypeMap
;