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.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 com.echothree.util.server.persistence.Session; 031import javax.enterprise.context.ApplicationScoped; 032import javax.enterprise.inject.spi.CDI; 033 034@ApplicationScoped 035public class ContactMechanismPurposeLogic 036 extends BaseLogic { 037 038 protected ContactMechanismPurposeLogic() { 039 super(); 040 } 041 042 public static ContactMechanismPurposeLogic getInstance() { 043 return CDI.current().select(ContactMechanismPurposeLogic.class).get(); 044 } 045 046 public ContactMechanismPurpose getContactMechanismPurposeByName(final ExecutionErrorAccumulator eea, final String contactMechanismPurposeName) { 047 var contactControl = Session.getModelController(ContactControl.class); 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 contactControl = Session.getModelController(ContactControl.class); 061 var contactMechanismPurposeName = universalSpec.getContactMechanismPurposeName(); 062 var parameterCount = (contactMechanismPurposeName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 063 064 switch(parameterCount) { 065 case 1 -> { 066 if(contactMechanismPurposeName == null) { 067 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 068 ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactMechanismPurpose.name()); 069 070 if(!eea.hasExecutionErrors()) { 071 contactMechanismPurpose = contactControl.getContactMechanismPurposeByEntityInstance(entityInstance); 072 } 073 } else { 074 contactMechanismPurpose = getContactMechanismPurposeByName(eea, contactMechanismPurposeName); 075 } 076 } 077 default -> 078 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 079 } 080 081 return contactMechanismPurpose; 082 } 083 084}