001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.content.server.indexer;
018
019import com.echothree.model.control.content.server.control.ContentControl;
020import com.echothree.model.control.index.common.IndexConstants;
021import com.echothree.model.control.index.common.IndexFieldVariations;
022import com.echothree.model.control.index.common.IndexFields;
023import com.echothree.model.control.content.server.analyzer.ContentCategoryAnalyzer;
024import com.echothree.model.control.index.server.indexer.BaseIndexer;
025import com.echothree.model.control.index.server.indexer.FieldTypes;
026import com.echothree.model.data.content.server.entity.ContentCategory;
027import com.echothree.model.data.core.server.entity.EntityInstance;
028import com.echothree.model.data.index.server.entity.Index;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.Session;
031import org.apache.lucene.analysis.Analyzer;
032import org.apache.lucene.document.Document;
033import org.apache.lucene.document.Field;
034import org.apache.lucene.document.SortedDocValuesField;
035import org.apache.lucene.util.BytesRef;
036
037public class ContentCategoryIndexer
038        extends BaseIndexer<ContentCategory> {
039    
040    ContentControl contentControl = Session.getModelController(ContentControl.class);
041
042    /** Creates a new instance of ContentCategoryIndexer */
043    public ContentCategoryIndexer(final ExecutionErrorAccumulator eea, final Index index) {
044        super(eea, index);
045    }
046
047    @Override
048    protected Analyzer getAnalyzer() {
049        return new ContentCategoryAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
050    }
051    
052    @Override
053    protected ContentCategory getEntity(final EntityInstance entityInstance) {
054        return contentControl.getContentCategoryByEntityInstance(entityInstance);
055    }
056    
057    @Override
058    protected Document convertToDocument(final EntityInstance entityInstance, final ContentCategory contentCategory) {
059        var contentCategoryDetail = contentCategory.getLastDetail();
060        var contentCatalogDetail = contentCategoryDetail.getContentCatalog().getLastDetail();
061        var parentContentCategory = contentCategoryDetail.getParentContentCategory();
062        var description = contentControl.getBestContentCategoryDescription(contentCategory, language);
063
064        var document = newDocumentWithEntityInstanceFields(entityInstance, contentCategory.getPrimaryKey());
065
066        document.add(new Field(IndexFields.contentCollectionName.name(), contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName(),
067                FieldTypes.NOT_STORED_TOKENIZED));
068        document.add(new SortedDocValuesField(IndexFields.contentCollectionName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
069                new BytesRef(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName())));
070        document.add(new Field(IndexFields.contentCatalogName.name(), contentCatalogDetail.getContentCatalogName(), FieldTypes.NOT_STORED_TOKENIZED));
071        document.add(new SortedDocValuesField(IndexFields.contentCatalogName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
072                new BytesRef(contentCatalogDetail.getContentCatalogName())));
073        document.add(new Field(IndexFields.contentCategoryName.name(), contentCategoryDetail.getContentCategoryName(), FieldTypes.NOT_STORED_TOKENIZED));
074        document.add(new SortedDocValuesField(IndexFields.contentCategoryName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
075                new BytesRef(contentCategoryDetail.getContentCategoryName())));
076        if(parentContentCategory != null) {
077            document.add(new Field(IndexFields.parentContentCategoryName.name(), parentContentCategory.getLastDetail().getContentCategoryName(),
078                    FieldTypes.NOT_STORED_TOKENIZED));
079        }
080        
081        if(description != null) {
082            document.add(new Field(IndexFields.description.name(), description, FieldTypes.NOT_STORED_TOKENIZED));
083            document.add(new SortedDocValuesField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
084                    new BytesRef(description)));
085        }
086
087        return document;
088    }
089
090}