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