You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2013/03/14 11:14:23 UTC

svn commit: r1456383 - in /manifoldcf/trunk: connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/ framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/ framework/core/src/main/java/org/apache/manifoldcf/co...

Author: kwright
Date: Thu Mar 14 10:14:23 2013
New Revision: 1456383

URL: http://svn.apache.org/r1456383
Log:
Add native generic date field support to RepositoryDocument, as promised.

Modified:
    manifoldcf/trunk/connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/HttpPoster.java
    manifoldcf/trunk/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/RepositoryDocument.java
    manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/common/DateParser.java

Modified: manifoldcf/trunk/connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/HttpPoster.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/HttpPoster.java?rev=1456383&r1=1456382&r2=1456383&view=diff
==============================================================================
--- manifoldcf/trunk/connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/HttpPoster.java (original)
+++ manifoldcf/trunk/connectors/solr/connector/src/main/java/org/apache/manifoldcf/agents/output/solr/HttpPoster.java Thu Mar 14 10:14:23 2013
@@ -21,6 +21,7 @@ package org.apache.manifoldcf.agents.out
 import org.apache.manifoldcf.core.interfaces.*;
 import org.apache.manifoldcf.core.common.Base64;
 import org.apache.manifoldcf.core.common.XMLDoc;
+import org.apache.manifoldcf.core.common.DateParser;
 import org.apache.manifoldcf.agents.interfaces.*;
 import org.apache.manifoldcf.agents.system.*;
 
@@ -809,14 +810,14 @@ public class HttpPoster
             Date date = document.getModifiedDate();
             if (date != null)
               // Write value
-              writeField(out,LITERAL+modifiedDateAttributeName,convertToISO(date));
+              writeField(out,LITERAL+modifiedDateAttributeName,DateParser.formatISO8601Date(date));
           }
           if (createdDateAttributeName != null)
           {
             Date date = document.getCreatedDate();
             if (date != null)
               // Write value
-              writeField(out,LITERAL+createdDateAttributeName,convertToISO(date));
+              writeField(out,LITERAL+createdDateAttributeName,DateParser.formatISO8601Date(date));
           }
           if (fileNameAttributeName != null)
           {
@@ -983,12 +984,6 @@ public class HttpPoster
     }
   }
 
