3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.Hibernate
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.springframework
.stereotype
.Service
;
21 import org
.springframework
.transaction
.annotation
.Propagation
;
22 import org
.springframework
.transaction
.annotation
.Transactional
;
24 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
25 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeConfigurator
;
26 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeNotSupportedException
;
27 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
29 import eu
.etaxonomy
.cdm
.common
.IProgressMonitor
;
30 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
31 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
36 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
37 import eu
.etaxonomy
.cdm
.model
.location
.WaterbodyOrCountry
;
38 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
39 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
40 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
41 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
42 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnitBase
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldObservation
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
48 import eu
.etaxonomy
.cdm
.persistence
.dao
.AbstractBeanInitializer
;
49 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
50 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
51 import eu
.etaxonomy
.cdm
.persistence
.dao
.occurrence
.IOccurrenceDao
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
53 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
54 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
57 * @author a.babadshanjan
61 @Transactional(propagation
= Propagation
.SUPPORTS
, readOnly
= true)
62 public class OccurrenceServiceImpl
extends IdentifiableServiceBase
<SpecimenOrObservationBase
,IOccurrenceDao
> implements IOccurrenceService
{
64 static private final Logger logger
= Logger
.getLogger(OccurrenceServiceImpl
.class);
67 private IDefinedTermDao definedTermDao
;
70 private IDescriptionService descriptionService
;
73 private AbstractBeanInitializer beanInitializer
;
76 private ITaxonDao taxonDao
;
80 public OccurrenceServiceImpl() {
81 logger
.debug("Load OccurrenceService Bean");
86 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
89 public void updateTitleCache(Class
<?
extends SpecimenOrObservationBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<SpecimenOrObservationBase
> cacheStrategy
, IProgressMonitor monitor
) {
91 clazz
= SpecimenOrObservationBase
.class;
93 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
98 * FIXME Candidate for harmonization
101 public WaterbodyOrCountry
getCountryByIso(String iso639
) {
102 return this.definedTermDao
.getCountryByIso(iso639
);
107 * FIXME Candidate for harmonization
108 * move to termService
110 public List
<WaterbodyOrCountry
> getWaterbodyOrCountryByName(String name
) {
111 List
<?
extends DefinedTermBase
> terms
= this.definedTermDao
.getDefinedTermByRepresentationText(name
, WaterbodyOrCountry
.class);
112 List
<WaterbodyOrCountry
> countries
= new ArrayList
<WaterbodyOrCountry
>();
113 for (int i
=0;i
<terms
.size();i
++){
114 countries
.add((WaterbodyOrCountry
)terms
.get(i
));
120 protected void setDao(IOccurrenceDao dao
) {
124 public Pager
<DerivationEvent
> getDerivationEvents(SpecimenOrObservationBase occurence
, Integer pageSize
,Integer pageNumber
, List
<String
> propertyPaths
) {
125 Integer numberOfResults
= dao
.countDerivationEvents(occurence
);
127 List
<DerivationEvent
> results
= new ArrayList
<DerivationEvent
>();
128 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
129 results
= dao
.getDerivationEvents(occurence
, pageSize
, pageNumber
,propertyPaths
);
132 return new DefaultPagerImpl
<DerivationEvent
>(pageNumber
, numberOfResults
, pageSize
, results
);
135 public Pager
<DeterminationEvent
> getDeterminations(SpecimenOrObservationBase occurrence
, TaxonBase taxonBase
, Integer pageSize
,Integer pageNumber
, List
<String
> propertyPaths
) {
136 Integer numberOfResults
= dao
.countDeterminations(occurrence
, taxonBase
);
138 List
<DeterminationEvent
> results
= new ArrayList
<DeterminationEvent
>();
139 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
140 results
= dao
.getDeterminations(occurrence
,taxonBase
, pageSize
, pageNumber
, propertyPaths
);
143 return new DefaultPagerImpl
<DeterminationEvent
>(pageNumber
, numberOfResults
, pageSize
, results
);
146 public Pager
<Media
> getMedia(SpecimenOrObservationBase occurence
,Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
147 Integer numberOfResults
= dao
.countMedia(occurence
);
149 List
<Media
> results
= new ArrayList
<Media
>();
150 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
151 results
= dao
.getMedia(occurence
, pageSize
, pageNumber
, propertyPaths
);
154 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
158 * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#list(java.lang.Class, eu.etaxonomy.cdm.model.taxon.TaxonBase, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
160 public Pager
<SpecimenOrObservationBase
> list(Class
<?
extends SpecimenOrObservationBase
> type
, TaxonBase determinedAs
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
161 Integer numberOfResults
= dao
.count(type
,determinedAs
);
162 List
<SpecimenOrObservationBase
> results
= new ArrayList
<SpecimenOrObservationBase
>();
163 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
164 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
165 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
166 results
= dao
.list(type
,determinedAs
, pageSize
, start
, orderHints
,propertyPaths
);
168 return new DefaultPagerImpl
<SpecimenOrObservationBase
>(pageNumber
, numberOfResults
, pageSize
, results
);
172 public List
<UuidAndTitleCache
<DerivedUnitBase
>> getDerivedUnitBaseUuidAndTitleCache() {
173 return dao
.getDerivedUnitBaseUuidAndTitleCache();
177 public List
<UuidAndTitleCache
<FieldObservation
>> getFieldObservationUuidAndTitleCache() {
178 return dao
.getFieldObservationUuidAndTitleCache();
182 * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#getDerivedUnitFacade(eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase)
185 public DerivedUnitFacade
getDerivedUnitFacade(DerivedUnitBase derivedUnit
, List
<String
> propertyPaths
) throws DerivedUnitFacadeNotSupportedException
{
186 derivedUnit
= (DerivedUnitBase
<?
>)dao
.load(derivedUnit
.getUuid(), null);
187 DerivedUnitFacadeConfigurator config
= DerivedUnitFacadeConfigurator
.NewInstance();
188 config
.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
189 DerivedUnitFacade derivedUnitFacade
= DerivedUnitFacade
.NewInstance(derivedUnit
, config
);
190 beanInitializer
.initialize(derivedUnitFacade
, propertyPaths
);
191 return derivedUnitFacade
;
195 * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#listDerivedUnitFacades(eu.etaxonomy.cdm.model.description.DescriptionBase, java.util.List)
198 public List
<DerivedUnitFacade
> listDerivedUnitFacades(
199 DescriptionBase description
, List
<String
> propertyPaths
) {
201 List
<DerivedUnitFacade
> derivedUnitFacadeList
= new ArrayList
<DerivedUnitFacade
>();
202 IndividualsAssociation tempIndividualsAssociation
;
203 SpecimenOrObservationBase tempSpecimenOrObservationBase
;
204 List
<DescriptionElementBase
> elements
= descriptionService
.listDescriptionElements(description
, null, IndividualsAssociation
.class, null, 0, Arrays
.asList(new String
[]{"associatedSpecimenOrObservation"}));
205 for(DescriptionElementBase element
: elements
){
206 if(element
instanceof IndividualsAssociation
){
207 tempIndividualsAssociation
= (IndividualsAssociation
)element
;
208 if(tempIndividualsAssociation
.getAssociatedSpecimenOrObservation() != null){
209 tempSpecimenOrObservationBase
= HibernateProxyHelper
.deproxy(tempIndividualsAssociation
.getAssociatedSpecimenOrObservation(), SpecimenOrObservationBase
.class);
210 if(tempSpecimenOrObservationBase
instanceof DerivedUnitBase
){
212 derivedUnitFacadeList
.add(DerivedUnitFacade
.NewInstance((DerivedUnitBase
)tempSpecimenOrObservationBase
));
213 } catch (DerivedUnitFacadeNotSupportedException e
) {
214 logger
.warn(tempIndividualsAssociation
.getAssociatedSpecimenOrObservation().getTitleCache() + " : " +e
.getMessage());
222 beanInitializer
.initializeAll(derivedUnitFacadeList
, propertyPaths
);
224 return derivedUnitFacadeList
;
228 * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#listSpecimenOrObservationsFor(java.lang.Class, eu.etaxonomy.cdm.model.taxon.Taxon, java.util.List)
231 public <T
extends SpecimenOrObservationBase
> List
<T
> listByAnyAssociation(Class
<T
> type
,
232 Taxon associatedTaxon
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
)
235 associatedTaxon
= (Taxon
) taxonDao
.load(associatedTaxon
.getUuid());
236 return dao
.listByAnyAssociation(type
, associatedTaxon
, limit
, start
, orderHints
, propertyPaths
);