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.core.server.indexer;
018
019import com.echothree.model.control.index.common.IndexConstants;
020import com.echothree.model.control.index.common.IndexFieldVariations;
021import com.echothree.model.control.index.common.IndexFields;
022import com.echothree.model.control.core.server.analyzer.EntityAttributeGroupAnalyzer;
023import com.echothree.model.control.index.server.indexer.BaseIndexer;
024import com.echothree.model.control.index.server.indexer.FieldTypes;
025import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
026import com.echothree.model.data.core.server.entity.EntityInstance;
027import com.echothree.model.data.index.server.entity.Index;
028import com.echothree.util.server.message.ExecutionErrorAccumulator;
029import org.apache.lucene.analysis.Analyzer;
030import org.apache.lucene.document.Document;
031import org.apache.lucene.document.Field;
032import org.apache.lucene.document.SortedDocValuesField;
033import org.apache.lucene.util.BytesRef;
034
035public class EntityAttributeGroupIndexer
036        extends BaseIndexer<EntityAttributeGroup> {
037    
038    /** Creates a new instance of EntityAttributeGroupIndexer */
039    public EntityAttributeGroupIndexer(final ExecutionErrorAccumulator eea, final Index index) {
040        super(eea, index);
041    }
042
043    @Override
044    protected Analyzer getAnalyzer() {
045        return new EntityAttributeGroupAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
046    }
047    
048    @Override
049    protected EntityAttributeGroup getEntity(final EntityInstance entityInstance) {
050        return coreControl.getEntityAttributeGroupByEntityInstance(entityInstance);
051    }
052    
053    @Override
054    protected Document convertToDocument(final EntityInstance entityInstance, final EntityAttributeGroup entityAttributeGroup) {
055        var entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
056        var description = coreControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, language);
057
058        var document = newDocumentWithEntityInstanceFields(entityInstance, entityAttributeGroup.getPrimaryKey());
059
060        document.add(new Field(IndexFields.entityAttributeGroupName.name(),
061                entityAttributeGroupDetail.getEntityAttributeGroup().getLastDetail().getEntityAttributeGroupName(), FieldTypes.NOT_STORED_TOKENIZED));
062        document.add(new SortedDocValuesField(IndexFields.entityAttributeGroupName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
063                new BytesRef(entityAttributeGroupDetail.getEntityAttributeGroup().getLastDetail().getEntityAttributeGroupName())));
064
065        if(description != null) {
066            document.add(new Field(IndexFields.description.name(), description, FieldTypes.NOT_STORED_TOKENIZED));
067            document.add(new SortedDocValuesField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
068                    new BytesRef(description)));
069        }
070
071        return document;
072    }
073
074}