-  protected static String convertToISO(Date date)
-  {
-    java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
-    return df.format(date);
-  }
-  
   /** Killable thread that does deletions.
   * Java 1.5 stopped permitting thread interruptions to abort socket waits.  As a result, it is impossible to get threads to shutdown cleanly that are doing
   * such waits.  So, the places where this happens are segregated in their own threads so that they can be just abandoned.

Modified: manifoldcf/trunk/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/RepositoryDocument.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/RepositoryDocument.java?rev=1456383&r1=1456382&r2=1456383&view=diff
==============================================================================
--- manifoldcf/trunk/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/RepositoryDocument.java (original)
+++ manifoldcf/trunk/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/RepositoryDocument.java Thu Mar 14 10:14:23 2013
@@ -19,6 +19,7 @@
 package org.apache.manifoldcf.agents.interfaces;
 
 import org.apache.manifoldcf.core.interfaces.*;
+import org.apache.manifoldcf.core.common.DateParser;
 import java.util.*;
 import java.io.*;
 
@@ -41,6 +42,7 @@ public class RepositoryDocument
   protected Map<String,Object> fields = new HashMap<String,Object>();
   protected Map<String,String[]> stringFields = new HashMap<String,String[]>();
   protected Map<String,Reader[]> readerFields = new HashMap<String,Reader[]>();
+  protected Map<String,Date[]> dateFields = new HashMap<String,Date[]>();
   protected Security fileSecurity = new Security();
   protected Security shareSecurity = new Security();
   protected List<Security> directorySecurity = new ArrayList<Security>();
@@ -235,7 +237,44 @@ public class RepositoryDocument
     return binaryLength;
   }
 
-  /** Add a multivalue character field.
+  /** Add/remove a multivalue date field.
+  *@param fieldName is the field name.
+  *@param fieldData is the multi-valued data (an array of Dates).  Null means
+  * to remove the entry.
+  */
+  public void addField(String fieldName, Date[] fieldData)
+    throws ManifoldCFException
+  {
+    if (fieldData == null)
+    {
+      fields.remove(fieldName);
+      stringFields.remove(fieldName);
+      readerFields.remove(fieldName);
+      dateFields.remove(fieldName);
+    }
+    else
+    {
+      fields.put(fieldName,fieldData);
+      stringFields.remove(fieldName);
+      readerFields.remove(fieldName);
+      dateFields.put(fieldName,fieldData);
+    }
+  }
+  
+  /** Add/remove a date field.
+  *@param fieldName is the field name.
+  *@param fieldData is the single-valued data (a Date).  Null means "no value".
+  */
+  public void addField(String fieldName, Date fieldData)
+    throws ManifoldCFException
+  {
+    if (fieldData == null)
+      addField(fieldName, (Date[])null);
+    else
+      addField(fieldName,new Date[]{fieldData});
+  }
+
+  /** Add/remove a multivalue character field.
   *@param fieldName is the field name.
   *@param fieldData is the multi-valued data (as an array of Readers).  Null means
   * to remove the entry from the document.
@@ -248,23 +287,28 @@ public class RepositoryDocument
       fields.remove(fieldName);
       stringFields.remove(fieldName);
       readerFields.remove(fieldName);
+      dateFields.remove(fieldName);
     }
     else
     {
       fields.put(fieldName,fieldData);
       stringFields.remove(fieldName);
       readerFields.put(fieldName,fieldData);
+      dateFields.remove(fieldName);
     }
   }
 
-  /** Add a character field.
+  /** Add/remove a character field.
   *@param fieldName is the field name.
   *@param fieldData is the single-valued data (as a Reader).  Null means "no value".
   */
   public void addField(String fieldName, Reader fieldData)
     throws ManifoldCFException
   {
-    addField(fieldName,new Reader[]{fieldData});
+    if (fieldData == null)
+      addField(fieldName, (Reader[])null);
+    else
+      addField(fieldName,new Reader[]{fieldData});
   }
 
   /** Add/Remove a multivalue character field.
@@ -280,12 +324,14 @@ public class RepositoryDocument
       fields.remove(fieldName);
       stringFields.remove(fieldName);
       readerFields.remove(fieldName);
+      dateFields.remove(fieldName);
     }
     else
     {
       fields.put(fieldName,fieldData);
       readerFields.remove(fieldName);
       stringFields.put(fieldName,fieldData);
+      dateFields.remove(fieldName);
     }
   }
 
@@ -296,7 +342,10 @@ public class RepositoryDocument
   public void addField(String fieldName, String fieldData)
     throws ManifoldCFException
   {
-    addField(fieldName,new String[]{fieldData});
+    if (fieldData == null)
+      addField(fieldName,(String)null);
+    else
+      addField(fieldName,new String[]{fieldData});
   }
 
   /** Get a field.
@@ -310,7 +359,8 @@ public class RepositoryDocument
 
   /** Get a field as an array of strings.  If the data was originally in the form
   * of Readers, a one-time conversion is made to the String form, so that the same
-  * field can be fetched multiple times.
+  * field can be fetched multiple times.  If the data was originally in the form
+  * of Dates, then the dates are converted to standard ISO8601 format.
   *@param fieldName is the field name.
   *@return the field data.
   */
@@ -320,28 +370,39 @@ public class RepositoryDocument
     String[] stringFieldData = stringFields.get(fieldName);
     if (stringFieldData != null)
       return stringFieldData;
