changeset 103:1149eb948036

add isodate tag to XML dump format 4.9.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 14 Nov 2018 18:31:55 +0100
parents 68e759210b53
children 8ce343218005
files src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java
diffstat 2 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Fri Nov 09 15:12:17 2018 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Wed Nov 14 18:31:55 2018 +0100
@@ -15,6 +15,11 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormatter;
+import org.joda.time.format.ISODateTimeFormat;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.mpi.openmind.repository.bo.Attribute;
 import org.mpi.openmind.repository.bo.Entity;
 import org.mpi.openmind.repository.bo.Node;
@@ -32,7 +37,7 @@
  */
 public class OM4StreamWriter {
 
-    protected static final String FORMAT_VERSION = "4.8";
+    protected static final String FORMAT_VERSION = "4.9";
 
     private static Logger logger = Logger.getLogger(OM4StreamWriter.class);
 
@@ -44,6 +49,9 @@
     /** key for entity count in attribute counts map */
     private static final String ENT_KEY = "<entity-count>";
 
+    /** formatter for isodate tag */
+    public static DateTimeFormatter dateFormatter = ISODateTimeFormat.date();
+    
     /**
      * Return the object's string representation or "null" if its null.
      * 
@@ -389,6 +397,29 @@
             	writer.writeCharacters(nov);
             	writer.writeEndElement();
             }
+			// convert date JSON into additional isodate
+			if (ov.startsWith("{")) {
+				try {
+					JSONObject json = new JSONObject(ov);
+					JSONObject date = null;
+					if (json.has("date")) {
+						date = json.getJSONObject("date"); 
+					} else if (json.has("from")) {
+						date = json.getJSONObject("from"); 
+					}
+					if (date != null) {
+						int year = date.getInt("year");
+						int month = date.getInt("month");
+						int day = date.getInt("dayOfMonth");
+						DateTime dt = new DateTime(year, month, day, 0, 0);
+						writer.writeStartElement(XMLUtil.ISODATE);
+						writer.writeCharacters(dateFormatter.print(dt));
+		            	writer.writeEndElement();						
+					}
+				} catch (JSONException e) {
+					// maybe not JSON...
+				}
+			}
         }
         
         writer.writeEndElement();
--- a/src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java	Fri Nov 09 15:12:17 2018 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java	Wed Nov 14 18:31:55 2018 +0100
@@ -92,6 +92,8 @@
     public static String MERGE = "merge";
     public static String REPLACE = "replace";
     public static String INSERT = "insert";
+    
+    public static String ISODATE = "isodate";
 
     public static Node getNodeByName(NodeList nodeList, String name){
         for(int i=0; i < nodeList.getLength(); i++){