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.item.server.indexer;
018
019import com.echothree.model.control.core.common.EntityAttributeTypes;
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.control.item.server.analyzer.HarmonizedTariffScheduleCodeAnalyzer;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.data.core.server.entity.EntityInstance;
028import com.echothree.model.data.index.server.entity.Index;
029import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCode;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
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
039@Dependent
040public class HarmonizedTariffScheduleCodeIndexer
041        extends BaseIndexer<HarmonizedTariffScheduleCode> {
042
043    @Inject
044    ItemControl itemControl;
045
046    @Override
047    public BaseIndexer<HarmonizedTariffScheduleCode> setup(final ExecutionErrorAccumulator eea, final Index index) {
048        return super.setup(eea, index);
049    }
050
051    @Override
052    protected Analyzer getAnalyzer() {
053        return new HarmonizedTariffScheduleCodeAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
054    }
055    
056    @Override
057    protected HarmonizedTariffScheduleCode getEntity(final EntityInstance entityInstance) {
058        return itemControl.getHarmonizedTariffScheduleCodeByEntityInstance(entityInstance);
059    }
060    
061    @Override
062    protected Document convertToDocument(final EntityInstance entityInstance, final HarmonizedTariffScheduleCode harmonizedTariffScheduleCode) {
063        var harmonizedTariffScheduleCodeDetail = harmonizedTariffScheduleCode.getLastDetail();
064        var harmonizedTariffScheduleCodeTranslation = itemControl.getBestHarmonizedTariffScheduleCodeTranslation(harmonizedTariffScheduleCode, language);
065
066        var document = newDocumentWithEntityInstanceFields(entityInstance, harmonizedTariffScheduleCode.getPrimaryKey());
067
068        document.add(new Field(IndexFields.countryGeoCodeName.name(), harmonizedTariffScheduleCodeDetail.getCountryGeoCode().getLastDetail().getGeoCodeName(), FieldTypes.NOT_STORED_TOKENIZED));
069        
070        document.add(new Field(IndexFields.harmonizedTariffScheduleCodeName.name(), harmonizedTariffScheduleCodeDetail.getHarmonizedTariffScheduleCodeName(), FieldTypes.NOT_STORED_TOKENIZED));
071        document.add(new SortedDocValuesField(IndexFields.harmonizedTariffScheduleCodeName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
072                new BytesRef(harmonizedTariffScheduleCodeDetail.getHarmonizedTariffScheduleCodeName())));
073
074        if(harmonizedTariffScheduleCodeTranslation != null) {
075            var description = harmonizedTariffScheduleCodeTranslation.getDescription();
076            var overview = harmonizedTariffScheduleCodeTranslation.getOverview();
077            
078            document.add(new Field(IndexFields.description.name(), description, FieldTypes.NOT_STORED_TOKENIZED));
079            document.add(new SortedDocValuesField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
080                    new BytesRef(description)));
081            
082            if(overview != null) {
083                if(harmonizedTariffScheduleCodeTranslation.getOverviewMimeType().getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.CLOB.name())) {
084                    // TODO: mime type conversion to text/plain happens here
085                    document.add(new Field(IndexFields.overview.name(), overview, FieldTypes.NOT_STORED_TOKENIZED));
086                }
087            }
088        }
089
090        return document;
091    }
092
093}