708e65c6542af0c0138a91bb8a76bdc0d72ced8e
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStore.java
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.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.security.providers.ProviderManager;
19
20 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.service.ILocationService;
23 import eu.etaxonomy.cdm.api.service.INameService;
24 import eu.etaxonomy.cdm.api.service.IReferenceService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
27 import eu.etaxonomy.cdm.api.service.config.impl.TaxonServiceConfiguratorImpl;
28 import eu.etaxonomy.cdm.database.DbSchemaValidation;
29 import eu.etaxonomy.cdm.database.ICdmDataSource;
30 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
31 import eu.etaxonomy.cdm.model.common.Language;
32 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33 import eu.etaxonomy.cdm.model.reference.Article;
34 import eu.etaxonomy.cdm.model.reference.Book;
35 import eu.etaxonomy.cdm.model.reference.BookSection;
36 import eu.etaxonomy.cdm.model.reference.Generic;
37 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
38 import eu.etaxonomy.cdm.model.taxon.Taxon;
39 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
40 import eu.etaxonomy.cdm.persistence.query.MatchMode;
41 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
42
43 /**
44 * This implementation of ICdmDataRepository depends on hibernate sessions to store the data correctly
45 * for the current session. No state is held in this class.
46 *
47 * Only methods that either get or manipulate data are exposed here. So this class acts as a facade
48 * for the methods in cdmlib-service.
49 *
50 *
51 * @author n.hoffmann
52 * @created 17.03.2009
53 * @version 1.0
54 */
55 public class CdmStore{
56 private static final Logger logger = Logger.getLogger(CdmStore.class);
57
58 // FIXME change this to ClassPathResources as soon as it is included into the plugin
59 private static String DEFAULT_APPLICATION_CONTEXT = null;
60 private static DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.UPDATE;
61
62 private static CdmStore instance;
63
64 private CdmApplicationController applicationController;
65
66 private ConversationHolder globalReadOnlyConversation;
67
68 private static DbSchemaValidation dbSchemaValidation;
69
70 /**
71 *
72 * @return
73 */
74 public static CdmStore getDefault(){
75 return getDefault(DEFAULT_APPLICATION_CONTEXT);
76 }
77
78 /**
79 *
80 * @param applicationContextBean
81 * @return
82 */
83 public static CdmStore getDefault(String applicationContextBean){
84 if(instance == null){
85 logger.info("Initializing application context ...");
86 ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().
87 getCurrentDataSource();
88
89 instance = new CdmStore(cdmDatasource, getDbSchemaValidation(), applicationContextBean);
90
91 logger.info("Application context initialized.");
92 }
93 return instance;
94 }
95
96 /**
97 * @return
98 */
99 private static DbSchemaValidation getDbSchemaValidation() {
100 return (dbSchemaValidation == null) ? DEFAULT_DB_SCHEMA_VALIDATION : dbSchemaValidation;
101 }
102
103 /**
104 *
105 */
106 private CdmStore(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, String applicationContextBean) {
107 // TODO application context bean is not honored by application controller at the moment.
108 // cdmDefaultApplicationController bean gets loaded per default always.
109
110 try {
111 CdmStore.DEFAULT_APPLICATION_CONTEXT = applicationContextBean;
112 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
113
114 // logger.warn("OMITTING TERM LOADING FOR DEBUGGING");
115 // applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation, true);
116 applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation);
117 } catch (Exception e) {
118 throw new RuntimeException(e);
119 }
120 }
121
122
123 public ReferenceBase<?> getDefaultSec() {
124 // TODO why is this returning null? and of course, replace w the real deal
125 return applicationController.getReferenceService().getReferenceByUuid(
126 UUID.fromString("f3593c18-a8d2-4e51-bdad-0befbf8fb2d1"));
127 }
128
129 /**
130 * @deprecated use taxonomic tree instead
131 */
132 public List<Taxon> getRootTaxa() {
133 boolean onlyWithChildren = false;
134 boolean withMisapplications = true;
135
136 return applicationController.getTaxonService().getRootTaxa(
137 getDefaultSec(), onlyWithChildren, withMisapplications);
138 }
139
140 /**
141 * @return
142 */
143 public List<TaxonomicTree> getTaxonomicTrees() {
144 return applicationController.getTaxonService().getAllTaxonomicTrees(10000, 0);
145 }
146
147
148 private CdmApplicationController getApplicationControllerInternal(){
149 return applicationController;
150 }
151
152 public static CdmApplicationController getApplicationController(){
153 return getDefault().getApplicationControllerInternal();
154 }
155
156
157 /**
158 * Create a new conversation and bind resources to it
159 *
160 * @return
161 */
162 public static ConversationHolder NewConversation(){
163
164 CdmStore store = getDefault();
165 CdmApplicationController controller = store.getApplicationController();
166
167 ConversationHolder conversation = controller.NewConversation();
168
169 return conversation;
170 }
171
172 /**
173 * Creates a new conversation, binds resources to the conversation and
174 * start a transaction for this conversation.
175 *
176 * @return
177 */
178 public static ConversationHolder NewTransactionalConversation() {
179 ConversationHolder conversation = NewConversation();
180
181 conversation.startTransaction();
182 return conversation;
183 }
184
185
186 private ConversationHolder getGlobalReadOnlyConversation() {
187 ConversationHolder conversation = globalReadOnlyConversation == null
188 ? NewConversation()
189 : globalReadOnlyConversation;
190 conversation.bind();
191 return conversation;
192 }
193
194 public static ConversationHolder getGlobalConversation(){
195 return getDefault().getGlobalReadOnlyConversation();
196 }
197
198
199 /*
200 * EXPOSING SERVICES
201 */
202
203 public static ITaxonService getTaxonService(){ return getDefault().getApplicationControllerInternal().getTaxonService();}
204
205 public static INameService getNameService(){ return getDefault().getApplicationControllerInternal().getNameService();}
206
207 public static IReferenceService getReferenceService(){ return getDefault().getApplicationControllerInternal().getReferenceService();}
208
209 public static ILocationService getLocationService(){ return getDefault().getApplicationControllerInternal().getLocationService();}
210
211 public static ProviderManager getAuthenticationManager() { return getDefault().getApplicationControllerInternal().getAuthenticationManager();}
212
213
214 /**
215 * @param searchText
216 * @return
217 */
218 @SuppressWarnings("unchecked")
219 public static List<TaxonNameBase> searchNameString(String searchText) {
220 List<TaxonNameBase> resultSet = new ArrayList<TaxonNameBase>();
221 resultSet.addAll(getNameService()
222 .getNamesByName(searchText.replace("*", "%")));
223 return resultSet;
224 }
225
226 private List<IdentifiableEntity> findTaxaByName(String queryString, boolean restrictToTaxonObjs) {
227
228 ITaxonServiceConfigurator configurator = new TaxonServiceConfiguratorImpl();
229
230 configurator.setSearchString(queryString);
231 configurator.setDoTaxa(true);
232 configurator.setMatchMode(MatchMode.BEGINNING);
233 if (restrictToTaxonObjs) {
234 configurator.setDoNamesWithoutTaxa(false);
235 configurator.setDoSynonyms(false);
236 } else {
237 configurator.setDoNamesWithoutTaxa(true);
238 configurator.setDoSynonyms(true);
239 }
240 configurator.setSec(null);
241 configurator.setPageNumber(0);
242 // TODO currently limit results to 1000
243 configurator.setPageSize(1000);
244
245 List<IdentifiableEntity> result = getTaxonService().findTaxaAndNames(configurator).getRecords();
246
247 return result;
248 }
249
250 public static List<IdentifiableEntity> searchTaxaByName(String queryString){
251 return getDefault().findTaxaByName(queryString, false);
252 }
253
254 public static List<IdentifiableEntity> searchTaxaByName(String queryString, boolean restrictToTaxonObjs){
255 return getDefault().findTaxaByName(queryString, restrictToTaxonObjs);
256 }
257
258 /**
259 * Searches for references by string. "%" is used as a wildcard.
260 *
261 * @param text
262 * @return
263 */
264 public static List<ReferenceBase> getReferencesByTitle(String reference) {
265
266 reference = reference.replace("*", "%");
267 List<ReferenceBase> resultsList = null;
268 try {
269 resultsList = getReferenceService().getReferencesByTitle(reference);
270 } catch (RuntimeException e) {
271 // MessageDialog.openError(GlobalController.getShell(), "Search reference error",
272 // "Reference search returned an error. This could be a Hibernate concurrency problem. " +
273 // "Please try saving your work, then searching again.");
274 e.printStackTrace();
275 }
276 return resultsList;
277 }
278
279 /**
280 * FIXME mock
281 *
282 * @return
283 */
284 public static Language getDefaultLanguage(){
285 return Language.ENGLISH();
286 }
287
288 /**
289 * @return
290 */
291 public static LinkedHashMap<Class<?>, String> getReferenceTypes() {
292 LinkedHashMap<Class<?>, String> nomReferenceTypeMap = new LinkedHashMap<Class<?>, String>();
293
294 // referenceTypeMap.put(BibtexReference.class, "BibTeX Reference");
295 nomReferenceTypeMap.put(Article.class, "Article");
296 nomReferenceTypeMap.put(Generic.class, "Generic");
297 nomReferenceTypeMap.put(Book.class, "Book");
298 nomReferenceTypeMap.put(BookSection.class, "Book Section");
299
300 return nomReferenceTypeMap;
301 }
302
303 }