001: /*
002: * Created on Oct 19, 2004
003: */
004: package org.openedit.store.customer;
005:
006: import java.io.File;
007: import java.io.FilenameFilter;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.apache.lucene.analysis.Analyzer;
012: import org.apache.lucene.document.Document;
013: import org.apache.lucene.document.Field;
014: import org.apache.lucene.document.Field.Index;
015: import org.apache.lucene.document.Field.Store;
016: import org.apache.lucene.index.IndexWriter;
017: import org.openedit.store.CustomerArchive;
018:
019: import com.openedit.OpenEditException;
020: import com.openedit.modules.search.BaseLuceneSearch;
021: import com.openedit.modules.search.RecordLookUpAnalyzer;
022: import com.openedit.users.User;
023: import com.openedit.util.FileUtils;
024: import com.openedit.util.PathUtilities;
025:
026: /**
027: * @author cburkey
028: *
029: */
030: public class CustomerSearch extends BaseLuceneSearch {
031: private static final Log log = LogFactory
032: .getLog(CustomerSearch.class);
033: protected CustomerArchive fieldCustomerArchive;
034:
035: /* (non-javadoc)
036: * @see com.openedit.modules.search.BaseLuceneSearch#reIndexAll()
037: */
038: public void reIndexAll() throws OpenEditException {
039: log.info("Reindex of customer users directory");
040: //http://today.java.net/pub/a/today/2003/07/30/LuceneIntro.html?page=last&x-maxdepth=0
041:
042: //Analyzer analyzer = new LuceneWordsAndNumbersAnalyzer();//org.apache.lucene.analysis.SimpleAnalyzer();//WhitespaceAnalyzer();//SimpleAnalyzer();//StopAnalyzer();
043: File indexDir = buildLiveIndexDir();
044: new FileUtils().deleteAll(indexDir);
045: indexDir.mkdirs();
046: IndexWriter writer = null;
047: try {
048: writer = new IndexWriter(indexDir, getAnalyzer(), true);
049: writer.setMergeFactor(50);
050: // FIXME: Move this to XmlCustomerArchive, e.g. getAllUserNames()
051: File[] usersxml = getSearchDirectory().listFiles(
052: new FilenameFilter() {
053: public boolean accept(File inDir, String inName) {
054: if (inName.endsWith(".xml")) {
055: return true;
056: }
057: return false;
058: }
059: });
060:
061: for (int i = 0; i < usersxml.length; i++) {
062: Document doc = new Document();
063: File xconf = usersxml[i];
064: String username = PathUtilities.extractPageName(xconf
065: .getPath());
066: doc.add(new Field(User.USERNAME_PROPERTY, username,
067: Store.YES, Index.TOKENIZED));
068: Customer customer = getCustomerArchive().getCustomer(
069: username);
070: //make sure its loaded
071: customer.getUser().getPassword();
072:
073: String phone = customer.cleanPhoneNumber();
074: if (phone != null) {
075: doc.add(new Field("Phone1", phone, Store.YES,
076: Index.TOKENIZED)); //If tokenized then we use our lowercase analyser
077: }
078:
079: String last = customer.getUser().getLastName();
080: if (last != null) {
081: //TODO: We should not have to lower case this since we have a lower casing analyser
082: doc.add(new Field("lastName", last, Store.YES,
083: Index.TOKENIZED));
084: }
085:
086: //
087: // for (Iterator iter = customer.getUser().getProperties().keySet().iterator(); iter.hasNext();)
088: // {
089: // String key = (String) iter.next();
090: // String value = (String)customer.getUser().get(key);
091: // if ( value != null)
092: // {
093: // if ( key.equals( Customer.PHONE1 ) )
094: // {
095: // doc.add( new Field( key, customer.cleanPhoneNumber(), Store.YES, Index.NO_NORMS) );
096: // }
097: // else
098: // {
099: // doc.add( new Field( key, value, Store.YES, Index.NO_NORMS ) );
100: // }
101: // }
102: // }
103: writer.addDocument(doc);
104:
105: }
106: writer.optimize();
107: writer.close();
108: } catch (Exception e) {
109: // TODO Auto-generated catch block
110:
111: throw new OpenEditException(e);
112: }
113:
114: }
115:
116: public Analyzer getAnalyzer() {
117: if (fieldAnalyzer == null) {
118: fieldAnalyzer = new RecordLookUpAnalyzer();
119: }
120: return fieldAnalyzer;
121: }
122:
123: public CustomerArchive getCustomerArchive() {
124: return fieldCustomerArchive;
125: }
126:
127: public void setCustomerArchive(CustomerArchive inCustomerArchive) {
128: fieldCustomerArchive = inCustomerArchive;
129: }
130: }
|