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.inventory.server.logic;
018
019import com.echothree.control.user.inventory.common.spec.InventoryCostingMethodUniversalSpec;
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.inventory.common.exception.DuplicateInventoryCostingMethodNameException;
025import com.echothree.model.control.inventory.common.exception.UnknownDefaultInventoryCostingMethodException;
026import com.echothree.model.control.inventory.common.exception.UnknownInventoryCostingMethodNameException;
027import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl;
028import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod;
029import com.echothree.model.data.inventory.server.value.InventoryCostingMethodDetailValue;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseLogic;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.EntityPermission;
036import javax.enterprise.context.ApplicationScoped;
037import javax.inject.Inject;
038
039@ApplicationScoped
040public class InventoryCostingMethodLogic
041    extends BaseLogic {
042
043    @Inject
044    InventoryCostingMethodControl inventoryCostingMethodControl;
045
046    @Inject
047    EntityInstanceLogic entityInstanceLogic;
048
049    protected InventoryCostingMethodLogic() {
050        super();
051    }
052
053    public InventoryCostingMethod createInventoryCostingMethod(final ExecutionErrorAccumulator eea, final String inventoryCostingMethodName,
054            final Boolean isDefault, final Integer sortOrder,
055            final Language language, final String description, final BasePK createdBy) {
056        var inventoryCostingMethod = inventoryCostingMethodControl.getInventoryCostingMethodByName(inventoryCostingMethodName);
057
058        if(inventoryCostingMethod == null) {
059            inventoryCostingMethod = inventoryCostingMethodControl.createInventoryCostingMethod(inventoryCostingMethodName,
060                    isDefault, sortOrder, createdBy);
061
062            if(description != null) {
063                inventoryCostingMethodControl.createInventoryCostingMethodDescription(inventoryCostingMethod, language, description, createdBy);
064            }
065        } else {
066            handleExecutionError(DuplicateInventoryCostingMethodNameException.class, eea, ExecutionErrors.DuplicateInventoryCostingMethodName.name(), inventoryCostingMethodName);
067        }
068
069        return inventoryCostingMethod;
070    }
071
072    public InventoryCostingMethod getInventoryCostingMethodByName(final ExecutionErrorAccumulator eea, final String inventoryCostingMethodName,
073            final EntityPermission entityPermission) {
074        var inventoryCostingMethod = inventoryCostingMethodControl.getInventoryCostingMethodByName(inventoryCostingMethodName, entityPermission);
075
076        if(inventoryCostingMethod == null) {
077            handleExecutionError(UnknownInventoryCostingMethodNameException.class, eea, ExecutionErrors.UnknownInventoryCostingMethodName.name(), inventoryCostingMethodName);
078        }
079
080        return inventoryCostingMethod;
081    }
082
083    public InventoryCostingMethod getInventoryCostingMethodByName(final ExecutionErrorAccumulator eea, final String inventoryCostingMethodName) {
084        return getInventoryCostingMethodByName(eea, inventoryCostingMethodName, EntityPermission.READ_ONLY);
085    }
086
087    public InventoryCostingMethod getInventoryCostingMethodByNameForUpdate(final ExecutionErrorAccumulator eea, final String inventoryCostingMethodName) {
088        return getInventoryCostingMethodByName(eea, inventoryCostingMethodName, EntityPermission.READ_WRITE);
089    }
090
091    public InventoryCostingMethod getDefaultInventoryCostingMethod(final ExecutionErrorAccumulator eea,
092            final EntityPermission entityPermission) {
093        var inventoryCostingMethod = inventoryCostingMethodControl.getDefaultInventoryCostingMethod(entityPermission);
094
095        if(inventoryCostingMethod == null) {
096            handleExecutionError(UnknownDefaultInventoryCostingMethodException.class, eea,
097                    ExecutionErrors.UnknownDefaultInventoryCostingMethod.name());
098        }
099
100        return inventoryCostingMethod;
101    }
102
103    public InventoryCostingMethod getDefaultInventoryCostingMethod(final ExecutionErrorAccumulator eea) {
104        return getDefaultInventoryCostingMethod(eea, EntityPermission.READ_ONLY);
105    }
106
107    public InventoryCostingMethod getInventoryCostingMethodByUniversalSpec(final ExecutionErrorAccumulator eea,
108            final InventoryCostingMethodUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
109        InventoryCostingMethod inventoryCostingMethod = null;
110        var inventoryCostingMethodName = universalSpec.getInventoryCostingMethodName();
111        var parameterCount = (inventoryCostingMethodName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
112
113        switch(parameterCount) {
114            case 0 -> {
115                if(allowDefault) {
116                    inventoryCostingMethod = getDefaultInventoryCostingMethod(eea, entityPermission);
117                } else {
118                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
119                }
120            }
121            case 1 -> {
122                if(inventoryCostingMethodName == null) {
123                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
124                            ComponentVendors.ECHO_THREE.name(), EntityTypes.InventoryCostingMethod.name());
125
126                    if(eea == null || !eea.hasExecutionErrors()) {
127                        inventoryCostingMethod = inventoryCostingMethodControl.getInventoryCostingMethodByEntityInstance(entityInstance, entityPermission);
128                    }
129                } else {
130                    inventoryCostingMethod = getInventoryCostingMethodByName(eea, inventoryCostingMethodName, entityPermission);
131                }
132            }
133            default ->
134                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
135        }
136
137        return inventoryCostingMethod;
138    }
139
140    public InventoryCostingMethod getInventoryCostingMethodByUniversalSpec(final ExecutionErrorAccumulator eea,
141            final InventoryCostingMethodUniversalSpec universalSpec, boolean allowDefault) {
142        return getInventoryCostingMethodByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
143    }
144
145    public InventoryCostingMethod getInventoryCostingMethodByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
146            final InventoryCostingMethodUniversalSpec universalSpec, boolean allowDefault) {
147        return getInventoryCostingMethodByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
148    }
149
150    public void updateInventoryCostingMethodFromValue(final InventoryCostingMethodDetailValue inventoryCostingMethodDetailValue,
151            final BasePK updatedBy) {
152        inventoryCostingMethodControl.updateInventoryCostingMethodFromValue(inventoryCostingMethodDetailValue, updatedBy);
153    }
154    
155    public void deleteInventoryCostingMethod(final ExecutionErrorAccumulator eea, final InventoryCostingMethod inventoryCostingMethod,
156            final BasePK deletedBy) {
157        inventoryCostingMethodControl.deleteInventoryCostingMethod(inventoryCostingMethod, deletedBy);
158    }
159
160}