KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > customer > CustomerSearch


1 /*
2  * Created on Oct 19, 2004
3  */

4 package com.openedit.store.customer;
5
6 import java.io.File JavaDoc;
7 import java.io.FilenameFilter JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.lucene.analysis.Analyzer;
12 import org.apache.lucene.document.Document;
13 import org.apache.lucene.document.Field;
14 import org.apache.lucene.document.Field.Index;
15 import org.apache.lucene.document.Field.Store;
16 import org.apache.lucene.index.IndexWriter;
17
18 import com.openedit.modules.search.BaseLuceneSearch;
19 import com.openedit.modules.search.RecordLookUpAnalyzer;
20 import com.openedit.store.CustomerArchive;
21 import com.openedit.users.User;
22 import com.openedit.util.FileUtils;
23 import com.openedit.util.PathUtilities;
24
25 /**
26  * @author cburkey
27  *
28  */

29 public class CustomerSearch extends BaseLuceneSearch
30 {
31     private static final Log log = LogFactory.getLog(CustomerSearch.class);
32     protected CustomerArchive fieldCustomerArchive;
33     
34     /* (non-javadoc)
35      * @see com.openedit.modules.search.BaseLuceneSearch#reIndexAll()
36      */

37     public void reIndexAll() throws Exception JavaDoc
38     {
39         log.info("Reindex of customer users directory");
40         //http://today.java.net/pub/a/today/2003/07/30/LuceneIntro.html?page=last&x-maxdepth=0
41

42         //Analyzer analyzer = new LuceneWordsAndNumbersAnalyzer();//org.apache.lucene.analysis.SimpleAnalyzer();//WhitespaceAnalyzer();//SimpleAnalyzer();//StopAnalyzer();
43
File JavaDoc indexDir = buildLiveIndexDir();
44         new FileUtils().deleteAll(indexDir);
45         indexDir.mkdirs();
46         IndexWriter writer = new IndexWriter(indexDir, getAnalyzer(), true);
47         writer.setMergeFactor(50);
48         // FIXME: Move this to XmlCustomerArchive, e.g. getAllUserNames()
49
File JavaDoc[] usersxml = getSearchDirectory().listFiles(new FilenameFilter JavaDoc()
50         {
51             public boolean accept(File JavaDoc inDir, String JavaDoc inName)
52             {
53                 if (inName.endsWith(".xml") )
54                 {
55                     return true;
56                 }
57                 return false;
58             }
59         });
60         
61         for (int i = 0; i < usersxml.length; i++)
62         {
63             Document doc = new Document();
64             File JavaDoc xconf = usersxml[i];
65             String JavaDoc username = PathUtilities.extractPageName(xconf.getPath());
66             doc.add( new Field( User.USERNAME_PROPERTY, username, Store.YES, Index.TOKENIZED ) );
67             Customer customer = getCustomerArchive().getCustomer(username);
68             //make sure its loaded
69
customer.getUser().getPassword();
70             
71             String JavaDoc phone = customer.cleanPhoneNumber();
72             if( phone != null)
73             {
74                 doc.add( new Field( "Phone1",phone , Store.YES, Index.TOKENIZED) ); //If tokenized then we use our lowercase analyser
75
}
76             
77             String JavaDoc last = customer.getUser().getLastName();
78             if( last != null)
79             {
80                 //TODO: We should not have to lower case this since we have a lower casing analyser
81
doc.add( new Field( "lastName",last, Store.YES, Index.TOKENIZED) );
82             }
83
84 //
85
// for (Iterator iter = customer.getUser().getProperties().keySet().iterator(); iter.hasNext();)
86
// {
87
// String key = (String) iter.next();
88
// String value = (String)customer.getUser().get(key);
89
// if ( value != null)
90
// {
91
// if ( key.equals( Customer.PHONE1 ) )
92
// {
93
// doc.add( new Field( key, customer.cleanPhoneNumber(), Store.YES, Index.NO_NORMS) );
94
// }
95
// else
96
// {
97
// doc.add( new Field( key, value, Store.YES, Index.NO_NORMS ) );
98
// }
99
// }
100
// }
101
writer.addDocument(doc);
102
103         }
104         writer.optimize();
105         writer.close();
106         
107     }
108
109     public Analyzer getAnalyzer()
110     {
111         if (fieldAnalyzer == null) {
112             fieldAnalyzer = new RecordLookUpAnalyzer();
113         }
114         return fieldAnalyzer;
115     }
116
117     public CustomerArchive getCustomerArchive()
118     {
119         return fieldCustomerArchive;
120     }
121     
122     public void setCustomerArchive(CustomerArchive inCustomerArchive)
123     {
124         fieldCustomerArchive = inCustomerArchive;
125     }
126 }
127
Popular Tags