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.offer.server.logic;
018
019import com.echothree.control.user.offer.common.spec.OfferNameElementUniversalSpec;
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.offer.common.exception.DuplicateOfferNameElementNameException;
025import com.echothree.model.control.offer.common.exception.UnknownOfferNameElementNameException;
026import com.echothree.model.control.offer.server.control.OfferNameElementControl;
027import com.echothree.model.data.offer.server.entity.OfferNameElement;
028import com.echothree.model.data.party.server.entity.Language;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.EntityPermission;
034import javax.enterprise.context.ApplicationScoped;
035import javax.enterprise.inject.spi.CDI;
036import javax.inject.Inject;
037
038@ApplicationScoped
039public class OfferNameElementLogic
040        extends BaseLogic {
041
042    @Inject
043    OfferNameElementControl offerNameElementControl;
044
045    @Inject
046    EntityInstanceLogic entityInstanceLogic;
047
048    protected OfferNameElementLogic() {
049        super();
050    }
051
052    public static OfferNameElementLogic getInstance() {
053        return CDI.current().select(OfferNameElementLogic.class).get();
054    }
055
056    public OfferNameElement createOfferNameElement(final ExecutionErrorAccumulator eea, final String offerNameElementName,
057            final Integer offset, final Integer length, final String validationPattern, final Language language, final String description,
058            final BasePK createdBy) {
059        var offerNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName);
060
061        if(offerNameElement == null) {
062            offerNameElement = offerNameElementControl.createOfferNameElement(offerNameElementName, offset, length, validationPattern,
063                    createdBy);
064
065            if(description != null) {
066                offerNameElementControl.createOfferNameElementDescription(offerNameElement, language, description, createdBy);
067            }
068        } else {
069            handleExecutionError(DuplicateOfferNameElementNameException.class, eea, ExecutionErrors.DuplicateOfferNameElementName.name(),
070                    offerNameElementName);
071        }
072
073        return offerNameElement;
074    }
075
076    public OfferNameElement getOfferNameElementByName(final ExecutionErrorAccumulator eea, final String offerNameElementName,
077            final EntityPermission entityPermission) {
078        var offerNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName, entityPermission);
079
080        if(offerNameElement == null) {
081            handleExecutionError(UnknownOfferNameElementNameException.class, eea, ExecutionErrors.UnknownOfferNameElementName.name(), offerNameElementName);
082        }
083
084        return offerNameElement;
085    }
086
087    public OfferNameElement getOfferNameElementByName(final ExecutionErrorAccumulator eea, final String offerNameElementName) {
088        return getOfferNameElementByName(eea, offerNameElementName, EntityPermission.READ_ONLY);
089    }
090
091    public OfferNameElement getOfferNameElementByNameForUpdate(final ExecutionErrorAccumulator eea, final String offerNameElementName) {
092        return getOfferNameElementByName(eea, offerNameElementName, EntityPermission.READ_WRITE);
093    }
094
095    public OfferNameElement getOfferNameElementByUniversalSpec(final ExecutionErrorAccumulator eea,
096            final OfferNameElementUniversalSpec universalSpec, final EntityPermission entityPermission) {
097        var offerNameElementName = universalSpec.getOfferNameElementName();
098        var parameterCount = (offerNameElementName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
099        OfferNameElement offerNameElement = null;
100
101        if(parameterCount == 1) {
102            if(offerNameElementName == null) {
103                var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
104                        ComponentVendors.ECHO_THREE.name(), EntityTypes.OfferNameElement.name());
105
106                if(eea == null || !eea.hasExecutionErrors()) {
107                    offerNameElement = offerNameElementControl.getOfferNameElementByEntityInstance(entityInstance, entityPermission);
108                }
109            } else {
110                offerNameElement = getOfferNameElementByName(eea, offerNameElementName, entityPermission);
111            }
112        } else {
113            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
114        }
115
116        return offerNameElement;
117    }
118
119    public OfferNameElement getOfferNameElementByUniversalSpec(final ExecutionErrorAccumulator eea,
120            final OfferNameElementUniversalSpec universalSpec) {
121        return getOfferNameElementByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
122    }
123
124    public OfferNameElement getOfferNameElementByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
125            final OfferNameElementUniversalSpec universalSpec) {
126        return getOfferNameElementByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
127    }
128
129    public void deleteOfferNameElement(final ExecutionErrorAccumulator eea, final OfferNameElement offerNameElement,
130            final BasePK deletedBy) {
131        offerNameElementControl.deleteOfferNameElement(offerNameElement, deletedBy);
132    }
133
134}