commit dcad6203ac9617c4ba4a2caa00260aa0f442ce92
Author: Patrick Plitzner
Date: Fri Jan 25 15:54:29 2019 +0100
Implement Kew Plant Glossary CSV import
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryActivator.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryActivator.java
new file mode 100644
index 00000000..53d20c4f
--- /dev/null
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryActivator.java
@@ -0,0 +1,53 @@
+package eu.etaxonomy.cdm.io.plantglossary.kew;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.app.common.CdmDestinations;
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
+import eu.etaxonomy.cdm.database.DbSchemaValidation;
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
+import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
+
+/**
+ *
+ * @author pplitzner
+ * @since Jan 25, 2019
+ *
+ */
+public class KewPlantGlossaryActivator {
+ @SuppressWarnings("unused")
+ private static final Logger logger = Logger.getLogger(KewPlantGlossaryActivator.class);
+
+ private void doImport(ICdmDataSource cdmDestination) throws IOException{
+
+ /*
+ * Source file:
+ * extracted data from Kew Plant Glossary
+ *
+ */
+ URI uri = URI.create("file:/home/pplitzner/projects/Additivity/KEW_Plant_Glossary.csv");
+ KewPlantGlossaryCsvImportConfigurator config = KewPlantGlossaryCsvImportConfigurator.NewInstance(uri, cdmDestination);
+ config.setCheck(CHECK.IMPORT_WITHOUT_CHECK);
+ config.setDbSchemaValidation(DbSchemaValidation.VALIDATE);
+
+ CdmDefaultImport myImport = new CdmDefaultImport<>();
+
+ System.out.println("Start import from ("+ cdmDestination.toString() + ") ...");
+ myImport.invoke(config);
+ System.out.println("End import from ("+ cdmDestination.toString() + ")...");
+ }
+
+ public static void main(String[] args) {
+ KewPlantGlossaryActivator activator = new KewPlantGlossaryActivator();
+ try {
+ ICdmDataSource dataSource = CdmDestinations.makeDestination(DatabaseTypeEnum.MySQL, "127.0.0.1", "empty", 3306, "root", null);
+ activator.doImport(dataSource);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportConfigurator.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportConfigurator.java
new file mode 100644
index 00000000..b2e0510f
--- /dev/null
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportConfigurator.java
@@ -0,0 +1,55 @@
+/**
+* Copyright (C) 2017 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.plantglossary.kew;
+
+import java.io.IOException;
+import java.net.URI;
+
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.io.csv.in.CsvImportConfiguratorBase;
+
+/**
+ *
+ * @author pplitzner
+ * @since Jan 25, 2019
+ *
+ */
+public class KewPlantGlossaryCsvImportConfigurator
+ extends CsvImportConfiguratorBase{
+
+ private static final long serialVersionUID = 987286481306951779L;
+
+ public static KewPlantGlossaryCsvImportConfigurator NewInstance(URI source,
+ ICdmDataSource cdmDestination) throws IOException {
+ return new KewPlantGlossaryCsvImportConfigurator(source, cdmDestination);
+ }
+
+// ****************** CONSTRUCTOR *****************************/
+
+ private KewPlantGlossaryCsvImportConfigurator(URI source,
+ ICdmDataSource cdmDestination) throws IOException{
+ super(source, cdmDestination, null);
+ }
+
+// *************************************
+
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected void makeIoClassList() {
+ ioClassList = new Class[] {
+ KewPlantGlossaryImport.class };
+ }
+
+ @Override
+ public KewPlantGlossaryCsvImportState getNewState() {
+ return new KewPlantGlossaryCsvImportState(this);
+ }
+
+}
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportState.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportState.java
new file mode 100644
index 00000000..7c435f71
--- /dev/null
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryCsvImportState.java
@@ -0,0 +1,77 @@
+/**
+* Copyright (C) 2017 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.plantglossary.kew;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.io.csv.in.CsvImportState;
+import eu.etaxonomy.cdm.model.agent.Institution;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.cdm.model.common.TermType;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+
+/**
+ *
+ * @author pplitzner
+ * @since Jan 25, 2019
+ *
+ */
+public class KewPlantGlossaryCsvImportState extends CsvImportState {
+
+ private List existingVocabularies = new ArrayList<>();
+ private List> existingTerms = new ArrayList<>();
+ private final Reference citation;
+ private final TermVocabulary structureVoc;
+
+
+ protected KewPlantGlossaryCsvImportState(KewPlantGlossaryCsvImportConfigurator config) {
+ super(config);
+ structureVoc = TermVocabulary.NewInstance(TermType.Feature);
+ structureVoc.setLabel("Structures");
+
+ citation = ReferenceFactory.newBook();
+ citation.setTitle("The Kew Plant Glossary, an illustrated dictionary of plant terms");
+ citation.setAuthorship(Person.NewInstance(null, "J. Beentje", null, "Henk"));
+ VerbatimTimePeriod datePublished = VerbatimTimePeriod.NewVerbatimInstance();
+ datePublished.setStartYear(2010);
+ citation.setDatePublished(datePublished);
+ Institution institution = Institution.NewNamedInstance("Royal Botanic Gardens, Kew");
+ citation.setInstitution(institution);
+ citation.setIsbn("978-1-84246-422-9");
+ }
+
+ @Override
+ public void resetSession(){
+ super.resetSession();
+ }
+
+ public boolean isTermPresent(String termName, ITermService termService) {
+ if(existingTerms.isEmpty()){
+ existingTerms = termService.getUuidAndTitleCache(null, "*");
+ }
+ return existingTerms.stream().anyMatch(term->term.getTitleCache().equals(termName));
+ }
+
+ TermVocabulary getStructureVoc() {
+ return structureVoc;
+ }
+
+ Reference getCitation() {
+ return citation;
+ }
+
+}
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryImport.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryImport.java
new file mode 100644
index 00000000..703638d6
--- /dev/null
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/plantglossary/kew/KewPlantGlossaryImport.java
@@ -0,0 +1,66 @@
+/**
+* Copyright (C) 2017 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.plantglossary.kew;
+
+import java.io.File;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.io.csv.in.CsvImportBase;
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;
+import eu.etaxonomy.cdm.model.common.OriginalSourceType;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.description.Feature;
+
+/**
+ *
+ * @author pplitzner
+ * @since Jan 25, 2019
+ *
+ */
+@Component
+public class KewPlantGlossaryImport extends CsvImportBase{
+ private static final long serialVersionUID = -5600766240192189822L;
+ private static Logger logger = Logger.getLogger(KewPlantGlossaryImport.class);
+
+ final String HEADER_LABEL = "term";
+ final String HEADER_DEFINITION = "definition";
+ final String HEADER_TYPE = "type";
+
+ @Override
+ protected void handleSingleLine(KewPlantGlossaryCsvImportState importState) {
+
+ Map currentRecord = importState.getCurrentRecord();
+
+ if(CdmUtils.isBlank(currentRecord.get(HEADER_TYPE)) || !currentRecord.get(HEADER_TYPE).equals("1")){
+ // only structures (type=1) are imported
+ return;
+ }
+
+ String termLabel = currentRecord.get(HEADER_LABEL);
+ //check if already present
+ if(importState.isTermPresent(termLabel, getTermService())){
+ return;
+ }
+
+ Feature structure = Feature.NewInstance(currentRecord.get(HEADER_DEFINITION), termLabel, null);
+
+ TermVocabulary vocabulary = importState.getStructureVoc();
+ vocabulary.addTerm(structure);
+
+ structure.addSource(IdentifiableSource.NewInstance(OriginalSourceType.Import, importState.getCitation().getTitle(), null, importState.getCitation(), null));
+
+ getVocabularyService().saveOrUpdate(vocabulary);
+ getTermService().saveOrUpdate(structure);
+ }
+
+}
\ No newline at end of file