Project

General

Profile

Revision dcad6203

IDdcad6203ac9617c4ba4a2caa00260aa0f442ce92
Parent 9d8ab33d
Child ef108bcf

Added by Patrick Plitzner almost 3 years ago

Implement Kew Plant Glossary CSV import

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryActivator.java
1
package eu.etaxonomy.cdm.io.plantglossary.kew;
2

  
3
import java.io.IOException;
4
import java.net.URI;
5

  
6
import org.apache.log4j.Logger;
7

  
8
import eu.etaxonomy.cdm.app.common.CdmDestinations;
9
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
10
import eu.etaxonomy.cdm.database.DbSchemaValidation;
11
import eu.etaxonomy.cdm.database.ICdmDataSource;
12
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
13
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
14

  
15
/**
16
 *
17
 * @author pplitzner
18
 * @since Jan 25, 2019
19
 *
20
 */
21
public class KewPlantGlossaryActivator {
22
	@SuppressWarnings("unused")
23
	private static final Logger logger = Logger.getLogger(KewPlantGlossaryActivator.class);
24

  
25
	private void doImport(ICdmDataSource cdmDestination) throws IOException{
26

  
27
	    /*
28
	     * Source file:
29
	     * extracted data from Kew Plant Glossary
30
	     *
31
	     */
32
	    URI uri = URI.create("file:/home/pplitzner/projects/Additivity/KEW_Plant_Glossary.csv");
33
		KewPlantGlossaryCsvImportConfigurator config = KewPlantGlossaryCsvImportConfigurator.NewInstance(uri, cdmDestination);
34
		config.setCheck(CHECK.IMPORT_WITHOUT_CHECK);
35
		config.setDbSchemaValidation(DbSchemaValidation.VALIDATE);
36

  
37
		CdmDefaultImport<KewPlantGlossaryCsvImportConfigurator> myImport = new CdmDefaultImport<>();
38

  
39
		System.out.println("Start import from ("+ cdmDestination.toString() + ") ...");
40
		myImport.invoke(config);
41
		System.out.println("End import from ("+ cdmDestination.toString() + ")...");
42
	}
43

  
44
	public static void main(String[] args) {
45
		KewPlantGlossaryActivator activator = new KewPlantGlossaryActivator();
46
		try {
47
	        ICdmDataSource dataSource = CdmDestinations.makeDestination(DatabaseTypeEnum.MySQL, "127.0.0.1", "empty", 3306, "root", null);
48
            activator.doImport(dataSource);
49
        } catch (IOException e) {
50
            e.printStackTrace();
51
        }
52
	}
53
}
app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportConfigurator.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.plantglossary.kew;
10

  
11
import java.io.IOException;
12
import java.net.URI;
13

  
14
import eu.etaxonomy.cdm.database.ICdmDataSource;
15
import eu.etaxonomy.cdm.io.csv.in.CsvImportConfiguratorBase;
16

  
17
/**
18
 *
19
 * @author pplitzner
20
 * @since Jan 25, 2019
21
 *
22
 */
23
public class KewPlantGlossaryCsvImportConfigurator
24
        extends CsvImportConfiguratorBase{
25

  
26
    private static final long serialVersionUID = 987286481306951779L;
27

  
28
    public static KewPlantGlossaryCsvImportConfigurator NewInstance(URI source,
29
            ICdmDataSource cdmDestination) throws IOException {
30
        return new KewPlantGlossaryCsvImportConfigurator(source, cdmDestination);
31
    }
32

  
33
// ****************** CONSTRUCTOR *****************************/
34

  
35
    private KewPlantGlossaryCsvImportConfigurator(URI source,
36
            ICdmDataSource cdmDestination) throws IOException{
37
        super(source, cdmDestination, null);
38
    }
39

  
40
// *************************************
41

  
42

  
43
    @Override
44
    @SuppressWarnings("unchecked")
45
    protected void makeIoClassList() {
46
        ioClassList = new Class[] {
47
                KewPlantGlossaryImport.class };
48
    }
49

  
50
    @Override
51
    public KewPlantGlossaryCsvImportState getNewState() {
52
        return new KewPlantGlossaryCsvImportState(this);
53
    }
54

  
55
}
app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportState.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.plantglossary.kew;
10

  
11
import java.util.ArrayList;
12
import java.util.List;
13

  
14
import eu.etaxonomy.cdm.api.service.ITermService;
15
import eu.etaxonomy.cdm.io.csv.in.CsvImportState;
16
import eu.etaxonomy.cdm.model.agent.Institution;
17
import eu.etaxonomy.cdm.model.agent.Person;
18
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19
import eu.etaxonomy.cdm.model.common.TermType;
20
import eu.etaxonomy.cdm.model.common.TermVocabulary;
21
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
22
import eu.etaxonomy.cdm.model.description.Feature;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
25
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
26

  
27
/**
28
 *
29
 * @author pplitzner
30
 * @since Jan 25, 2019
31
 *
32
 */
