producer performance upgrades in the form of entity caching, and preliminary
[mir.git] / source / mir / entity / adapter / EntityAdapterDefinition.java
index 1d96922..01c8b1b 100755 (executable)
@@ -3,6 +3,7 @@ package mir.entity.adapter;
 import java.util.*;
 import mir.entity.*;
 import mir.storage.*;
+import mir.util.*;
 
 public class EntityAdapterDefinition {
   Map calculatedFields;
@@ -27,22 +28,27 @@ public class EntityAdapterDefinition {
     calculatedFields.put(aFieldName, aField);
   }
 
-  public void addDateField(String aDestinationFieldName, String aSourceFieldName) {
-    addCalculatedField(aDestinationFieldName, new DateField(aSourceFieldName));
+  public void addMirDateField(String aDestinationFieldName, String aSourceFieldName) {
+    addCalculatedField(aDestinationFieldName, new MirDateField(aSourceFieldName));
+  }
+
+  public void addDBDateField(String aDestinationFieldName, String aSourceFieldName) {
+    addCalculatedField(aDestinationFieldName, new DBDateField(aSourceFieldName));
   }
 
   public interface CalculatedField {
     public Object getValue(EntityAdapter anEntityAdapter);
   }
 
-  private class DateField implements CalculatedField {
+  private class MirDateField implements CalculatedField {
     private String fieldName;
 
-    public DateField(String aFieldName) {
+    public MirDateField(String aFieldName) {
       fieldName = aFieldName;
     }
 
     public Object getValue(EntityAdapter anEntityAdapter) {
+
       Map result = new HashMap();
       String textValue = anEntityAdapter.getEntity().getValue(fieldName);
       Calendar calendar = GregorianCalendar.getInstance();
@@ -57,32 +63,77 @@ public class EntityAdapterDefinition {
           month = Integer.parseInt(textValue.substring(4,6));
           day = Integer.parseInt(textValue.substring(6,8));
 
-          calendar.set(year, month, day);
+          calendar.set(year, month-1, day);
+          date = calendar.getTime();
+          ;
+
+          result.put("date", date);
+          result.put("formatted", new DateToMapAdapter(date));
+
+        }
+        catch (Throwable t) {
+          result=null;
+        }
+      }
+
+      return result;
+
+    }
+
+  }
+
+  private class DBDateField implements CalculatedField {
+    private String fieldName;
+
+    public DBDateField(String aFieldName) {
+      fieldName = aFieldName;
+    }
+
+    public Object getValue(EntityAdapter anEntityAdapter) {
+
+      Map result = new HashMap();
+      String textValue = anEntityAdapter.getEntity().getValue(fieldName);
+      Calendar calendar = GregorianCalendar.getInstance();
+      int year;
+      int month;
+      int day;
+      int hours;
+      int minutes;
+
+      Date date;
+
+      if (textValue!=null) {
+        try {
+          year = Integer.parseInt(textValue.substring(0,4));
+          month = Integer.parseInt(textValue.substring(5,7));
+          day = Integer.parseInt(textValue.substring(8,10));
+          hours = Integer.parseInt(textValue.substring(11,13));
+          minutes = Integer.parseInt(textValue.substring(14,16));
+
+          calendar.set(year, month-1, day, hours, minutes);
           date = calendar.getTime();
 
           result.put("date", date);
-          result.put("year", textValue.substring(0,4));
-          result.put("month", textValue.substring(4,6));
-          result.put("day", textValue.substring(6,8));
+          result.put("formatted", new DateToMapAdapter(date));
+          result.put("raw", textValue);
         }
         catch (Throwable t) {
-          result.put("date", null);
-          result.put("year", null);
-          result.put("month", null);
-          result.put("day", null);
+          result=null;
         }
       }
 
       return result;
+
     }
+
   }
 
   static protected Object getRelation(StorageObject aStorageObject, String aWhereClause, String anOrderByClause, EntityAdapterDefinition aDefinition) {
     try {
       return
-          new EntityIteratorAdapter(
+          new CachingRewindableIterator( new EntityIteratorAdapter(
               new EntityBrowser( aStorageObject, aWhereClause, anOrderByClause, -1),
-              aDefinition);
+              aDefinition));
     }
     catch (Throwable t) {
       throw new RuntimeException(t.getMessage());