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.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 java.util.List; 030 031public class ItemCategoryLogic 032 extends BaseLogic { 033 034 private ItemCategoryLogic() { 035 super(); 036 } 037 038 private static class ItemCategoryLogicHolder { 039 static ItemCategoryLogic instance = new ItemCategoryLogic(); 040 } 041 042 public static ItemCategoryLogic getInstance() { 043 return ItemCategoryLogicHolder.instance; 044 } 045 046 public ItemCategory getItemCategoryByName(final ExecutionErrorAccumulator eea, final String itemCategoryName, EntityPermission entityPermission) { 047 var itemControl = Session.getModelController(ItemControl.class); 048 ItemCategory 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 List<ItemCategory> itemCategoryChildren = itemControl.getItemCategoriesByParentItemCategory(parentItemCategory); 067 long 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 var itemControl = Session.getModelController(ItemControl.class); 076 077 if(countItemsByItemCategoryChildren(itemControl, itemCategory) != 0) { 078 ema.addExecutionError(ExecutionErrors.CannotDeleteItemCategoryInUse.name(), itemCategory.getLastDetail().getItemCategoryName()); 079 } 080 } 081 082 083 public ItemCategory getDefaultItemCategory(final ExecutionErrorAccumulator eea) { 084 var itemControl = Session.getModelController(ItemControl.class); 085 ItemCategory itemCategory = itemControl.getDefaultItemCategory(); 086 087 if(itemCategory == null) { 088 handleExecutionError(MissingDefaultItemCategoryException.class, eea, ExecutionErrors.MissingDefaultItemCategory.name()); 089 } 090 091 return itemCategory; 092 } 093 094 public void deleteItemCategory(final ItemCategory itemCategory, final BasePK deletedBy) { 095 var itemControl = Session.getModelController(ItemControl.class); 096 097 itemControl.deleteItemCategory(itemCategory, deletedBy); 098 } 099 100}