View Javadoc

1   /*
2    * MMBase Lucene module
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  package net.sf.mmapps.modules.lucenesearch;
15  
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.lucene.document.Document;
27  import org.apache.lucene.document.Field;
28  import org.mmbase.bridge.Node;
29  import org.mmbase.bridge.NodeList;
30  
31  /***
32   * The Relation object, holds objects related to a table, these objects are tables also.
33   * 
34   * @author Wouter Heijke
35   * @version $Revision: 1.1 $
36   */
37  public class DataRelation {
38      private static Log log = LogFactory.getLog(DataRelation.class);
39  
40      private String tableName;
41  
42      private String type;
43  
44      private List fieldList = new ArrayList();
45  
46      private List relatedList = new ArrayList();
47  
48      /***
49       * Collects all related nodes and their fields
50       * 
51       * @param doc Lucene Document to put the collected fields in
52       * @param node Node of the parent, the starting node to find the related nodes from
53       */
54      protected void collectAll(Document doc, Node node) {
55          NodeList currentElements = node.getRelatedNodes(tableName);
56  
57          for (int i = 0; i < currentElements.size(); i++) {
58              Node currentNode = currentElements.getNode(i);
59  
60              for (int f = 0; f < fieldList.size(); f++) {
61                  DataField fld = (DataField) fieldList.get(f);
62                  if (fld != null) {
63                      Field result = null;
64                      try {
65                          result = fld.collectField(currentNode);
66                      } catch (IOException e) {
67                          log.warn("IO Problem: '" + e.getMessage() + "'");
68                      }
69                      if (result != null) {
70                          doc.add(result);
71                      }
72                  }
73              }
74  
75              for (int r = 0; r < relatedList.size(); r++) {
76                  DataRelation rel = (DataRelation) relatedList.get(r);
77                  rel.collectAll(doc, currentNode);
78              }
79          }
80      }
81  
82      /***
83       * Adds a field to this relation
84       * 
85       * @param field DataField object to add
86       */
87      public void addField(DataField field) {
88          fieldList.add(field);
89      }
90  
91      /***
92       * @return list of fields
93       */
94      public List getFields() {
95          return fieldList;
96      }
97  
98      /***
99       * @return collection of fields and fields of related elements
100      */
101     public Collection getAllFields() {
102         Set result = new HashSet();
103         result.addAll(getFields());
104         for (Iterator it = relatedList.iterator(); it.hasNext();) {
105             DataRelation rd = (DataRelation) it.next();
106             result.addAll(rd.getAllFields());
107         }
108         return result;
109     }
110 
111     /***
112      * @param relation
113      */
114     public void addRelated(DataRelation relation) {
115         relatedList.add(relation);
116     }
117 
118     /***
119      * @return The name of this Relation
120      */
121     public String getName() {
122         return tableName;
123     }
124 
125     /***
126      * @return The type of this Relation
127      */
128     public String getType() {
129         return type;
130     }
131 
132     /***
133      * @param string
134      */
135     public void setName(String string) {
136         tableName = string;
137     }
138 
139     /***
140      * @param string
141      */
142     public void setType(String string) {
143         type = string;
144     }
145 
146 }