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.content.server.analyzer;
018
019import com.echothree.model.control.core.common.ComponentVendors;
020import com.echothree.model.control.core.common.EntityTypes;
021import com.echothree.model.control.core.common.MimeTypeUsageTypes;
022import com.echothree.model.control.core.server.logic.EntityTypeLogic;
023import com.echothree.model.control.index.common.IndexConstants;
024import com.echothree.model.control.index.common.IndexFieldVariations;
025import com.echothree.model.control.index.common.IndexFields;
026import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
027import com.echothree.model.control.index.server.analyzer.DictionaryAnalyzer;
028import com.echothree.model.control.index.server.analyzer.WhitespaceLowerCaseAnalyzer;
029import com.echothree.model.control.item.server.control.ItemControl;
030import com.echothree.model.data.core.server.entity.EntityAliasType;
031import com.echothree.model.data.core.server.entity.EntityAttribute;
032import com.echothree.model.data.core.server.entity.EntityType;
033import com.echothree.model.data.item.server.entity.ItemDescriptionType;
034import com.echothree.model.data.party.server.entity.Language;
035import com.echothree.model.data.tag.server.entity.TagScope;
036import com.echothree.util.server.message.ExecutionErrorAccumulator;
037import com.echothree.util.server.persistence.Session;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041import java.util.Set;
042import org.apache.lucene.analysis.Analyzer;
043
044public class ContentCatalogItemAnalyzer
045        extends BasicAnalyzer {
046
047    public ContentCatalogItemAnalyzer(final ExecutionErrorAccumulator eea, final Language language,
048            final EntityType entityType, final List<EntityAliasType> entityAliasTypes,
049            final List<EntityAttribute> entityAttributes, final List<TagScope> tagScopes) {
050        super(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
051    }
052
053    public ContentCatalogItemAnalyzer(final ExecutionErrorAccumulator eea, final Language language, final EntityType entityType) {
054        super(eea, language, entityType);
055    }
056    
057    protected Map<String, Analyzer> getItemAliasTypeAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
058        var itemControl = Session.getModelController(ItemControl.class);
059        
060        itemControl.getItemAliasTypes().forEach(itemAliasType ->
061                fieldAnalyzers.put(itemAliasType.getLastDetail().getItemAliasTypeName(), new WhitespaceLowerCaseAnalyzer())
062        );
063        
064        return fieldAnalyzers;
065    }
066    
067    /**
068     * Adds dictionary analyzers for item description types that allow text content.
069     *
070     * @param fieldAnalyzers A Map of field Analyzers.
071     * @return the field Analyzer map with additional Analyzers for ItemDescriptionTypes added.
072     */
073    protected Map<String, Analyzer> getItemDescriptionAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
074        var itemControl = Session.getModelController(ItemControl.class);
075        
076        itemControl.getItemDescriptionTypes().stream().map(ItemDescriptionType::getLastDetail).forEach((itemDescriptionTypeDetail) -> {
077            var mimeTypeUsageType = itemDescriptionTypeDetail.getMimeTypeUsageType();
078            if (mimeTypeUsageType == null || mimeTypeUsageType.getMimeTypeUsageTypeName().equals(MimeTypeUsageTypes.TEXT.name())) {
079                fieldAnalyzers.put(itemDescriptionTypeDetail.getItemDescriptionTypeName() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.dictionary.name(),
080                        new DictionaryAnalyzer());
081            }
082        });
083        
084        return fieldAnalyzers;
085    }
086    
087    @Override
088    protected Map<String, Analyzer> getEntityTypeFieldAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
089        super.getEntityTypeFieldAnalyzers(fieldAnalyzers);
090
091        // Items
092        fieldAnalyzers.put(IndexFields.aliases.name(), new WhitespaceLowerCaseAnalyzer());
093        fieldAnalyzers.put(IndexFields.itemName.name(), new WhitespaceLowerCaseAnalyzer());
094        fieldAnalyzers.put(IndexFields.itemNameAndAliases.name(), new WhitespaceLowerCaseAnalyzer());
095        fieldAnalyzers.put(IndexFields.itemTypeName.name(), new WhitespaceLowerCaseAnalyzer());
096        fieldAnalyzers.put(IndexFields.itemUseTypeName.name(), new WhitespaceLowerCaseAnalyzer());
097        fieldAnalyzers.put(IndexFields.itemDeliveryTypeName.name(), new WhitespaceLowerCaseAnalyzer());
098        fieldAnalyzers.put(IndexFields.itemInventoryTypeName.name(), new WhitespaceLowerCaseAnalyzer());
099        fieldAnalyzers.put(IndexFields.inventorySerialized.name(), new WhitespaceLowerCaseAnalyzer());
100        fieldAnalyzers.put(IndexFields.shippingChargeExempt.name(), new WhitespaceLowerCaseAnalyzer());
101        fieldAnalyzers.put(IndexFields.allowClubDiscounts.name(), new WhitespaceLowerCaseAnalyzer());
102        fieldAnalyzers.put(IndexFields.allowCouponDiscounts.name(), new WhitespaceLowerCaseAnalyzer());
103        fieldAnalyzers.put(IndexFields.allowAssociatePayments.name(), new WhitespaceLowerCaseAnalyzer());
104        fieldAnalyzers.put(IndexFields.unitOfMeasureKindName.name(), new WhitespaceLowerCaseAnalyzer());
105        fieldAnalyzers.put(IndexFields.itemPriceTypeName.name(), new WhitespaceLowerCaseAnalyzer());
106
107        // Content Catalogs
108        fieldAnalyzers.put(IndexFields.contentCollectionName.name(), new WhitespaceLowerCaseAnalyzer());
109        fieldAnalyzers.put(IndexFields.contentCatalogName.name(), new WhitespaceLowerCaseAnalyzer());
110
111        // Content Category Items
112        fieldAnalyzers.put(IndexFields.inventoryConditionName.name(), new WhitespaceLowerCaseAnalyzer());
113        fieldAnalyzers.put(IndexFields.unitOfMeasureKindName.name(), new WhitespaceLowerCaseAnalyzer());
114        fieldAnalyzers.put(IndexFields.unitOfMeasureTypeName.name(), new WhitespaceLowerCaseAnalyzer());
115        fieldAnalyzers.put(IndexFields.currencyIsoName.name(), new WhitespaceLowerCaseAnalyzer());
116        fieldAnalyzers.put(IndexFields.contentCategoryNames.name(), new WhitespaceLowerCaseAnalyzer());
117
118        return getItemDescriptionAnalyzers(getItemAliasTypeAnalyzers(fieldAnalyzers));
119    }
120
121    @Override
122    protected Map<String, Analyzer> getFieldAnalyzers(final ExecutionErrorAccumulator eea, final EntityType entityType,
123            final List<EntityAttribute> entityAttributes, final List<TagScope> tagScopes) {
124        var fieldAnalyzers = new HashMap<String, Analyzer>();
125
126        // Items
127        fieldAnalyzers.putAll(super.getFieldAnalyzers(eea, EntityTypeLogic.getInstance().getEntityTypeByName(eea,
128                ComponentVendors.ECHO_THREE.name(), EntityTypes.Item.name()), entityAttributes, tagScopes));
129
130        // Content Catalogs
131        fieldAnalyzers.putAll(super.getFieldAnalyzers(eea, EntityTypeLogic.getInstance().getEntityTypeByName(eea,
132                ComponentVendors.ECHO_THREE.name(), EntityTypes.ContentCatalog.name()), entityAttributes, tagScopes));
133
134        // Content Category Items
135        fieldAnalyzers.putAll(super.getFieldAnalyzers(eea, entityType, entityAttributes, tagScopes));
136
137        return fieldAnalyzers;
138
139
140    }
141
142    @Override
143    public Set<String> getLongFields() {
144        return Set.of(
145                IndexFields.unitPrice.name(),
146                IndexFields.minimumUnitPrice.name(),
147                IndexFields.maximumUnitPrice.name(),
148                IndexFields.unitPriceIncrement.name()
149        );
150    }
151
152}