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.contact.server.logic; 018 019import com.echothree.control.user.contact.common.spec.ContactMechanismPurposeUniversalSpec; 020import com.echothree.model.control.contact.common.exception.UnknownContactMechanismPurposeNameException; 021import com.echothree.model.control.contact.server.control.ContactControl; 022import com.echothree.model.control.core.common.ComponentVendors; 023import com.echothree.model.control.core.common.EntityTypes; 024import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.server.control.BaseLogic; 029import com.echothree.util.server.message.ExecutionErrorAccumulator; 030import javax.enterprise.context.ApplicationScoped; 031import javax.inject.Inject; 032 033@ApplicationScoped 034public class ContactMechanismPurposeLogic 035 extends BaseLogic { 036 037 @Inject 038 ContactControl contactControl; 039 040 @Inject 041 EntityInstanceLogic entityInstanceLogic; 042 043 protected ContactMechanismPurposeLogic() { 044 super(); 045 } 046 047 public ContactMechanismPurpose getContactMechanismPurposeByName(final ExecutionErrorAccumulator eea, final String contactMechanismPurposeName) { 048 var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName); 049 050 if(contactMechanismPurpose == null) { 051 handleExecutionError(UnknownContactMechanismPurposeNameException.class, eea, ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName); 052 } 053 054 return contactMechanismPurpose; 055 } 056 057 public ContactMechanismPurpose getContactMechanismPurposeByUniversalSpec(final ExecutionErrorAccumulator eea, 058 final ContactMechanismPurposeUniversalSpec universalSpec) { 059 ContactMechanismPurpose contactMechanismPurpose = null; 060 var contactMechanismPurposeName = universalSpec.getContactMechanismPurposeName(); 061 var parameterCount = (contactMechanismPurposeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 062 063 switch(parameterCount) { 064 case 1 -> { 065 if(contactMechanismPurposeName == null) { 066 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 067 ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactMechanismPurpose.name()); 068 069 if(eea == null || !eea.hasExecutionErrors()) { 070 contactMechanismPurpose = contactControl.getContactMechanismPurposeByEntityInstance(entityInstance); 071 } 072 } else { 073 contactMechanismPurpose = getContactMechanismPurposeByName(eea, contactMechanismPurposeName); 074 } 075 } 076 default -> 077 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 078 } 079 080 return contactMechanismPurpose; 081 } 082 083}