/*
* Copyright (c) www.bugull.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.tooan.ynpay.third.mongodb.lucene.backend;
import net.tooan.ynpay.third.mongodb.cache.FieldsCache;
import net.tooan.ynpay.third.mongodb.lucene.annotations.BoostSwitch;
import net.tooan.ynpay.third.mongodb.lucene.handler.FieldHandler;
import net.tooan.ynpay.third.mongodb.lucene.handler.FieldHandlerFactory;
import net.tooan.ynpay.third.mongodb.mapper.FieldUtil;
import org.apache.lucene.document.Document;
import java.lang.reflect.Field;
/**
* @author Frank Wen(xbwen@hotmail.com)
*/
public class IndexCreator {
protected Object obj;
protected String prefix;
public IndexCreator(Object obj, String prefix) {
this.obj = obj;
this.prefix = prefix;
}
public void create(Document doc) {
Field[] fields = FieldsCache.getInstance().get(obj.getClass());
for (Field f : fields) {
BoostSwitch bs = f.getAnnotation(BoostSwitch.class);
if (bs != null) {
CompareChecker checker = new CompareChecker(obj);
boolean fit = checker.isFit(f, bs.compare(), bs.value());
if (fit) {
doc.setBoost(bs.fit());
} else {
doc.setBoost(bs.unfit());
}
}
Object objValue = FieldUtil.get(obj, f);
if (objValue != null) {
FieldHandler handler = FieldHandlerFactory.create(obj, f, prefix);
if (handler != null) {
handler.handle(doc);
}
}
}
}
}