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.enterprise.inject.spi.CDI; 030import javax.inject.Inject; 031 032@ApplicationScoped 033public class ItemCategoryLogic 034 extends BaseLogic { 035 036 @Inject 037 ItemControl itemControl; 038 039 protected ItemCategoryLogic() { 040 super(); 041 } 042 043 public static ItemCategoryLogic getInstance() { 044 return CDI.current().select(ItemCategoryLogic.class).get(); 045 } 046 047 public ItemCategory getItemCategoryByName(final ExecutionErrorAccumulator eea, final String itemCategoryName, EntityPermission entityPermission) { 048 var itemCategory = itemControl.getItemCategoryByName(itemCategoryName, entityPermission); 049 050 if(itemCategory == null) { 051 handleExecutionError(UnknownItemCategoryNameException.class, eea, ExecutionErrors.UnknownItemCategoryName.name(), itemCategoryName); 052 } 053 054 return itemCategory; 055 } 056 057 public ItemCategory getItemCategoryByName(final ExecutionErrorAccumulator eea, final String itemCategoryName) { 058 return getItemCategoryByName(eea, itemCategoryName, EntityPermission.READ_ONLY); 059 } 060 061 public ItemCategory getItemCategoryByNameForUpdate(final ExecutionErrorAccumulator eea, final String itemCategoryName) { 062 return getItemCategoryByName(eea, itemCategoryName, EntityPermission.READ_WRITE); 063 } 064 065 private long countItemsByItemCategoryChildren(final ItemControl itemControl, final ItemCategory parentItemCategory) { 066 var itemCategoryChildren = itemControl.getItemCategoriesByParentItemCategory(parentItemCategory); 067 var total = itemControl.countItemsByItemCategory(parentItemCategory); 068 069 total = itemCategoryChildren.stream().map((childItemCategory) -> countItemsByItemCategoryChildren(itemControl, childItemCategory)).reduce(total, (accumulator, _item) -> accumulator + _item); 070 071 return total; 072 } 073 074 public void checkDeleteItemCategory(final ExecutionErrorAccumulator ema, final ItemCategory itemCategory) { 075 if(countItemsByItemCategoryChildren(itemControl, itemCategory) != 0) { 076 ema.addExecutionError(ExecutionErrors.CannotDeleteItemCategoryInUse.name(), itemCategory.getLastDetail().getItemCategoryName()); 077 } 078 } 079 080 081 public ItemCategory getDefaultItemCategory(final ExecutionErrorAccumulator eea) { 082 var itemCategory = itemControl.getDefaultItemCategory(); 083 084 if(itemCategory == null) { 085 handleExecutionError(MissingDefaultItemCategoryException.class, eea, ExecutionErrors.MissingDefaultItemCategory.name()); 086 } 087 088 return itemCategory; 089 } 090 091 public void deleteItemCategory(final ItemCategory itemCategory, final BasePK deletedBy) { 092 itemControl.deleteItemCategory(itemCategory, deletedBy); 093 } 094 095}