001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not 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. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.model.control.forum.server.indexer; 018 019import com.echothree.model.control.core.common.MimeTypeUsageTypes; 020import com.echothree.model.control.forum.server.control.ForumControl; 021import com.echothree.model.control.index.common.IndexConstants; 022import com.echothree.model.control.index.common.IndexFieldVariations; 023import com.echothree.model.control.index.common.IndexFields; 024import com.echothree.model.control.index.server.analysis.ForumMessageAnalyzer; 025import com.echothree.model.control.index.server.indexer.BaseIndexer; 026import com.echothree.model.control.index.server.indexer.FieldTypes; 027import com.echothree.model.data.core.server.entity.EntityInstance; 028import com.echothree.model.data.core.server.entity.MimeTypeUsageType; 029import com.echothree.model.data.forum.server.entity.ForumClobMessagePart; 030import com.echothree.model.data.forum.server.entity.ForumForumThread; 031import com.echothree.model.data.forum.server.entity.ForumMessage; 032import com.echothree.model.data.forum.server.entity.ForumMessageDetail; 033import com.echothree.model.data.forum.server.entity.ForumMessagePart; 034import com.echothree.model.data.forum.server.entity.ForumMessageTypePartType; 035import com.echothree.model.data.forum.server.entity.ForumThread; 036import com.echothree.model.data.index.server.entity.Index; 037import com.echothree.util.server.message.ExecutionErrorAccumulator; 038import com.echothree.util.server.persistence.Session; 039import java.util.List; 040import org.apache.lucene.analysis.Analyzer; 041import org.apache.lucene.document.Document; 042import org.apache.lucene.document.Field; 043import org.apache.lucene.document.LongPoint; 044import org.apache.lucene.document.SortedDocValuesField; 045import org.apache.lucene.util.BytesRef; 046 047public class ForumMessageIndexer 048 extends BaseIndexer<ForumMessage> { 049 050 ForumControl forumControl = Session.getModelController(ForumControl.class); 051 052 /** Creates a new instance of ForumMessageIndexer */ 053 public ForumMessageIndexer(final ExecutionErrorAccumulator eea, final Index index) { 054 super(eea, index); 055 } 056 057 @Override 058 protected Analyzer getAnalyzer() { 059 return new ForumMessageAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes); 060 } 061 062 @Override 063 protected ForumMessage getEntity(final EntityInstance entityInstance) { 064 return forumControl.getForumMessageByEntityInstance(entityInstance); 065 } 066 067 @Override 068 protected Document convertToDocument(final EntityInstance entityInstance, final ForumMessage forumMessage) { 069 ForumMessageDetail forumMessageDetail = forumMessage.getLastDetail(); 070 ForumThread forumThread = forumMessageDetail.getForumThread(); 071 List<ForumForumThread> forumForumThreads = forumControl.getForumForumThreadsByForumThread(forumThread); 072 List<ForumMessageTypePartType> forumMessageTypePartTypes = forumControl.getForumMessageTypePartTypesByForumMessageTypeAndIncludeInIndex(forumMessageDetail.getForumMessageType()); 073 074 var document = newDocumentWithEntityInstanceFields(entityInstance, forumMessage.getPrimaryKey()); 075 076 document.add(new Field(IndexFields.forumMessageName.name(), forumMessageDetail.getForumMessageName(), FieldTypes.NOT_STORED_TOKENIZED)); 077 document.add(new Field(IndexFields.forumThreadName.name(), forumThread.getLastDetail().getForumThreadName(), FieldTypes.NOT_STORED_TOKENIZED)); 078 document.add(new LongPoint(IndexFields.postedTime.name(), forumMessageDetail.getPostedTime())); 079 080 forumMessageTypePartTypes.stream().map((forumMessageTypePartType) -> forumMessageTypePartType.getForumMessagePartType()).forEach((forumMessagePartType) -> { 081 ForumMessagePart forumMessagePart = forumControl.getBestForumMessagePart(forumMessage, forumMessagePartType, language); 082 if (forumMessagePart != null) { 083 MimeTypeUsageType mimeTypeUsageType = forumMessagePartType.getMimeTypeUsageType(); 084 085 if(mimeTypeUsageType == null) { 086 String forumMessagePartTypeName = forumMessagePartType.getForumMessagePartTypeName(); 087 String string = forumControl.getForumStringMessagePart(forumMessagePart).getString(); 088 089 document.add(new Field(forumMessagePartTypeName, string, FieldTypes.NOT_STORED_TOKENIZED)); 090 document.add(new SortedDocValuesField(forumMessagePartTypeName + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(), 091 new BytesRef(string))); 092 } else { 093 String mimeTypeUsageTypeName = mimeTypeUsageType.getMimeTypeUsageTypeName(); 094 095 if(mimeTypeUsageTypeName.equals(MimeTypeUsageTypes.TEXT.name())) { 096 ForumClobMessagePart forumClobMessagePart = forumControl.getForumClobMessagePart(forumMessagePart); 097 098 // TODO: mime type conversion to text/plain happens here 099 document.add(new Field(forumMessagePartType.getForumMessagePartTypeName(), forumClobMessagePart.getClob(), FieldTypes.NOT_STORED_TOKENIZED)); 100 } // Others are not supported at this time, DOCUMENT probably should be. 101 } 102 } 103 }); 104 105 if(forumForumThreads.size() > 0) { 106 StringBuilder forumNames = new StringBuilder(); 107 108 forumForumThreads.forEach((forumForumThread) -> { 109 if(forumNames.length() > 0) { 110 forumNames.append(' '); 111 } 112 113 forumNames.append(forumForumThread.getForum().getLastDetail().getForumName()); 114 }); 115 116 document.add(new Field(IndexFields.forumNames.name(), forumNames.toString(), FieldTypes.NOT_STORED_NOT_TOKENIZED)); 117 } 118 119 return document; 120 } 121 122}