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.model.control.vendor.server.logic;
018
019import com.echothree.control.user.vendor.common.spec.VendorItemUniversalSpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.vendor.common.exception.UnknownVendorItemNameException;
025import com.echothree.model.control.vendor.server.control.VendorControl;
026import com.echothree.model.data.vendor.server.entity.VendorItem;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.server.control.BaseLogic;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.EntityPermission;
031import com.echothree.util.server.validation.ParameterUtils;
032import javax.enterprise.context.ApplicationScoped;
033import javax.inject.Inject;
034
035@ApplicationScoped
036public class VendorItemLogic
037        extends BaseLogic {
038
039    @Inject
040    VendorControl vendorControl;
041
042    @Inject
043    EntityInstanceLogic entityInstanceLogic;
044
045    @Inject
046    VendorLogic vendorLogic;
047
048    protected VendorItemLogic() {
049        super();
050    }
051
052    public VendorItem getVendorItemByUniversalSpec(final ExecutionErrorAccumulator eea, final VendorItemUniversalSpec universalSpec,
053            final EntityPermission entityPermission) {
054        var vendorName = universalSpec.getVendorName();
055        var partyName = universalSpec.getPartyName();
056        var nameParameterCount = ParameterUtils.getInstance().countNonNullParameters(vendorName, partyName);
057        var possibleEntitySpecs = entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
058        VendorItem vendorItem = null;
059
060        if(nameParameterCount == 1 && possibleEntitySpecs == 0) {
061            var vendorItemName = universalSpec.getVendorItemName();
062
063            if(vendorItemName != null) {
064                var vendor = vendorLogic.getVendorByName(eea, vendorName, partyName, null);
065                var vendorParty = vendor.getParty();
066
067                vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName, entityPermission);
068
069                if(vendorItem == null) {
070                    handleExecutionError(UnknownVendorItemNameException.class, eea, ExecutionErrors.UnknownVendorItemName.name(),
071                            vendor.getVendorName(), vendorItemName);
072
073                }
074            } else {
075                handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
076            }
077        } else if(nameParameterCount == 0 && possibleEntitySpecs == 1) {
078            var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
079                    ComponentVendors.ECHO_THREE.name(), EntityTypes.VendorItem.name());
080
081            if(eea == null || !eea.hasExecutionErrors()) {
082                vendorItem = vendorControl.getVendorItemByEntityInstance(entityInstance, entityPermission);
083            }
084        } else {
085            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
086        }
087
088        return vendorItem;
089    }
090
091    public VendorItem getVendorItemByUniversalSpec(final ExecutionErrorAccumulator eea, final VendorItemUniversalSpec universalSpec) {
092        return getVendorItemByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
093    }
094
095    public VendorItem getVendorItemByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, final VendorItemUniversalSpec universalSpec) {
096        return getVendorItemByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
097    }
098
099}