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.inventory.server.transfer;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.inventory.common.InventoryOptions;
021import com.echothree.model.control.inventory.common.transfer.InventoryLocationGroupTransfer;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.warehouse.common.transfer.WarehouseTransfer;
024import com.echothree.model.control.warehouse.server.control.WarehouseControl;
025import com.echothree.model.control.inventory.common.workflow.InventoryLocationGroupStatusConstants;
026import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
027import com.echothree.model.control.workflow.server.control.WorkflowControl;
028import com.echothree.model.data.core.server.entity.EntityInstance;
029import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
030import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupDetail;
031import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupVolume;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.model.data.user.server.entity.UserVisit;
034import com.echothree.model.data.warehouse.server.entity.Warehouse;
035import com.echothree.util.common.transfer.ListWrapper;
036import com.echothree.util.server.persistence.Session;
037import java.util.Set;
038
039public class InventoryLocationGroupTransferCache
040        extends BaseInventoryTransferCache<InventoryLocationGroup, InventoryLocationGroupTransfer> {
041    
042    CoreControl coreControl = Session.getModelController(CoreControl.class);
043    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
044    boolean includeCapacities;
045    boolean includeVolume;
046    
047    /** Creates a new instance of InventoryLocationGroupTransferCache */
048    public InventoryLocationGroupTransferCache(UserVisit userVisit, InventoryControl inventoryControl) {
049        super(userVisit, inventoryControl);
050        
051        var options = session.getOptions();
052        if(options != null) {
053            includeCapacities = options.contains(InventoryOptions.InventoryLocationGroupIncludeCapacities);
054            includeVolume = options.contains(InventoryOptions.InventoryLocationGroupIncludeVolume);
055        }
056        
057        setIncludeEntityInstance(true);
058    }
059    
060    @Override
061    public InventoryLocationGroupTransfer getTransfer(InventoryLocationGroup inventoryLocationGroup) {
062        InventoryLocationGroupTransfer inventoryLocationGroupTransfer = get(inventoryLocationGroup);
063        
064        if(inventoryLocationGroupTransfer == null) {
065            WarehouseControl warehouseControl = Session.getModelController(WarehouseControl.class);
066            InventoryLocationGroupDetail inventoryLocationGroupDetail = inventoryLocationGroup.getLastDetail();
067            Party warehouseParty = inventoryLocationGroupDetail.getWarehouseParty();
068            Warehouse warehouse = warehouseControl.getWarehouse(warehouseParty);
069            WarehouseTransfer warehouseTransfer = warehouseControl.getWarehouseTransferCaches(userVisit).getWarehouseTransferCache().getWarehouseTransfer(warehouse);
070            String inventoryLocationGroupName = inventoryLocationGroupDetail.getInventoryLocationGroupName();
071            Boolean isDefault = inventoryLocationGroupDetail.getIsDefault();
072            Integer sortOrder = inventoryLocationGroupDetail.getSortOrder();
073            String description = inventoryControl.getBestInventoryLocationGroupDescription(inventoryLocationGroup, getLanguage());
074            
075            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey());
076            WorkflowEntityStatusTransfer inventoryLocationGroupStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
077                    InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS, entityInstance);
078            
079            inventoryLocationGroupTransfer = new InventoryLocationGroupTransfer(warehouseTransfer, inventoryLocationGroupName, isDefault, sortOrder,
080                    description, inventoryLocationGroupStatusTransfer);
081            put(inventoryLocationGroup, inventoryLocationGroupTransfer);
082            
083            if(includeCapacities) {
084                inventoryLocationGroupTransfer.setInventoryLocationGroupCapacities(new ListWrapper<>(inventoryControl.getInventoryLocationGroupCapacityTransfersByInventoryLocationGroup(userVisit, inventoryLocationGroup)));
085            }
086            
087            if(includeVolume) {
088                InventoryLocationGroupVolume inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
089                
090                if(inventoryLocationGroupVolume != null) {
091                    inventoryLocationGroupTransfer.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(userVisit, inventoryLocationGroupVolume));
092                }
093            }
094        }
095        
096        return inventoryLocationGroupTransfer;
097    }
098    
099}