prelim euskalherria localizer added + misc. work on producer nodes
[mir.git] / source / mir / producer / EntityBatchingProducerNode.java
1 /*package mir.producer;
2
3 import java.util.*;
4 import java.io.*;
5 import mir.entity.adapter.*;
6 import mir.entity.*;
7 import mir.storage.*;
8 import mir.util.*;
9
10 //      abstract public int getSize(String where) throws SQLException,StorageObjectException;
11
12
13 public class EntityBatchingProducerNode implements ProducerNode {
14   private Map verbs;
15   private EntityBatchingProducerNodeVerb defaultVerb;
16
17   private String key;
18   private StorageObject storage;
19   private EntityAdapterDefinition definition;
20   private String whereClause;
21   private String orderByClause;
22   private int nrArticlesPerBatch;
23   private int minNrArticlesInFirstBatch;
24   private ProducerNode batchSubNode;
25   private ProducerNode batchListSubNode;
26
27   public EntityBatchingProducerNode(
28         String aKey,
29         StorageObject aStorage,
30         EntityAdapterDefinition aDefinition,
31         String aWhereClause,
32         String anOrderByClause,
33         int aNrArticlesPerBatch,
34         int aMinNrArticlesInFirstBatch,
35         ProducerNode aBatchSubNode,
36         ProducerNode aBatchListSubNode) {
37
38     batchSubNode = aBatchSubNode;
39     batchListSubNode = aBatchListSubNode;
40
41     defaultVerb = null;
42     verbs = new HashMap();
43
44     key = aKey;
45     storage = aStorage;
46     definition = aDefinition;
47     whereClause = aWhereClause;
48     orderByClause = anOrderByClause;
49     nrArticlesPerBatch = aNrArticlesPerBatch;
50     minNrArticlesInFirstBatch = aMinNrArticlesInFirstBatch;
51   }
52
53   public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
54     EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb);
55     Iterator browser;
56
57     if (verb==null)
58       verb = defaultVerb;
59
60     if (verb==null)
61       throw new ProducerFailure("EntityBatchingProducerNode: unknown verb '"+aVerb+"'", null);
62
63     try {
64       browser = new EntityIteratorAdapter(
65           storage,
66           ParameterExpander.expandExpression( aValueMap, verb.whereClause ),
67           ParameterExpander.expandExpression( aValueMap, verb.orderByClause ),
68           -1,
69           definition );
70
71       while (browser.hasNext()) {
72         aLogger.println( verb.whereClause + ": next... ");
73         aValueMap.put(key, browser.next());
74         super.produce(aValueMap, aVerb, aLogger);
75       }
76     }
77     catch (Throwable t) {
78       throw new ProducerFailure(t.getMessage(), t);
79     }
80   };
81
82   public Set buildVerbSet() {
83     Set set;
84
85     set = super.buildVerbSet();
86     set.addAll(verbs.keySet());
87
88     return set;
89   };
90
91   public void addVerb(String aVerb, String aWhereClause, String anOrderByClause) {
92     verbs.put(aVerb, new EntityBatchingProducerNodeVerb(aWhereClause, anOrderByClause));
93   }
94
95
96 // int nrPagesToGenerate
97 //
98
99   private class EntityBatchingProducerNodeVerb {
100     int nrPagesToGenerate;
101
102     EntityBatchingProducerNodeVerb(String aWhereClause, String anOrderByClause) {
103       whereClause = aWhereClause;
104       orderByClause = anOrderByClause;
105     }
106   }
107 }
108
109 */