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.accounting.server.logic; 018 019import com.echothree.control.user.accounting.common.spec.GlAccountClassUniversalSpec; 020import com.echothree.model.control.accounting.common.exception.DuplicateGlAccountClassNameException; 021import com.echothree.model.control.accounting.common.exception.UnknownDefaultGlAccountClassException; 022import com.echothree.model.control.accounting.common.exception.UnknownGlAccountClassNameException; 023import com.echothree.model.control.accounting.server.control.AccountingControl; 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.accounting.server.entity.GlAccountClass; 029import com.echothree.model.data.accounting.server.value.GlAccountClassDetailValue; 030import com.echothree.model.data.party.server.entity.Language; 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 GlAccountClassLogic 042 extends BaseLogic { 043 044 @Inject 045 AccountingControl accountingControl; 046 047 @Inject 048 EntityInstanceLogic entityInstanceLogic; 049 050 protected GlAccountClassLogic() { 051 super(); 052 } 053 054 public static GlAccountClassLogic getInstance() { 055 return CDI.current().select(GlAccountClassLogic.class).get(); 056 } 057 058 public GlAccountClass createGlAccountClass(final ExecutionErrorAccumulator eea, final String glAccountClassName, 059 final GlAccountClass parentGlAccountClass, final Boolean isDefault, final Integer sortOrder, final Language language, 060 final String description, final BasePK createdBy) { 061 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 062 063 if(glAccountClass == null) { 064 glAccountClass = accountingControl.createGlAccountClass(glAccountClassName, 065 parentGlAccountClass, isDefault, sortOrder, createdBy); 066 067 if(description != null) { 068 accountingControl.createGlAccountClassDescription(glAccountClass, language, description, createdBy); 069 } 070 } else { 071 handleExecutionError(DuplicateGlAccountClassNameException.class, eea, ExecutionErrors.DuplicateGlAccountClassName.name(), 072 glAccountClassName); 073 } 074 075 return glAccountClass; 076 } 077 078 public GlAccountClass getGlAccountClassByName(final ExecutionErrorAccumulator eea, final String glAccountClassName, 079 final EntityPermission entityPermission) { 080 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName, entityPermission); 081 082 if(glAccountClass == null) { 083 handleExecutionError(UnknownGlAccountClassNameException.class, eea, ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 084 } 085 086 return glAccountClass; 087 } 088 089 public GlAccountClass getGlAccountClassByName(final ExecutionErrorAccumulator eea, final String glAccountClassName) { 090 return getGlAccountClassByName(eea, glAccountClassName, EntityPermission.READ_ONLY); 091 } 092 093 public GlAccountClass getGlAccountClassByNameForUpdate(final ExecutionErrorAccumulator eea, final String glAccountClassName) { 094 return getGlAccountClassByName(eea, glAccountClassName, EntityPermission.READ_WRITE); 095 } 096 097 public GlAccountClass getGlAccountClassByUniversalSpec(final ExecutionErrorAccumulator eea, 098 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 099 GlAccountClass glAccountClass = null; 100 var glAccountClassName = universalSpec.getGlAccountClassName(); 101 var parameterCount = (glAccountClassName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 102 103 switch(parameterCount) { 104 case 0 -> { 105 if(allowDefault) { 106 glAccountClass = accountingControl.getDefaultGlAccountClass(entityPermission); 107 108 if(glAccountClass == null) { 109 handleExecutionError(UnknownDefaultGlAccountClassException.class, eea, ExecutionErrors.UnknownDefaultGlAccountClass.name()); 110 } 111 } else { 112 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 113 } 114 } 115 case 1 -> { 116 if(glAccountClassName == null) { 117 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 118 ComponentVendors.ECHO_THREE.name(), EntityTypes.GlAccountClass.name()); 119 120 if(eea == null || !eea.hasExecutionErrors()) { 121 glAccountClass = accountingControl.getGlAccountClassByEntityInstance(entityInstance, entityPermission); 122 } 123 } else { 124 glAccountClass = getGlAccountClassByName(eea, glAccountClassName, entityPermission); 125 } 126 } 127 default -> 128 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 129 } 130 131 return glAccountClass; 132 } 133 134 public GlAccountClass getGlAccountClassByUniversalSpec(final ExecutionErrorAccumulator eea, 135 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault) { 136 return getGlAccountClassByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 137 } 138 139 public GlAccountClass getGlAccountClassByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 140 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault) { 141 return getGlAccountClassByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 142 } 143 144 public void updateGlAccountClassFromValue(GlAccountClassDetailValue glAccountClassDetailValue, BasePK updatedBy) { 145 accountingControl.updateGlAccountClassFromValue(glAccountClassDetailValue, updatedBy); 146 } 147 148 public void deleteGlAccountClass(final ExecutionErrorAccumulator eea, final GlAccountClass glAccountClass, final BasePK deletedBy) { 149 accountingControl.deleteGlAccountClass(glAccountClass, deletedBy); 150 } 151 152}