001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.AllocationPriorityUniversalSpec;
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.DuplicateAllocationPriorityNameException;
025import com.echothree.model.control.inventory.common.exception.UnknownAllocationPriorityNameException;
026import com.echothree.model.control.inventory.common.exception.UnknownDefaultAllocationPriorityException;
027import com.echothree.model.control.inventory.server.control.InventoryControl;
028import com.echothree.model.data.inventory.server.entity.AllocationPriority;
029import com.echothree.model.data.inventory.server.value.AllocationPriorityDetailValue;
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 com.echothree.util.server.persistence.Session;
037import javax.enterprise.context.ApplicationScoped;
038import javax.enterprise.inject.spi.CDI;
039
040@ApplicationScoped
041public class AllocationPriorityLogic
042        extends BaseLogic {
043
044    protected AllocationPriorityLogic() {
045        super();
046    }
047
048    public static AllocationPriorityLogic getInstance() {
049        return CDI.current().select(AllocationPriorityLogic.class).get();
050    }
051
052    public AllocationPriority createAllocationPriority(final ExecutionErrorAccumulator eea, final String allocationPriorityName,
053            final Integer priority, final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
054            final BasePK createdBy) {
055        var inventoryControl = Session.getModelController(InventoryControl.class);
056        var allocationPriority = inventoryControl.getAllocationPriorityByName(allocationPriorityName);
057
058        if(allocationPriority == null) {
059            allocationPriority = inventoryControl.createAllocationPriority(allocationPriorityName, priority, isDefault, sortOrder, createdBy);
060
061            if(description != null) {
062                inventoryControl.createAllocationPriorityDescription(allocationPriority, language, description, createdBy);
063            }
064        } else {
065            handleExecutionError(DuplicateAllocationPriorityNameException.class, eea, ExecutionErrors.DuplicateAllocationPriorityName.name(), allocationPriorityName);
066        }
067
068        return allocationPriority;
069    }
070
071    public AllocationPriority getAllocationPriorityByName(final ExecutionErrorAccumulator eea, final String allocationPriorityName,
072            final EntityPermission entityPermission) {
073        var inventoryControl = Session.getModelController(InventoryControl.class);
074        var allocationPriority = inventoryControl.getAllocationPriorityByName(allocationPriorityName, entityPermission);
075
076        if(allocationPriority == null) {
077            handleExecutionError(UnknownAllocationPriorityNameException.class, eea, ExecutionErrors.UnknownAllocationPriorityName.name(), allocationPriorityName);
078        }
079
080        return allocationPriority;
081    }
082
083    public AllocationPriority getAllocationPriorityByName(final ExecutionErrorAccumulator eea, final String allocationPriorityName) {
084        return getAllocationPriorityByName(eea, allocationPriorityName, EntityPermission.READ_ONLY);
085    }
086
087    public AllocationPriority getAllocationPriorityByNameForUpdate(final ExecutionErrorAccumulator eea, final String allocationPriorityName) {
088        return getAllocationPriorityByName(eea, allocationPriorityName, EntityPermission.READ_WRITE);
089    }
090
091    public AllocationPriority getAllocationPriorityByUniversalSpec(final ExecutionErrorAccumulator eea,
092            final AllocationPriorityUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
093        AllocationPriority allocationPriority = null;
094        var inventoryControl = Session.getModelController(InventoryControl.class);
095        var allocationPriorityName = universalSpec.getAllocationPriorityName();
096        var parameterCount = (allocationPriorityName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
097
098        switch(parameterCount) {
099            case 0 -> {
100                if(allowDefault) {
101                    allocationPriority = inventoryControl.getDefaultAllocationPriority(entityPermission);
102
103                    if(allocationPriority == null) {
104                        handleExecutionError(UnknownDefaultAllocationPriorityException.class, eea, ExecutionErrors.UnknownDefaultAllocationPriority.name());
105                    }
106                } else {
107                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
108                }
109            }
110            case 1 -> {
111                if(allocationPriorityName == null) {
112                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
113                            ComponentVendors.ECHO_THREE.name(), EntityTypes.AllocationPriority.name());
114
115                    if(!eea.hasExecutionErrors()) {
116                        allocationPriority = inventoryControl.getAllocationPriorityByEntityInstance(entityInstance, entityPermission);
117                    }
118                } else {
119                    allocationPriority = getAllocationPriorityByName(eea, allocationPriorityName, entityPermission);
120                }
121            }
122            default ->
123                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
124        }
125
126        return allocationPriority;
127    }
128
129    public AllocationPriority getAllocationPriorityByUniversalSpec(final ExecutionErrorAccumulator eea,
130            final AllocationPriorityUniversalSpec universalSpec, boolean allowDefault) {
131        return getAllocationPriorityByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
132    }
133
134    public AllocationPriority getAllocationPriorityByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
135            final AllocationPriorityUniversalSpec universalSpec, boolean allowDefault) {
136        return getAllocationPriorityByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
137    }
138
139    public void updateAllocationPriorityFromValue(final AllocationPriorityDetailValue allocationPriorityDetailValue,
140            final BasePK updatedBy) {
141        var inventoryControl = Session.getModelController(InventoryControl.class);
142
143        inventoryControl.updateAllocationPriorityFromValue(allocationPriorityDetailValue, updatedBy);
144    }
145    
146    public void deleteAllocationPriority(final ExecutionErrorAccumulator eea, final AllocationPriority allocationPriority,
147            final BasePK deletedBy) {
148        var inventoryControl = Session.getModelController(InventoryControl.class);
149
150        inventoryControl.deleteAllocationPriority(allocationPriority, deletedBy);
151    }
152
153}