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.warehouse.server.search;
018
019import com.echothree.model.control.core.server.control.EntityInstanceControl;
020import com.echothree.model.control.index.common.IndexFields;
021import com.echothree.model.control.index.common.Indexes;
022import com.echothree.model.control.index.server.analyzer.BasicAnalyzer;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.search.PartySearchEvaluator;
025import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
026import com.echothree.model.control.warehouse.server.analyzer.WarehouseAnalyzer;
027import com.echothree.model.control.warehouse.server.control.WarehouseControl;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
030import com.echothree.model.data.search.server.entity.SearchSortDirection;
031import com.echothree.model.data.search.server.entity.SearchSortOrder;
032import com.echothree.model.data.search.server.entity.SearchType;
033import com.echothree.model.data.user.server.entity.UserVisit;
034import com.echothree.model.data.warehouse.server.entity.Warehouse;
035import com.echothree.util.server.message.ExecutionErrorAccumulator;
036import com.echothree.util.server.persistence.Session;
037
038public class WarehouseSearchEvaluator
039        extends PartySearchEvaluator {
040    
041    private String warehouseName;
042    
043    /** Creates a new instance of WarehouseSearchEvaluator */
044    public WarehouseSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator,
045            SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection) {
046        super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, PartyTypes.WAREHOUSE.name(),
047                IndexFields.warehouseName.name(), Indexes.WAREHOUSE.name());
048    }
049    
050    public String getWarehouseName() {
051        return warehouseName;
052    }
053    
054    public void setWarehouseName(String warehouseName) {
055        this.warehouseName = warehouseName;
056    }
057
058    @Override
059    public BasicAnalyzer getAnalyzer(final ExecutionErrorAccumulator eea, final Language language) {
060        return new WarehouseAnalyzer(eea, language, entityType, partyType, entityNameIndexField);
061    }
062
063    @Override
064    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
065        EntityInstancePKHolder resultSet = null;
066
067        if(warehouseName == null) {
068            resultSet = super.executeSearch(eea);
069        } else {
070            Warehouse warehouse = null;
071            
072            if(warehouseName != null) {
073                var warehouseControl = Session.getModelController(WarehouseControl.class);
074                
075                warehouse = warehouseControl.getWarehouseByName(warehouseName);
076            }
077            
078            if(warehouse != null) {
079                var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
080
081                resultSet = new EntityInstancePKHolder(1);
082                resultSet.add(entityInstanceControl.getEntityInstanceByBasePK(warehouse.getPartyPK()).getPrimaryKey(), null);
083            }
084        }
085        
086        return resultSet;
087    }
088    
089}