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