bugfixes mainly...
[mir.git] / source / mir / producer / EntityBatchingProducerNode.java
index 19df0f1..e12bd79 100755 (executable)
@@ -12,38 +12,38 @@ public class EntityBatchingProducerNode implements ProducerNode {
 
   private String batchInfoKey;
   private String batchDataKey;
-  private StorageObject storage;
-  private EntityAdapterDefinition definition;
+  private EntityAdapterModel model;
+  private String definition;
   private String whereClause;
   private String orderByClause;
-  private int nrEntitiesToSkip;
-  private int nrEntitiesPerBatch;
-  private int minNrEntitiesInFirstBatch;
+  private String nrEntitiesToSkipExpression;
+  private String nrEntitiesPerBatchExpression;
+  private String minNrEntitiesInFirstBatchExpression;
+  private String defaultNrBatchesToProcessExpression;
   private ProducerNode batchSubNode;
   private ProducerNode batchListSubNode;
 
   public EntityBatchingProducerNode(
         String aBatchDataKey,
         String aBatchInfoKey,
-        StorageObject aStorage,
-        EntityAdapterDefinition aDefinition,
+        EntityAdapterModel aModel,
+        String aDefinition,
         String aWhereClause,
         String anOrderByClause,
-        int aNrEntitiesPerBatch,
-        int aMinNrEntitiesInFirstBatch,
-        int aNrEntitiesToSkip,
+        int anrEntitiesPerBatchExpression,
+        int aminNrEntitiesInFirstBatchExpression,
+        int anrEntitiesToSkipExpression,
         ProducerNode aBatchSubNode) {
-    this(aBatchDataKey, aBatchInfoKey, aStorage, aDefinition, aWhereClause,
-        anOrderByClause, aNrEntitiesPerBatch, aMinNrEntitiesInFirstBatch, aNrEntitiesToSkip,
+    this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition, aWhereClause,
+        anOrderByClause, anrEntitiesPerBatchExpression, aminNrEntitiesInFirstBatchExpression, anrEntitiesToSkipExpression,
         aBatchSubNode, null);
   }
 
-
   public EntityBatchingProducerNode(
         String aBatchDataKey,
         String aBatchInfoKey,
-        StorageObject aStorage,
-        EntityAdapterDefinition aDefinition,
+        EntityAdapterModel aModel,
+        String aDefinition,
         String aWhereClause,
         String anOrderByClause,
         int aNrEntitiesPerBatch,
@@ -52,6 +52,30 @@ public class EntityBatchingProducerNode implements ProducerNode {
         ProducerNode aBatchSubNode,
         ProducerNode aBatchListSubNode) {
 
+    this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition,
+         aWhereClause, anOrderByClause,
+        Integer.toString(aNrEntitiesPerBatch),
+        Integer.toString(aMinNrEntitiesInFirstBatch),
+        Integer.toString(aNrEntitiesToSkip),
+        "1",
+        aBatchSubNode, aBatchListSubNode);
+
+  }
+
+  public EntityBatchingProducerNode(
+        String aBatchDataKey,
+        String aBatchInfoKey,
+        EntityAdapterModel aModel,
+        String aDefinition,
+        String aWhereClause,
+        String anOrderByClause,
+        String anrEntitiesPerBatchExpression,
+        String aminNrEntitiesInFirstBatchExpression,
+        String anrEntitiesToSkipExpression,
+        String aDefaultNrBatchesToProcessExpression,
+        ProducerNode aBatchSubNode,
+        ProducerNode aBatchListSubNode) {
+
     batchSubNode = aBatchSubNode;
     batchListSubNode = aBatchListSubNode;
 
@@ -59,13 +83,14 @@ public class EntityBatchingProducerNode implements ProducerNode {
 
     batchDataKey = aBatchDataKey;
     batchInfoKey = aBatchInfoKey;
-    storage = aStorage;
+    model = aModel;
     definition = aDefinition;
     whereClause = aWhereClause;
     orderByClause = anOrderByClause;
-    nrEntitiesToSkip = aNrEntitiesToSkip;
-    nrEntitiesPerBatch = aNrEntitiesPerBatch;
-    minNrEntitiesInFirstBatch = aMinNrEntitiesInFirstBatch;
+    nrEntitiesToSkipExpression = anrEntitiesToSkipExpression;
+    nrEntitiesPerBatchExpression = anrEntitiesPerBatchExpression;
+    minNrEntitiesInFirstBatchExpression = aminNrEntitiesInFirstBatchExpression;
+    defaultNrBatchesToProcessExpression = aDefaultNrBatchesToProcessExpression;
   }
 
   public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
@@ -85,21 +110,33 @@ public class EntityBatchingProducerNode implements ProducerNode {
     List batchLocations;
     BatchLocation location;
 
+    int nrEntitiesToSkip;
+    int nrEntitiesPerBatch;
+    int minNrEntitiesInFirstBatch;
+
 //  ML: The order by clause should lead to a result set in _reverse order_: the first row will be
 //      the last entity presented on the last page
 
 
-    if (verb==null)
-      throw new ProducerFailure("EntityBatchingProducerNode: unknown verb '"+aVerb+"'", null);
-
     try {
+      if (verb==null) {
+        nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault( aValueMap, defaultNrBatchesToProcessExpression, -1 );
+      }
+      else {
+        nrBatchesToProcess=verb.nrBatchesToProcess;
+      }
+
       expandedWhereClause = ParameterExpander.expandExpression( aValueMap, whereClause );
       expandedOrderByClause = ParameterExpander.expandExpression( aValueMap, orderByClause );
 
+      nrEntitiesToSkip = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesToSkipExpression);
+      nrEntitiesPerBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesPerBatchExpression);
+      minNrEntitiesInFirstBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, minNrEntitiesInFirstBatchExpression);
+
       batchesData = new Vector();
       batchLocations = new Vector();
 
-      nrEntities = storage.getSize(expandedWhereClause)-nrEntitiesToSkip;
+      nrEntities = model.getMappingForName(definition).getStorage().getSize(expandedWhereClause)-nrEntitiesToSkip;
       nrEntitiesInFirstBatch = nrEntities % nrEntitiesPerBatch;
       while (nrEntitiesInFirstBatch<minNrEntitiesInFirstBatch && nrEntities-nrEntitiesInFirstBatch>=nrEntitiesPerBatch)
         nrEntitiesInFirstBatch = nrEntitiesInFirstBatch + nrEntitiesPerBatch;
@@ -108,33 +145,32 @@ public class EntityBatchingProducerNode implements ProducerNode {
       batchLocations.add(new BatchLocation(nrBatchesAfterFirst*nrEntitiesPerBatch, nrEntitiesInFirstBatch));
       batchData = new HashMap();
       batchData.put("identifier", "");
-      batchData.put("index", Integer.toString(nrBatchesAfterFirst+1));
-      batchData.put("size", Integer.toString(nrEntitiesInFirstBatch));
+      batchData.put("index", new Integer(nrBatchesAfterFirst+1));
+      batchData.put("size", new Integer(nrEntitiesInFirstBatch));
       batchesData.add(batchData);
 
       for (i=0; i<nrBatchesAfterFirst; i++) {
         batchLocations.add(1, new BatchLocation(i*nrEntitiesPerBatch, nrEntitiesPerBatch));
         batchData = new HashMap();
-        batchData.put("identifier", Integer.toString(i));
-        batchData.put("index", Integer.toString(i+1));
-        batchData.put("size", Integer.toString(nrEntitiesPerBatch));
+        batchData.put("identifier", new Integer(i));
+        batchData.put("index", new Integer(i+1));
+        batchData.put("size", new Integer(nrEntitiesPerBatch));
         batchesData.add(1, batchData);
       }
 
       batchData = new HashMap();
-      aValueMap.put(batchInfoKey, batchData);
+      ParameterExpander.setValueForKey(aValueMap, batchInfoKey, batchData);
       batchData.put("all", batchesData);
       batchData.put("first", batchesData.get(0));
       batchData.put("last", batchesData.get(batchesData.size()-1));
-      batchData.put("count", Integer.toString(batchesData.size()));
+      batchData.put("count", new Integer(batchesData.size()));
 
       if (batchListSubNode!=null) {
         batchListSubNode.produce(aValueMap, aVerb, aLogger);
       }
 
-      nrBatchesToProcess = nrBatchesAfterFirst+1;
-      if (verb.nrBatchesToProcess>-1 && verb.nrBatchesToProcess<nrBatchesToProcess) {
-        nrBatchesToProcess=verb.nrBatchesToProcess;
+      if (nrBatchesToProcess<0 || nrBatchesToProcess>nrBatchesAfterFirst+1) {
+        nrBatchesToProcess = nrBatchesAfterFirst+1;
       }
 
       if (batchSubNode!=null) {
@@ -152,14 +188,14 @@ public class EntityBatchingProducerNode implements ProducerNode {
           else
             batchData.put("next", null);
 
-          Iterator j = new EntityIteratorAdapter(storage, expandedWhereClause, expandedOrderByClause,
-                    location.nrEntities, definition, location.nrEntities, location.firstEntity);
+          Iterator j = new EntityIteratorAdapter(expandedWhereClause, expandedOrderByClause,
+                    location.nrEntities, model, definition, location.nrEntities, location.firstEntity);
           List entities = new Vector();
 
           while (j.hasNext())
             entities.add(0, j.next());
 
-          aValueMap.put( batchDataKey, entities );
+          ParameterExpander.setValueForKey(aValueMap, batchDataKey, entities );
 
           batchSubNode.produce(aValueMap, aVerb, aLogger);
         }