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.analyzer;
018
019import com.echothree.model.control.core.common.MimeTypeUsageTypes;
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.analyzer.BasicAnalyzer;
024import com.echothree.model.control.index.server.analyzer.DictionaryAnalyzer;
025import com.echothree.model.control.index.server.analyzer.WhitespaceLowerCaseAnalyzer;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.data.core.server.entity.EntityAliasType;
028import com.echothree.model.data.core.server.entity.EntityAttribute;
029import com.echothree.model.data.core.server.entity.EntityType;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.tag.server.entity.TagScope;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034import java.util.List;
035import java.util.Map;
036import org.apache.lucene.analysis.Analyzer;
037
038public class ItemAnalyzer
039        extends BasicAnalyzer {
040    
041    public ItemAnalyzer(final ExecutionErrorAccumulator eea, final Language language, final EntityType entityType, final List<EntityAliasType> entityAliasTypes, final List<EntityAttribute> entityAttributes,
042            final List<TagScope> tagScopes) {
043        super(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
044    }
045
046    public ItemAnalyzer(final ExecutionErrorAccumulator eea, final Language language, final EntityType entityType) {
047        super(eea, language, entityType);
048    }
049    
050    protected Map<String, Analyzer> getItemAliasTypeAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
051        var itemControl = Session.getModelController(ItemControl.class);
052        
053        itemControl.getItemAliasTypes().forEach((itemAliasType) -> {
054            fieldAnalyzers.put(itemAliasType.getLastDetail().getItemAliasTypeName(), new WhitespaceLowerCaseAnalyzer());
055        });
056        
057        return fieldAnalyzers;
058    }
059    
060    /**
061     * Adds dictionary analyzers for item description types that allow text content.
062     *
063     * @param fieldAnalyzers A Map of field Analyzers.
064     * @return the field Analyzer map with additional Analyzers for ItemDescriptionTypes added.
065     */
066    protected Map<String, Analyzer> getItemDescriptionAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
067        var itemControl = Session.getModelController(ItemControl.class);
068        
069        itemControl.getItemDescriptionTypes().stream().map((itemDescriptionType) -> itemDescriptionType.getLastDetail()).forEach((itemDescriptionTypeDetail) -> {
070            var mimeTypeUsageType = itemDescriptionTypeDetail.getMimeTypeUsageType();
071            if (mimeTypeUsageType == null || mimeTypeUsageType.getMimeTypeUsageTypeName().equals(MimeTypeUsageTypes.TEXT.name())) {
072                fieldAnalyzers.put(itemDescriptionTypeDetail.getItemDescriptionTypeName() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.dictionary.name(),
073                        new DictionaryAnalyzer());
074            }
075        });
076        
077        return fieldAnalyzers;
078    }
079    
080    @Override
081    protected Map<String, Analyzer> getEntityTypeFieldAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
082        super.getEntityTypeFieldAnalyzers(fieldAnalyzers);
083        
084        fieldAnalyzers.put(IndexFields.aliases.name(), new WhitespaceLowerCaseAnalyzer());
085        fieldAnalyzers.put(IndexFields.itemName.name(), new WhitespaceLowerCaseAnalyzer());
086        fieldAnalyzers.put(IndexFields.itemNameAndAliases.name(), new WhitespaceLowerCaseAnalyzer());
087        fieldAnalyzers.put(IndexFields.itemTypeName.name(), new WhitespaceLowerCaseAnalyzer());
088        fieldAnalyzers.put(IndexFields.itemUseTypeName.name(), new WhitespaceLowerCaseAnalyzer());
089        fieldAnalyzers.put(IndexFields.itemDeliveryTypeName.name(), new WhitespaceLowerCaseAnalyzer());
090        fieldAnalyzers.put(IndexFields.itemInventoryTypeName.name(), new WhitespaceLowerCaseAnalyzer());
091        fieldAnalyzers.put(IndexFields.inventorySerialized.name(), new WhitespaceLowerCaseAnalyzer());
092        fieldAnalyzers.put(IndexFields.shippingChargeExempt.name(), new WhitespaceLowerCaseAnalyzer());
093        fieldAnalyzers.put(IndexFields.allowClubDiscounts.name(), new WhitespaceLowerCaseAnalyzer());
094        fieldAnalyzers.put(IndexFields.allowCouponDiscounts.name(), new WhitespaceLowerCaseAnalyzer());
095        fieldAnalyzers.put(IndexFields.allowAssociatePayments.name(), new WhitespaceLowerCaseAnalyzer());
096        fieldAnalyzers.put(IndexFields.unitOfMeasureKindName.name(), new WhitespaceLowerCaseAnalyzer());
097        fieldAnalyzers.put(IndexFields.itemPriceTypeName.name(), new WhitespaceLowerCaseAnalyzer());
098
099        return getItemDescriptionAnalyzers(getItemAliasTypeAnalyzers(fieldAnalyzers));
100    }
101    
102}