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.inject.Inject;
037
038@ApplicationScoped
039public class PaymentProcessorResultCodeLogic
040    extends BaseLogic {
041
042    @Inject
043    PaymentProcessorResultCodeControl paymentProcessorResultCodeControl;
044
045    @Inject
046    EntityInstanceLogic entityInstanceLogic;
047
048    protected PaymentProcessorResultCodeLogic() {
049        super();
050    }
051
052    public PaymentProcessorResultCode createPaymentProcessorResultCode(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName,
053            final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
054            final BasePK createdBy) {
055        var paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByName(paymentProcessorResultCodeName);
056
057        if(paymentProcessorResultCode == null) {
058            paymentProcessorResultCode = paymentProcessorResultCodeControl.createPaymentProcessorResultCode(paymentProcessorResultCodeName, isDefault, sortOrder, createdBy);
059
060            if(description != null) {
061                paymentProcessorResultCodeControl.createPaymentProcessorResultCodeDescription(paymentProcessorResultCode, language, description, createdBy);
062            }
063        } else {
064            handleExecutionError(DuplicatePaymentProcessorResultCodeNameException.class, eea, ExecutionErrors.DuplicatePaymentProcessorResultCodeName.name(), paymentProcessorResultCodeName);
065        }
066
067        return paymentProcessorResultCode;
068    }
069
070    public PaymentProcessorResultCode getPaymentProcessorResultCodeByName(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName,
071            final EntityPermission entityPermission) {
072        var paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByName(paymentProcessorResultCodeName, entityPermission);
073
074        if(paymentProcessorResultCode == null) {
075            handleExecutionError(UnknownPaymentProcessorResultCodeNameException.class, eea, ExecutionErrors.UnknownPaymentProcessorResultCodeName.name(), paymentProcessorResultCodeName);
076        }
077
078        return paymentProcessorResultCode;
079    }
080
081    public PaymentProcessorResultCode getPaymentProcessorResultCodeByName(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName) {
082        return getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, EntityPermission.READ_ONLY);
083    }
084
085    public PaymentProcessorResultCode getPaymentProcessorResultCodeByNameForUpdate(final ExecutionErrorAccumulator eea, final String paymentProcessorResultCodeName) {
086        return getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, EntityPermission.READ_WRITE);
087    }
088
089    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
090            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
091        PaymentProcessorResultCode paymentProcessorResultCode = null;
092        var paymentProcessorResultCodeName = universalSpec.getPaymentProcessorResultCodeName();
093        var parameterCount = (paymentProcessorResultCodeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
094
095        switch(parameterCount) {
096            case 0 -> {
097                if(allowDefault) {
098                    paymentProcessorResultCode = paymentProcessorResultCodeControl.getDefaultPaymentProcessorResultCode(entityPermission);
099
100                    if(paymentProcessorResultCode == null) {
101                        handleExecutionError(UnknownDefaultPaymentProcessorResultCodeException.class, eea, ExecutionErrors.UnknownDefaultPaymentProcessorResultCode.name());
102                    }
103                } else {
104                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
105                }
106            }
107            case 1 -> {
108                if(paymentProcessorResultCodeName == null) {
109                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
110                            ComponentVendors.ECHO_THREE.name(), EntityTypes.PaymentProcessorResultCode.name());
111
112                    if(eea == null || !eea.hasExecutionErrors()) {
113                        paymentProcessorResultCode = paymentProcessorResultCodeControl.getPaymentProcessorResultCodeByEntityInstance(entityInstance, entityPermission);
114                    }
115                } else {
116                    paymentProcessorResultCode = getPaymentProcessorResultCodeByName(eea, paymentProcessorResultCodeName, entityPermission);
117                }
118            }
119            default ->
120                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
121        }
122
123        return paymentProcessorResultCode;
124    }
125
126    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpec(final ExecutionErrorAccumulator eea,
127            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault) {
128        return getPaymentProcessorResultCodeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
129    }
130
131    public PaymentProcessorResultCode getPaymentProcessorResultCodeByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
132            final PaymentProcessorResultCodeUniversalSpec universalSpec, boolean allowDefault) {
133        return getPaymentProcessorResultCodeByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
134    }
135
136    public void deletePaymentProcessorResultCode(final ExecutionErrorAccumulator eea, final PaymentProcessorResultCode paymentProcessorResultCode,
137            final BasePK deletedBy) {
138        paymentProcessorResultCodeControl.deletePaymentProcessorResultCode(paymentProcessorResultCode, deletedBy);
139    }
140}