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.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 javax.enterprise.context.ApplicationScoped;
028import javax.inject.Inject;
029
030@ApplicationScoped
031public class PartyPrinterGroupUseLogic
032        extends BaseLogic {
033
034    @Inject
035    PrinterControl printerControl;
036
037    protected PartyPrinterGroupUseLogic() {
038        super();
039    }
040
041    public PartyPrinterGroupUse getPartyPrinterGroupUse(final ExecutionErrorAccumulator ema, final Party party, final PrinterGroupUseType printerGroupUseType,
042            final BasePK createdBy) {
043        var partyPrinterGroupUse = printerControl.getPartyPrinterGroupUse(party, printerGroupUseType);
044
045        if(partyPrinterGroupUse == null) {
046            var printerGroup = printerControl.getDefaultPrinterGroup();
047
048            if(printerGroup == null) {
049                addExecutionError(ema, ExecutionErrors.MissingDefaultPartyPrinterGroup.name());
050            } else {
051                partyPrinterGroupUse = printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, printerGroup, createdBy);
052            }
053        }
054
055        return partyPrinterGroupUse;
056    }
057
058    public PartyPrinterGroupUse getPartyPrinterGroupUseUsingNames(final ExecutionErrorAccumulator ema, final Party party, final String printerGroupUseTypeName,
059            final BasePK createdBy) {
060        var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(printerGroupUseTypeName);
061        PartyPrinterGroupUse partyPrinterGroupUse = null;
062
063        if(printerGroupUseType == null) {
064            addExecutionError(ema, ExecutionErrors.UnknownPrinterGroupUseTypeName.name(), printerGroupUseTypeName);
065        } else {
066            partyPrinterGroupUse = getPartyPrinterGroupUse(ema, party, printerGroupUseType, createdBy);
067        }
068
069        return partyPrinterGroupUse;
070    }
071
072}