33
public class KewPlantGlossaryCsvImportState extends CsvImportState<KewPlantGlossaryCsvImportConfigurator> {
34

  
35
    private List<TermVocabulary> existingVocabularies = new ArrayList<>();
36
    private List<UuidAndTitleCache<DefinedTermBase>> existingTerms = new ArrayList<>();
37
    private final Reference citation;
38
    private final TermVocabulary<Feature> structureVoc;
39

  
40

  
41
    protected KewPlantGlossaryCsvImportState(KewPlantGlossaryCsvImportConfigurator config) {
42
        super(config);
43
        structureVoc = TermVocabulary.NewInstance(TermType.Feature);
44
        structureVoc.setLabel("Structures");
45

  
46
        citation = ReferenceFactory.newBook();
47
        citation.setTitle("The Kew Plant Glossary, an illustrated dictionary of plant terms");
48
        citation.setAuthorship(Person.NewInstance(null, "J. Beentje", null, "Henk"));
49
        VerbatimTimePeriod datePublished = VerbatimTimePeriod.NewVerbatimInstance();
50
        datePublished.setStartYear(2010);
51
        citation.setDatePublished(datePublished);
52
        Institution institution = Institution.NewNamedInstance("Royal Botanic Gardens, Kew");
53
        citation.setInstitution(institution);
54
        citation.setIsbn("978-1-84246-422-9");
55
    }
56

  
57
    @Override
58
    public void resetSession(){
59
        super.resetSession();
60
    }
61

  
62
    public boolean isTermPresent(String termName, ITermService termService) {
63
        if(existingTerms.isEmpty()){
64
            existingTerms = termService.getUuidAndTitleCache(null, "*");
65
        }
66
        return existingTerms.stream().anyMatch(term->term.getTitleCache().equals(termName));
67
    }
68

  
69
    TermVocabulary<Feature> getStructureVoc() {
70
        return structureVoc;
71
    }
72

  
73
    Reference getCitation() {
74
        return citation;
75
    }
76

  
77
}
app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryImport.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.plantglossary.kew;
10

  
11
import java.io.File;
12
import java.util.Map;
13

  
14
import org.apache.log4j.Logger;
15
import org.springframework.stereotype.Component;
16

  
17
import eu.etaxonomy.cdm.common.CdmUtils;
18
import eu.etaxonomy.cdm.io.csv.in.CsvImportBase;
19
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
20
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
21
import eu.etaxonomy.cdm.model.common.TermVocabulary;
22
import eu.etaxonomy.cdm.model.description.Feature;
23

  
24
/**
25
 *
26
 * @author pplitzner
27
 * @since Jan 25, 2019
28
 *
29
 */
30
@Component
31
public class KewPlantGlossaryImport extends CsvImportBase<KewPlantGlossaryCsvImportConfigurator, KewPlantGlossaryCsvImportState, File>{
32
    private static final long serialVersionUID = -5600766240192189822L;
33
    private static Logger logger = Logger.getLogger(KewPlantGlossaryImport.class);
34

  
35
    final String HEADER_LABEL = "term";
36
    final String HEADER_DEFINITION = "definition";
37
    final String HEADER_TYPE = "type";
38

  
39
    @Override
40
    protected void handleSingleLine(KewPlantGlossaryCsvImportState importState) {
41

  
42
        Map<String, String> currentRecord = importState.getCurrentRecord();
43

  
44
        if(CdmUtils.isBlank(currentRecord.get(HEADER_TYPE)) || !currentRecord.get(HEADER_TYPE).equals("1")){
45
            // only structures (type=1) are imported
46
            return;
47
        }
48

  
49
        String termLabel = currentRecord.get(HEADER_LABEL);
50
        //check if already present
51
        if(importState.isTermPresent(termLabel, getTermService())){
52
            return;
53
        }
54

  
55
        Feature structure = Feature.NewInstance(currentRecord.get(HEADER_DEFINITION), termLabel, null);
56

  
57
        TermVocabulary vocabulary = importState.getStructureVoc();
58
        vocabulary.addTerm(structure);
59

  
60
        structure.addSource(IdentifiableSource.NewInstance(OriginalSourceType.Import, importState.getCitation().getTitle(), null, importState.getCitation(), null));
61

  
62
        getVocabularyService().saveOrUpdate(vocabulary);
63
        getTermService().saveOrUpdate(structure);
64
    }
65

  
66
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)