import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
+import eu.etaxonomy.cdm.api.service.dto.DNASampleDTO;
import eu.etaxonomy.cdm.api.service.dto.DerivateDTO;
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO;
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.ContigFile;
import eu.etaxonomy.cdm.model.description.TaxonDescription;
import eu.etaxonomy.cdm.model.location.Country;
import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.location.Point;
import eu.etaxonomy.cdm.model.media.Media;
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
private static final String SEPARATOR_STRING = ", ";
+ private static final List<String> DERIVED_UNIT_INIT_STRATEGY = Arrays.asList(new String []{
+ "derivedFrom.derivatives",
+ "derivedFrom.originals",
+ "specimenTypeDesignations.*",
+ "specimenTypeDesignations.citation.*",
+ "specimenTypeDesignations.homotypicalGroup.*",
+ "specimenTypeDesignations.typifiedNames",
+ "collection.$"
+ });
+
public OccurrenceServiceImpl() {
logger.debug("Load OccurrenceService Bean");
}
*/
@Override
public List<Country> getCountryByName(String name) {
- List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitle(Country.class, name, null, null, null, null, null, null);
+ List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitleWithRestrictions(Country.class, name, null, null, null, null, null, null);
List<Country> countries = new ArrayList<>();
for (int i = 0; i < terms.size(); i++) {
countries.add((Country) terms.get(i));
Set<UUID> fieldUnitUuids = new HashSet<>();
List<SpecimenOrObservationBase> records = listByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, null, null, orderHints, propertyPaths);
for (SpecimenOrObservationBase<?> specimen : records) {
- for (FieldUnit fieldUnit : getFieldUnits(specimen.getUuid(), null)) {
+ for (FieldUnit fieldUnit : findFieldUnits(specimen.getUuid(), null)) {
fieldUnitUuids.add(fieldUnit.getUuid());
}
}
}
@Override
- public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit, UUID associatedTaxonUuid) {
+ public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit) {
if (!getSession().contains(fieldUnit)) {
fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
}
- TaxonBase associatedTaxon = taxonService.load(associatedTaxonUuid);
- FieldUnitDTO fieldUnitDTO = new FieldUnitDTO();
+ FieldUnitDTO fieldUnitDTO = new FieldUnitDTO(fieldUnit);
if (fieldUnit.getGatheringEvent() != null) {
GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
fieldUnitDTO.setDate(dateString);
}
- // Taxon Name
- fieldUnitDTO.setTaxonName(associatedTaxon.getName().getTitleCache());
-
// Herbaria map
Map<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> collectionToCountMap = new HashMap<>();
// List of accession numbers for citation
continue;
}
// collect accession numbers for citation
- String mostSignificantIdentifier = getMostSignificantIdentifier(derivedUnit);
- if (mostSignificantIdentifier != null) {
- preservedSpecimenAccessionNumbers.add(mostSignificantIdentifier);
- }
+ String identifier = getMostSignificantIdentifier(derivedUnit);
// collect collections for herbaria column
- if (derivedUnit.getCollection() != null) {
- Integer herbariumCount = collectionToCountMap.get(derivedUnit.getCollection());
+ eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
+ if (collection != null) {
+ //combine collection with identifier
+ if (identifier != null) {
+ if(collection.getCode()!=null){
+ identifier = (collection.getCode()!=null?collection.getCode():"[no collection]")+" "+identifier;
+ }
+ preservedSpecimenAccessionNumbers.add(identifier);
+ }
+
+ Integer herbariumCount = collectionToCountMap.get(collection);
if (herbariumCount == null) {
herbariumCount = 0;
}
- collectionToCountMap.put(derivedUnit.getCollection(), herbariumCount + 1);
+ collectionToCountMap.put(collection, herbariumCount + 1);
}
if (derivedUnit.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen)) {
PreservedSpecimenDTO preservedSpecimenDTO = assemblePreservedSpecimenDTO(derivedUnit, fieldUnitDTO);
- fieldUnitDTO.addPreservedSpecimenDTO(preservedSpecimenDTO);
+ fieldUnitDTO.addDerivate(preservedSpecimenDTO);
fieldUnitDTO.setHasCharacterData(fieldUnitDTO.isHasCharacterData() || preservedSpecimenDTO.isHasCharacterData());
fieldUnitDTO.setHasDetailImage(fieldUnitDTO.isHasDetailImage() || preservedSpecimenDTO.isHasDetailImage());
fieldUnitDTO.setHasDna(fieldUnitDTO.isHasDna() || preservedSpecimenDTO.isHasDna());
herbariaString += SEPARATOR_STRING;
}
herbariaString = removeTail(herbariaString, SEPARATOR_STRING);
- fieldUnitDTO.setHerbarium(herbariaString);
+ fieldUnitDTO.setCollection(herbariaString);
return fieldUnitDTO;
}
if (!getSession().contains(derivedUnit)) {
derivedUnit = (DerivedUnit) load(derivedUnit.getUuid());
}
- PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO();
+ PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO(derivedUnit);
//specimen identifier
FormatKey collectionKey = FormatKey.COLLECTION_CODE;
specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
collectionKey, FormatKey.SPACE,
FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE });
+ if(CdmUtils.isBlank(specimenIdentifier)){
+ specimenIdentifier = derivedUnit.getTitleCache();
+ }
if(CdmUtils.isBlank(specimenIdentifier)){
specimenIdentifier = derivedUnit.getUuid().toString();
}
preservedSpecimenDTO.setAccessionNumber(specimenIdentifier);
- preservedSpecimenDTO.setUuid(derivedUnit.getUuid().toString());
+
//preferred stable URI
preservedSpecimenDTO.setPreferredStableUri(derivedUnit.getPreferredStableUri());
fieldUnitDTO.setHasType(true);
}
TypeDesignationStatusBase<?> typeStatus = specimenTypeDesignation.getTypeStatus();
- if (typeStatus != null) {
- List<String> typedTaxaNames = new ArrayList<>();
- String label = typeStatus.getLabel();
- Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
- for (TaxonName taxonName : typifiedNames) {
- typedTaxaNames.add(taxonName.getNameCache());
- }
- preservedSpecimenDTO.addTypes(label, typedTaxaNames);
+ Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
+ List<String> typedTaxaNames = new ArrayList<>();
+ for (TaxonName taxonName : typifiedNames) {
+ typedTaxaNames.add(taxonName.getTitleCache());
}
+ preservedSpecimenDTO.addTypes(typeStatus!=null?typeStatus.getLabel():"", typedTaxaNames);
}
// individuals associations
return derivedUnits;
}
+ private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+ Set<DerivateDTO> derivedUnits = new HashSet<>();
+// load
+ for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
+ for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
+ if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
+ PreservedSpecimenDTO dto;
+ if (derivative instanceof DnaSample) {
+ dto = new DNASampleDTO(derivative);
+ } else {
+ dto = new PreservedSpecimenDTO(derivative);
+ }
+ alreadyCollectedSpecimen.put(dto.getUuid(), dto);
+ dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
+ derivedUnits.add(dto);
+ }
+ }
+ }
+ return derivedUnits;
+ }
+
+// private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+// Set<DerivateDTO> derivedUnits = new HashSet<>();
+//// load
+// for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
+// for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
+// if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
+// PreservedSpecimenDTO dto;
+// if (derivative instanceof DnaSample){
+// dto = DNASampleDTO.newInstance(derivative);
+// }else{
+// dto = PreservedSpecimenDTO.newInstance(derivative);
+// }
+// alreadyCollectedSpecimen.put(dto.getUuid(), dto);
+// dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
+// derivedUnits.add(dto);
+// }
+// }
+// }
+// return derivedUnits;
+// }
+
@SuppressWarnings("unchecked")
@Override
}
occurrences = (List<T>) dao.loadList(occurrenceIds, propertyPaths);
- return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrenceIds.size()), pageSize, occurrences);
+ return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrences.size()), pageSize, occurrences);
}
}
+ @SuppressWarnings("unchecked")
+ @Override
+ public List<FieldUnitDTO> findFieldUnitDTOByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships,
+ UUID associatedTaxonUuid) {
+
+ Set<Taxon> taxa = new HashSet<>();
+ Set<Integer> occurrenceIds = new HashSet<>();
+ List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
+ HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen = new HashMap<>();
+ List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
+ boolean includeUnpublished = INCLUDE_UNPUBLISHED;
+
+ // Integer limit = PagerUtils.limitFor(pageSize);
+ // Integer start = PagerUtils.startFor(pageSize, pageNumber);
+
+ Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
+
+
+ if (includedRelationships != null) {
+ taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
+ }
+
+ taxa.add(associatedTaxon);
+
+ for (Taxon taxon : taxa) {
+ List<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null,taxon, null, null, null, DERIVED_UNIT_INIT_STRATEGY);
+ for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
+ if (o.isInstanceOf(DerivedUnit.class)){
+ DerivedUnit derivedUnit;
+ DerivateDTO derivedUnitDTO;
+ if (o.isInstanceOf(DnaSample.class)) {
+ derivedUnit = HibernateProxyHelper.deproxy(o, DnaSample.class);
+ derivedUnitDTO = new DNASampleDTO(derivedUnit);
+ } else {
+ derivedUnit = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
+ derivedUnitDTO = new PreservedSpecimenDTO(derivedUnit);
+ }
+ if (alreadyCollectedSpecimen.get(derivedUnitDTO.getUuid()) == null){
+ alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
+ }
+ derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, derivedUnit, alreadyCollectedSpecimen));
+ this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs, alreadyCollectedSpecimen);
+ }
+ }
+
+ }
+
+ return fieldUnitDTOs;
+
+ }
+
+
+
@Override
public List<DerivedUnit> findByAccessionNumber(
String accessionNumberString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
List<String> propertyPaths) {
List<DerivedUnit> records = new ArrayList<>();
- records = dao.getByGeneticAccessionNumber(accessionNumberString, propertyPaths);
+ records = dao.findByGeneticAccessionNumber(accessionNumberString, propertyPaths);
return records;
@Override
- public Collection<FieldUnit> getFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
+ public Collection<FieldUnit> findFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
//It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
//from which this DerivedUnit was derived until all FieldUnits are found.
return fieldUnits;
}
+
+
+ @Override
+ public FieldUnitDTO findFieldUnitDTO(DerivateDTO derivedUnitDTO, Collection<FieldUnitDTO> fieldUnits, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+ //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
+ //from which this DerivedUnit was derived until all FieldUnits are found.
+ List<SpecimenOrObservationBase> specimens = new ArrayList<>();
+ List<String> propertyPaths = new ArrayList<>();
+
+ propertyPaths.add("descriptions.elements.media.title");
+ propertyPaths.add("kindOfUnit");
+
+ specimens = dao.findOriginalsForDerivedUnit(derivedUnitDTO.getUuid(), propertyPaths);
+
+ if (specimens.size() > 1){
+ logger.debug("The derived unit with uuid " + derivedUnitDTO.getUuid() + "has more than one orginal");
+ }
+ // for (SpecimenOrObservationBase specimen: specimens){
+ SpecimenOrObservationBase specimen = null;
+ if (specimens.size() > 0){
+ specimen = specimens.get(0);
+ }else{
+ return null;
+ }
+ FieldUnitDTO fieldUnitDto = null;
+ if (alreadyCollectedSpecimen.get(specimen.getUuid()) != null){
+ alreadyCollectedSpecimen.get(specimen.getUuid()).addDerivate(derivedUnitDTO);
+ if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
+ ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
+ }
+ }else{
+ if (specimen.isInstanceOf(FieldUnit.class)){
+ fieldUnitDto = FieldUnitDTO.newInstance((FieldUnit)specimen);
+ fieldUnitDto.addDerivate(derivedUnitDTO);
+ fieldUnits.add(fieldUnitDto);
+ }else{
+ DerivateDTO originalDTO;
+ if (specimen instanceof DnaSample){
+ originalDTO = new DNASampleDTO((DnaSample)specimen);
+ } else {
+ originalDTO = new PreservedSpecimenDTO((DerivedUnit)specimen);
+ }
+ originalDTO.addDerivate(derivedUnitDTO);
+ fieldUnitDto = findFieldUnitDTO(originalDTO, fieldUnits, alreadyCollectedSpecimen);
+ }
+
+ }
+ // }
+ alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
+ if (fieldUnitDto != null){
+ fieldUnitDto.addTaxonRelatedDerivedUnits(derivedUnitDTO);
+ }
+ return fieldUnitDto;
+
+ }
+
@Override
@Transactional(readOnly = false)
public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
- occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
+ occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
}
return dao.countOccurrences(occurrenceConfig.getClazz(),
- occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
+ occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
}
else{
- return dao.countByTitle(config.getTitleSearchString());
+ return super.countByTitle(config);
}
}
if(config.getAssociatedTaxonUuid()!=null){
TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
if(taxonBase.isInstanceOf(Taxon.class)){
- taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
+ taxon = CdmBase.deproxy(taxonBase, Taxon.class);
}
}
TaxonName taxonName = null;
config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
config.getOrderHints()));
- return new DefaultPagerImpl<UuidAndTitleCache<SpecimenOrObservationBase>>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
+ return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
}
@Override
- public Pager<SpecimenOrObservationBase> findByTitle(
- IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config) {
+ public <S extends SpecimenOrObservationBase> Pager<S> findByTitle(
+ IIdentifiableEntityServiceConfigurator<S> config) {
if (config instanceof FindOccurrencesConfigurator) {
FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
Taxon taxon = null;
if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
- TaxonBase taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
+ TaxonBase<?> taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
if(taxonBase.isInstanceOf(Taxon.class)){
taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
}
if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
}
- occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
+ List<? extends SpecimenOrObservationBase> foundOccurrences = dao.findOccurrences(occurrenceConfig.getClazz(),
occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
- occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
+ occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
+ occurrences.addAll(foundOccurrences);
occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
- return new DefaultPagerImpl<SpecimenOrObservationBase>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
+ return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), (List<S>)occurrences);
}
return super.findByTitle(config);
}
//filter out (un-)assigned specimens
if(taxon==null && taxonName==null){
AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
- List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
+ List<SpecimenOrObservationBase> specimenWithAssociations = new ArrayList<>();
if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
boolean includeUnpublished = true; //TODO not sure if this is correct, maybe we have to propagate publish flag to higher methods.
// indirectly associated specimens
if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
- for (SpecimenOrObservationBase specimen : occurrences) {
+ for (SpecimenOrObservationBase<?> specimen : occurrences) {
List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
if(!occurrences.contains(specimenOrObservationBase)){
@Override
public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
- Collection<FieldUnit> fieldUnits = getFieldUnits(specimen.getUuid(), null);
+ Collection<FieldUnit> fieldUnits = findFieldUnits(specimen.getUuid(), null);
if(fieldUnits.isEmpty()){
allHierarchyDerivatives.add(specimen);
allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
* {@inheritDoc}
*/
@Override
- public List<FieldUnit> getFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
- return dao.getFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
+ public List<FieldUnit> findFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
+ return dao.findFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<Point> findPointsForFieldUnitList(List<UUID> fieldUnitUuids) {
+
+ return dao.findPointsForFieldUnitList(fieldUnitUuids);
}
}