Project

General

Profile

Download (5.69 KB) Statistics
| Branch: | Revision:
1
package eu.etaxonomy.cdm.io.pesi.indexFungorum;
2

    
3
import java.sql.ResultSet;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.UUID;
8
import java.util.stream.Collectors;
9

    
10
import org.apache.log4j.Logger;
11
import org.springframework.stereotype.Component;
12
import org.springframework.transaction.TransactionStatus;
13

    
14
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
15
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
16
import eu.etaxonomy.cdm.model.common.CdmBase;
17
import eu.etaxonomy.cdm.model.name.TaxonName;
18
import eu.etaxonomy.cdm.model.taxon.Classification;
19
import eu.etaxonomy.cdm.model.taxon.Taxon;
20
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
21

    
22
/**
23
 * @author k.luther
24
 * @since 19.08.2014
25
 */
26
@Component
27
public class IndexFungorumSubSpeciesImport extends IndexFungorumImportBase {
28

    
29
    private static final long serialVersionUID = -2877755674188760685L;
30
    private static final Logger logger = Logger.getLogger(IndexFungorumSubSpeciesImport.class);
31

    
32
	private static final String pluralString = "subSpecies";
33

    
34
	public IndexFungorumSubSpeciesImport(){
35
		super(pluralString, null);
36
	}
37

    
38
	@Override
39
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(
40
			ResultSet rs, IndexFungorumImportState state) {
41
		return null;  //not used here
42
	}
43

    
44
	@Override
45
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
46
		return null;  //not used here
47
	}
48

    
49
	@Override
50
    protected void doInvoke(IndexFungorumImportState state){
51

    
52
		logger.info("create infraspecific - specific relationship: " + state.getInfraspecificTaxaUUIDs().size() + " taxa");
53

    
54
		List<String> propertyPaths = new ArrayList<>();
55
        propertyPaths.add("taxonNodes.*");
56
        propertyPaths.add("taxonNodes.classification");
57
        propertyPaths.add("taxonNodes.childNodes.*");
58
        propertyPaths.add("taxonNodes.childNodes.taxon.*");
59
        propertyPaths.add("taxonNodes.parent.*");
60
        propertyPaths.add("taxonNodes.parent.taxon.*");
61
        Classification classification = getClassification(state);
62
        for (UUID infraspecificTaxonUUID: state.getInfraspecificTaxaUUIDs().keySet()){
63
        	TransactionStatus txStatus = startTransaction();
64
        	Taxon infraspecificTaxon = (Taxon)getTaxonService().load(infraspecificTaxonUUID, propertyPaths);
65

    
66
            TaxonName name = infraspecificTaxon.getName();
67

    
68
            String parentNameString = getParentNameInfraSpecific(name);
69
            MatchingTaxonConfigurator matchingConfig = new MatchingTaxonConfigurator();
70
            matchingConfig.setTaxonNameTitle(parentNameString);
71

    
72
            matchingConfig.setPropertyPath(propertyPaths);
73
            @SuppressWarnings("rawtypes")
74
            List<TaxonBase> potentialParents = getTaxonService().findTaxaByName(matchingConfig)
75
                    .stream().filter(tb->isIndexFungorumTaxon(tb)).collect(Collectors.toList());
76
            boolean matched = false;
77
            if (potentialParents.size()>1){
78
                for (@SuppressWarnings("rawtypes") TaxonBase potentialParent : potentialParents){
79
                    if (potentialParent.getTitleCache().equals(parentNameString + " sec*")){
80
                        classification.addParentChild((Taxon)potentialParent, infraspecificTaxon, null, null);
81
                        matched = true;
82
                        if(matched == true){
83
                            logger.warn("There is more than 1 potential parent: " + parentNameString);
84
                        }
85
                    }else{
86
                        logger.warn("Potential parent is not sec*: " + potentialParent.getTitleCache());
87
                    }
88
                }
89
                if (matched == false){
90
                    logger.warn("Multiple match candidates but no match for " + name.getTitleCache());
91
                }
92
            }else if (!potentialParents.isEmpty()){
93
                Taxon parent = CdmBase.deproxy(potentialParents.get(0), Taxon.class);
94
                classification.addParentChild(parent, infraspecificTaxon, null, null);
95
                matched = true;
96
            } else{
97
                Integer genusId = state.getInfraspecificTaxaUUIDs().get(infraspecificTaxonUUID);
98
                Taxon genusParent = getParentGenus(state, genusId);
99
                classification.addParentChild(genusParent, infraspecificTaxon, null, null);
100
                logger.warn("Added infraspecific taxon to genus because species does not exist: " + infraspecificTaxon.getTitleCache());
101
                matched = true;
102
            }
103
            if (!matched){
104
                System.out.println("No parent for: " + name.getTitleCache());
105
            }
106

    
107
            getTaxonService().saveOrUpdate(infraspecificTaxon);
108
            commitTransaction(txStatus);
109
        }
110
	}
111

    
112
	private boolean isIndexFungorumTaxon(TaxonBase<?> taxon){
113
	    return taxon.getSources().stream()
114
	            .anyMatch(osb->osb.getCitation() != null && osb.getCitation().getUuid().equals(PesiTransformer.uuidSourceRefIndexFungorum));
115
	}
116

    
117
    private Taxon getParentGenus(IndexFungorumImportState state, Integer genusId) {
118
        Taxon result = getCommonService().getSourcedObjectByIdInSource(Taxon.class, String.valueOf(genusId), NAMESPACE_GENERA);
119
        return result;
120
    }
121

    
122
    private String getParentNameInfraSpecific(TaxonName taxonName){
123
        String parentName = taxonName.getGenusOrUninomial() + " " + taxonName.getSpecificEpithet();
124
        return parentName;
125
    }
126

    
127
    @Override
128
    protected boolean doCheck(IndexFungorumImportState state) {
129
        return false;
130
    }
131

    
132
    @Override
133
    protected boolean isIgnore(IndexFungorumImportState state) {
134
        return false;
135
    }
136
}
(8-8/10)