KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > legacy > CmsCosIndexer


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsCosIndexer.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.15 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package com.opencms.legacy;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsRequestContext;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42 import org.opencms.report.I_CmsReport;
43 import org.opencms.search.A_CmsIndexResource;
44 import org.opencms.search.CmsIndexException;
45 import org.opencms.search.CmsSearchIndexUpdateData;
46 import org.opencms.search.CmsIndexingThreadManager;
47 import org.opencms.search.CmsSearchDocumentType;
48 import org.opencms.search.CmsSearchIndex;
49 import org.opencms.search.CmsSearchIndexSource;
50 import org.opencms.search.I_CmsIndexer;
51 import org.opencms.search.documents.I_CmsDocumentFactory;
52 import org.opencms.util.CmsStringUtil;
53 import org.opencms.util.CmsUUID;
54
55 import com.opencms.defaults.master.*;
56
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Vector JavaDoc;
60
61 import org.apache.lucene.document.Document;
62 import org.apache.lucene.document.Field;
63 import org.apache.lucene.index.IndexReader;
64 import org.apache.lucene.index.IndexWriter;
65
66 /**
67  * Implements the indexing of cos data.<p>
68  *
69  * @version $Revision: 1.15 $ $Date: 2006/03/27 14:53:03 $
70  * @author Carsten Weinholz (c.weinholz@alkacon.com)
71  * @author Thomas Weckert (t.weckert@alkacon.com)
72  * @since 5.3.1
73  *
74  * @deprecated Will not be supported past the OpenCms 6 release.
75  */

