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.GlAccountClassDescriptionSpec; 020import com.echothree.control.user.accounting.common.spec.GlAccountClassUniversalSpec; 021import com.echothree.model.control.accounting.common.exception.DuplicateGlAccountClassNameException; 022import com.echothree.model.control.accounting.common.exception.UnknownDefaultGlAccountClassException; 023import com.echothree.model.control.accounting.common.exception.UnknownGlAccountClassDescriptionException; 024import com.echothree.model.control.accounting.common.exception.UnknownGlAccountClassNameException; 025import com.echothree.model.control.accounting.server.control.AccountingControl; 026import com.echothree.model.control.core.common.ComponentVendors; 027import com.echothree.model.control.core.common.EntityTypes; 028import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 029import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 030import com.echothree.model.control.party.server.logic.LanguageLogic; 031import com.echothree.model.data.accounting.server.entity.GlAccountClass; 032import com.echothree.model.data.accounting.server.entity.GlAccountClassDescription; 033import com.echothree.model.data.accounting.server.value.GlAccountClassDetailValue; 034import com.echothree.model.data.party.server.entity.Language; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.persistence.BasePK; 037import com.echothree.util.server.control.BaseLogic; 038import com.echothree.util.server.message.ExecutionErrorAccumulator; 039import com.echothree.util.server.persistence.EntityPermission; 040import javax.enterprise.context.ApplicationScoped; 041import javax.inject.Inject; 042 043@ApplicationScoped 044public class GlAccountClassLogic 045 extends BaseLogic { 046 047 @Inject 048 AccountingControl accountingControl; 049 050 @Inject 051 EntityInstanceLogic entityInstanceLogic; 052 053 @Inject 054 LanguageLogic languageLogic; 055 056 protected GlAccountClassLogic() { 057 super(); 058 } 059 060 public GlAccountClass createGlAccountClass(final ExecutionErrorAccumulator eea, final String glAccountClassName, 061 final GlAccountClass parentGlAccountClass, final Boolean isDefault, final Integer sortOrder, final Language language, 062 final String description, final BasePK createdBy) { 063 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 064 065 if(glAccountClass == null) { 066 glAccountClass = accountingControl.createGlAccountClass(glAccountClassName, 067 parentGlAccountClass, isDefault, sortOrder, createdBy); 068 069 if(description != null) { 070 accountingControl.createGlAccountClassDescription(glAccountClass, language, description, createdBy); 071 } 072 } else { 073 handleExecutionError(DuplicateGlAccountClassNameException.class, eea, ExecutionErrors.DuplicateGlAccountClassName.name(), 074 glAccountClassName); 075 } 076 077 return glAccountClass; 078 } 079 080 public GlAccountClass getGlAccountClassByName(final ExecutionErrorAccumulator eea, final String glAccountClassName, 081 final EntityPermission entityPermission) { 082 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName, entityPermission); 083 084 if(glAccountClass == null) { 085 handleExecutionError(UnknownGlAccountClassNameException.class, eea, ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 086 } 087 088 return glAccountClass; 089 } 090 091 public GlAccountClass getGlAccountClassByName(final ExecutionErrorAccumulator eea, final String glAccountClassName) { 092 return getGlAccountClassByName(eea, glAccountClassName, EntityPermission.READ_ONLY); 093 } 094 095 public GlAccountClass getGlAccountClassByNameForUpdate(final ExecutionErrorAccumulator eea, final String glAccountClassName) { 096 return getGlAccountClassByName(eea, glAccountClassName, EntityPermission.READ_WRITE); 097 } 098 099 public GlAccountClass getGlAccountClassByUniversalSpec(final ExecutionErrorAccumulator eea, 100 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 101 GlAccountClass glAccountClass = null; 102 var glAccountClassName = universalSpec.getGlAccountClassName(); 103 var parameterCount = (glAccountClassName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 104 105 switch(parameterCount) { 106 case 0 -> { 107 if(allowDefault) { 108 glAccountClass = accountingControl.getDefaultGlAccountClass(entityPermission); 109 110 if(glAccountClass == null) { 111 handleExecutionError(UnknownDefaultGlAccountClassException.class, eea, ExecutionErrors.UnknownDefaultGlAccountClass.name()); 112 } 113 } else { 114 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 115 } 116 } 117 case 1 -> { 118 if(glAccountClassName == null) { 119 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 120 ComponentVendors.ECHO_THREE.name(), EntityTypes.GlAccountClass.name()); 121 122 if(eea == null || !eea.hasExecutionErrors()) { 123 glAccountClass = accountingControl.getGlAccountClassByEntityInstance(entityInstance, entityPermission); 124 } 125 } else { 126 glAccountClass = getGlAccountClassByName(eea, glAccountClassName, entityPermission); 127 } 128 } 129 default -> 130 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 131 } 132 133 return glAccountClass; 134 } 135 136 public GlAccountClass getGlAccountClassByUniversalSpec(final ExecutionErrorAccumulator eea, 137 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault) { 138 return getGlAccountClassByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 139 } 140 141 public GlAccountClass getGlAccountClassByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 142 final GlAccountClassUniversalSpec universalSpec, boolean allowDefault) { 143 return getGlAccountClassByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 144 } 145 146 public GlAccountClassDescription getGlAccountClassDescriptionBySpec(final ExecutionErrorAccumulator eea, 147 final GlAccountClassDescriptionSpec spec, final EntityPermission entityPermission) { 148 GlAccountClassDescription glAccountClassDescription = null; 149 var glAccountClassName = spec.getGlAccountClassName(); 150 var glAccountClass = getGlAccountClassByName(eea, glAccountClassName); 151 152 if(!hasExecutionErrors(eea)) { 153 var languageIsoName = spec.getLanguageIsoName(); 154 var language = languageLogic.getLanguageByName(eea, languageIsoName); 155 156 if(!hasExecutionErrors(eea)) { 157 glAccountClassDescription = accountingControl.getGlAccountClassDescription(glAccountClass, language, entityPermission); 158 159 if(glAccountClassDescription == null) { 160 handleExecutionError(UnknownGlAccountClassDescriptionException.class, eea, ExecutionErrors.UnknownGlAccountClassDescription.name(), glAccountClassName, languageIsoName); 161 } 162 } 163 } 164 165 return glAccountClassDescription; 166 } 167 168 public void updateGlAccountClassFromValue(GlAccountClassDetailValue glAccountClassDetailValue, BasePK updatedBy) { 169 accountingControl.updateGlAccountClassFromValue(glAccountClassDetailValue, updatedBy); 170 } 171 172 public void deleteGlAccountClass(final ExecutionErrorAccumulator eea, final GlAccountClass glAccountClass, final BasePK deletedBy) { 173 accountingControl.deleteGlAccountClass(glAccountClass, deletedBy); 174 } 175 176}