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.cancellationpolicy.server.logic;
018
019import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindUniversalSpec;
020import com.echothree.model.control.cancellationpolicy.common.exception.DuplicateCancellationKindNameException;
021import com.echothree.model.control.cancellationpolicy.common.exception.UnknownCancellationKindNameException;
022import com.echothree.model.control.cancellationpolicy.common.exception.UnknownDefaultCancellationKindException;
023import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
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.cancellationpolicy.server.entity.CancellationKind;
029import com.echothree.model.data.party.server.entity.Language;
030import com.echothree.model.data.sequence.server.entity.SequenceType;
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.inject.Inject;
038
039@ApplicationScoped
040public class CancellationKindLogic
041        extends BaseLogic {
042
043    @Inject
044    CancellationPolicyControl cancellationPolicyControl;
045
046    @Inject
047    EntityInstanceLogic entityInstanceLogic;
048
049    protected CancellationKindLogic() {
050        super();
051    }
052
053    public CancellationKind createCancellationKind(final ExecutionErrorAccumulator eea, final String cancellationKindName,
054            final SequenceType cancellationSequenceType, final Boolean isDefault, final Integer sortOrder, final Language language,
055            final String description, final BasePK createdBy) {
056        var cancellationControl = cancellationPolicyControl;
057        var cancellationKind = cancellationControl.getCancellationKindByName(cancellationKindName);
058
059        if(cancellationKind == null) {
060            cancellationKind = cancellationControl.createCancellationKind(cancellationKindName, cancellationSequenceType,
061                    isDefault, sortOrder, createdBy);
062
063            if(description != null) {
064                cancellationControl.createCancellationKindDescription(cancellationKind, language, description, createdBy);
065            }
066        } else {
067            handleExecutionError(DuplicateCancellationKindNameException.class, eea, ExecutionErrors.DuplicateCancellationKindName.name(), cancellationKindName);
068        }
069
070        return cancellationKind;
071    }
072
073    public CancellationKind getCancellationKindByName(final ExecutionErrorAccumulator eea, final String cancellationKindName,
074            final EntityPermission entityPermission) {
075        var cancellationControl = cancellationPolicyControl;
076        var cancellationKind = cancellationControl.getCancellationKindByName(cancellationKindName, entityPermission);
077
078        if(cancellationKind == null) {
079            handleExecutionError(UnknownCancellationKindNameException.class, eea, ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
080        }
081
082        return cancellationKind;
083    }
084
085    public CancellationKind getCancellationKindByName(final ExecutionErrorAccumulator eea, final String cancellationKindName) {
086        return getCancellationKindByName(eea, cancellationKindName, EntityPermission.READ_ONLY);
087    }
088
089    public CancellationKind getCancellationKindByNameForUpdate(final ExecutionErrorAccumulator eea, final String cancellationKindName) {
090        return getCancellationKindByName(eea, cancellationKindName, EntityPermission.READ_WRITE);
091    }
092
093    public CancellationKind getCancellationKindByUniversalSpec(final ExecutionErrorAccumulator eea,
094            final CancellationKindUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
095        CancellationKind cancellationKind = null;
096        var cancellationControl = cancellationPolicyControl;
097        var cancellationKindName = universalSpec.getCancellationKindName();
098        var parameterCount = (cancellationKindName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
099
100        switch(parameterCount) {
101            case 0 -> {
102                if(allowDefault) {
103                    cancellationKind = cancellationControl.getDefaultCancellationKind(entityPermission);
104
105                    if(cancellationKind == null) {
106                        handleExecutionError(UnknownDefaultCancellationKindException.class, eea, ExecutionErrors.UnknownDefaultCancellationKind.name());
107                    }
108                } else {
109                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
110                }
111            }
112            case 1 -> {
113                if(cancellationKindName == null) {
114                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
115                            ComponentVendors.ECHO_THREE.name(), EntityTypes.CancellationKind.name());
116
117                    if(eea == null || !eea.hasExecutionErrors()) {
118                        cancellationKind = cancellationControl.getCancellationKindByEntityInstance(entityInstance, entityPermission);
119                    }
120                } else {
121                    cancellationKind = getCancellationKindByName(eea, cancellationKindName, entityPermission);
122                }
123            }
124            default ->
125                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
126        }
127
128        return cancellationKind;
129    }
130
131    public CancellationKind getCancellationKindByUniversalSpec(final ExecutionErrorAccumulator eea,
132            final CancellationKindUniversalSpec universalSpec, boolean allowDefault) {
133        return getCancellationKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
134    }
135
136    public CancellationKind getCancellationKindByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
137            final CancellationKindUniversalSpec universalSpec, boolean allowDefault) {
138        return getCancellationKindByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
139    }
140
141}