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.payment.server.logic;
018
019import com.echothree.control.user.payment.common.spec.PaymentProcessorResultCodeUniversalSpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.payment.common.exception.DuplicatePaymentProcessorResultCodeNameException;
025import com.echothree.model.control.payment.common.exception.UnknownDefaultPaymentProcessorResultCodeException;
026import com.echothree.model.control.payment.common.exception.UnknownPaymentProcessorResultCodeNameException;
027import com.echothree.model.control.payment.server.control.PaymentProcessorResultCodeControl;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.model.data.payment.server.entity.PaymentProcessorResultCode;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.persistence.BasePK;
032import com.echothree.util.server.control.BaseLogic;
033import com.echothree.util.server.message.ExecutionErrorAccumulator;
034import com.echothree.util.server.persistence.EntityPermission;
035import javax.enterprise.context.ApplicationScoped;
036import javax.enterprise.inject.spi.CDI;
037import javax.inject.Inject;
038
039@ApplicationScoped
040public class PaymentProcessorResultCodeLogic
041    extends BaseLogic {
042
043    @Inject
044    PaymentProcessorResultCodeControl paymentProcessorResultCodeControl;
045
046    @Inject
047    EntityInstanceLogic entityInstanceLogic;
048
049    protected PaymentProcessorResultCodeLogic() {
050        super();
051    }
052
053    public static PaymentProcessorResultCodeLogic getInstance() {
054        return CDI.current().select(PaymentProcessorResultCodeLogic.class).get();
055    }
056
057    public PaymentProcessorResultCode createPaymentProcessorResultCode(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName,
058            final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
059            final BasePK createdBy) {
060        var paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByName(paymentProcessorResultCodeName);
061
062        if(paymentProcessorResultCode == null) {
063            paymentProcessorResultCode = paymentProcessorResultCodeControl.createPaymentProcessorResultCode(paymentProcessorResultCodeName, isDefault, sortOrder, createdBy);
064
065            if(description != null) {
066                paymentProcessorResultCodeControl.createPaymentProcessorResultCodeDescription(paymentProcessorResultCode, language, description, createdBy);
067            }
068        } else {
069            handleExecutionError(DuplicatePaymentProcessorResultCodeNameException.class, eea, ExecutionErrors.DuplicatePaymentProcessorResultCodeName.name(), paymentProcessorResultCodeName);
070        }
071
072        return paymentProcessorResultCode;
073    }
074
075    public PaymentProcessorResultCode getPaymentProcessorResultCodeByName(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName,
076            final EntityPermission entityPermission) {
077        var paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByName(paymentProcessorResultCodeName, entityPermission);
078
079        if(paymentProcessorResultCode == null) {
080            handleExecutionError(UnknownPaymentProcessorResultCodeNameException.class, eea, ExecutionErrors.UnknownPaymentProcessorResultCodeName.name(), paymentProcessorResultCodeName);
081        }
082
083        return paymentProcessorResultCode;
084    }
085
086    public PaymentProcessorResultCode getPaymentProcessorResultCodeByName(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName) {
087        return getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, EntityPermission.READ_ONLY);
088    }
089
090    public PaymentProcessorResultCode getPaymentProcessorResultCodeByNameForUpdate(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName) {
091        return getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, EntityPermission.READ_WRITE);
092    }
093
094    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
095            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
096        PaymentProcessorResultCode paymentProcessorResultCode = null;
097        var paymentProcessorResultCodeName = universalSpec.getPaymentProcessorResultCodeName();
098        var parameterCount = (paymentProcessorResultCodeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
099
100        switch(parameterCount) {
101            case 0 -> {
102                if(allowDefault) {
103                    paymentProcessorResultCode = paymentProcessorResultCodeControl.getDefaultPaymentProcessorResultCode(entityPermission);
104
105                    if(paymentProcessorResultCode == null) {
106                        handleExecutionError(UnknownDefaultPaymentProcessorResultCodeException.class, eea, ExecutionErrors.UnknownDefaultPaymentProcessorResultCode.name());
107                    }
108                } else {
109                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
110                }
111            }
112            case 1 -> {
113                if(paymentProcessorResultCodeName == null) {
114                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
115                            ComponentVendors.ECHO_THREE.name(), EntityTypes.PaymentProcessorResultCode.name());
116
117                    if(eea == null || !eea.hasExecutionErrors()) {
118                        paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByEntityInstance(entityInstance, entityPermission);
119                    }
120                } else {
121                    paymentProcessorResultCode = getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, entityPermission);
122                }
123            }
124            default ->
125                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
126        }
127
128        return paymentProcessorResultCode;
129    }
130
131    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
132            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault) {
133        return getPaymentProcessorResultCodeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
134    }
135
136    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
137            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault) {
138        return getPaymentProcessorResultCodeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
139    }
140
141    public void deletePaymentProcessorResultCode(final ExecutionErrorAccumulator eea, final PaymentProcessorResultCode paymentProcessorResultCode,
142            final BasePK deletedBy) {
143        paymentProcessorResultCodeControl.deletePaymentProcessorResultCode(paymentProcessorResultCode, deletedBy);
144    }
145}