1 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 ; 58 import java.util.List ; 59 import java.util.Vector ; 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 76 public class CmsCosIndexer extends CmsMasterContent implements I_CmsIndexer { 77 78 79 public static final String C_PARAM_CHANNEL_DISPLAY_PARAM = "displayparam"; 80 81 82 public static final String C_PARAM_CHANNEL_DISPLAY_URI = "displayuri"; 83 84 85 private CmsMasterContent m_contentDefinition; 86 87 88 private CmsSearchIndex m_index; 89 90 91 private CmsSearchIndexSource m_indexSource; 92 93 94 private I_CmsReport m_report; 95 96 97 private int m_subId; 98 99 100 private CmsIndexingThreadManager m_threadManager; 101 102 103 private IndexWriter m_writer; 104 105 108 public void deleteResources(IndexReader reader, List resourcesToDelete) { 109 110 } 112 113 116 public A_CmsIndexResource getIndexResource(CmsObject cms, Document doc) throws CmsException { 117 118 Field f = null; 119 String 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 138 public int getSubId() { 139 140 return 0; 141 } 142 143 146 public CmsSearchIndexUpdateData getUpdateData(CmsSearchIndexSource source, List publishedResources) { 147 148 return new CmsSearchIndexUpdateData(source, this); 149 } 150 151 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 171 public A_CmsIndexResource readResource(CmsObject cms, Document doc) throws CmsException { 172 173 try { 174 String channel = doc.getField(CmsCosDocument.DOC_CHANNEL).stringValue(); 175 String path = doc.getField(I_CmsDocumentFactory.DOC_PATH).stringValue(); 176 String cdClass = doc.getField(CmsCosDocument.DOC_CONTENT_DEFINITION).stringValue(); 177 String contentId = doc.getField(CmsCosDocument.DOC_CONTENT_ID).stringValue(); 178 179 Class clazz = Class.forName(cdClass); 180 CmsMasterContent contentDefinition = (CmsMasterContent)clazz.getDeclaredConstructor( 181 new Class [] {org.opencms.file.CmsObject.class}).newInstance(new Object [] {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 exc) { 197 throw new CmsLegacyException("Instanciation of index resource failed", exc); 198 } 199 } 200 201 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 resourceType = (String )m_indexSource.getDocumentTypes().get(0); 212 CmsSearchDocumentType documentType = OpenCms.getSearchManager().getDocumentTypeConfig(resourceType); 213 String cdClassName = (String )documentType.getResourceTypes().get(0); 214 215 try { 216 m_contentDefinition = (CmsMasterContent)Class.forName(cdClassName).newInstance(); 217 } catch (Exception 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 resourceNames = source.getResourcesNames(); 225 Iterator it = resourceNames.iterator(); 226 while (it.hasNext()) { 227 String channel = (String )it.next(); 229 internalUpdateIndex(channel, channel); 230 } 231 } 232 233 236 public void updateResources(IndexWriter writer, CmsIndexingThreadManager threadManager, List resourcesToUpdate) { 237 238 } 240 241 249 protected CmsUUID getChannelId(CmsObject cms, String channelName) throws CmsIndexException { 250 251 String 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 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 273 protected void internalUpdateIndex(String channel, String root) throws CmsIndexException { 274 275 boolean channelReported = false; 276 CmsProject currentProject = null; 277 CmsRequestContext context = m_cms.getRequestContext(); 278 279 try { 280 currentProject = context.currentProject(); 282 context.setCurrentProject(m_cms.readProject(m_index.getProject())); 284 285 String channelId = getChannelId(m_cms, channel).toString(); 286 Vector subChannels = CmsMasterContent.getAllSubChannelsOf(m_cms, channel); 287 288 for (int i = 0; i < subChannels.size(); i++) { 290 291 String subChannel = (String )subChannels.get(i); 292 internalUpdateIndex(subChannel, root); 293 } 294 295 Vector resources = readAllByChannel(m_cms, channelId, m_subId); 297 for (Iterator 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 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 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 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 context.setCurrentProject(currentProject); 358 } 359 } 360 } | Popular Tags |