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.InventoryTransferTransactionUniversalSpec;
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.DuplicateInventoryTransferTransactionNameException;
025import com.echothree.model.control.inventory.common.exception.InvalidInventoryTransactionTypeException;
026import com.echothree.model.control.inventory.common.exception.UnknownInventoryTransferTransactionNameException;
027import com.echothree.model.control.inventory.server.control.InventoryTransferTransactionControl;
028import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
029import com.echothree.model.control.workflow.server.control.WorkflowControl;
030import com.echothree.model.data.inventory.server.entity.InventoryTransaction;
031import com.echothree.model.data.inventory.server.entity.InventoryTransactionType;
032import com.echothree.model.data.inventory.server.entity.InventoryTransferTransaction;
033import com.echothree.model.data.inventory.server.value.InventoryTransferTransactionDetailValue;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.persistence.BasePK;
036import com.echothree.util.server.control.BaseLogic;
037import com.echothree.util.server.message.ExecutionErrorAccumulator;
038import com.echothree.util.server.persistence.EntityPermission;
039import javax.enterprise.context.ApplicationScoped;
040import javax.inject.Inject;
041
042@ApplicationScoped
043public class InventoryTransferTransactionLogic
044    extends BaseLogic {
045
046    @Inject
047    InventoryTransferTransactionControl inventoryTransferTransactionControl;
048
049    @Inject
050    WorkflowControl workflowControl;
051
052    @Inject
053    EntityInstanceLogic entityInstanceLogic;
054
055    @Inject
056    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
057
058    @Inject
059    SequenceGeneratorLogic sequenceGeneratorLogic;
060
061    protected InventoryTransferTransactionLogic() {
062        super();
063    }
064
065    // --------------------------------------------------------------------------------
066    //   Inventory Transfer Transactions
067    // --------------------------------------------------------------------------------
068
069    public InventoryTransferTransaction createInventoryTransferTransaction(final ExecutionErrorAccumulator eea,
070            final String inventoryTransactionTypeName, String inventoryTransferTransactionName,
071            final InventoryTransaction sourceInventoryTransaction, final InventoryTransaction destinationInventoryTransaction,
072            final String reference, final String description, final BasePK createdBy) {
073        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(eea, inventoryTransactionTypeName);
074        InventoryTransferTransaction inventoryTransferTransaction = null;
075
076        if(eea == null || !eea.hasExecutionErrors()) {
077            if(inventoryTransferTransactionName == null) {
078                var inventoryTransferTransactionSequenceType = inventoryTransactionType.getLastDetail().getInventoryTransferTransactionSequenceType();
079
080                if(inventoryTransferTransactionSequenceType != null) {
081                    inventoryTransferTransactionName = sequenceGeneratorLogic.getNextSequenceValue(eea, inventoryTransferTransactionSequenceType);
082                }
083            }
084
085            if(eea == null || !eea.hasExecutionErrors()) {
086                inventoryTransferTransaction = createInventoryTransferTransaction(eea, inventoryTransactionType, inventoryTransferTransactionName,
087                        sourceInventoryTransaction, destinationInventoryTransaction, reference, description, createdBy);
088
089                if(eea == null || !eea.hasExecutionErrors()) {
090                    var inventoryTransferTransactionWorkflowEntrance = inventoryTransactionType.getLastDetail().getInventoryTransferTransactionWorkflowEntrance();
091
092                    if(inventoryTransferTransactionWorkflowEntrance != null) {
093                        workflowControl.addEntityToWorkflow(inventoryTransferTransactionWorkflowEntrance,
094                                getEntityInstanceByBaseEntity(inventoryTransferTransaction), null, null, createdBy);
095                    }
096                }
097            }
098        }
099
100        return inventoryTransferTransaction;
101    }
102
103    public InventoryTransferTransaction createInventoryTransferTransaction(final ExecutionErrorAccumulator eea,
104            final InventoryTransactionType inventoryTransactionType, final String inventoryTransferTransactionName,
105            final InventoryTransaction sourceInventoryTransaction, final InventoryTransaction destinationInventoryTransaction,
106            final String reference, final String description, final BasePK createdBy) {
107        var inventoryTransferTransaction = inventoryTransferTransactionControl.getInventoryTransferTransactionByName(inventoryTransactionType,
108                inventoryTransferTransactionName);
109
110        if(inventoryTransferTransaction == null) {
111            inventoryTransferTransaction = inventoryTransferTransactionControl.createInventoryTransferTransaction(inventoryTransactionType,
112                    inventoryTransferTransactionName, sourceInventoryTransaction, destinationInventoryTransaction, reference, description, createdBy);
113        } else {
114            handleExecutionError(DuplicateInventoryTransferTransactionNameException.class, eea,
115                    ExecutionErrors.DuplicateInventoryTransferTransactionName.name(),
116                    inventoryTransactionType.getLastDetail().getInventoryTransactionTypeName(), inventoryTransferTransactionName);
117        }
118
119        return inventoryTransferTransaction;
120    }
121
122    public InventoryTransferTransaction getInventoryTransferTransactionByName(final ExecutionErrorAccumulator eea,
123            final InventoryTransactionType inventoryTransactionType, final String inventoryTransferTransactionName, final EntityPermission entityPermission) {
124        var inventoryTransferTransaction = inventoryTransferTransactionControl.getInventoryTransferTransactionByName(inventoryTransactionType,
125                inventoryTransferTransactionName, entityPermission);
126
127        if(inventoryTransferTransaction == null) {
128            handleExecutionError(UnknownInventoryTransferTransactionNameException.class, eea, ExecutionErrors.UnknownInventoryTransferTransactionName.name(),
129                    inventoryTransactionType.getLastDetail().getInventoryTransactionTypeName(), inventoryTransferTransactionName);
130        }
131
132        return inventoryTransferTransaction;
133    }
134
135    public InventoryTransferTransaction getInventoryTransferTransactionByName(final ExecutionErrorAccumulator eea,
136            final InventoryTransactionType inventoryTransactionType, final String inventoryTransferTransactionName) {
137        return getInventoryTransferTransactionByName(eea, inventoryTransactionType, inventoryTransferTransactionName, EntityPermission.READ_ONLY);
138    }
139
140    public InventoryTransferTransaction getInventoryTransferTransactionByNameForUpdate(final ExecutionErrorAccumulator eea,
141            final InventoryTransactionType inventoryTransactionType, final String inventoryTransferTransactionName) {
142        return getInventoryTransferTransactionByName(eea, inventoryTransactionType, inventoryTransferTransactionName, EntityPermission.READ_WRITE);
143    }
144
145    public InventoryTransferTransaction getInventoryTransferTransactionByName(final ExecutionErrorAccumulator eea, final String inventoryTransactionTypeName,
146            final String inventoryTransferTransactionName, final EntityPermission entityPermission) {
147        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(eea, inventoryTransactionTypeName);
148        InventoryTransferTransaction inventoryTransferTransaction = null;
149
150        if(eea == null || !eea.hasExecutionErrors()) {
151            inventoryTransferTransaction = getInventoryTransferTransactionByName(eea, inventoryTransactionType, inventoryTransferTransactionName, entityPermission);
152        }
153
154        return inventoryTransferTransaction;
155    }
156
157    public InventoryTransferTransaction getInventoryTransferTransactionByName(final ExecutionErrorAccumulator eea, final String inventoryTransactionTypeName,
158            final String inventoryTransferTransactionName) {
159        return getInventoryTransferTransactionByName(eea, inventoryTransactionTypeName, inventoryTransferTransactionName, EntityPermission.READ_ONLY);
160    }
161
162    public InventoryTransferTransaction getInventoryTransferTransactionByNameForUpdate(final ExecutionErrorAccumulator eea, final String inventoryTransactionTypeName,
163            final String inventoryTransferTransactionName) {
164        return getInventoryTransferTransactionByName(eea, inventoryTransactionTypeName, inventoryTransferTransactionName, EntityPermission.READ_WRITE);
165    }
166
167    public InventoryTransferTransaction getInventoryTransferTransactionByUniversalSpec(final ExecutionErrorAccumulator eea,
168            final InventoryTransferTransactionUniversalSpec universalSpec, final String inventoryTransactionTypeName,
169            final EntityPermission entityPermission) {
170        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(eea, inventoryTransactionTypeName);
171        InventoryTransferTransaction inventoryTransferTransaction = null;
172
173        if(eea == null || !eea.hasExecutionErrors()) {
174            var inventoryTransferTransactionName = universalSpec.getInventoryTransferTransactionName();
175            var possibleEntitySpecs = entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
176
177            if(inventoryTransferTransactionName != null && possibleEntitySpecs == 0) {
178                inventoryTransferTransaction = getInventoryTransferTransactionByName(eea, inventoryTransactionType, inventoryTransferTransactionName, entityPermission);
179            } else if(inventoryTransferTransactionName == null && possibleEntitySpecs == 1) {
180                var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, ComponentVendors.ECHO_THREE.name(), EntityTypes.InventoryTransferTransaction.name());
181
182                if(eea == null || !eea.hasExecutionErrors()) {
183                    inventoryTransferTransaction = inventoryTransferTransactionControl.getInventoryTransferTransactionByEntityInstance(entityInstance, entityPermission);
184                }
185            } else {
186                handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
187            }
188
189            if((eea == null || !eea.hasExecutionErrors()) && inventoryTransferTransaction != null) {
190                var foundInventoryTransactionType = inventoryTransferTransaction.getLastDetail().getInventoryTransactionType();
191
192                if(!foundInventoryTransactionType.equals(inventoryTransactionType)) {
193                    handleExecutionError(InvalidInventoryTransactionTypeException.class, eea, ExecutionErrors.InvalidInventoryTransactionType.name(),
194                            foundInventoryTransactionType.getLastDetail().getInventoryTransactionTypeName());
195                }
196            }
197        }
198
199        return inventoryTransferTransaction;
200    }
201
202    public InventoryTransferTransaction getInventoryTransferTransactionByUniversalSpec(final ExecutionErrorAccumulator eea,
203            final InventoryTransferTransactionUniversalSpec universalSpec, final String inventoryTransactionTypeName) {
204        return getInventoryTransferTransactionByUniversalSpec(eea, universalSpec, inventoryTransactionTypeName, EntityPermission.READ_ONLY);
205    }
206
207    public InventoryTransferTransaction getInventoryTransferTransactionByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
208            final InventoryTransferTransactionUniversalSpec universalSpec, final String inventoryTransactionTypeName) {
209        return getInventoryTransferTransactionByUniversalSpec(eea, universalSpec, inventoryTransactionTypeName, EntityPermission.READ_WRITE);
210    }
211
212    public void updateInventoryTransferTransactionFromValue(final InventoryTransferTransactionDetailValue inventoryTransferTransactionDetailValue,
213            final BasePK updatedBy) {
214        inventoryTransferTransactionControl.updateInventoryTransferTransactionFromValue(inventoryTransferTransactionDetailValue, updatedBy);
215    }
216
217    public void deleteInventoryTransferTransaction(final ExecutionErrorAccumulator eea, final InventoryTransferTransaction inventoryTransferTransaction,
218            final BasePK deletedBy) {
219        inventoryTransferTransactionControl.deleteInventoryTransferTransaction(inventoryTransferTransaction, deletedBy);
220    }
221
222}