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.printer.server.logic;
018
019import com.echothree.model.control.printer.server.control.PrinterControl;
020import com.echothree.model.data.party.server.entity.Party;
021import com.echothree.model.data.printer.server.entity.PartyPrinterGroupUse;
022import com.echothree.model.data.printer.server.entity.PrinterGroupUseType;
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 PartyPrinterGroupUseLogic
033        extends BaseLogic {
034
035    protected PartyPrinterGroupUseLogic() {
036        super();
037    }
038
039    public static PartyPrinterGroupUseLogic getInstance() {
040        return CDI.current().select(PartyPrinterGroupUseLogic.class).get();
041    }
042
043    public PartyPrinterGroupUse getPartyPrinterGroupUse(final ExecutionErrorAccumulator ema, final Party party, final PrinterGroupUseType printerGroupUseType,
044            final BasePK createdBy) {
045        var printerControl = Session.getModelController(PrinterControl.class);
046        var partyPrinterGroupUse = printerControl.getPartyPrinterGroupUse(party, printerGroupUseType);
047
048        if(partyPrinterGroupUse == null) {
049            var printerGroup = printerControl.getDefaultPrinterGroup();
050
051            if(printerGroup == null) {
052                addExecutionError(ema, ExecutionErrors.MissingDefaultPartyPrinterGroup.name());
053            } else {
054                partyPrinterGroupUse = printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, printerGroup, createdBy);
055            }
056        }
057
058        return partyPrinterGroupUse;
059    }
060
061    public PartyPrinterGroupUse getPartyPrinterGroupUseUsingNames(final ExecutionErrorAccumulator ema, final Party party, final String printerGroupUseTypeName,
062            final BasePK createdBy) {
063        var printerControl = Session.getModelController(PrinterControl.class);
064        var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(printerGroupUseTypeName);
065        PartyPrinterGroupUse partyPrinterGroupUse = null;
066
067        if(printerGroupUseType == null) {
068            addExecutionError(ema, ExecutionErrors.UnknownPrinterGroupUseTypeName.name(), printerGroupUseTypeName);
069        } else {
070            partyPrinterGroupUse = getPartyPrinterGroupUse(ema, party, printerGroupUseType, createdBy);
071        }
072
073        return partyPrinterGroupUse;
074    }
075
076}