import java.util.HashSet;\r
import java.util.List;\r
import java.util.Map;\r
+import java.util.Map.Entry;\r
import java.util.Set;\r
import java.util.UUID;\r
\r
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;\r
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;\r
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;\r
+import eu.etaxonomy.cdm.api.service.dto.DerivateHierarchyDTO;\r
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;\r
import eu.etaxonomy.cdm.api.service.pager.Pager;\r
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;\r
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;\r
import eu.etaxonomy.cdm.model.location.Country;\r
import eu.etaxonomy.cdm.model.media.Media;\r
+import eu.etaxonomy.cdm.model.media.MediaRepresentation;\r
+import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;\r
import eu.etaxonomy.cdm.model.molecular.DnaSample;\r
import eu.etaxonomy.cdm.model.molecular.Sequence;\r
+import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;\r
+import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;\r
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;\r
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;\r
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;\r
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;\r
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;\r
+import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;\r
+import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;\r
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;\r
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;\r
import eu.etaxonomy.cdm.model.taxon.Taxon;\r
return pageByAssociatedTaxon(type, includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths).getRecords();\r
}\r
\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#listByAnyAssociation(java.lang.Class, java.util.Set, eu.etaxonomy.cdm.model.taxon.Taxon, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)\r
+ */\r
+ @Override\r
+ public Collection<FieldUnit> listFieldUnitsByAssociatedTaxon(Set<TaxonRelationshipEdge> includeRelationships,\r
+ Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {\r
+\r
+ if(!getSession().contains(associatedTaxon)){\r
+ associatedTaxon = (Taxon) taxonDao.load(associatedTaxon.getUuid());\r
+ }\r
+\r
+ Set<FieldUnit> fieldUnits = new HashSet<FieldUnit>();\r
+\r
+ List<SpecimenOrObservationBase> records = pageByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths).getRecords();\r
+ for(SpecimenOrObservationBase<?> specimen:records){\r
+ fieldUnits.addAll(getFieldUnits(specimen.getUuid()));\r
+ }\r
+ return fieldUnits;\r
+ }\r
+\r
+ @Override\r
+ public Collection<DerivateHierarchyDTO> listDerivateHierarchyDTOsByAssociatedTaxon(Set<TaxonRelationshipEdge> includeRelationships,\r
+ Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {\r
+\r
+ if(!getSession().contains(associatedTaxon)){\r
+ associatedTaxon = (Taxon) taxonDao.load(associatedTaxon.getUuid());\r
+ }\r
+\r
+ Collection<FieldUnit> fieldUnits = listFieldUnitsByAssociatedTaxon(includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths);\r
+\r
+ Collection<DerivateHierarchyDTO> derivateHierarchyDTOs = new ArrayList<DerivateHierarchyDTO>();\r
+ for(FieldUnit fieldUnit:fieldUnits){\r
+ derivateHierarchyDTOs.add(assembleDerivateHierarchyDTO(fieldUnit, associatedTaxon));\r
+ }\r
+ return derivateHierarchyDTOs;\r
+ }\r
+\r
+ private DerivateHierarchyDTO assembleDerivateHierarchyDTO(FieldUnit fieldUnit, Taxon associatedTaxon){\r
+ final String separator = ", ";\r
+\r
+ DerivateHierarchyDTO dto = new DerivateHierarchyDTO();\r
+ Map<UUID, SpecimenTypeDesignation> typeSpecimenUUIDtoTypeDesignation = new HashMap<UUID, SpecimenTypeDesignation>();\r
+\r
+ //types\r
+ TaxonNameBase<?,?> name = associatedTaxon.getName();\r
+ Set<?> typeDesignations = name.getSpecimenTypeDesignations();\r
+ for (Object object : typeDesignations) {\r
+ if(object instanceof SpecimenTypeDesignation){\r
+ SpecimenTypeDesignation specimenTypeDesignation = (SpecimenTypeDesignation)object;\r
+ DerivedUnit typeSpecimen = specimenTypeDesignation.getTypeSpecimen();\r
+ typeSpecimenUUIDtoTypeDesignation.put(typeSpecimen.getUuid(), specimenTypeDesignation);\r
+ }\r
+ }\r
+\r
+ if(fieldUnit.getGatheringEvent()!=null){\r
+ GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();\r
+ //Country\r
+ dto.setCountry(gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getDescription():"");\r
+ //Collection\r
+ dto.setCollection((gatheringEvent.getCollector()!=null?gatheringEvent.getCollector():"") + fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():"");\r
+ //Date\r
+ dto.setDate(gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate().toString():"");\r
+ }\r
+\r
+ //Taxon Name\r
+ dto.setTaxonName(associatedTaxon.getName().getFullTitleCache());\r
+\r
+\r
+ Collection<DerivedUnit> derivedUnits = new ArrayList<DerivedUnit>();\r
+ getDerivedUnitsFor(fieldUnit, derivedUnits);\r
+ //Herbaria map\r
+ Map<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> collectionToCountMap = new HashMap<eu.etaxonomy.cdm.model.occurrence.Collection, Integer>();\r
+ //Type map\r
+ Map<String, List<String>> typeStatusToAccessionNumber = new HashMap<String, List<String>>();\r
+\r
+ //iterate over sub derivates\r
+ for (DerivedUnit derivedUnit : derivedUnits) {\r
+ //current accession number\r
+ String currentAccessionNumber = derivedUnit.getAccessionNumber()!=null?derivedUnit.getAccessionNumber():"";\r
+ //current herbarium\r
+ String currentHerbarium = "";\r
+ eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();\r
+ if(collection!=null){\r
+ currentHerbarium = collection.getCode()!=null?collection.getCode():"";\r
+ //count herbaria\r
+ Integer count = collectionToCountMap.get(collection);\r
+ if(count==null){\r
+ count = 1;\r
+ }\r
+ else{\r
+ count++;\r
+ }\r
+ collectionToCountMap.put(collection, count);\r
+ }\r
+ //check if derived unit is a type\r
+ if(typeSpecimenUUIDtoTypeDesignation.keySet().contains(derivedUnit.getUuid())){\r
+ dto.setHasType(true);\r
+ SpecimenTypeDesignation specimenTypeDesignation = typeSpecimenUUIDtoTypeDesignation.get(derivedUnit.getUuid());\r
+ SpecimenTypeDesignationStatus specimenTypeDesignationStatus = specimenTypeDesignation.getTypeStatus();\r
+ String typeStatus = specimenTypeDesignationStatus.getLabel(Language.DEFAULT());\r
+ dto.addTypes(typeStatus, currentAccessionNumber);\r
+ }\r
+ //assemble molecular data\r
+ if(derivedUnit instanceof DnaSample){//.getRecordBasis()==SpecimenOrObservationType.DnaSample){\r
+ dto.setHasDna(true);\r
+\r
+ DnaSample dna = (DnaSample)derivedUnit;\r
+ if(dna.getBankNumber()!=null){\r
+ dto.addMolecularData(dna.getBankNumber(), "SampleDesignation?");//FIXME replace with actual getter\r
+ }\r
+ }\r
+ //assemble media data\r
+ else if(derivedUnit instanceof MediaSpecimen){\r
+\r
+ MediaSpecimen media = (MediaSpecimen)derivedUnit;\r
+ String mediaUriString = getMediaUriString(media);\r
+ if(media.getKindOfUnit()!=null){\r
+ if(media.getKindOfUnit().getUuid().equals(UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){\r
+ dto.setHasSpecimenScan(true);\r
+ if(mediaUriString!=null){\r
+ dto.addSpecimenScan(mediaUriString, currentHerbarium+" "+currentAccessionNumber);\r
+ }\r
+ }\r
+ else if(media.getKindOfUnit().getUuid().equals(UUID.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){\r
+ dto.setHasDetailImage(true);\r
+ if(mediaUriString!=null){\r
+ String motif = "";\r
+ if(media.getMediaSpecimen()!=null && media.getMediaSpecimen().getTitle(Language.DEFAULT())!=null){\r
+ motif = media.getMediaSpecimen().getTitle(Language.DEFAULT()).getText();\r
+ }\r
+ dto.addDetailImage(mediaUriString, motif);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ //assemble preserved specimen data\r
+ else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){\r
+\r
+ }\r
+ }\r
+\r
+ //assemble citation\r
+ String citation = "";\r
+ citation += dto.getCountry();\r
+ citation += separator;\r
+ if(fieldUnit.getGatheringEvent()!=null){\r
+ if(fieldUnit.getGatheringEvent().getLocality()!=null){\r
+ citation += fieldUnit.getGatheringEvent().getLocality().getText();\r
+ citation += separator;\r
+ }\r
+ if(fieldUnit.getGatheringEvent().getExactLocation()!=null\r
+ && fieldUnit.getGatheringEvent().getExactLocation().getLatitude()!=null\r
+ && fieldUnit.getGatheringEvent().getExactLocation().getLongitude()!=null){\r
+ citation += fieldUnit.getGatheringEvent().getExactLocation().getLatitude().toString();\r
+ citation += separator;\r
+ citation += fieldUnit.getGatheringEvent().getExactLocation().getLongitude().toString();\r
+ citation += separator;\r
+ }\r
+ }\r
+ if(citation.endsWith(separator)){\r
+ citation = citation.substring(0, citation.length()-separator.length());\r
+ }\r
+ dto.setCitation(citation);\r
+\r
+ //assemble herbaria string\r
+ String herbariaString = "";\r
+ for(Entry<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> e:collectionToCountMap.entrySet()){\r
+ eu.etaxonomy.cdm.model.occurrence.Collection collection = e.getKey();\r
+ if(collection.getCode()!=null){\r
+ herbariaString += collection.getCode();\r
+ }\r
+ if(e.getValue()>1){\r
+ herbariaString += "("+e.getValue()+")";\r
+ }\r
+ herbariaString += separator;\r
+ }\r
+ if(herbariaString.endsWith(separator)){\r
+ herbariaString = herbariaString.substring(0, herbariaString.length()-separator.length());\r
+ }\r
+ dto.setHerbarium(herbariaString);\r
+ return dto;\r
+ }\r
+\r
+ private String getMediaUriString(MediaSpecimen mediaSpecimen){\r
+ if(!getSession().contains(mediaSpecimen)){\r
+ mediaSpecimen = (MediaSpecimen) load(mediaSpecimen.getUuid());\r
+ }\r
+ String mediaUri = null;\r
+ Collection<MediaRepresentation> mediaRepresentations = mediaSpecimen.getMediaSpecimen().getRepresentations();\r
+ if(mediaRepresentations!=null && !mediaRepresentations.isEmpty()){\r
+ Collection<MediaRepresentationPart> mediaRepresentationParts = mediaRepresentations.iterator().next().getParts();\r
+ if(mediaRepresentationParts!=null && !mediaRepresentationParts.isEmpty()){\r
+ MediaRepresentationPart part = mediaRepresentationParts.iterator().next();\r
+ if(part.getUri()!=null){\r
+ mediaUri = part.getUri().toASCIIString();\r
+ }\r
+ }\r
+ }\r
+ return mediaUri;\r
+ }\r
+\r
+ private void getDerivedUnitsFor(SpecimenOrObservationBase<?> specimen, Collection<DerivedUnit> derivedUnits){\r
+ for(DerivationEvent derivationEvent:specimen.getDerivationEvents()){\r
+ for(DerivedUnit derivative:derivationEvent.getDerivatives()){\r
+ derivedUnits.add(derivative);\r
+ getDerivedUnitsFor(derivative, derivedUnits);\r
+ }\r
+ }\r
+ }\r
+\r
\r
/* (non-Javadoc)\r
* @see eu.etaxonomy.cdm.api.service.IOccurrenceService#pageByAssociatedTaxon(java.lang.Class, java.util.Set, eu.etaxonomy.cdm.model.taxon.Taxon, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)\r
// Integer limit = PagerUtils.limitFor(pageSize);\r
// Integer start = PagerUtils.startFor(pageSize, pageNumber);\r
\r
- associatedTaxon = (Taxon) taxonDao.load(associatedTaxon.getUuid());\r
+ if(!getSession().contains(associatedTaxon)){\r
+ associatedTaxon = (Taxon) taxonDao.load(associatedTaxon.getUuid());\r
+ }\r
\r
if(includeRelationships != null) {\r
taxa = taxonService.listRelatedTaxa(associatedTaxon, includeRelationships, maxDepth, null, null, propertyPaths);\r
//from which this DerivedUnit was derived until all FieldUnits are found.\r
\r
//FIXME: use HQL queries to increase performance\r
+ SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid);\r
+// specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);\r
Collection<FieldUnit> fieldUnits = new ArrayList<FieldUnit>();\r
- SpecimenOrObservationBase derivedUnit = load(derivedUnitUuid);\r
- if(derivedUnit instanceof DerivedUnit){\r
- getFieldUnits((DerivedUnit) derivedUnit, fieldUnits);\r
+\r
+ if(specimen instanceof FieldUnit){\r
+ fieldUnits.add((FieldUnit) specimen);\r
+ }\r
+ else if(specimen instanceof DerivedUnit){\r
+ getFieldUnits((DerivedUnit) specimen, fieldUnits);\r
}\r
return fieldUnits;\r
}\r