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