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.model.control.inventory.server.control.InventoryTransactionRoleControl;
020import com.echothree.model.data.inventory.server.entity.InventoryTransactionLine;
021import com.echothree.model.data.inventory.server.entity.InventoryTransactionLineRole;
022import com.echothree.model.data.inventory.server.entity.InventoryTransactionRoleType;
023import com.echothree.model.data.party.server.entity.Party;
024import com.echothree.util.common.persistence.BasePK;
025import com.echothree.util.server.control.BaseLogic;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027import javax.enterprise.context.ApplicationScoped;
028import javax.inject.Inject;
029
030@ApplicationScoped
031public class InventoryTransactionLineRoleLogic
032        extends BaseLogic {
033
034    @Inject
035    InventoryTransactionRoleControl inventoryTransactionRoleControl;
036
037    @Inject
038    InventoryTransactionRoleTypeLogic inventoryTransactionRoleTypeLogic;
039
040    protected InventoryTransactionLineRoleLogic() {
041        super();
042    }
043
044    public InventoryTransactionLineRole createInventoryTransactionLineRole(final ExecutionErrorAccumulator eea,
045            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionRoleTypeName,
046            final Party party, final BasePK createdBy) {
047        var inventoryTransactionRoleType = getInventoryTransactionRoleTypeByName(eea, inventoryTransactionLine, inventoryTransactionRoleTypeName);
048        InventoryTransactionLineRole inventoryTransactionLineRole = null;
049
050        if(eea == null || !eea.hasExecutionErrors()) {
051            inventoryTransactionLineRole = createInventoryTransactionLineRole(inventoryTransactionLine, inventoryTransactionRoleType, party, createdBy);
052        }
053
054        return inventoryTransactionLineRole;
055    }
056
057    public InventoryTransactionLineRole createInventoryTransactionLineRole(final InventoryTransactionLine inventoryTransactionLine,
058            final InventoryTransactionRoleType inventoryTransactionRoleType, final Party party, final BasePK createdBy) {
059        return inventoryTransactionRoleControl.createInventoryTransactionLineRole(inventoryTransactionLine, inventoryTransactionRoleType, party, createdBy);
060    }
061
062    private InventoryTransactionRoleType getInventoryTransactionRoleTypeByName(final ExecutionErrorAccumulator eea,
063            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionRoleTypeName) {
064        return inventoryTransactionRoleTypeLogic.getInventoryTransactionRoleTypeByName(eea,
065                inventoryTransactionLine.getLastDetail().getInventoryTransaction().getLastDetail().getInventoryTransactionType(), inventoryTransactionRoleTypeName);
066    }
067
068    public void ensureInventoryTransactionLineRole(final ExecutionErrorAccumulator eea,
069            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionRoleTypeName,
070            final Party party, final BasePK createdBy) {
071        var inventoryTransactionRoleType = getInventoryTransactionRoleTypeByName(eea, inventoryTransactionLine, inventoryTransactionRoleTypeName);
072
073        if(eea == null || !eea.hasExecutionErrors()) {
074            if(!inventoryTransactionRoleControl.inventoryTransactionLineRoleExists(inventoryTransactionLine, inventoryTransactionRoleType, party)) {
075                inventoryTransactionRoleControl.createInventoryTransactionLineRole(inventoryTransactionLine, inventoryTransactionRoleType, party, createdBy);
076            }
077        }
078    }
079
080    public void deleteInventoryTransactionLineRole(final ExecutionErrorAccumulator eea, final InventoryTransactionLineRole inventoryTransactionLineRole,
081            final BasePK deletedBy) {
082        inventoryTransactionRoleControl.deleteInventoryTransactionLineRole(inventoryTransactionLineRole, deletedBy);
083    }
084
085}