-/*package mir.producer;
+package mir.producer;
import java.util.*;
import java.io.*;
import mir.storage.*;
import mir.util.*;
-// abstract public int getSize(String where) throws SQLException,StorageObjectException;
-
-
public class EntityBatchingProducerNode implements ProducerNode {
private Map verbs;
- private EntityBatchingProducerNodeVerb defaultVerb;
- private String key;
- private StorageObject storage;
- private EntityAdapterDefinition definition;
+ private String batchInfoKey;
+ private String batchDataKey;
+ private EntityAdapterModel model;
+ private String definition;
private String whereClause;
private String orderByClause;
- private int nrArticlesPerBatch;
- private int minNrArticlesInFirstBatch;
+ private int nrEntitiesToSkip;
+ private int nrEntitiesPerBatch;
+ private int minNrEntitiesInFirstBatch;
private ProducerNode batchSubNode;
private ProducerNode batchListSubNode;
public EntityBatchingProducerNode(
- String aKey,
- StorageObject aStorage,
- EntityAdapterDefinition aDefinition,
+ String aBatchDataKey,
+ String aBatchInfoKey,
+ EntityAdapterModel aModel,
+ String aDefinition,
+ String aWhereClause,
+ String anOrderByClause,
+ int aNrEntitiesPerBatch,
+ int aMinNrEntitiesInFirstBatch,
+ int aNrEntitiesToSkip,
+ ProducerNode aBatchSubNode) {
+ this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition, aWhereClause,
+ anOrderByClause, aNrEntitiesPerBatch, aMinNrEntitiesInFirstBatch, aNrEntitiesToSkip,
+ aBatchSubNode, null);
+ }
+
+
+ public EntityBatchingProducerNode(
+ String aBatchDataKey,
+ String aBatchInfoKey,
+ EntityAdapterModel aModel,
+ String aDefinition,
String aWhereClause,
String anOrderByClause,
- int aNrArticlesPerBatch,
- int aMinNrArticlesInFirstBatch,
+ int aNrEntitiesPerBatch,
+ int aMinNrEntitiesInFirstBatch,
+ int aNrEntitiesToSkip,
ProducerNode aBatchSubNode,
ProducerNode aBatchListSubNode) {
batchSubNode = aBatchSubNode;
batchListSubNode = aBatchListSubNode;
- defaultVerb = null;
verbs = new HashMap();
- key = aKey;
- storage = aStorage;
+ batchDataKey = aBatchDataKey;
+ batchInfoKey = aBatchInfoKey;
+ model = aModel;
definition = aDefinition;
whereClause = aWhereClause;
orderByClause = anOrderByClause;
- nrArticlesPerBatch = aNrArticlesPerBatch;
- minNrArticlesInFirstBatch = aMinNrArticlesInFirstBatch;
+ nrEntitiesToSkip = aNrEntitiesToSkip;
+ nrEntitiesPerBatch = aNrEntitiesPerBatch;
+ minNrEntitiesInFirstBatch = aMinNrEntitiesInFirstBatch;
}
public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
- EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb);
Iterator browser;
+ 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;
+
+// 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)
- verb = defaultVerb;
if (verb==null)
throw new ProducerFailure("EntityBatchingProducerNode: unknown verb '"+aVerb+"'", null);
try {
- browser = new EntityIteratorAdapter(
- storage,
- ParameterExpander.expandExpression( aValueMap, verb.whereClause ),
- ParameterExpander.expandExpression( aValueMap, verb.orderByClause ),
- -1,
- definition );
-
- while (browser.hasNext()) {
- aLogger.println( verb.whereClause + ": next... ");
- aValueMap.put(key, browser.next());
- super.produce(aValueMap, aVerb, aLogger);
+ expandedWhereClause = ParameterExpander.expandExpression( aValueMap, whereClause );
+ expandedOrderByClause = ParameterExpander.expandExpression( aValueMap, orderByClause );
+
+ batchesData = new Vector();
+ batchLocations = new Vector();
+
+ nrEntities = model.getMappingForName(definition).getStorage().getSize(expandedWhereClause)-nrEntitiesToSkip;
+ nrEntitiesInFirstBatch = nrEntities % nrEntitiesPerBatch;
+ while (nrEntitiesInFirstBatch<minNrEntitiesInFirstBatch && nrEntities-nrEntitiesInFirstBatch>=nrEntitiesPerBatch)
+ nrEntitiesInFirstBatch = nrEntitiesInFirstBatch + nrEntitiesPerBatch;
+ nrBatchesAfterFirst = (nrEntities-nrEntitiesInFirstBatch)/nrEntitiesPerBatch;
+
+ 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));
+ 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));
+ batchesData.add(1, batchData);
+ }
+
+ batchData = new HashMap();
+ aValueMap.put(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()));
+
+ if (batchListSubNode!=null) {
+ batchListSubNode.produce(aValueMap, aVerb, aLogger);
+ }
+
+ nrBatchesToProcess = nrBatchesAfterFirst+1;
+ if (verb.nrBatchesToProcess>-1 && verb.nrBatchesToProcess<nrBatchesToProcess) {
+ nrBatchesToProcess=verb.nrBatchesToProcess;
+ }
+
+ if (batchSubNode!=null) {
+ for (i=0; i<nrBatchesToProcess; i++) {
+ location = (BatchLocation) batchLocations.get(i);
+
+ batchData.put("current", batchesData.get(i));
+ if (i>0)
+ batchData.put("previous", batchesData.get(i-1));
+ else
+ batchData.put("previous", null);
+
+ if (i<batchesData.size()-1)
+ batchData.put("next", batchesData.get(i+1));
+ else
+ batchData.put("next", null);
+
+ 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 );
+
+ batchSubNode.produce(aValueMap, aVerb, aLogger);
+ }
}
}
catch (Throwable t) {
}
};
- public Set buildVerbSet() {
- Set set;
+ private class BatchLocation {
+ int nrEntities;
+ int firstEntity;
- set = super.buildVerbSet();
- set.addAll(verbs.keySet());
+ public BatchLocation(int aFirstEntity, int aNrEntities) {
+ firstEntity = aFirstEntity;
+ nrEntities = aNrEntities;
+ }
+ }
- return set;
+ public Set buildVerbSet() {
+ return verbs.keySet();
};
- public void addVerb(String aVerb, String aWhereClause, String anOrderByClause) {
- verbs.put(aVerb, new EntityBatchingProducerNodeVerb(aWhereClause, anOrderByClause));
+ public void addVerb(String aVerb, int aNrPagesToGenerate) {
+ verbs.put(aVerb, new EntityBatchingProducerNodeVerb(aNrPagesToGenerate));
}
-
-// int nrPagesToGenerate
-//
-
private class EntityBatchingProducerNodeVerb {
- int nrPagesToGenerate;
+ int nrBatchesToProcess;
- EntityBatchingProducerNodeVerb(String aWhereClause, String anOrderByClause) {
- whereClause = aWhereClause;
- orderByClause = anOrderByClause;
+ EntityBatchingProducerNodeVerb(int aNrBatchesToProcess) {
+ nrBatchesToProcess = aNrBatchesToProcess;
}
}
}
-*/
\ No newline at end of file