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