001: /*
002:
003: * LIUS - Lucene Index Update and Search
004: * http://sourceforge.net/projects/lius/
005: *
006: * Copyright (c) 2005, Laval University Library. All rights reserved.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package ca.ulaval.bibl.lius.index.JavaObject;
024:
025: /**
026:
027: * @author Rida Benjelloun
028:
029: */
030:
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033: import java.util.ArrayList;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.util.Map;
037:
038: import org.apache.log4j.Logger;
039: import org.apache.lucene.document.Document;
040:
041: import ca.ulaval.bibl.lius.Lucene.LuceneActions;
042: import ca.ulaval.bibl.lius.config.LiusConfig;
043: import ca.ulaval.bibl.lius.config.LiusConfigBuilder;
044: import ca.ulaval.bibl.lius.config.LiusField;
045: import ca.ulaval.bibl.lius.index.Indexer;
046:
047: /**
048: *
049: * Classe permettant d'indexer des objets JavaBeans
050: *
051: * <br/><br/>
052: *
053: * Class for indexing JavaBeans objects.
054: *
055: * @author Rida Benjelloun (rida.benjelloun@bibl.ulaval.ca)
056: *
057: *
058: *
059: */
060:
061: public class BeanIndexer
062:
063: extends Indexer {
064:
065: static Logger logger = Logger.getRootLogger();
066:
067: /**
068: *
069: * Méthode retournant un objet de type Lucene document à partir du fichier à
070: *
071: * indexer et du fichier de configuration de Lius.
072: *
073: * <br/><br/>
074: *
075: * Method that returns a Lucene document object from the file to index and
076: *
077: * the Lius configuration file.
078: *
079: */
080:
081: public Document createLuceneDocument(Object bean,
082:
083: String liusXmlConfigFilePath) {
084:
085: LiusConfig lc = LiusConfigBuilder.getSingletonInstance().
086:
087: getLiusConfig(liusXmlConfigFilePath);
088:
089: Document doc = createLuceneDocument(bean, lc);
090:
091: return doc;
092:
093: }
094:
095: /**
096: *
097: * Méthode retournant un objet de type Lucene document à partir du fichier
098: *
099: * à indexer et du fichier de configuration de Lius exprimé sous forme
100: *
101: * d'objet de type LiusConfig.
102: *
103: * <br/><br/>
104: *
105: * Method that returns a Lucene document object from a file to index and
106: *
107: * the Lius configuration as a LiusConfig object.
108: *
109: */
110:
111: public Document createLuceneDocument(Object bean, LiusConfig lc) {
112:
113: Collection collRes = getPopulatedCollection(bean, lc);
114:
115: Document doc = LuceneActions.getSingletonInstance()
116: .populateLuceneDoc(collRes);
117:
118: return doc;
119:
120: }
121:
122: public Collection getLiusFields(LiusConfig lc) {
123:
124: return null;
125:
126: }
127:
128: public Collection getPopulatedCollection(Object bean,
129: Collection liusFields) {
130:
131: Collection collRes = new ArrayList();
132:
133: try {
134:
135: Class c = bean.getClass();
136:
137: Iterator i = liusFields.iterator();
138:
139: while (i.hasNext()) {
140:
141: Object field = i.next();
142:
143: if (field instanceof LiusField) {
144:
145: LiusField lf = (LiusField) field;
146:
147: Method m = c.getMethod(lf.getGetMethod(), null);
148:
149: Object o = m.invoke(bean, null);
150:
151: String value = o.toString();
152:
153: lf.setValue(value);
154:
155: collRes.add(lf);
156:
157: }
158:
159: else {
160:
161: collRes.add(field);
162:
163: }
164:
165: }
166:
167: }
168:
169: catch (NoSuchMethodException e) {
170:
171: logger.error(e.getMessage());
172:
173: }
174:
175: catch (IllegalAccessException e) {
176:
177: logger.error(e.getMessage());
178:
179: }
180:
181: catch (InvocationTargetException e) {
182:
183: logger.error(e.getMessage());
184:
185: }
186:
187: return collRes;
188:
189: }
190:
191: public Collection getPopulatedCollection(Object bean,
192: String liusConfig) {
193:
194: LiusConfig lc = LiusConfigBuilder.getSingletonInstance()
195: .getLiusConfig(
196:
197: liusConfig);
198:
199: return getPopulatedCollection(bean, lc);
200:
201: }
202:
203: public Collection getPopulatedCollection(Object bean, LiusConfig lc) {
204:
205: Class c = bean.getClass();
206:
207: Map config = lc.getJavaBeansFields();
208:
209: Document doc = null;
210:
211: Collection liusFields = (Collection) config.get(c.getName());
212:
213: return getPopulatedCollection(bean, liusFields);
214:
215: }
216:
217: public Document createLuceneDocument(String file, LiusConfig lc) {
218:
219: return null;
220:
221: }
222:
223: }
|