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.item.server.logic; 018 019import com.echothree.model.control.item.common.exception.MissingDefaultItemCategoryException; 020import com.echothree.model.control.item.common.exception.UnknownItemCategoryNameException; 021import com.echothree.model.control.item.server.control.ItemControl; 022import com.echothree.model.data.item.server.entity.ItemCategory; 023import com.echothree.util.common.message.ExecutionErrors; 024import com.echothree.util.common.persistence.BasePK; 025import com.echothree.util.server.control.BaseLogic; 026import com.echothree.util.server.message.ExecutionErrorAccumulator; 027import com.echothree.util.server.persistence.EntityPermission; 028import javax.enterprise.context.ApplicationScoped; 029import javax.inject.Inject; 030 031@ApplicationScoped 032public class ItemCategoryLogic 033 extends BaseLogic { 034 035 @Inject 036 ItemControl itemControl; 037 038 protected ItemCategoryLogic() { 039 super(); 040 } 041 042 public ItemCategory getItemCategoryByName(final ExecutionErrorAccumulator eea, final String itemCategoryName, EntityPermission entityPermission) { 043 var itemCategory = itemControl.getItemCategoryByName(itemCategoryName, entityPermission); 044 045 if(itemCategory == null) { 046 handleExecutionError(UnknownItemCategoryNameException.class, eea, ExecutionErrors.UnknownItemCategoryName.name(), itemCategoryName); 047 } 048 049 return itemCategory; 050 } 051 052 public ItemCategory getItemCategoryByName(final ExecutionErrorAccumulator eea, final String itemCategoryName) { 053 return getItemCategoryByName(eea, itemCategoryName, EntityPermission.READ_ONLY); 054 } 055 056 public ItemCategory getItemCategoryByNameForUpdate(final ExecutionErrorAccumulator eea, final String itemCategoryName) { 057 return getItemCategoryByName(eea, itemCategoryName, EntityPermission.READ_WRITE); 058 } 059 060 private long countItemsByItemCategoryChildren(final ItemControl itemControl, final ItemCategory parentItemCategory) { 061 var itemCategoryChildren = itemControl.getItemCategoriesByParentItemCategory(parentItemCategory); 062 var total = itemControl.countItemsByItemCategory(parentItemCategory); 063 064 total = itemCategoryChildren.stream().map((childItemCategory) -> countItemsByItemCategoryChildren(itemControl, childItemCategory)).reduce(total, (accumulator, _item) -> accumulator + _item); 065 066 return total; 067 } 068 069 public void checkDeleteItemCategory(final ExecutionErrorAccumulator ema, final ItemCategory itemCategory) { 070 if(countItemsByItemCategoryChildren(itemControl, itemCategory) != 0) { 071 ema.addExecutionError(ExecutionErrors.CannotDeleteItemCategoryInUse.name(), itemCategory.getLastDetail().getItemCategoryName()); 072 } 073 } 074 075 076 public ItemCategory getDefaultItemCategory(final ExecutionErrorAccumulator eea) { 077 var itemCategory = itemControl.getDefaultItemCategory(); 078 079 if(itemCategory == null) { 080 handleExecutionError(MissingDefaultItemCategoryException.class, eea, ExecutionErrors.MissingDefaultItemCategory.name()); 081 } 082 083 return itemCategory; 084 } 085 086 public void deleteItemCategory(final ItemCategory itemCategory, final BasePK deletedBy) { 087 itemControl.deleteItemCategory(itemCategory, deletedBy); 088 } 089 090}