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