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.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.printer.common.PrinterOptions;
021import com.echothree.model.control.printer.common.transfer.PrinterGroupTransfer;
022import com.echothree.model.control.printer.common.workflow.PrinterGroupStatusConstants;
023import com.echothree.model.control.printer.server.control.PrinterControl;
024import com.echothree.model.control.uom.common.UomConstants;
025import com.echothree.model.control.uom.server.control.UomControl;
026import com.echothree.model.control.workflow.server.control.WorkflowControl;
027import com.echothree.model.data.printer.server.entity.PrinterGroup;
028import com.echothree.model.data.user.server.entity.UserVisit;
029import com.echothree.util.common.transfer.ListWrapper;
030import com.echothree.util.server.string.UnitOfMeasureUtils;
031import javax.enterprise.context.RequestScoped;
032
033@RequestScoped
034public class PrinterGroupTransferCache
035        extends BasePrinterTransferCache<PrinterGroup, PrinterGroupTransfer> {
036
037    @Inject
038    PrinterControl printerControl;
039
040    @Inject
041    UomControl uomControl;
042
043    @Inject
044    WorkflowControl workflowControl;
045
046    boolean includePrinters;
047    
048    /** Creates a new instance of PrinterGroupTransferCache */
049    protected PrinterGroupTransferCache() {
050        super();
051
052        var options = session.getOptions();
053        if(options != null) {
054            includePrinters = options.contains(PrinterOptions.PrinterGroupIncludePrinters);
055        }
056        
057        setIncludeEntityInstance(true);
058    }
059    
060    public PrinterGroupTransfer getPrinterGroupTransfer(UserVisit userVisit, PrinterGroup printerGroup) {
061        var printerGroupTransfer = get(printerGroup);
062        
063        if(printerGroupTransfer == null) {
064            var printerGroupDetail = printerGroup.getLastDetail();
065            var timeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_TIME);
066            var printerGroupName = printerGroupDetail.getPrinterGroupName();
067            var unformattedKeepPrintedJobsTime = printerGroupDetail.getKeepPrintedJobsTime();
068            var keepPrintedJobsTime = UnitOfMeasureUtils.getInstance().formatUnitOfMeasure(userVisit, timeUnitOfMeasureKind, unformattedKeepPrintedJobsTime);
069            var isDefault = printerGroupDetail.getIsDefault();
070            var sortOrder = printerGroupDetail.getSortOrder();
071            var description = printerControl.getBestPrinterGroupDescription(printerGroup, getLanguage(userVisit));
072
073            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(printerGroup.getPrimaryKey());
074            var printerGroupStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
075                    PrinterGroupStatusConstants.Workflow_PRINTER_GROUP_STATUS, entityInstance);
076            
077            printerGroupTransfer = new PrinterGroupTransfer(printerGroupName, unformattedKeepPrintedJobsTime, keepPrintedJobsTime, isDefault, sortOrder,
078                    printerGroupStatusTransfer, description);
079            put(userVisit, printerGroup, printerGroupTransfer);
080
081            if(includePrinters) {
082                printerGroupTransfer.setPrinters(new ListWrapper<>(printerControl.getPrinterTransfersByPrinterGroup(userVisit, printerGroup)));
083            }
084        }
085        
086        return printerGroupTransfer;
087    }
088    
089}