changed flush mode to COMMIT. Solves lots of issues regarding hibernate conversations.
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / IdentifiableServiceBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.api.service;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.transaction.annotation.Transactional;
18
19 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
20 import eu.etaxonomy.cdm.api.service.pager.Pager;
21 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.common.ISourceable;
24 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
26 import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
27 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
28 import eu.etaxonomy.cdm.model.media.Rights;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
31
32 @Transactional(readOnly = true)
33 public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO extends IIdentifiableDao<T>> extends AnnotatableServiceBase<T,DAO>
34 implements IIdentifiableEntityService<T>{
35 @SuppressWarnings("unused")
36 private static final Logger logger = Logger.getLogger(IdentifiableServiceBase.class);
37
38
39 public Pager<Rights> getRights(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
40 Integer numberOfResults = dao.countRights(t);
41
42 List<Rights> results = new ArrayList<Rights>();
43 if(numberOfResults > 0) { // no point checking again
44 results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
45 }
46
47 return new DefaultPagerImpl<Rights>(pageNumber, numberOfResults, pageSize, results);
48 }
49
50 public Pager<IdentifiableSource> getSources(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
51 Integer numberOfResults = dao.countSources(t);
52
53 List<IdentifiableSource> results = new ArrayList<IdentifiableSource>();
54 if(numberOfResults > 0) { // no point checking again
55 results = dao.getSources(t, pageSize, pageNumber,propertyPaths);
56 }
57
58 return new DefaultPagerImpl<IdentifiableSource>(pageNumber, numberOfResults, pageSize, results);
59 }
60
61 protected List<T> findByTitle(IIdentifiableEntityServiceConfigurator config){
62 return ((IIdentifiableDao)dao).findByTitle(config.getTitleSearchString(),
63 config.getMatchMode(), 0, -1, null);
64 // TODO: Implement parameters pageSize, pageNumber, and criteria
65 }
66 /**
67 * FIXME Candidate for harmonization
68 * Given that this method is strongly typed, and generic, could we not simply expose it as
69 * List<T> findByTitle(String title) as it is somewhat less cumbersome. Admittedly, I don't
70 * understand what is going on with the configurators etc. so maybe there is a good reason for
71 * the design of this method.
72 * @param title
73 * @return
74 */
75 protected List<T> findCdmObjectsByTitle(String title){
76 return ((IIdentifiableDao)dao).findByTitle(title);
77 }
78
79 protected List<T> findCdmObjectsByTitle(String title, Class<T> clazz){
80 return ((IIdentifiableDao)dao).findByTitleAndClass(title, clazz);
81 }
82 protected List<T> findCdmObjectsByTitle(String title, CdmBase sessionObject){
83 return ((IIdentifiableDao)dao).findByTitle(title, sessionObject);
84 }
85
86 /*
87 * TODO - Migrated from CommonServiceBase
88 * (non-Javadoc)
89 * @see eu.etaxonomy.cdm.api.service.ICommonService#getSourcedObjectById(java.lang.String, java.lang.String)
90 */
91 public ISourceable getSourcedObjectByIdInSource(Class clazz, String idInSource, String idNamespace) {
92 ISourceable result = null;
93
94 List<T> list = dao.findOriginalSourceByIdInSource(idInSource, idNamespace);
95 if (! list.isEmpty()){
96 result = list.get(0);
97 }
98 return result;
99 }
100
101 /* (non-Javadoc)
102 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#getUuidAndTitleCache()
103 */
104 public List<UuidAndTitleCache<T>> getUuidAndTitleCache() {
105 return dao.getUuidAndTitleCache();
106 }
107 }