apache.commons.config
[mir.git] / source / mir / producer / EntityBatchingProducerNode.java
index fd8e8ba..22cae78 100755 (executable)
 
 package mir.producer;
 
-import java.util.*;
-import java.io.*;
-import mir.entity.adapter.*;
-import mir.entity.*;
-import mir.storage.*;
-import mir.util.*;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
 
-public class EntityBatchingProducerNode implements ProducerNode {
-  private Map verbs;
+import mir.entity.adapter.EntityAdapterModel;
+import mir.entity.adapter.EntityIteratorAdapter;
+import mir.log.LoggerWrapper;
+import mir.util.ParameterExpander;
 
+public class EntityBatchingProducerNode implements ProducerNode {
   private String batchInfoKey;
   private String batchDataKey;
   private EntityAdapterModel model;
@@ -50,7 +52,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
   private String nrEntitiesToSkipExpression;
   private String nrEntitiesPerBatchExpression;
   private String minNrEntitiesInFirstBatchExpression;
-  private String defaultNrBatchesToProcessExpression;
+  private String nrBatchesToProcessExpression;
   private ProducerNode batchSubNode;
   private ProducerNode batchListSubNode;
 
@@ -61,57 +63,16 @@ public class EntityBatchingProducerNode implements ProducerNode {
         String aDefinition,
         String aWhereClause,
         String anOrderByClause,
-        int anrEntitiesPerBatchExpression,
-        int aminNrEntitiesInFirstBatchExpression,
-        int anrEntitiesToSkipExpression,
-        ProducerNode aBatchSubNode) {
-    this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition, aWhereClause,
-        anOrderByClause, anrEntitiesPerBatchExpression, aminNrEntitiesInFirstBatchExpression, anrEntitiesToSkipExpression,
-        aBatchSubNode, null);
-  }
-
-  public EntityBatchingProducerNode(
-        String aBatchDataKey,
-        String aBatchInfoKey,
-        EntityAdapterModel aModel,
-        String aDefinition,
-        String aWhereClause,
-        String anOrderByClause,
-        int aNrEntitiesPerBatch,
-        int aMinNrEntitiesInFirstBatch,
-        int aNrEntitiesToSkip,
-        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,
+        String aNrBatchesToProcessExpression,
         ProducerNode aBatchSubNode,
         ProducerNode aBatchListSubNode) {
 
     batchSubNode = aBatchSubNode;
     batchListSubNode = aBatchListSubNode;
 
-    verbs = new HashMap();
-
     batchDataKey = aBatchDataKey;
     batchInfoKey = aBatchInfoKey;
     model = aModel;
@@ -121,10 +82,17 @@ public class EntityBatchingProducerNode implements ProducerNode {
     nrEntitiesToSkipExpression = anrEntitiesToSkipExpression;
     nrEntitiesPerBatchExpression = anrEntitiesPerBatchExpression;
     minNrEntitiesInFirstBatchExpression = aminNrEntitiesInFirstBatchExpression;
-    defaultNrBatchesToProcessExpression = aDefaultNrBatchesToProcessExpression;
+    nrBatchesToProcessExpression = aNrBatchesToProcessExpression;
+  }
+
+  protected boolean isAborted(Map aValueMap) {
+    Object producerValue = aValueMap.get(NodedProducer.PRODUCER_KEY);
+    return (
+       (producerValue instanceof NodedProducer) &&
+      ((NodedProducer) producerValue).getIsAborted());
   }
 
-  public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
+  public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
     Iterator browser;
     int nrEntities;
     int nrBatchesAfterFirst;
@@ -136,7 +104,6 @@ public class EntityBatchingProducerNode implements ProducerNode {
     Map batchData;
     String expandedWhereClause;
     String expandedOrderByClause;
-    EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb);
 
     List batchLocations;
     BatchLocation location;
@@ -150,12 +117,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
 
 
     try {
-      if (verb==null) {
-        nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault( aValueMap, defaultNrBatchesToProcessExpression, -1 );
-      }
-      else {
-        nrBatchesToProcess=verb.nrBatchesToProcess;
-      }
+      nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault( aValueMap, nrBatchesToProcessExpression, -1 );
 
       expandedWhereClause = ParameterExpander.expandExpression( aValueMap, whereClause );
       expandedOrderByClause = ParameterExpander.expandExpression( aValueMap, orderByClause );
@@ -196,7 +158,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
       batchData.put("last", batchesData.get(batchesData.size()-1));
       batchData.put("count", new Integer(batchesData.size()));
 
-      if (batchListSubNode!=null) {
+      if (batchListSubNode!=null && (!isAborted(aValueMap))) {
         batchListSubNode.produce(aValueMap, aVerb, aLogger);
       }
 
@@ -205,7 +167,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
       }
 
       if (batchSubNode!=null) {
-        for (i=0; i<nrBatchesToProcess; i++) {
+        for (i=0; i<nrBatchesToProcess && !isAborted(aValueMap); i++) {
           location = (BatchLocation) batchLocations.get(i);
 
           batchData.put("current", batchesData.get(i));
@@ -233,7 +195,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
       }
     }
     catch (Throwable t) {
-      throw new ProducerFailure(t.getMessage(), t);
+      aLogger.error("EntityBatchingProducerNode caused an exception: " + t.getMessage());
     }
   };
 
@@ -246,21 +208,5 @@ public class EntityBatchingProducerNode implements ProducerNode {
       nrEntities = aNrEntities;
     }
   }
-
-  public Set buildVerbSet() {
-    return verbs.keySet();
-  };
-
-  public void addVerb(String aVerb, int aNrPagesToGenerate) {
-    verbs.put(aVerb, new EntityBatchingProducerNodeVerb(aNrPagesToGenerate));
-  }
-
-  private class EntityBatchingProducerNodeVerb {
-    int nrBatchesToProcess;
-
-    EntityBatchingProducerNodeVerb(int aNrBatchesToProcess) {
-      nrBatchesToProcess = aNrBatchesToProcess;
-    }
-  }
 }