Use jakarta feedparser to parse RSS2 files.
[mir.git] / source / mir / producer / EntityBatchingProducerNode.java
index e12bd79..3f23bb2 100755 (executable)
@@ -1,25 +1,58 @@
+/*
+ * Copyright (C) 2001-2006 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
 package mir.producer;
 
+import mir.entity.adapter.EntityAdapterModel;
+import mir.entity.adapter.EntityIteratorAdapter;
+import mir.util.ParameterExpander;
+import mir.util.StringRoutines;
+
 import java.util.*;
-import java.io.*;
-import mir.entity.adapter.*;
-import mir.entity.*;
-import mir.storage.*;
-import mir.util.*;
 
-public class EntityBatchingProducerNode implements ProducerNode {
-  private Map verbs;
+/**
+ * This producer makes it possible to show articles in batches, like on archive pages.
+ * <emph> The order by clause should lead to a result set in <b>reverse order</b>:
+ *         the first row will be the last entity in the last batch </emph>
+ */
 
+public class EntityBatchingProducerNode implements ProducerNode {
   private String batchInfoKey;
   private String batchDataKey;
   private EntityAdapterModel model;
+  private String mainTablePrefix;
+  private String extraTables;
   private String definition;
   private String whereClause;
   private String orderByClause;
   private String nrEntitiesToSkipExpression;
   private String nrEntitiesPerBatchExpression;
   private String minNrEntitiesInFirstBatchExpression;
-  private String defaultNrBatchesToProcessExpression;
+  private String nrBatchesToProcessExpression;
   private ProducerNode batchSubNode;
   private ProducerNode batchListSubNode;
 
@@ -27,85 +60,52 @@ public class EntityBatchingProducerNode implements ProducerNode {
         String aBatchDataKey,
         String aBatchInfoKey,
         EntityAdapterModel aModel,
-        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 aMainTablePrefix,
+        String someExtraTables,
         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;
+    mainTablePrefix = aMainTablePrefix;
+    extraTables = someExtraTables;
     definition = aDefinition;
     whereClause = aWhereClause;
     orderByClause = anOrderByClause;
     nrEntitiesToSkipExpression = anrEntitiesToSkipExpression;
     nrEntitiesPerBatchExpression = anrEntitiesPerBatchExpression;
     minNrEntitiesInFirstBatchExpression = aminNrEntitiesInFirstBatchExpression;
-    defaultNrBatchesToProcessExpression = aDefaultNrBatchesToProcessExpression;
+    nrBatchesToProcessExpression = aNrBatchesToProcessExpression;
   }
 
-  public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
-    Iterator browser;
+  protected boolean isAborted(Map aValueMap) {
+    Object producerValue = aValueMap.get(NodedProducer.PRODUCER_KEY);
+    return (
+       (producerValue instanceof NodedProducer) &&
+      ((NodedProducer) producerValue).getIsAborted());
+  }
+
+  public void produce(ProductionContext aProducerContext) throws ProducerExc, ProducerFailure {
     int nrEntities;
     int nrBatchesAfterFirst;
     int nrEntitiesInFirstBatch;
     int nrBatchesToProcess;
     List batchesData;
     int i;
-    int position;
     Map batchData;
     String expandedWhereClause;
     String expandedOrderByClause;
-    EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb);
 
     List batchLocations;
     BatchLocation location;
@@ -114,29 +114,22 @@ public class EntityBatchingProducerNode implements ProducerNode {
     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
-
-
     try {
-      if (verb==null) {
-        nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault( aValueMap, defaultNrBatchesToProcessExpression, -1 );
-      }
-      else {
-        nrBatchesToProcess=verb.nrBatchesToProcess;
-      }
+      nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault(aProducerContext.getValueSet(), nrBatchesToProcessExpression, -1);
 
-      expandedWhereClause = ParameterExpander.expandExpression( aValueMap, whereClause );
-      expandedOrderByClause = ParameterExpander.expandExpression( aValueMap, orderByClause );
+      expandedWhereClause = ParameterExpander.expandExpression( aProducerContext.getValueSet(), whereClause );
+      expandedOrderByClause = ParameterExpander.expandExpression( aProducerContext.getValueSet(), orderByClause );
 
-      nrEntitiesToSkip = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesToSkipExpression);
-      nrEntitiesPerBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesPerBatchExpression);
-      minNrEntitiesInFirstBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, minNrEntitiesInFirstBatchExpression);
+      nrEntitiesToSkip = ParameterExpander.evaluateIntegerExpression( aProducerContext.getValueSet(), nrEntitiesToSkipExpression);
+      nrEntitiesPerBatch = ParameterExpander.evaluateIntegerExpression( aProducerContext.getValueSet(), nrEntitiesPerBatchExpression);
+      minNrEntitiesInFirstBatch = ParameterExpander.evaluateIntegerExpression( aProducerContext.getValueSet(), minNrEntitiesInFirstBatchExpression);
+      List extraTableList = StringRoutines.splitString(ParameterExpander.expandExpression( aProducerContext.getValueSet(), extraTables).trim(), ",");
 
