001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.InventoryConditionUniversalSpec; 020import com.echothree.model.control.inventory.common.exception.DuplicateInventoryConditionNameException; 021import com.echothree.model.control.inventory.common.exception.UnknownInventoryConditionNameException; 022import com.echothree.model.control.inventory.common.exception.UnknownDefaultInventoryConditionException; 023import com.echothree.model.control.inventory.server.control.InventoryControl; 024import com.echothree.model.control.core.common.ComponentVendors; 025import com.echothree.model.control.core.common.EntityTypes; 026import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 027import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 028import com.echothree.model.data.inventory.server.entity.InventoryCondition; 029import com.echothree.model.data.core.server.entity.EntityInstance; 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; 037 038public class InventoryConditionLogic 039 extends BaseLogic { 040 041 private InventoryConditionLogic() { 042 super(); 043 } 044 045 private static class InventoryConditionLogicHolder { 046 static InventoryConditionLogic instance = new InventoryConditionLogic(); 047 } 048 049 public static InventoryConditionLogic getInstance() { 050 return InventoryConditionLogicHolder.instance; 051 } 052 053 public InventoryCondition createInventoryCondition(final ExecutionErrorAccumulator eea, final String inventoryConditionName, 054 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 055 final BasePK createdBy) { 056 var inventoryControl = Session.getModelController(InventoryControl.class); 057 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 058 059 if(inventoryCondition == null) { 060 inventoryCondition = inventoryControl.createInventoryCondition(inventoryConditionName, isDefault, sortOrder, createdBy); 061 062 if(description != null) { 063 inventoryControl.createInventoryConditionDescription(inventoryCondition, language, description, createdBy); 064 } 065 } else { 066 handleExecutionError(DuplicateInventoryConditionNameException.class, eea, ExecutionErrors.DuplicateInventoryConditionName.name(), inventoryConditionName); 067 } 068 069 return inventoryCondition; 070 } 071 072 public InventoryCondition getInventoryConditionByName(final ExecutionErrorAccumulator eea, final String inventoryConditionName, 073 final EntityPermission entityPermission) { 074 var inventoryControl = Session.getModelController(InventoryControl.class); 075 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName, entityPermission); 076 077 if(inventoryCondition == null) { 078 handleExecutionError(UnknownInventoryConditionNameException.class, eea, ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 079 } 080 081 return inventoryCondition; 082 } 083 084 public InventoryCondition getInventoryConditionByName(final ExecutionErrorAccumulator eea, final String inventoryConditionName) { 085 return getInventoryConditionByName(eea, inventoryConditionName, EntityPermission.READ_ONLY); 086 } 087 088 public InventoryCondition getInventoryConditionByNameForUpdate(final ExecutionErrorAccumulator eea, final String inventoryConditionName) { 089 return getInventoryConditionByName(eea, inventoryConditionName, EntityPermission.READ_WRITE); 090 } 091 092 public InventoryCondition getInventoryConditionByUniversalSpec(final ExecutionErrorAccumulator eea, 093 final InventoryConditionUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 094 InventoryCondition inventoryCondition = null; 095 var inventoryControl = Session.getModelController(InventoryControl.class); 096 String inventoryConditionName = universalSpec.getInventoryConditionName(); 097 var parameterCount = (inventoryConditionName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 098 099 switch(parameterCount) { 100 case 0: 101 if(allowDefault) { 102 inventoryCondition = inventoryControl.getDefaultInventoryCondition(entityPermission); 103 104 if(inventoryCondition == null) { 105 handleExecutionError(UnknownDefaultInventoryConditionException.class, eea, ExecutionErrors.UnknownDefaultInventoryCondition.name()); 106 } 107 } else { 108 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 109 } 110 break; 111 case 1: 112 if(inventoryConditionName == null) { 113 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 114 ComponentVendors.ECHO_THREE.name(), EntityTypes.InventoryCondition.name()); 115 116 if(!eea.hasExecutionErrors()) { 117 inventoryCondition = inventoryControl.getInventoryConditionByEntityInstance(entityInstance, entityPermission); 118 } 119 } else { 120 inventoryCondition = getInventoryConditionByName(eea, inventoryConditionName, entityPermission); 121 } 122 break; 123 default: 124 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 125 break; 126 } 127 128 return inventoryCondition; 129 } 130 131 public InventoryCondition getInventoryConditionByUniversalSpec(final ExecutionErrorAccumulator eea, 132 final InventoryConditionUniversalSpec universalSpec, boolean allowDefault) { 133 return getInventoryConditionByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 134 } 135 136 public InventoryCondition getInventoryConditionByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 137 final InventoryConditionUniversalSpec universalSpec, boolean allowDefault) { 138 return getInventoryConditionByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 139 } 140 141 public void deleteInventoryCondition(final ExecutionErrorAccumulator eea, final InventoryCondition inventoryCondition, 142 final BasePK deletedBy) { 143 var inventoryControl = Session.getModelController(InventoryControl.class); 144 145 inventoryControl.deleteInventoryCondition(inventoryCondition, deletedBy); 146 } 147 148}