Project

General

Profile

Revision fad21775

IDfad217757f5ae87ba83447917ba31eeb798ce1d7
Parent 6e858cb7
Child f852bff0

Added by Patrick Plitzner almost 2 years ago

ref #7095 Move specimen and description loading to service layer

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IWorkingSetService.java
1 1
package eu.etaxonomy.cdm.api.service;
2 2

  
3
import java.util.Collection;
3 4
import java.util.List;
4 5
import java.util.Map;
5 6
import java.util.Set;
6 7
import java.util.UUID;
7 8

  
9
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
8 10
import eu.etaxonomy.cdm.model.description.DescriptionBase;
9 11
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
10 12
import eu.etaxonomy.cdm.model.description.DescriptiveSystemRole;
......
34 36
     * @param pattern
35 37
     * @return
36 38
     */
37
    List<UuidAndTitleCache<WorkingSet>> getWorkingSetUuidAndTitleCache(Integer limitOfInitialElements, String pattern);
39
    public List<UuidAndTitleCache<WorkingSet>> getWorkingSetUuidAndTitleCache(Integer limitOfInitialElements, String pattern);
38 40

  
41
    /**
42
     * Returns a collection of {@link RowWrapperDTO} objects for the given {@link WorkingSet}.<br>
43
     * A RowWrapper represents on row in the character matrix.
44
     * @param workingSet the working set for which the row wrapper objects should be fetched
45
     * @return a collection of row wrapper objects
46
     */
47
    public Collection<RowWrapperDTO> getRowWrapper(WorkingSet workingSet);
48

  
49
    /**
50
     * Loads all avaliable specimens wrapped in a {@link RowWrapperDTO} object for
51
     * a given {@link WorkingSet} according to the filters set in the working set
52
     * @param workingSet the working set for which the specimens should be fetched
53
     * @return a collection of wrapper objects containing the specimen
54
     */
55
    public Collection<RowWrapperDTO> loadSpecimens(WorkingSet workingSet);
