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.EntityAttributeAnalyzer;
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.indexer.BaseIndexer;
024import com.echothree.model.control.index.server.indexer.FieldTypes;
025import com.echothree.model.data.core.server.entity.EntityAttribute;
026import com.echothree.model.data.core.server.entity.EntityInstance;
027import com.echothree.model.data.index.server.entity.Index;
028import javax.enterprise.context.Dependent;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import org.apache.lucene.analysis.Analyzer;
031import org.apache.lucene.document.Document;
032import org.apache.lucene.document.Field;
033import org.apache.lucene.document.SortedDocValuesField;
034import org.apache.lucene.util.BytesRef;
035
036@Dependent
037public class EntityAttributeIndexer
038        extends BaseIndexer<EntityAttribute> {
039
040    @Override
041    public BaseIndexer<EntityAttribute> setup(final ExecutionErrorAccumulator eea, final Index index) {
042        return super.setup(eea, index);
043    }
044
045    @Override
046    protected Analyzer getAnalyzer() {
047        return new EntityAttributeAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
048    }
049    
050    @Override
051    protected EntityAttribute getEntity(final EntityInstance entityInstance) {
052        return coreControl.getEntityAttributeByEntityInstance(entityInstance);
053    }
054    
055    @Override
056    protected Document convertToDocument(final EntityInstance entityInstance, final EntityAttribute entityAttribute) {
057        var entityAttributeDetail = entityAttribute.getLastDetail();
058        var entityTypeDetail = entityAttributeDetail.getEntityType().getLastDetail();
059        var description = coreControl.getBestEntityAttributeDescription(entityAttribute, language);
060
061        var document = newDocumentWithEntityInstanceFields(entityInstance, entityAttribute.getPrimaryKey());
062
063        document.add(new Field(IndexFields.componentVendorName.name(),
064                entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), FieldTypes.NOT_STORED_TOKENIZED));
065        document.add(new SortedDocValuesField(IndexFields.componentVendorName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
066                new BytesRef(entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName())));
067        document.add(new Field(IndexFields.entityTypeName.name(),
068                entityTypeDetail.getEntityTypeName(), FieldTypes.NOT_STORED_TOKENIZED));
069        document.add(new SortedDocValuesField(IndexFields.entityTypeName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
070                new BytesRef(entityTypeDetail.getEntityTypeName())));
071        document.add(new Field(IndexFields.entityAttributeName.name(),
072                entityAttributeDetail.getEntityAttribute().getLastDetail().getEntityAttributeName(), FieldTypes.NOT_STORED_TOKENIZED));
073        document.add(new SortedDocValuesField(IndexFields.entityAttributeName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
074                new BytesRef(entityAttributeDetail.getEntityAttribute().getLastDetail().getEntityAttributeName())));
075        document.add(new Field(IndexFields.entityAttributeTypeName.name(),
076                entityAttributeDetail.getEntityAttributeType().getEntityAttributeTypeName(), FieldTypes.NOT_STORED_TOKENIZED));
077        document.add(new Field(IndexFields.trackRevisions.name(), entityAttributeDetail.getTrackRevisions().toString(), FieldTypes.NOT_STORED_NOT_TOKENIZED));
078
079        if(description != null) {
080            document.add(new Field(IndexFields.description.name(), description, FieldTypes.NOT_STORED_TOKENIZED));
081            document.add(new SortedDocValuesField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
082                    new BytesRef(description)));
083        }
084
085        return document;
086    }
087
088}