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