001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.PrinterGroup;
023import com.echothree.model.data.printer.server.entity.PrinterGroupUseType;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.persistence.BasePK;
026import com.echothree.util.server.control.BaseLogic;
027import com.echothree.util.server.message.ExecutionErrorAccumulator;
028import com.echothree.util.server.persistence.Session;
029
030public class PartyPrinterGroupUseLogic
031        extends BaseLogic {
032
033    private PartyPrinterGroupUseLogic() {
034        super();
035    }
036
037    private static class PartyPrinterGroupUseLogicHolder {
038        static PartyPrinterGroupUseLogic instance = new PartyPrinterGroupUseLogic();
039    }
040
041    public static PartyPrinterGroupUseLogic getInstance() {
042        return PartyPrinterGroupUseLogicHolder.instance;
043    }
044
045    public PartyPrinterGroupUse getPartyPrinterGroupUse(final ExecutionErrorAccumulator ema, final Party party, final PrinterGroupUseType printerGroupUseType,
046            final BasePK createdBy) {
047        var printerControl = Session.getModelController(PrinterControl.class);
048        PartyPrinterGroupUse partyPrinterGroupUse = printerControl.getPartyPrinterGroupUse(party, printerGroupUseType);
049
050        if(partyPrinterGroupUse == null) {
051            PrinterGroup printerGroup = printerControl.getDefaultPrinterGroup();
052
053            if(printerGroup == null) {
054                addExecutionError(ema, ExecutionErrors.MissingDefaultPartyPrinterGroup.name());
055            } else {
056                partyPrinterGroupUse = printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, printerGroup, createdBy);
057            }
058        }
059
060        return partyPrinterGroupUse;
061    }
062
063    public PartyPrinterGroupUse getPartyPrinterGroupUseUsingNames(final ExecutionErrorAccumulator ema, final Party party, final String printerGroupUseTypeName,
064            final BasePK createdBy) {
065        var printerControl = Session.getModelController(PrinterControl.class);
066        PrinterGroupUseType printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(printerGroupUseTypeName);
067        PartyPrinterGroupUse partyPrinterGroupUse = null;
068
069        if(printerGroupUseType == null) {
070            addExecutionError(ema, ExecutionErrors.UnknownPrinterGroupUseTypeName.name(), printerGroupUseTypeName);
071        } else {
072            partyPrinterGroupUse = getPartyPrinterGroupUse(ema, party, printerGroupUseType, createdBy);
073        }
074
075        return partyPrinterGroupUse;
076    }
077
078}