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