001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.scale.server.logic;
018
019import com.echothree.model.control.scale.server.control.ScaleControl;
020import com.echothree.model.data.party.server.entity.Party;
021import com.echothree.model.data.scale.server.entity.PartyScaleUse;
022import com.echothree.model.data.scale.server.entity.ScaleUseType;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.common.persistence.BasePK;
025import com.echothree.util.server.control.BaseLogic;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.ApplicationScoped;
029import javax.enterprise.inject.spi.CDI;
030
031@ApplicationScoped
032public class PartyScaleUseLogic
033        extends BaseLogic {
034
035    protected PartyScaleUseLogic() {
036        super();
037    }
038
039    public static PartyScaleUseLogic getInstance() {
040        return CDI.current().select(PartyScaleUseLogic.class).get();
041    }
042
043    public PartyScaleUse getPartyScaleUse(final ExecutionErrorAccumulator ema, final Party party, final ScaleUseType scaleUseType,
044            final BasePK createdBy) {
045        var scaleControl = Session.getModelController(ScaleControl.class);
046        var partyScaleUse = scaleControl.getPartyScaleUse(party, scaleUseType);
047
048        if(partyScaleUse == null) {
049            var scale = scaleControl.getDefaultScale();
050
051            if(scale == null) {
052                addExecutionError(ema, ExecutionErrors.MissingDefaultPartyScale.name());
053            } else {
054                partyScaleUse = scaleControl.createPartyScaleUse(party, scaleUseType, scale, createdBy);
055            }
056        }
057
058        return partyScaleUse;
059    }
060
061    public PartyScaleUse getPartyScaleUseUsingNames(final ExecutionErrorAccumulator ema, final Party party, final String scaleUseTypeName,
062            final BasePK createdBy) {
063        var scaleControl = Session.getModelController(ScaleControl.class);
064        var scaleUseType = scaleControl.getScaleUseTypeByName(scaleUseTypeName);
065        PartyScaleUse partyScaleUse = null;
066
067        if(scaleUseType == null) {
068            addExecutionError(ema, ExecutionErrors.UnknownScaleUseTypeName.name(), scaleUseTypeName);
069        } else {
070            partyScaleUse = getPartyScaleUse(ema, party, scaleUseType, createdBy);
071        }
072
073        return partyScaleUse;
074    }
075
076}