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