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.chain.server.logic; 018 019import com.echothree.control.user.chain.common.spec.ChainKindUniversalSpec; 020import com.echothree.model.control.chain.common.exception.CannotDeleteChainKindInUseException; 021import com.echothree.model.control.chain.common.exception.DuplicateChainKindNameException; 022import com.echothree.model.control.chain.common.exception.UnknownChainKindNameException; 023import com.echothree.model.control.chain.common.exception.UnknownDefaultChainKindException; 024import com.echothree.model.control.chain.server.control.ChainControl; 025import com.echothree.model.control.core.common.ComponentVendors; 026import com.echothree.model.control.core.common.EntityTypes; 027import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 028import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 029import com.echothree.model.data.chain.server.entity.ChainKind; 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 javax.enterprise.context.ApplicationScoped; 037import javax.inject.Inject; 038 039@ApplicationScoped 040public class ChainKindLogic 041 extends BaseLogic { 042 043 @Inject 044 ChainControl chainControl; 045 046 @Inject 047 EntityInstanceLogic entityInstanceLogic; 048 049 protected ChainKindLogic() { 050 super(); 051 } 052 053 public ChainKind createChainKind(final ExecutionErrorAccumulator eea, final String chainKindName, 054 final Boolean isDefault, final Integer sortOrder, final Language language, final String description, 055 final BasePK createdBy) { 056 var chainKind = chainControl.getChainKindByName(chainKindName); 057 058 if(chainKind == null) { 059 chainKind = chainControl.createChainKind(chainKindName, isDefault, sortOrder, createdBy); 060 061 if(description != null) { 062 chainControl.createChainKindDescription(chainKind, language, description, createdBy); 063 } 064 } else { 065 handleExecutionError(DuplicateChainKindNameException.class, eea, ExecutionErrors.DuplicateChainKindName.name(), chainKindName); 066 } 067 068 return chainKind; 069 } 070 071 public ChainKind getChainKindByName(final ExecutionErrorAccumulator eea, final String chainKindName, 072 final EntityPermission entityPermission) { 073 var chainKind = chainControl.getChainKindByName(chainKindName, entityPermission); 074 075 if(chainKind == null) { 076 handleExecutionError(UnknownChainKindNameException.class, eea, ExecutionErrors.UnknownChainKindName.name(), chainKindName); 077 } 078 079 return chainKind; 080 } 081 082 public ChainKind getChainKindByName(final ExecutionErrorAccumulator eea, final String chainKindName) { 083 return getChainKindByName(eea, chainKindName, EntityPermission.READ_ONLY); 084 } 085 086 public ChainKind getChainKindByNameForUpdate(final ExecutionErrorAccumulator eea, final String chainKindName) { 087 return getChainKindByName(eea, chainKindName, EntityPermission.READ_WRITE); 088 } 089 090 public ChainKind getChainKindByUniversalSpec(final ExecutionErrorAccumulator eea, 091 final ChainKindUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 092 ChainKind chainKind = null; 093 var chainKindName = universalSpec.getChainKindName(); 094 var parameterCount = (chainKindName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 095 096 switch(parameterCount) { 097 case 0 -> { 098 if(allowDefault) { 099 chainKind = chainControl.getDefaultChainKind(entityPermission); 100 101 if(chainKind == null) { 102 handleExecutionError(UnknownDefaultChainKindException.class, eea, ExecutionErrors.UnknownDefaultChainKind.name()); 103 } 104 } else { 105 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 106 } 107 } 108 case 1 -> { 109 if(chainKindName == null) { 110 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 111 ComponentVendors.ECHO_THREE.name(), EntityTypes.ChainKind.name()); 112 113 if(eea == null || !eea.hasExecutionErrors()) { 114 chainKind = chainControl.getChainKindByEntityInstance(entityInstance, entityPermission); 115 } 116 } else { 117 chainKind = getChainKindByName(eea, chainKindName, entityPermission); 118 } 119 } 120 default -> 121 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 122 } 123 124 return chainKind; 125 } 126 127 public ChainKind getChainKindByUniversalSpec(final ExecutionErrorAccumulator eea, 128 final ChainKindUniversalSpec universalSpec, boolean allowDefault) { 129 return getChainKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 130 } 131 132 public ChainKind getChainKindByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 133 final ChainKindUniversalSpec universalSpec, boolean allowDefault) { 134 return getChainKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 135 } 136 137 public void deleteChainKind(final ExecutionErrorAccumulator eea, final ChainKind chainKind, 138 final BasePK deletedBy) { 139 var chainTypeCount = chainControl.countChainTypesByChainKind(chainKind); 140 141 if(chainTypeCount == 0) { 142 chainControl.deleteChainKind(chainKind, deletedBy); 143 } else { 144 handleExecutionError(CannotDeleteChainKindInUseException.class, eea, ExecutionErrors.CannotDeleteChainKindInUse.name(), 145 chainKind.getLastDetail().getChainKindName()); 146 } 147 } 148 149}