-      batchesData = new Vector();
-      batchLocations = new Vector();
+      batchesData = new ArrayList();
+      batchLocations = new ArrayList();
 
-      nrEntities = model.getMappingForName(definition).getStorage().getSize(expandedWhereClause)-nrEntitiesToSkip;
+      nrEntities = model.getMappingForName(definition).getDatabase().getSize(
+          mainTablePrefix, extraTableList, expandedWhereClause)-nrEntitiesToSkip;
       nrEntitiesInFirstBatch = nrEntities % nrEntitiesPerBatch;
       while (nrEntitiesInFirstBatch<minNrEntitiesInFirstBatch && nrEntities-nrEntitiesInFirstBatch>=nrEntitiesPerBatch)
         nrEntitiesInFirstBatch = nrEntitiesInFirstBatch + nrEntitiesPerBatch;
@@ -159,14 +152,14 @@ public class EntityBatchingProducerNode implements ProducerNode {
       }
 
       batchData = new HashMap();
-      ParameterExpander.setValueForKey(aValueMap, batchInfoKey, batchData);
+      ParameterExpander.setValueForKey(aProducerContext.getValueSet(), batchInfoKey, batchData);
       batchData.put("all", batchesData);
       batchData.put("first", batchesData.get(0));
       batchData.put("last", batchesData.get(batchesData.size()-1));
       batchData.put("count", new Integer(batchesData.size()));
 
-      if (batchListSubNode!=null) {
-        batchListSubNode.produce(aValueMap, aVerb, aLogger);
+      if (batchListSubNode!=null && (!isAborted(aProducerContext.getValueSet()))) {
+        batchListSubNode.produce(aProducerContext);
       }
 
       if (nrBatchesToProcess<0 || nrBatchesToProcess>nrBatchesAfterFirst+1) {
@@ -174,7 +167,7 @@ public class EntityBatchingProducerNode implements ProducerNode {
       }
 
       if (batchSubNode!=null) {
-        for (i=0; i<nrBatchesToProcess; i++) {
+        for (i=0; i<nrBatchesToProcess && !isAborted(aProducerContext.getValueSet()); i++) {
           location = (BatchLocation) batchLocations.get(i);
 
           batchData.put("current", batchesData.get(i));
@@ -188,23 +181,23 @@ public class EntityBatchingProducerNode implements ProducerNode {
           else
             batchData.put("next", null);
 
-          Iterator j = new EntityIteratorAdapter(expandedWhereClause, expandedOrderByClause,
+          Iterator j = new EntityIteratorAdapter(mainTablePrefix, extraTableList, expandedWhereClause, expandedOrderByClause,
                     location.nrEntities, model, definition, location.nrEntities, location.firstEntity);
-          List entities = new Vector();
+          List entities = new ArrayList();
 
           while (j.hasNext())
             entities.add(0, j.next());
 
-          ParameterExpander.setValueForKey(aValueMap, batchDataKey, entities );
+          ParameterExpander.setValueForKey(aProducerContext.getValueSet(), batchDataKey, entities );
 
-          batchSubNode.produce(aValueMap, aVerb, aLogger);
+          batchSubNode.produce(aProducerContext);
         }
       }
     }
     catch (Throwable t) {
-      throw new ProducerFailure(t.getMessage(), t);
+      aProducerContext.getLogger().warn("EntityBatchingProducerNode caused an exception: " + t.getMessage());
     }
-  };
+  }
 
   private class BatchLocation {
     int nrEntities;
@@ -215,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;
-    }
-  }
 }