39 56

  
40 57

  
41 58
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/WorkingSetService.java
1 1
package eu.etaxonomy.cdm.api.service;
2 2

  
3
import java.util.ArrayList;
4
import java.util.Collection;
3 5
import java.util.List;
4 6
import java.util.Map;
5 7
import java.util.Set;
6 8
import java.util.UUID;
9
import java.util.stream.Collectors;
7 10

  
11
import org.apache.log4j.Logger;
8 12
import org.springframework.beans.factory.annotation.Autowired;
9 13
import org.springframework.stereotype.Service;
10 14
import org.springframework.transaction.annotation.Transactional;
11 15

  
16
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
17
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
18
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
12 20
import eu.etaxonomy.cdm.model.description.DescriptionBase;
13 21
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
14 22
import eu.etaxonomy.cdm.model.description.DescriptiveSystemRole;
15 23
import eu.etaxonomy.cdm.model.description.Feature;
16 24
import eu.etaxonomy.cdm.model.description.WorkingSet;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
27
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
28
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
17 32
import eu.etaxonomy.cdm.persistence.dao.description.IWorkingSetDao;
18 33
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
19 34

  
......
22 37
public class WorkingSetService extends
23 38
		AnnotatableServiceBase<WorkingSet, IWorkingSetDao> implements IWorkingSetService {
24 39

  
40
    private static Logger logger = Logger.getLogger(WorkingSetService.class);
41

  
42
    @Autowired
43
    private IOccurrenceService occurrenceService;
44

  
45
    @Autowired
46
    private ITaxonNodeService taxonNodeService;
47

  
25 48
	@Override
26 49
	@Autowired
27 50
	protected void setDao(IWorkingSetDao dao) {
......
44 67
    public List<UuidAndTitleCache<WorkingSet>> getWorkingSetUuidAndTitleCache(Integer limitOfInitialElements, String pattern) {
45 68
        return dao.getWorkingSetUuidAndTitleCache( limitOfInitialElements, pattern);
46 69
    }
70

  
71
	@Override
72
	public Collection<RowWrapperDTO> getRowWrapper(WorkingSet workingSet) {
73
	    return workingSet.getDescriptions().stream()
74
	            .map(description->createRowWrapper(null, description, workingSet))
75
	            .collect(Collectors.toList());
76
	}
77

  
78
    @Override
79
    public Collection<RowWrapperDTO> loadSpecimens(WorkingSet workingSet){
80
        List<RowWrapperDTO> specimenCache = new ArrayList<>();
81
        //set filter parameters
82
        TaxonNodeFilter filter = TaxonNodeFilter.NewRankInstance(workingSet.getMinRank(), workingSet.getMaxRank());
83
        workingSet.getGeoFilter().forEach(area -> filter.orArea(area.getUuid()));
84
        workingSet.getTaxonSubtreeFilter().forEach(node -> filter.orSubtree(node));
85
        filter.setIncludeUnpublished(true);
86

  
87
        List<UUID> filteredNodes = taxonNodeService.uuidList(filter);
88
        for (UUID uuid : filteredNodes) {
89
            //TODO implement occurrence service for taxon nodes
90
            // let it return UuidAndTitleCache
91
            TaxonNode taxonNode = taxonNodeService.load(uuid);
92
            Taxon taxon = taxonNode.getTaxon();
93
            if(taxon!=null){
94
                FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
95
                config.setAssociatedTaxonUuid(taxon.getUuid());
96
                List<SpecimenOrObservationBase> specimensForTaxon = occurrenceService.findByTitle(config).getRecords();
97
                specimensForTaxon.forEach(specimen -> specimenCache.add(createRowWrapper(specimen, null, workingSet)));
98
            }
99
        }
100
        return specimenCache;
101
    }
102

  
103
	private RowWrapperDTO createRowWrapper(SpecimenOrObservationBase specimen, DescriptionBase description, WorkingSet workingSet){
104
	    if(description!=null){
105
	        specimen = description.getDescribedSpecimenOrObservation();
106
	    }
107
        TaxonNode taxonNode = null;
108
        FieldUnit fieldUnit = null;
109
        String identifier = null;
110
        NamedArea country = null;
111
        //supplemental information
112
        if(specimen!=null){
113
            Collection<TaxonBase<?>> associatedTaxa = occurrenceService.listAssociatedTaxa(specimen, null, null, null, null);
114
            if(associatedTaxa!=null){
115
                //FIXME: what about multiple associated taxa
116
                Set<TaxonNode> taxonSubtreeFilter = workingSet.getTaxonSubtreeFilter();
117
                if(taxonSubtreeFilter!=null && !taxonSubtreeFilter.isEmpty()){
118
                    taxonNode = ((Taxon) associatedTaxa.iterator().next()).getTaxonNode(taxonSubtreeFilter.iterator().next().getClassification());
119
                }
120
            }
121
            Collection<FieldUnit> fieldUnits = occurrenceService.getFieldUnits(specimen.getUuid());
122
            if(fieldUnits.size()!=1){
123
                logger.error("More than one or no field unit found for specimen"); //$NON-NLS-1$
124
            }
125
            else{
126
                fieldUnit = fieldUnits.iterator().next();
127
            }
128
            if(specimen instanceof DerivedUnit){
129
                identifier = occurrenceService.getMostSignificantIdentifier(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
130
            }
131
            if(fieldUnit!=null && fieldUnit.getGatheringEvent()!=null){
132
                country = fieldUnit.getGatheringEvent().getCountry();
133
            }
134
        }
135
        return new RowWrapperDTO(description, taxonNode, fieldUnit, identifier, country);
136
	}
47 137
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/RowWrapperDTO.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.api.service.dto;
11

  
12
import java.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.Map;
17
import java.util.Set;
18

  
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.description.CategoricalData;
21
import eu.etaxonomy.cdm.model.description.DescriptionBase;
22
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23
import eu.etaxonomy.cdm.model.description.Feature;
24
import eu.etaxonomy.cdm.model.description.QuantitativeData;
25
import eu.etaxonomy.cdm.model.description.State;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
28
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30

  
31
/**
32
 * @author pplitzner
33
 * @date 16.04.2018
34
 *
35
 */
36
public class RowWrapperDTO implements Serializable {
37

  
38
    private static final long serialVersionUID = -7817164423660563673L;
39

  
40
    private DescriptionBase description;
41

  
42
    private SpecimenOrObservationBase specimen;
43
    private TaxonNode taxonNode;
44
    private FieldUnit fieldUnit;
45
    private String identifier;
46
    private NamedArea country;
47
    private Map<Feature, DescriptionElementBase> featureToElementMap;
48

  
49
    public RowWrapperDTO(DescriptionBase description, TaxonNode taxonNode, FieldUnit fieldUnit, String identifier,
50
            NamedArea country) {
51
        this.taxonNode = taxonNode;
52
        this.fieldUnit = fieldUnit;
53
        this.identifier = identifier;
54
        this.country = country;
55
        this.featureToElementMap = new HashMap<>();
56
        if(description!=null){
57
            setDescription(description);
58
        }
59
    }
60

  
61
    public QuantitativeData addQuantitativeData(Feature feature){
62
        QuantitativeData data = QuantitativeData.NewInstance(feature);
63
        description.addElement(data);
64
        featureToElementMap.put(feature, data);
65
        return data;
66
    }
67

  
68
    public CategoricalData addCategoricalData(Feature feature){
69
        CategoricalData data = CategoricalData.NewInstance(feature);
70
        description.addElement(data);
71
        featureToElementMap.put(feature, data);
72
        return data;
73
    }
74

  
75
    public DescriptionBase getSpecimenDescription() {
76
        return description;
77
    }
78

  
79
    public SpecimenOrObservationBase getSpecimen() {
80
        return specimen;
81
    }
82

  
83
    public TaxonNode getTaxonNode() {
84
        return taxonNode;
85
    }
86

  
87
    public FieldUnit getFieldUnit() {
88
        return fieldUnit;
89
    }
90

  
91
    public String getIdentifier() {
92
        return identifier;
93
    }
94

  
95
    public NamedArea getCountry() {
96
        return country;
97
    }
98

  
99
    public void setDescription(DescriptionBase description) {
100
        this.description = description;
101
        this.specimen = description.getDescribedSpecimenOrObservation();
102
        Set<DescriptionElementBase> elements = description.getElements();
103
        for (DescriptionElementBase descriptionElementBase : elements) {
104
            Feature feature = descriptionElementBase.getFeature();
105
            featureToElementMap.put(feature, descriptionElementBase);
106
        }
107
    }
108

  
109
    public Object getDataValueForFeature(Feature feature){
110
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
111
        return descriptionElementBase;
112
    }
113

  
114
    public void setDataValueForFeature(Feature feature, Object newValue){
115
        /* Only CategoricalData is handled here because for QuantitativeData the value
116
         * is set in the ModifyListener of the swt.Text in the CellEditor
117
         * for each StatisticalMeasure. So no need to set it again here.
118
         */
119
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
120
        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class) && newValue instanceof Collection){
121
            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
122
            categoricalData.setStateDataOnly(new ArrayList<>((Collection<State>) newValue));
123
        }
124
    }
125
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)