001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /* Created on Jul 16, 2003 */
019: package org.apache.roller.business.search.operations;
020:
021: import java.io.IOException;
022: import java.text.MessageFormat;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.lucene.index.IndexReader;
030: import org.apache.lucene.index.IndexWriter;
031: import org.apache.lucene.index.Term;
032: import org.apache.roller.RollerException;
033: import org.apache.roller.business.search.IndexManagerImpl;
034: import org.apache.roller.business.search.FieldConstants;
035: import org.apache.roller.business.search.IndexUtil;
036: import org.apache.roller.business.Roller;
037: import org.apache.roller.business.RollerFactory;
038: import org.apache.roller.business.WeblogManager;
039: import org.apache.roller.pojos.WeblogEntryData;
040: import org.apache.roller.pojos.WebsiteData;
041:
042: /**
043: * An index operation that rebuilds a given users index (or all indexes).
044: * @author Mindaugas Idzelis (min@idzelis.com)
045: */
046: public class RebuildWebsiteIndexOperation extends WriteToIndexOperation {
047:
048: //~ Static fields/initializers =============================================
049:
050: private static Log mLogger = LogFactory.getFactory().getInstance(
051: RebuildWebsiteIndexOperation.class);
052:
053: //~ Instance fields ========================================================
054:
055: private WebsiteData website;
056:
057: //~ Constructors ===========================================================
058:
059: /**
060: * Create a new operation that will recreate an index.
061: *
062: * @param website The website to rebuild the index for, or null for all users.
063: */
064: public RebuildWebsiteIndexOperation(IndexManagerImpl mgr,
065: WebsiteData website) {
066: super (mgr);
067: this .website = website;
068: }
069:
070: //~ Methods ================================================================
071:
072: public void doRun() {
073: Date start = new Date();
074:
075: IndexReader reader = beginDeleting();
076:
077: try {
078: if (reader != null) {
079: Term tWebsite = null;
080: if (website != null) {
081: tWebsite = IndexUtil.getTerm(
082: FieldConstants.WEBSITE_HANDLE, website
083: .getHandle());
084: }
085: if (tWebsite != null) {
086: reader.delete(tWebsite);
087: } else {
088: Term all = IndexUtil.getTerm(
089: FieldConstants.CONSTANT,
090: FieldConstants.CONSTANT_V);
091: reader.delete(all);
092: }
093: }
094: } catch (IOException e) {
095: mLogger.info("Problems deleting doc from index", e);
096: } finally {
097: endDeleting();
098: }
099:
100: IndexWriter writer = beginWriting();
101:
102: Roller roller = RollerFactory.getRoller();
103: try {
104: if (writer != null) {
105: WeblogManager weblogManager = roller.getWeblogManager();
106:
107: List entries = weblogManager.getWeblogEntries(website, // website
108: null, null, // startDate
109: new Date(), // endDate (don't index 'future' entries)
110: null, // catName
111: null, WeblogEntryData.PUBLISHED, // status
112: null, // sortby (null means pubTime)
113: null, 0, -1); // offset, length, locale
114:
115: for (Iterator wbItr = entries.iterator(); wbItr
116: .hasNext();) {
117: WeblogEntryData entry = (WeblogEntryData) wbItr
118: .next();
119: writer.addDocument(getDocument(entry));
120: mLogger.debug(MessageFormat.format(
121: "Indexed entry {0}: {1}", new Object[] {
122: entry.getPubTime(),
123: entry.getAnchor() }));
124: }
125: // release the database connection
126: roller.release();
127: }
128: } catch (Exception e) {
129: mLogger.error("ERROR adding doc to index", e);
130: } finally {
131: endWriting();
132: if (roller != null)
133: roller.release();
134: }
135:
136: Date end = new Date();
137: double length = (end.getTime() - start.getTime())
138: / (double) 1000;
139:
140: if (website == null) {
141: mLogger
142: .info("Completed rebuilding index for all users in '"
143: + length + "' secs");
144: } else {
145: mLogger
146: .info("Completed rebuilding index for website handle: '"
147: + website.getHandle()
148: + "' in '"
149: + length
150: + "' seconds");
151: }
152: }
153: }
|