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.UseUniversalSpec;
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.CannotDeleteUseInUseException;
025import com.echothree.model.control.offer.common.exception.DuplicateUseNameException;
026import com.echothree.model.control.offer.common.exception.UnknownDefaultUseException;
027import com.echothree.model.control.offer.common.exception.UnknownUseNameException;
028import com.echothree.model.control.offer.server.control.OfferUseControl;
029import com.echothree.model.control.offer.server.control.UseControl;
030import com.echothree.model.data.offer.server.entity.Use;
031import com.echothree.model.data.offer.server.entity.UseType;
032import com.echothree.model.data.offer.server.value.UseDetailValue;
033import com.echothree.model.data.party.server.entity.Language;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.persistence.BasePK;
036import com.echothree.util.server.control.BaseLogic;
037import com.echothree.util.server.message.ExecutionErrorAccumulator;
038import com.echothree.util.server.persistence.EntityPermission;
039import javax.enterprise.context.ApplicationScoped;
040import javax.inject.Inject;
041
042@ApplicationScoped
043public class UseLogic
044        extends BaseLogic {
045
046    @Inject
047    OfferUseControl offerUseControl;
048
049    @Inject
050    UseControl useControl;
051
052    @Inject
053    EntityInstanceLogic entityInstanceLogic;
054
055    protected UseLogic() {
056        super();
057    }
058
059    public Use createUse(final ExecutionErrorAccumulator eea, final String useName, final UseType useType,
060            final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
061            final BasePK createdBy) {
062        var use = useControl.getUseByName(useName);
063
064        if(use == null) {
065            use = useControl.createUse(useName, useType, isDefault, sortOrder, createdBy);
066
067            if(description != null) {
068                useControl.createUseDescription(use, language, description, createdBy);
069            }
070        } else {
071            handleExecutionError(DuplicateUseNameException.class, eea, ExecutionErrors.DuplicateUseName.name(), useName);
072        }
073
074        return use;
075    }
076
077    public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName,
078            final EntityPermission entityPermission) {
079        var use = useControl.getUseByName(useName, entityPermission);
080
081        if(use == null) {
082            handleExecutionError(UnknownUseNameException.class, eea, ExecutionErrors.UnknownUseName.name(), useName);
083        }
084
085        return use;
086    }
087
088    public Use getUseByName(final ExecutionErrorAccumulator eea, final String useName) {
089        return getUseByName(eea, useName, EntityPermission.READ_ONLY);
090    }
091
092    public Use getUseByNameForUpdate(final ExecutionErrorAccumulator eea, final String useName) {
093        return getUseByName(eea, useName, EntityPermission.READ_WRITE);
094    }
095
096    public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea,
097            final UseUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
098        var useName = universalSpec.getUseName();
099        var parameterCount = (useName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
100        Use use = null;
101
102        switch(parameterCount) {
103            case 0 -> {
104                if(allowDefault) {
105                    use = useControl.getDefaultUse(entityPermission);
106
107                    if(use == null) {
108                        handleExecutionError(UnknownDefaultUseException.class, eea, ExecutionErrors.UnknownDefaultUse.name());
109                    }
110                } else {
111                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
112                }
113            }
114            case 1 -> {
115                if(useName == null) {
116                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
117                            ComponentVendors.ECHO_THREE.name(), EntityTypes.Use.name());
118
119                    if(eea == null || !eea.hasExecutionErrors()) {
120                        use = useControl.getUseByEntityInstance(entityInstance, entityPermission);
121                    }
122                } else {
123                    use = getUseByName(eea, useName, entityPermission);
124                }
125            }
126            default ->
127                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
128        }
129
130        return use;
131    }
132
133    public Use getUseByUniversalSpec(final ExecutionErrorAccumulator eea,
134            final UseUniversalSpec universalSpec, boolean allowDefault) {
135        return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
136    }
137
138    public Use getUseByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
139            final UseUniversalSpec universalSpec, boolean allowDefault) {
140        return getUseByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
141    }
142
143    public void updateUseFromValue(UseDetailValue useDetailValue, BasePK updatedBy) {
144        useControl.updateUseFromValue(useDetailValue, updatedBy);
145    }
146
147    public void deleteUse(final ExecutionErrorAccumulator eea, final Use use, final BasePK deletedBy) {
148        if(offerUseControl.countOfferUsesByUse(use) == 0) {
149            useControl.deleteUse(use, deletedBy);
150        } else {
151            handleExecutionError(CannotDeleteUseInUseException.class, eea, ExecutionErrors.CannotDeleteUseInUse.name());
152        }
153    }
154    
155}