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