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.contactlist.server.logic; 018 019import com.echothree.control.user.contactlist.common.spec.ContactListGroupUniversalSpec; 020import com.echothree.model.control.contactlist.common.exception.UnknownContactListGroupNameException; 021import com.echothree.model.control.contactlist.common.exception.UnknownDefaultContactListGroupException; 022import com.echothree.model.control.contactlist.server.control.ContactListControl; 023import com.echothree.model.control.core.common.ComponentVendors; 024import com.echothree.model.control.core.common.EntityTypes; 025import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 026import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 027import com.echothree.model.data.contactlist.server.entity.ContactListGroup; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.server.control.BaseLogic; 030import com.echothree.util.server.message.ExecutionErrorAccumulator; 031import com.echothree.util.server.persistence.EntityPermission; 032import javax.enterprise.context.ApplicationScoped; 033import javax.enterprise.inject.spi.CDI; 034import javax.inject.Inject; 035 036@ApplicationScoped 037public class ContactListGroupLogic 038 extends BaseLogic { 039 040 @Inject 041 ContactListControl contactListControl; 042 043 @Inject 044 EntityInstanceLogic entityInstanceLogic; 045 046 protected ContactListGroupLogic() { 047 super(); 048 } 049 050 public static ContactListGroupLogic getInstance() { 051 return CDI.current().select(ContactListGroupLogic.class).get(); 052 } 053 054 public ContactListGroup getContactListGroupByName(final ExecutionErrorAccumulator eea, final String contactListGroupName, 055 final EntityPermission entityPermission) { 056 var contactListGroup = contactListControl.getContactListGroupByName(contactListGroupName, entityPermission); 057 058 if(contactListGroup == null) { 059 handleExecutionError(UnknownContactListGroupNameException.class, eea, ExecutionErrors.UnknownContactListGroupName.name(), contactListGroupName); 060 } 061 062 return contactListGroup; 063 } 064 065 public ContactListGroup getContactListGroupByName(final ExecutionErrorAccumulator eea, final String contactListGroupName) { 066 return getContactListGroupByName(eea, contactListGroupName, EntityPermission.READ_ONLY); 067 } 068 069 public ContactListGroup getContactListGroupByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactListGroupName) { 070 return getContactListGroupByName(eea, contactListGroupName, EntityPermission.READ_WRITE); 071 } 072 073 public ContactListGroup getContactListGroupByUniversalSpec(final ExecutionErrorAccumulator eea, 074 final ContactListGroupUniversalSpec spec, final boolean allowDefault, final EntityPermission entityPermission) { 075 ContactListGroup contactListGroup = null; 076 var contactListGroupName = spec.getContactListGroupName(); 077 var parameterCount = (contactListGroupName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(spec); 078 079 switch(parameterCount) { 080 case 0 -> { 081 if(allowDefault) { 082 contactListGroup = contactListControl.getDefaultContactListGroup(entityPermission); 083 084 if(contactListGroup == null) { 085 handleExecutionError(UnknownDefaultContactListGroupException.class, eea, ExecutionErrors.UnknownDefaultContactListGroup.name()); 086 } 087 } else { 088 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 089 } 090 } 091 case 1 -> { 092 if(contactListGroupName == null) { 093 var entityInstance = entityInstanceLogic.getEntityInstance(eea, spec, 094 ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactListGroup.name()); 095 096 if(eea == null || !eea.hasExecutionErrors()) { 097 contactListGroup = contactListControl.getContactListGroupByEntityInstance(entityInstance, entityPermission); 098 } 099 } else { 100 contactListGroup = getContactListGroupByName(eea, contactListGroupName, entityPermission); 101 } 102 } 103 default -> 104 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 105 } 106 107 return contactListGroup; 108 } 109 110 public ContactListGroup getContactListGroupByUniversalSpec(final ExecutionErrorAccumulator eea, 111 final ContactListGroupUniversalSpec spec, final boolean allowDefault) { 112 return getContactListGroupByUniversalSpec(eea, spec, allowDefault, EntityPermission.READ_ONLY); 113 } 114 115 public ContactListGroup getContactListGroupByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 116 final ContactListGroupUniversalSpec spec, final boolean allowDefault) { 117 return getContactListGroupByUniversalSpec(eea, spec, allowDefault, EntityPermission.READ_WRITE); 118 } 119 120}