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.control.user.inventory.server.command.common;
018
019import com.echothree.control.user.inventory.common.spec.PartyInventoryLevelSpec;
020import com.echothree.model.control.party.common.PartyTypes;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.warehouse.server.control.WarehouseControl;
023import com.echothree.model.data.party.server.entity.Party;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.server.message.ExecutionErrorAccumulator;
026import javax.enterprise.context.ApplicationScoped;
027import javax.inject.Inject;
028
029@ApplicationScoped
030public class PartyInventoryLevelUtil {
031
032    @Inject
033    PartyControl partyControl;
034    
035    @Inject
036    WarehouseControl warehouseControl;
037
038    protected PartyInventoryLevelUtil() {
039        super();
040    }
041
042    public String getPartyTypeName(final Party party) {
043        return party.getLastDetail().getPartyType().getPartyTypeName();
044    }
045
046    public Party getParty(final ExecutionErrorAccumulator eea, final String partyName, final String companyName, final String warehouseName) {
047        Party party = null;
048        
049        if(partyName != null || companyName != null) {
050            if(partyName != null) {
051                party = partyControl.getPartyByName(partyName);
052                
053                if(party != null) {
054                    var partyTypeName = getPartyTypeName(party);
055                    
056                    if(!partyTypeName.equals(PartyTypes.COMPANY.name())
057                    && !partyTypeName.equals(PartyTypes.WAREHOUSE.name())) {
058                        party = null;
059                        eea.addExecutionError(ExecutionErrors.InvalidPartyType.name());
060                    }
061                }  else {
062                    eea.addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
063                }
064            } else if(companyName != null) {
065                var partyCompany = partyControl.getPartyCompanyByName(companyName);
066                
067                if(partyCompany != null) {
068                    party = partyCompany.getParty();
069                } else {
070                    eea.addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
071                }
072            }
073        } else if(warehouseName != null) {
074            var warehouse = warehouseControl.getWarehouseByName(warehouseName);
075            
076            if(warehouse != null) {
077                party = warehouse.getParty();
078            } else {
079                eea.addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
080            }
081        }
082        return party;
083    }
084
085    public Party getParty(final ExecutionErrorAccumulator eea, PartyInventoryLevelSpec spec) {
086        var partyName = spec.getPartyName();
087        var companyName = spec.getCompanyName();
088        var warehouseName = spec.getWarehouseName();
089        var parameterCount = (partyName == null ? 0 : 1) + (companyName == null ? 0 : 1) + (warehouseName == null ? 0 : 1);
090        Party party = null;
091        
092        if(parameterCount == 1) {
093            party = getParty(eea, partyName, companyName, warehouseName);
094        }  else {
095            eea.addExecutionError(ExecutionErrors.InvalidParameterCount.name());
096        }
097        
098        return party;
099    }
100
101}