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