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