1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.mmapps.modules.lucenesearch;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.mmbase.bridge.Cloud;
28 import org.mmbase.bridge.Node;
29 import org.mmbase.bridge.NodeList;
30 import org.mmbase.bridge.NodeManager;
31
32 /***
33 * Represents a Table (aka Builder) in MMBase and it's fields and relations
34 *
35 * @author Wouter Heijke
36 * @version $Revision: 1.1 $
37 */
38 public class SourceTable {
39 private static Log log = LogFactory.getLog(SourceTable.class);
40
41
42 private String tableName;
43
44
45 private List fieldList = new ArrayList();
46
47
48 private List relatedList = new ArrayList();
49
50 /***
51 * Collect all nodes in the table this object represents
52 *
53 * @param cloud MMBase cloud to use for indexing
54 * @param writer Lucene document writer
55 */
56 protected void collectAll(Cloud cloud, IndexHelper writer) {
57 NodeManager nm = cloud.getNodeManager(tableName);
58 NodeList currentElements = nm.getList(null, null, null);
59
60 for (int i = 0; i < currentElements.size(); i++) {
61 Node currentNode = currentElements.getNode(i);
62 collectOne(currentNode, writer);
63 }
64 }
65
66 /***
67 * Collect and index a single MMBase Node
68 *
69 * @param node MMBase node to index
70 * @param writer Lucene writer to use for indexing this node
71 */
72 public void collectOne(Node node, IndexHelper writer) {
73 try {
74 Document doc = new Document();
75 int number = node.getNumber();
76
77
78 doc.add(Field.Keyword("builder", tableName));
79 doc.add(Field.Keyword("node", String.valueOf(number)));
80
81 for (int f = 0; f < fieldList.size(); f++) {
82 DataField fld = (DataField) fieldList.get(f);
83 if (fld != null) {
84 Field result = fld.collectField(node);
85 if (result != null) {
86 doc.add(result);
87 }
88 }
89 }
90
91 for (int r = 0; r < relatedList.size(); r++) {
92 DataRelation rel = (DataRelation) relatedList.get(r);
93 rel.collectAll(doc, node);
94 }
95
96 writer.write(doc);
97 } catch (Exception e) {
98 log.error("IO Problem: '" + e.getMessage() + "' on: '" + tableName + "'");
99 }
100 }
101
102 /***
103 * Set's the name of the MMBase builder/table this object represents
104 *
105 * @param newName Name of the builder/table
106 */
107 public void setName(String newName) {
108 tableName = newName;
109 }
110
111 /***
112 * Get's the name of the MMBase builder/table this object represents
113 *
114 * @return name The name
115 */
116 public String getName() {
117 return tableName;
118 }
119
120 /***
121 * Add a field from this table
122 *
123 * @param field Field object representing information about the field
124 */
125 public void addField(DataField field) {
126 fieldList.add(field);
127 }
128
129 /***
130 * @return list of fields
131 */
132 public List getFields() {
133 return fieldList;
134 }
135
136 /***
137 * @return collection of fields and fields of related elements
138 */
139 public Collection getAllFields() {
140 Set result = new HashSet();
141 result.addAll(getFields());
142 for (Iterator it = relatedList.iterator(); it.hasNext();) {
143 DataRelation rd = (DataRelation) it.next();
144 result.addAll(rd.getAllFields());
145 }
146 return result;
147 }
148
149 /***
150 * Add a related table
151 *
152 * @param relation DataRelation object holding the information on the related table
153 */
154 public void addRelated(DataRelation relation) {
155 log.info("RELATED: " + relation.getName());
156 relatedList.add(relation);
157 }
158
159 /***
160 * @return list of related elements
161 */
162 public List getRelated() {
163 return relatedList;
164 }
165 }