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.chain.server.logic;
018
019import com.echothree.control.user.chain.common.spec.ChainKindUniversalSpec;
020import com.echothree.model.control.chain.common.exception.CannotDeleteChainKindInUseException;
021import com.echothree.model.control.chain.common.exception.DuplicateChainKindNameException;
022import com.echothree.model.control.chain.common.exception.UnknownChainKindNameException;
023import com.echothree.model.control.chain.common.exception.UnknownDefaultChainKindException;
024import com.echothree.model.control.chain.server.control.ChainControl;
025import com.echothree.model.control.core.common.ComponentVendors;
026import com.echothree.model.control.core.common.EntityTypes;
027import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
028import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
029import com.echothree.model.data.chain.server.entity.ChainKind;
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 ChainKindLogic
042        extends BaseLogic {
043
044    protected ChainKindLogic() {
045        super();
046    }
047
048    public static ChainKindLogic getInstance() {
049        return CDI.current().select(ChainKindLogic.class).get();
050    }
051
052    @Inject
053    ChainControl chainControl;
054
055    public ChainKind createChainKind(final ExecutionErrorAccumulator eea, final String chainKindName,
056            final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
057            final BasePK createdBy) {
058        var chainKind = chainControl.getChainKindByName(chainKindName);
059
060        if(chainKind == null) {
061            chainKind = chainControl.createChainKind(chainKindName, isDefault, sortOrder, createdBy);
062
063            if(description != null) {
064                chainControl.createChainKindDescription(chainKind, language, description, createdBy);
065            }
066        } else {
067            handleExecutionError(DuplicateChainKindNameException.class, eea, ExecutionErrors.DuplicateChainKindName.name(), chainKindName);
068        }
069
070        return chainKind;
071    }
072
073    public ChainKind getChainKindByName(final ExecutionErrorAccumulator eea, final String chainKindName,
074            final EntityPermission entityPermission) {
075        var chainKind = chainControl.getChainKindByName(chainKindName, entityPermission);
076
077        if(chainKind == null) {
078            handleExecutionError(UnknownChainKindNameException.class, eea, ExecutionErrors.UnknownChainKindName.name(), chainKindName);
079        }
080
081        return chainKind;
082    }
083
084    public ChainKind getChainKindByName(final ExecutionErrorAccumulator eea, final String chainKindName) {
085        return getChainKindByName(eea, chainKindName, EntityPermission.READ_ONLY);
086    }
087
088    public ChainKind getChainKindByNameForUpdate(final ExecutionErrorAccumulator eea, final String chainKindName) {
089        return getChainKindByName(eea, chainKindName, EntityPermission.READ_WRITE);
090    }
091
092    public ChainKind getChainKindByUniversalSpec(final ExecutionErrorAccumulator eea,
093            final ChainKindUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
094        ChainKind chainKind = null;
095        var chainKindName = universalSpec.getChainKindName();
096        var parameterCount = (chainKindName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
097
098        switch(parameterCount) {
099            case 0 -> {
100                if(allowDefault) {
101                    chainKind = chainControl.getDefaultChainKind(entityPermission);
102
103                    if(chainKind == null) {
104                        handleExecutionError(UnknownDefaultChainKindException.class, eea, ExecutionErrors.UnknownDefaultChainKind.name());
105                    }
106                } else {
107                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
108                }
109            }
110            case 1 -> {
111                if(chainKindName == null) {
112                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
113                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ChainKind.name());
114
115                    if(!eea.hasExecutionErrors()) {
116                        chainKind = chainControl.getChainKindByEntityInstance(entityInstance, entityPermission);
117                    }
118                } else {
119                    chainKind = getChainKindByName(eea, chainKindName, entityPermission);
120                }
121            }
122            default ->
123                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
124        }
125
126        return chainKind;
127    }
128
129    public ChainKind getChainKindByUniversalSpec(final ExecutionErrorAccumulator eea,
130            final ChainKindUniversalSpec universalSpec, boolean allowDefault) {
131        return getChainKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
132    }
133
134    public ChainKind getChainKindByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
135            final ChainKindUniversalSpec universalSpec, boolean allowDefault) {
136        return getChainKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
137    }
138
139    public void deleteChainKind(final ExecutionErrorAccumulator eea, final ChainKind chainKind,
140            final BasePK deletedBy) {
141        var chainTypeCount = chainControl.countChainTypesByChainKind(chainKind);
142
143        if(chainTypeCount == 0) {
144            chainControl.deleteChainKind(chainKind, deletedBy);
145        } else {
146            handleExecutionError(CannotDeleteChainKindInUseException.class, eea, ExecutionErrors.CannotDeleteChainKindInUse.name(),
147                    chainKind.getLastDetail().getChainKindName());
148        }
149    }
150
151}