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.cancellationpolicy.server.logic; 018 019import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindUniversalSpec; 020import com.echothree.model.control.cancellationpolicy.common.exception.DuplicateCancellationKindNameException; 021import com.echothree.model.control.cancellationpolicy.common.exception.UnknownCancellationKindNameException; 022import com.echothree.model.control.cancellationpolicy.common.exception.UnknownDefaultCancellationKindException; 023import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl; 024import com.echothree.model.control.core.common.ComponentVendors; 025import com.echothree.model.control.core.common.EntityTypes; 026import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 027import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 028import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKind; 029import com.echothree.model.data.party.server.entity.Language; 030import com.echothree.model.data.sequence.server.entity.SequenceType; 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 CancellationKindLogic 042 extends BaseLogic { 043 044 @Inject 045 CancellationPolicyControl cancellationPolicyControl; 046 047 @Inject 048 EntityInstanceLogic entityInstanceLogic; 049 050 protected CancellationKindLogic() { 051 super(); 052 } 053 054 public static CancellationKindLogic getInstance() { 055 return CDI.current().select(CancellationKindLogic.class).get(); 056 } 057 058 public CancellationKind createCancellationKind(final ExecutionErrorAccumulator eea, final String cancellationKindName, 059 final SequenceType cancellationSequenceType, final Boolean isDefault, final Integer sortOrder, final Language language, 060 final String description, final BasePK createdBy) { 061 var cancellationControl = cancellationPolicyControl; 062 var cancellationKind = cancellationControl.getCancellationKindByName(cancellationKindName); 063 064 if(cancellationKind == null) { 065 cancellationKind = cancellationControl.createCancellationKind(cancellationKindName, cancellationSequenceType, 066 isDefault, sortOrder, createdBy); 067 068 if(description != null) { 069 cancellationControl.createCancellationKindDescription(cancellationKind, language, description, createdBy); 070 } 071 } else { 072 handleExecutionError(DuplicateCancellationKindNameException.class, eea, ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName); 073 } 074 075 return cancellationKind; 076 } 077 078 public CancellationKind getCancellationKindByName(final ExecutionErrorAccumulator eea, final String cancellationKindName, 079 final EntityPermission entityPermission) { 080 var cancellationControl = cancellationPolicyControl; 081 var cancellationKind = cancellationControl.getCancellationKindByName(cancellationKindName, entityPermission); 082 083 if(cancellationKind == null) { 084 handleExecutionError(UnknownCancellationKindNameException.class, eea, ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 085 } 086 087 return cancellationKind; 088 } 089 090 public CancellationKind getCancellationKindByName(final ExecutionErrorAccumulator eea, final String cancellationKindName) { 091 return getCancellationKindByName(eea, cancellationKindName, EntityPermission.READ_ONLY); 092 } 093 094 public CancellationKind getCancellationKindByNameForUpdate(final ExecutionErrorAccumulator eea, final String cancellationKindName) { 095 return getCancellationKindByName(eea, cancellationKindName, EntityPermission.READ_WRITE); 096 } 097 098 public CancellationKind getCancellationKindByUniversalSpec(final ExecutionErrorAccumulator eea, 099 final CancellationKindUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 100 CancellationKind cancellationKind = null; 101 var cancellationControl = cancellationPolicyControl; 102 var cancellationKindName = universalSpec.getCancellationKindName(); 103 var parameterCount = (cancellationKindName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 104 105 switch(parameterCount) { 106 case 0 -> { 107 if(allowDefault) { 108 cancellationKind = cancellationControl.getDefaultCancellationKind(entityPermission); 109 110 if(cancellationKind == null) { 111 handleExecutionError(UnknownDefaultCancellationKindException.class, eea, ExecutionErrors.UnknownDefaultCancellationKind.name()); 112 } 113 } else { 114 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 115 } 116 } 117 case 1 -> { 118 if(cancellationKindName == null) { 119 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 120 ComponentVendors.ECHO_THREE.name(), EntityTypes.CancellationKind.name()); 121 122 if(eea == null || !eea.hasExecutionErrors()) { 123 cancellationKind = cancellationControl.getCancellationKindByEntityInstance(entityInstance, entityPermission); 124 } 125 } else { 126 cancellationKind = getCancellationKindByName(eea, cancellationKindName, entityPermission); 127 } 128 } 129 default -> 130 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 131 } 132 133 return cancellationKind; 134 } 135 136 public CancellationKind getCancellationKindByUniversalSpec(final ExecutionErrorAccumulator eea, 137 final CancellationKindUniversalSpec universalSpec, boolean allowDefault) { 138 return getCancellationKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 139 } 140 141 public CancellationKind getCancellationKindByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 142 final CancellationKindUniversalSpec universalSpec, boolean allowDefault) { 143 return getCancellationKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 144 } 145 146}