76 public class CmsCosIndexer extends CmsMasterContent implements I_CmsIndexer {
77
78     /** Constant for display parameter. */
79     public static final String JavaDoc C_PARAM_CHANNEL_DISPLAY_PARAM = "displayparam";
80
81     /** Constant for display uri. */
82     public static final String JavaDoc C_PARAM_CHANNEL_DISPLAY_URI = "displayuri";
83
84     /** General class to handle module content. */
85     private CmsMasterContent m_contentDefinition;
86
87     /** The current index. */
88     private CmsSearchIndex m_index;
89
90     /** The search index source. */
91     private CmsSearchIndexSource m_indexSource;
92
93     /** The report. */
94     private I_CmsReport m_report;
95
96     /** The id for identifying module content. */
97     private int m_subId;
98
99     /** The thread manager. */
100     private CmsIndexingThreadManager m_threadManager;
101
102     /** The index writer. */
103     private IndexWriter m_writer;
104
105     /**
106      * @see org.opencms.search.I_CmsIndexer#deleteResources(org.apache.lucene.index.IndexReader, java.util.List)
107      */

108     public void deleteResources(IndexReader reader, List JavaDoc resourcesToDelete) {
109
110         // NYI
111
}
112
113     /**
114      * @see org.opencms.search.I_CmsIndexer#getIndexResource(CmsObject, org.apache.lucene.document.Document)
115      */

116     public A_CmsIndexResource getIndexResource(CmsObject cms, Document doc) throws CmsException {
117
118         Field f = null;
119         String JavaDoc channel = null;
120         A_CmsIndexResource result = null;
121
122         if ((f = doc.getField(CmsCosDocument.DOC_CHANNEL)) != null) {
123             channel = f.stringValue();
124
125             if (channel != null) {
126                 result = readResource(cms, doc);
127             }
128         }
129
130         return result;
131     }
132
133     /**
134      * Just to fulfill implementation requirements of CmsMasterContent.<p>
135      *
136      * @see com.opencms.defaults.master.CmsMasterContent#getSubId()
137      */

138     public int getSubId() {
139
140         return 0;
141     }
142
143     /**
144      * @see org.opencms.search.I_CmsIndexer#getUpdateData(org.opencms.search.CmsSearchIndexSource, java.util.List)
145      */

146     public CmsSearchIndexUpdateData getUpdateData(CmsSearchIndexSource source, List JavaDoc publishedResources) {
147
148         return new CmsSearchIndexUpdateData(source, this);
149     }
150
151     /**
152      * @see org.opencms.search.I_CmsIndexer#newInstance(org.opencms.file.CmsObject, org.opencms.report.I_CmsReport, org.opencms.search.CmsSearchIndex)
153      */

154     public I_CmsIndexer newInstance(CmsObject cms, I_CmsReport report, CmsSearchIndex index) {
155
156         CmsCosIndexer result = new CmsCosIndexer();
157         result.m_cms = cms;
158         result.m_report = report;
159         result.m_index = index;
160         return result;
161     }
162
163     /**
164      * Reads the data of a cos resource specified by the given search document.<p>
165      *
166      * @param cms the cms object
167      * @param doc the document retrived from search index
168      * @return the cos data
169      * @throws CmsException if something goes wrong
170      */

171     public A_CmsIndexResource readResource(CmsObject cms, Document doc) throws CmsException {
172
173         try {
174             String JavaDoc channel = doc.getField(CmsCosDocument.DOC_CHANNEL).stringValue();
175             String JavaDoc path = doc.getField(I_CmsDocumentFactory.DOC_PATH).stringValue();
176             String JavaDoc cdClass = doc.getField(CmsCosDocument.DOC_CONTENT_DEFINITION).stringValue();
177             String JavaDoc contentId = doc.getField(CmsCosDocument.DOC_CONTENT_ID).stringValue();
178
179             Class JavaDoc clazz = Class.forName(cdClass);
180             CmsMasterContent contentDefinition = (CmsMasterContent)clazz.getDeclaredConstructor(
181                 new Class JavaDoc[] {org.opencms.file.CmsObject.class}).newInstance(new Object JavaDoc[] {cms});
182
183             CmsMasterDataSet ds = new CmsMasterDataSet();
184             CmsMasterContent.getDbAccessObject(contentDefinition.getSubId()).read(
185                 cms,
186                 contentDefinition,
187                 ds,
188                 new CmsUUID(contentId));
189
190             if (ds != null) {
191                 return new CmsCosIndexResource(ds, path, channel, cdClass);
192             }
193
194             return null;
195
196         } catch (Exception JavaDoc exc) {
197             throw new CmsLegacyException("Instanciation of index resource failed", exc);
198         }
199     }
200
201     /**
202      * @see org.opencms.search.I_CmsIndexer#rebuildIndex(org.apache.lucene.index.IndexWriter, org.opencms.search.CmsIndexingThreadManager, org.opencms.search.CmsSearchIndexSource)
203      */

204     public void rebuildIndex(IndexWriter writer, CmsIndexingThreadManager threadManager, CmsSearchIndexSource source)
205     throws CmsIndexException {
206
207         m_writer = writer;
208         m_indexSource = source;
209         m_threadManager = threadManager;
210
211         String JavaDoc resourceType = (String JavaDoc)m_indexSource.getDocumentTypes().get(0);
212         CmsSearchDocumentType documentType = OpenCms.getSearchManager().getDocumentTypeConfig(resourceType);
213         String JavaDoc cdClassName = (String JavaDoc)documentType.getResourceTypes().get(0);
214
215         try {
216             m_contentDefinition = (CmsMasterContent)Class.forName(cdClassName).newInstance();
217         } catch (Exception JavaDoc e) {
218             throw new CmsIndexException(Messages.get().container(
219                 Messages.ERR_COS_INDEXING_CONTENTS_OF_CLASS_1,
220                 cdClassName), e);
221         }
222         m_subId = m_contentDefinition.getSubId();
223
224         List JavaDoc resourceNames = source.getResourcesNames();
225         Iterator JavaDoc it = resourceNames.iterator();
226         while (it.hasNext()) {
227             // read the resources from all configured source folders
228
String JavaDoc channel = (String JavaDoc)it.next();
229             internalUpdateIndex(channel, channel);
230         }
231     }
232
233     /**
234      * @see org.opencms.search.I_CmsIndexer#updateResources(org.apache.lucene.index.IndexWriter, org.opencms.search.CmsIndexingThreadManager, java.util.List)
235      */

236     public void updateResources(IndexWriter writer, CmsIndexingThreadManager threadManager, List JavaDoc resourcesToUpdate) {
237
238         // NYI
239
}
240
241     /**
242      * Returns the uuid of the channel.<p>
243      *
244      * @param cms the current user's CmsObject
245      * @param channelName name of the channel
246      * @return the uuid of the channel
247      * @throws CmsIndexException if something goes wrong
248      */

249     protected CmsUUID getChannelId(CmsObject cms, String JavaDoc channelName) throws CmsIndexException {
250
251         String JavaDoc siteRoot = cms.getRequestContext().getSiteRoot();
252         CmsUUID id = null;
253         try {
254             cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
255             CmsResource channel = cms.readFolder(channelName, CmsResourceFilter.IGNORE_EXPIRATION);
256             id = channel.getResourceId();
257         } catch (Exception JavaDoc exc) {
258             throw new CmsIndexException(Messages.get().container(Messages.ERR_COS_ACCESS_CHANNEL_1, channelName), exc);
259         } finally {
260             cms.getRequestContext().setSiteRoot(siteRoot);
261         }
262         return id;
263     }
264
265     /**
266      * Creates new index entries for all cos resources below the given path.<p>
267      *
268      * @param channel the channel to index
269      * @param root the root channel
270      *
271      * @throws CmsIndexException if something goes wrong
272      */

273     protected void internalUpdateIndex(String JavaDoc channel, String JavaDoc root) throws CmsIndexException {
274
275         boolean channelReported = false;
276         CmsProject currentProject = null;
277         CmsRequestContext context = m_cms.getRequestContext();
278
279         try {
280             // save the current project
281
currentProject = context.currentProject();
282             // switch to the configured project
283
context.setCurrentProject(m_cms.readProject(m_index.getProject()));
284
285             String JavaDoc channelId = getChannelId(m_cms, channel).toString();
286             Vector JavaDoc subChannels = CmsMasterContent.getAllSubChannelsOf(m_cms, channel);
287
288             // index subchannels
289
for (int i = 0; i < subChannels.size(); i++) {
290
291                 String JavaDoc subChannel = (String JavaDoc)subChannels.get(i);
292                 internalUpdateIndex(subChannel, root);
293             }
294
295             // now index channel
296
Vector JavaDoc resources = readAllByChannel(m_cms, channelId, m_subId);
297             for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
298
299                 CmsMasterDataSet ds = (CmsMasterDataSet)i.next();
300
301                 if (m_report != null && !channelReported) {
302
303                     m_report.print(Messages.get().container(Messages.RPT_INDEX_CHANNEL_0), I_CmsReport.FORMAT_NOTE);
304                     m_report.println(org.opencms.report.Messages.get().container(
305                         org.opencms.report.Messages.RPT_ARGUMENT_1,
306                         channel));
307                     channelReported = true;
308                 }
309
310                 if (m_report != null) {
311                     m_report.print(org.opencms.report.Messages.get().container(
312                         org.opencms.report.Messages.RPT_SUCCESSION_1,
313                         String.valueOf(m_threadManager.getCounter() + 1)), I_CmsReport.FORMAT_NOTE);
314                     m_report.print(org.opencms.search.Messages.get().container(
315                         org.opencms.search.Messages.RPT_SEARCH_INDEXING_FILE_BEGIN_0), I_CmsReport.FORMAT_NOTE);
316                     if (ds.m_title != null) {
317                         String JavaDoc title = ds.m_title;
318                         title = CmsStringUtil.substitute(title, "'", "\\'");
319                         title = CmsStringUtil.substitute(title, "\"", "\\\"");
320                         m_report.print(org.opencms.report.Messages.get().container(
321                             org.opencms.report.Messages.RPT_ARGUMENT_1,
322                             title));
323                     }
324                     m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
325                 }
326
327                 String JavaDoc path = m_indexSource.getParam(C_PARAM_CHANNEL_DISPLAY_URI)
328                     + "?"
329                     + m_indexSource.getParam(C_PARAM_CHANNEL_DISPLAY_PARAM)
330                     + "="
331                     + ds.m_masterId;
332
333                 A_CmsIndexResource ires = new CmsCosIndexResource(
334                     ds,
335                     path,
336                     channel,
337                     m_contentDefinition.getClass().getName());
338                 m_threadManager.createIndexingThread(m_cms, m_writer, ires, m_index);
339             }
340         } catch (Exception JavaDoc exc) {
341
342             if (m_report != null) {
343                 m_report.println(
344                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
345                     I_CmsReport.FORMAT_WARNING);
346             }
347             if (CmsLog.getLog(this).isWarnEnabled()) {
348                 CmsLog.getLog(this).warn("Failed to index " + channel, exc);
349             }
350
351             throw new CmsIndexException(
352                 Messages.get().container(Messages.ERR_COS_INDEXING_CONTENTS_OF_CLASS_1, channel),
353                 exc);
354         } finally {
355
356             // switch back to the original project
357
context.setCurrentProject(currentProject);
358         }
359     }
360 }
Popular Tags