001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.GlAccountCategoryUniversalSpec; 020import com.echothree.model.control.accounting.common.exception.DuplicateGlAccountCategoryNameException; 021import com.echothree.model.control.accounting.common.exception.UnknownDefaultGlAccountCategoryException; 022import com.echothree.model.control.accounting.common.exception.UnknownGlAccountCategoryNameException; 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.GlAccountCategory; 029import com.echothree.model.data.accounting.server.value.GlAccountCategoryDetailValue; 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; 037 038public class GlAccountCategoryLogic 039 extends BaseLogic { 040 041 private GlAccountCategoryLogic() { 042 super(); 043 } 044 045 private static class GlAccountCategoryLogicHolder { 046 static GlAccountCategoryLogic instance = new GlAccountCategoryLogic(); 047 } 048 049 public static GlAccountCategoryLogic getInstance() { 050 return GlAccountCategoryLogicHolder.instance; 051 } 052 053 public GlAccountCategory createGlAccountCategory(final ExecutionErrorAccumulator eea, final String glAccountCategoryName, 054 final GlAccountCategory parentGlAccountCategory, final Boolean isDefault, final Integer sortOrder, final Language language, 055 final String description, final BasePK createdBy) { 056 var accountingControl = Session.getModelController(AccountingControl.class); 057 var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 058 059 if(glAccountCategory == null) { 060 glAccountCategory = accountingControl.createGlAccountCategory(glAccountCategoryName, 061 parentGlAccountCategory, isDefault, sortOrder, createdBy); 062 063 if(description != null) { 064 accountingControl.createGlAccountCategoryDescription(glAccountCategory, language, description, createdBy); 065 } 066 } else { 067 handleExecutionError(DuplicateGlAccountCategoryNameException.class, eea, ExecutionErrors.DuplicateGlAccountCategoryName.name(), 068 glAccountCategoryName); 069 } 070 071 return glAccountCategory; 072 } 073 074 public GlAccountCategory getGlAccountCategoryByName(final ExecutionErrorAccumulator eea, final String glAccountCategoryName, 075 final EntityPermission entityPermission) { 076 var accountingControl = Session.getModelController(AccountingControl.class); 077 var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName, entityPermission); 078 079 if(glAccountCategory == null) { 080 handleExecutionError(UnknownGlAccountCategoryNameException.class, eea, ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 081 } 082 083 return glAccountCategory; 084 } 085 086 public GlAccountCategory getGlAccountCategoryByName(final ExecutionErrorAccumulator eea, final String glAccountCategoryName) { 087 return getGlAccountCategoryByName(eea, glAccountCategoryName, EntityPermission.READ_ONLY); 088 } 089 090 public GlAccountCategory getGlAccountCategoryByNameForUpdate(final ExecutionErrorAccumulator eea, final String glAccountCategoryName) { 091 return getGlAccountCategoryByName(eea, glAccountCategoryName, EntityPermission.READ_WRITE); 092 } 093 094 public GlAccountCategory getGlAccountCategoryByUniversalSpec(final ExecutionErrorAccumulator eea, 095 final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 096 GlAccountCategory glAccountCategory = null; 097 var accountingControl = Session.getModelController(AccountingControl.class); 098 var glAccountCategoryName = universalSpec.getGlAccountCategoryName(); 099 var parameterCount = (glAccountCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 100 101 switch(parameterCount) { 102 case 0 -> { 103 if(allowDefault) { 104 glAccountCategory = accountingControl.getDefaultGlAccountCategory(entityPermission); 105 106 if(glAccountCategory == null) { 107 handleExecutionError(UnknownDefaultGlAccountCategoryException.class, eea, ExecutionErrors.UnknownDefaultGlAccountCategory.name()); 108 } 109 } else { 110 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 111 } 112 } 113 case 1 -> { 114 if(glAccountCategoryName == null) { 115 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 116 ComponentVendors.ECHO_THREE.name(), EntityTypes.GlAccountCategory.name()); 117 118 if(!eea.hasExecutionErrors()) { 119 glAccountCategory = accountingControl.getGlAccountCategoryByEntityInstance(entityInstance, entityPermission); 120 } 121 } else { 122 glAccountCategory = getGlAccountCategoryByName(eea, glAccountCategoryName, entityPermission); 123 } 124 } 125 default -> 126 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 127 } 128 129 return glAccountCategory; 130 } 131 132 public GlAccountCategory getGlAccountCategoryByUniversalSpec(final ExecutionErrorAccumulator eea, 133 final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault) { 134 return getGlAccountCategoryByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 135 } 136 137 public GlAccountCategory getGlAccountCategoryByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 138 final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault) { 139 return getGlAccountCategoryByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 140 } 141 142 public void updateGlAccountCategoryFromValue(GlAccountCategoryDetailValue glAccountCategoryDetailValue, BasePK updatedBy) { 143 var accountingControl = Session.getModelController(AccountingControl.class); 144 145 accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, updatedBy); 146 } 147 148 public void deleteGlAccountCategory(final ExecutionErrorAccumulator eea, final GlAccountCategory glAccountCategory, final BasePK deletedBy) { 149 var accountingControl = Session.getModelController(AccountingControl.class); 150 151 accountingControl.deleteGlAccountCategory(glAccountCategory, deletedBy); 152 } 153 154}