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