001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.party.server.analyzer;
018
019import com.echothree.model.control.index.common.IndexFields;
020import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
021import com.echothree.model.control.index.server.analyzer.WhitespaceLowerCaseAnalyzer;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.core.server.entity.EntityAliasType;
024import com.echothree.model.data.core.server.entity.EntityAttribute;
025import com.echothree.model.data.core.server.entity.EntityType;
026import com.echothree.model.data.party.server.entity.Language;
027import com.echothree.model.data.party.server.entity.PartyType;
028import com.echothree.model.data.tag.server.entity.TagScope;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.Session;
031import java.util.List;
032import java.util.Map;
033import org.apache.lucene.analysis.Analyzer;
034
035public class PartyAnalyzer
036        extends BasicAnalyzer {
037
038    private PartyType partyType;
039    private String entityNameIndexField;
040
041    private void init(final PartyType partyType, final String entityNameIndexField) {
042        this.partyType = partyType;
043        this.entityNameIndexField = entityNameIndexField;
044    }
045
046    public PartyAnalyzer(final ExecutionErrorAccumulator eea, final Language language, final EntityType entityType, final List<EntityAliasType> entityAliasTypes, final List<EntityAttribute> entityAttributes,
047            final List<TagScope> tagScopes, final PartyType partyType, final String entityNameIndexField) {
048        super(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
049
050        init(partyType, entityNameIndexField);
051    }
052
053    public PartyAnalyzer(final ExecutionErrorAccumulator eea, final Language language, final EntityType entityType,
054            final PartyType partyType, final String entityNameIndexField) {
055        super(eea, language, entityType);
056
057        init(partyType, entityNameIndexField);
058    }
059
060    protected Map<String, Analyzer> getPartyAliasTypeAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
061        var partyControl = Session.getModelController(PartyControl.class);
062
063        partyControl.getPartyAliasTypes(partyType).forEach((partyAliasType)->
064                fieldAnalyzers.put(partyAliasType.getLastDetail().getPartyAliasTypeName(),
065                        new WhitespaceLowerCaseAnalyzer()));
066
067        return fieldAnalyzers;
068    }
069
070    protected Map<String, Analyzer> getAdditionalAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
071        return fieldAnalyzers;
072    }
073
074    @Override
075    protected Map<String, Analyzer> getEntityTypeFieldAnalyzers(final Map<String, Analyzer> fieldAnalyzers) {
076        super.getEntityTypeFieldAnalyzers(fieldAnalyzers);
077        
078        fieldAnalyzers.put(IndexFields.partyName.name(), new WhitespaceLowerCaseAnalyzer());
079        fieldAnalyzers.put(entityNameIndexField, new WhitespaceLowerCaseAnalyzer());
080
081        return getAdditionalAnalyzers(getPartyAliasTypeAnalyzers(fieldAnalyzers));
082    }
083    
084}