001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.core.server.analyzer.EntityAttributeGroupAnalyzer;
020import com.echothree.model.control.core.server.control.EntityAttributeGroupControl;
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.indexer.BaseIndexer;
025import com.echothree.model.control.index.server.indexer.FieldTypes;
026import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
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 javax.enterprise.context.Dependent;
031import javax.inject.Inject;
032import org.apache.lucene.analysis.Analyzer;
033import org.apache.lucene.document.Document;
034import org.apache.lucene.document.Field;
035import org.apache.lucene.document.SortedDocValuesField;
036import org.apache.lucene.util.BytesRef;
037
038@Dependent
039public class EntityAttributeGroupIndexer
040        extends BaseIndexer<EntityAttributeGroup> {
041
042    @Inject
043    protected EntityAttributeGroupControl entityAttributeGroupControl;
044
045    @Override
046    public BaseIndexer<EntityAttributeGroup> setup(final ExecutionErrorAccumulator eea, final Index index) {
047        return super.setup(eea, index);
048    }
049
050    @Override
051    protected Analyzer getAnalyzer() {
052        return new EntityAttributeGroupAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
053    }
054    
055    @Override
056    protected EntityAttributeGroup getEntity(final EntityInstance entityInstance) {
057        return entityAttributeGroupControl.getEntityAttributeGroupByEntityInstance(entityInstance);
058    }
059    
060    @Override
061    protected Document convertToDocument(final EntityInstance entityInstance, final EntityAttributeGroup entityAttributeGroup) {
062        var entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
063        var entityTypeDetail = entityAttributeGroupDetail.getEntityType().getLastDetail();
064        var description = entityAttributeGroupControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, language);
065
066        var document = newDocumentWithEntityInstanceFields(entityInstance, entityAttributeGroup.getPrimaryKey());
067
068        document.add(new Field(IndexFields.componentVendorName.name(),
069                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), FieldTypes.NOT_STORED_TOKENIZED));
070        document.add(new SortedDocValuesField(IndexFields.componentVendorName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
071                new BytesRef(entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName())));
072        document.add(new Field(IndexFields.entityTypeName.name(),
073                entityTypeDetail.getEntityTypeName(), FieldTypes.NOT_STORED_TOKENIZED));
074        document.add(new SortedDocValuesField(IndexFields.entityTypeName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
075                new BytesRef(entityTypeDetail.getEntityTypeName())));
076        document.add(new Field(IndexFields.entityAttributeGroupName.name(),
077                entityAttributeGroupDetail.getEntityAttributeGroup().getLastDetail().getEntityAttributeGroupName(), FieldTypes.NOT_STORED_TOKENIZED));
078        document.add(new SortedDocValuesField(IndexFields.entityAttributeGroupName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
079                new BytesRef(entityAttributeGroupDetail.getEntityAttributeGroup().getLastDetail().getEntityAttributeGroupName())));
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}