Import missing parameters for ggbn ABCD import
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / abcd206 / in / AbcdParseUtility.java
index d9aef32b1000fa31606bcf964b86208e542074c5..b62c06bf044e352a3be623e1eae1877721b3523b 100644 (file)
@@ -12,6 +12,7 @@ package eu.etaxonomy.cdm.io.specimen.abcd206.in;
 import java.io.InputStream;
 import java.net.URI;
 import java.util.Date;
+import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -23,6 +24,11 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
+import eu.etaxonomy.cdm.persistence.query.MatchMode;
+
 /**
  * @author pplitzner
  * @date 16.06.2015
@@ -33,14 +39,16 @@ public class AbcdParseUtility {
     private static final Logger logger = Logger.getLogger(AbcdParseUtility.class);
 
 
-    public static URI parseFirstUri(NodeList nodeList){
+    public static URI parseFirstUri(NodeList nodeList, Abcd206ImportReport report){
         URI uri = null;
         String textContent = parseFirstTextContent(nodeList);
         if(textContent!=null){
             try {
                 uri = URI.create(textContent);
             } catch (IllegalArgumentException e) {
-                //nothing
+                if(report!=null){
+                    report.addException("Exception during URI parsing!", e);
+                }
             }
         }
         return uri;
@@ -61,14 +69,14 @@ public class AbcdParseUtility {
         return string;
     }
 
-    public static Double parseFirstDouble(NodeList nodeList){
+    public static Double parseFirstDouble(NodeList nodeList, Abcd206ImportReport report){
         if(nodeList.getLength()>0){
-            return parseDouble(nodeList.item(0));
+            return parseDouble(nodeList.item(0), report);
         }
         return null;
     }
 
-    public static Double parseDouble(Node node){
+    public static Double parseDouble(Node node, Abcd206ImportReport report){
         String message = "Could not parse double value for node " + node.getNodeName();
         Double doubleValue = null;
         try{
@@ -80,8 +88,14 @@ public class AbcdParseUtility {
             doubleValue = Double.parseDouble(textContent);
         } catch (NullPointerException npe){
             logger.error(message, npe);
+            if(report!=null){
+                report.addException(message, npe);
+            }
         } catch (NumberFormatException nfe){
             logger.error(message, nfe);
+            if(report!=null){
+                report.addException(message, nfe);
+            }
         }
         return doubleValue;
     }
@@ -104,13 +118,29 @@ public class AbcdParseUtility {
         return date;
     }
 
+    public static Reference parseFirstReference(NodeList referenceNodeList, ICdmApplicationConfiguration cdmAppController){
+        String referenceCitation = AbcdParseUtility.parseFirstTextContent(referenceNodeList);
+        //check if reference already exists
+        List<Reference> matchingReferences = cdmAppController.getReferenceService().findByTitle(Reference.class, referenceCitation, MatchMode.EXACT, null, null, null, null, null).getRecords();
+        Reference<?> reference;
+        if(matchingReferences.size()==1){
+            reference = matchingReferences.iterator().next();
+        }
+        else{
+            reference = ReferenceFactory.newGeneric();
+            reference.setTitle(referenceCitation);
+            cdmAppController.getReferenceService().saveOrUpdate(reference);
+        }
+        return reference;
+    }
+
     /**
-     * Return the list of root nodes for an ABCD XML file
+     * Return the wrapper with the list of root nodes for an ABCD XML file
      * @param fileName: the file's location
-     * @return the list of root nodes ("Unit")
+     * @return a wrapper with a list of root nodes ("Unit")
      */
-    public static NodeList parseUnitsNodeList(Abcd206ImportState state) {
-        InputStream inputStream = state.getConfig().getSource();
+    public static UnitAssociationWrapper parseUnitsNodeList(InputStream inputStream, Abcd206ImportReport report) {
+        UnitAssociationWrapper unitAssociationWrapper = new UnitAssociationWrapper();
         NodeList unitList = null;
         try {
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -120,22 +150,28 @@ public class AbcdParseUtility {
             Element root = document.getDocumentElement();
             unitList = root.getElementsByTagName("Unit");
             if (unitList.getLength()>0) {
-                state.setPrefix("");
-                return unitList;
+                unitAssociationWrapper.setPrefix("");
+                unitAssociationWrapper.setAssociatedUnits(unitList);
+                return unitAssociationWrapper;
             }
             unitList = root.getElementsByTagName("abcd:Unit");
             if (unitList.getLength()>0) {
-                state.setPrefix("abcd:");
-                return unitList;
+                unitAssociationWrapper.setPrefix("abcd:");
+                unitAssociationWrapper.setAssociatedUnits(unitList);
+                return unitAssociationWrapper;
             }
             unitList = root.getElementsByTagName("abcd21:Unit");
             if (unitList.getLength()>0) {
-                state.setPrefix("abcd21:");
+                unitAssociationWrapper.setPrefix("abcd21:");
+                unitAssociationWrapper.setAssociatedUnits(unitList);
             }
         } catch (Exception e) {
             logger.warn(e);
+            if(report!=null){
+                report.addException("Exception during parsing of nodeList!", e);
+            }
         }
-        return unitList;
+        return unitAssociationWrapper;
     }
 
 }