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