+    Date[] dateFieldData = dateFields.get(fieldName);
+    if (dateFieldData != null)
+    {
+      String[] newValues = new String[dateFieldData.length];
+      for (int i = 0; i < dateFieldData.length; i++)
+      {
+        newValues[i] = DateParser.formatISO8601Date(dateFieldData[i]);
+      }
+      return newValues;
+    }
     Reader[] oldValues = readerFields.get(fieldName);
-    if (oldValues == null)
-      return null;
-    
-    String[] newValues = new String[oldValues.length];
-    char[] buffer = new char[65536];
-    int i = 0;
-    while (i < newValues.length)
-    {
-      Reader oldValue = oldValues[i];
-      StringBuilder newValue = new StringBuilder();
-      while (true)
+    if (oldValues != null)
+    {
+      String[] newValues = new String[oldValues.length];
+      char[] buffer = new char[65536];
+      for (int i = 0; i < newValues.length; i++)
       {
-        int amt = oldValue.read(buffer);
-        if (amt == -1)
-          break;
-        newValue.append(buffer,0,amt);
+        Reader oldValue = oldValues[i];
+        StringBuilder newValue = new StringBuilder();
+        while (true)
+        {
+          int amt = oldValue.read(buffer);
+          if (amt == -1)
+            break;
+          newValue.append(buffer,0,amt);
+        }
+        newValues[i] = newValue.toString();
       }
-      newValues[i++] = newValue.toString();
+      stringFields.put(fieldName,newValues);
+      return newValues;
     }
-    stringFields.put(fieldName,newValues);
-    return newValues;
+    else
+      return null;
   }
 
   /** Get a field as an array of Readers.  If the field was originally
@@ -354,21 +415,42 @@ public class RepositoryDocument
     Reader[] readerFieldData = readerFields.get(fieldName);
     if (readerFieldData != null)
       return readerFieldData;
+    Date[] dateFieldData = dateFields.get(fieldName);
+    if (dateFieldData != null)
+    {
+      Reader[] newValues = new Reader[dateFieldData.length];
+      for (int i = 0; i < newValues.length; i++)
+      {
+        newValues[i] = new StringReader(DateParser.formatISO8601Date(dateFieldData[i]));
+      }
+      readerFields.put(fieldName,newValues);
+      return newValues;
+    }
     String[] oldValues = stringFields.get(fieldName);
-    if (oldValues == null)
-      return null;
-    
-    Reader[] newValues = new Reader[oldValues.length];
-    int i = 0;
-    while (i < newValues.length)
+    if (oldValues != null)
     {
-      newValues[i] = new StringReader(oldValues[i]);
-      i++;
+      Reader[] newValues = new Reader[oldValues.length];
+      for (int i = 0; i < newValues.length; i++)
+      {
+        newValues[i] = new StringReader(oldValues[i]);
+      }
+      readerFields.put(fieldName,newValues);
+      return newValues;
     }
-    readerFields.put(fieldName,newValues);
-    return newValues;
+    else
+      return null;
   }
 
+  /** Get field as an array of Date objects.
+  * If the field was originally not a Date field, null is returned.
+  *@param fieldName is the field name.
+  *@return the field data.
+  */
+  public Date[] getFieldAsDates(String fieldName)
+  {
+    return dateFields.get(fieldName);
+  }
+  
   /** Get the number of fields.
   */
   public int fieldCount()

Modified: manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/common/DateParser.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/common/DateParser.java?rev=1456383&r1=1456382&r2=1456383&view=diff
==============================================================================
--- manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/common/DateParser.java (original)
+++ manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/common/DateParser.java Thu Mar 14 10:14:23 2013
@@ -55,6 +55,15 @@ public class DateParser
     }
   }
   
+  /** Format ISO8601 date.
+  */
+  public static String formatISO8601Date(Date dateValue)
+  {
+    java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
+    df.setTimeZone(TimeZone.getTimeZone("GMT"));
+    return df.format(dateValue);
+  }
+  
   /** Timezone mapping from RFC822 timezones to ones understood by Java */
   
   // Month map