001: package net.jforum.tools.search;
002:
003: import jargs.gnu.CmdLineParser;
004:
005: import java.text.ParseException;
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008:
009: import net.jforum.ConfigLoader;
010: import net.jforum.ForumStartup;
011: import net.jforum.search.LuceneReindexArgs;
012: import net.jforum.search.LuceneReindexer;
013: import net.jforum.search.LuceneSettings;
014: import net.jforum.search.SearchFacade;
015: import net.jforum.util.preferences.ConfigKeys;
016: import net.jforum.util.preferences.SystemGlobals;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.apache.log4j.xml.DOMConfigurator;
020:
021: /**
022: * @author Rafael Steil
023: * @version $Id: LuceneCommandLineReindexer.java,v 1.1 2007/09/01 15:10:32 rafaelsteil Exp $
024: */
025: public class LuceneCommandLineReindexer {
026: private LuceneReindexArgs reindexerArgs;
027: private boolean recreate;
028: private String path;
029:
030: public static void main(String[] args) {
031: LuceneCommandLineReindexer reindexer = new LuceneCommandLineReindexer();
032: reindexer.init(args);
033:
034: System.out.println("*** INITIALIZING \n");
035: System.out
036: .println("** At any time, press CTRL+C to stop the process \n");
037:
038: reindexer.start();
039:
040: System.out.println("*** FINISHED ");
041: }
042:
043: private void start() {
044: LuceneReindexer reindexer = new LuceneReindexer(
045: (LuceneSettings) SystemGlobals
046: .getObjectValue(ConfigKeys.LUCENE_SETTINGS),
047: this .reindexerArgs, this .recreate);
048:
049: reindexer.startProcess();
050: }
051:
052: private void init(String[] args) {
053: this .parseCmdArgs(args);
054:
055: DOMConfigurator.configure(this .path + "/WEB-INF/log4j.xml");
056:
057: ConfigLoader.startSystemglobals(this .path);
058:
059: SystemGlobals.loadQueries(SystemGlobals
060: .getValue(ConfigKeys.SQL_QUERIES_GENERIC));
061: SystemGlobals.loadQueries(SystemGlobals
062: .getValue(ConfigKeys.SQL_QUERIES_DRIVER));
063:
064: ConfigLoader.createLoginAuthenticator();
065: ConfigLoader.loadDaoImplementation();
066:
067: SearchFacade.init();
068:
069: ForumStartup.startDatabase();
070: }
071:
072: private void parseCmdArgs(String[] args) {
073: StringBuffer description = new StringBuffer(512);
074: description
075: .append("\n*** Going to reindex using the following options: \n");
076:
077: CmdLineParser parser = new CmdLineParser();
078:
079: CmdLineParser.Option recreateOption = parser
080: .addBooleanOption("recreateIndex");
081: CmdLineParser.Option typeOption = parser
082: .addStringOption("type");
083: CmdLineParser.Option pathOption = parser
084: .addStringOption("path");
085: CmdLineParser.Option firstPostIdOption = parser
086: .addIntegerOption("firstPostId");
087: CmdLineParser.Option lastPostIdOption = parser
088: .addIntegerOption("lastPostId");
089: CmdLineParser.Option fromDateOption = parser
090: .addStringOption("fromDate");
091: CmdLineParser.Option toDateOption = parser
092: .addStringOption("toDate");
093: CmdLineParser.Option avoidDuplicatedOption = parser
094: .addBooleanOption("avoidDuplicatedRecords");
095:
096: try {
097: parser.parse(args);
098: } catch (CmdLineParser.OptionException e) {
099: System.out.println(e.getMessage());
100: this .printUsage();
101: }
102:
103: if (parser.getRemainingArgs().length > 0) {
104: this .printUsage();
105: }
106:
107: // Type
108: String type = (String) parser.getOptionValue(typeOption);
109:
110: if (StringUtils.isEmpty(type)
111: || (!type.equals("date") && !type.equals("message"))) {
112: System.out
113: .println("*** --type should be either date or message");
114: this .printUsage();
115: }
116:
117: description.append("\t-> Searching by ").append(type).append(
118: '\n');
119:
120: // Path
121: this .path = (String) parser.getOptionValue(pathOption);
122:
123: if (StringUtils.isEmpty(this .path)) {
124: System.out
125: .println("*** --path is a required option. It should point to the root directory where JForum is installed");
126: this .printUsage();
127: }
128:
129: description.append("\t->App path: ").append(path).append('\n');
130:
131: // FirstPostId and LastPostId
132: int firstPostId = ((Integer) parser.getOptionValue(
133: firstPostIdOption, new Integer(0))).intValue();
134: int lastPostId = ((Integer) parser.getOptionValue(
135: lastPostIdOption, new Integer(0))).intValue();
136:
137: if ("message".equals(type)) {
138: if (firstPostId == 0 || lastPostId == 0
139: || lastPostId <= firstPostId) {
140: System.out
141: .println("*** --firstPostId and --lastPostId are required fields when --type=message. "
142: + "Also, --lastPostId should be greater than --firstPostId");
143: this .printUsage();
144: }
145:
146: description.append("\t-> From Post #").append(firstPostId)
147: .append(" to Post #").append(lastPostId).append(
148: '\n');
149: }
150:
151: // FromDate and ToDate
152: Date fromDate = null;
153: Date toDate = null;
154:
155: if ("date".equals(type)) {
156: fromDate = this .parseDate((String) parser
157: .getOptionValue(fromDateOption));
158: toDate = this .parseDate((String) parser
159: .getOptionValue(toDateOption));
160:
161: if (fromDate == null || toDate == null) {
162: System.out
163: .println("*** --fromDate and --toDate are required fields when --type=date");
164: this .printUsage();
165: }
166:
167: description.append("\t-> From date ").append(fromDate)
168: .append(" to ").append(toDate).append('\n');
169: }
170:
171: // Recreate
172: this .recreate = ((Boolean) parser.getOptionValue(
173: recreateOption, Boolean.FALSE)).booleanValue();
174: description.append("\t->Recreate the index? ").append(
175: this .recreate ? "Yes" : "No").append('\n');
176:
177: // AvoidDuplicatedRecords
178: boolean avoidDuplicated = ((Boolean) parser.getOptionValue(
179: avoidDuplicatedOption, Boolean.FALSE)).booleanValue();
180: description.append("\t->Avoid duplicated records? ").append(
181: avoidDuplicated ? "Yes" : "No").append('\n');
182:
183: this .reindexerArgs = new LuceneReindexArgs(fromDate, toDate,
184: firstPostId, lastPostId, avoidDuplicated, "date"
185: .equals(type) ? LuceneReindexArgs.TYPE_DATE
186: : LuceneReindexArgs.TYPE_MESSAGE);
187:
188: System.out.println(description);
189: }
190:
191: private Date parseDate(String s) {
192: Date date = null;
193:
194: if (!StringUtils.isEmpty(s)) {
195: try {
196: date = new SimpleDateFormat("dd/MM/yyyy").parse(s);
197: } catch (ParseException e) {
198: }
199: }
200:
201: return date;
202: }
203:
204: private void printUsage() {
205: System.out.println("\nUsage: LuceneCommandLineReindexer \n"
206: + " --path full_path_to_JForum_root_directory \n"
207: + " --type {date|message} \n"
208: + " --firstPostId a_id \n" + " --lastPostId a_id \n"
209: + " --fromDate dd/MM/yyyy \n"
210: + " --toDate dd/MM/yyyy \n" + " [--recreateIndex]\n"
211: + " [--avoidDuplicatedRecords]");
212: System.exit(1);
213: }
214: }
|