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.warehouse.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.inventory.server.control.InventoryControl;
021import com.echothree.model.control.warehouse.common.WarehouseOptions;
022import com.echothree.model.control.warehouse.common.transfer.LocationTransfer;
023import com.echothree.model.control.warehouse.common.workflow.LocationStatusConstants;
024import com.echothree.model.control.warehouse.server.control.LocationUseTypeControl;
025import com.echothree.model.control.warehouse.server.control.WarehouseControl;
026import com.echothree.model.control.workflow.server.control.WorkflowControl;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.model.data.warehouse.server.entity.Location;
029import com.echothree.util.common.transfer.ListWrapper;
030import javax.enterprise.context.RequestScoped;
031
032@RequestScoped
033public class LocationTransferCache
034        extends BaseWarehouseTransferCache<Location, LocationTransfer> {
035
036    @Inject
037    InventoryControl inventoryControl;
038
039    @Inject
040    LocationUseTypeControl locationUseTypeControl;
041
042    @Inject
043    WarehouseControl warehouseControl;
044
045    @Inject
046    WorkflowControl workflowControl;
047
048    boolean includeCapacities;
049    boolean includeVolume;
050    
051    /** Creates a new instance of LocationTransferCache */
052    protected LocationTransferCache() {
053        super();
054        
055        var options = session.getOptions();
056        if(options != null) {
057            includeCapacities = options.contains(WarehouseOptions.LocationIncludeCapacities);
058            includeVolume = options.contains(WarehouseOptions.LocationIncludeVolume);
059            setIncludeEntityAttributeGroups(options.contains(WarehouseOptions.LocationIncludeEntityAttributeGroups));
060            setIncludeTagScopes(options.contains(WarehouseOptions.LocationIncludeTagScopes));
061        }
062        
063        setIncludeEntityInstance(true);
064    }
065    
066    public LocationTransfer getLocationTransfer(UserVisit userVisit, Location location) {
067        var locationTransfer = get(location);
068        
069        if(locationTransfer == null) {
070            var locationDetail = location.getLastDetail();
071            var warehouse = warehouseControl.getWarehouse(locationDetail.getWarehouseParty());
072            var warehouseTransfer = warehouseControl.getWarehouseTransfer(userVisit, warehouse);
073            var locationName = locationDetail.getLocationName();
074            var locationTypeTransfer = warehouseControl.getLocationTypeTransfer(userVisit, locationDetail.getLocationType());
075            var locationUseTypeTransfer = locationUseTypeControl.getLocationUseTypeTransfer(userVisit, locationDetail.getLocationUseType());
076            var velocity = locationDetail.getVelocity();
077            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupTransfer(userVisit, locationDetail.getInventoryLocationGroup());
078            var description = warehouseControl.getBestLocationDescription(location, getLanguage(userVisit));
079
080            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(location.getPrimaryKey());
081            var locationStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
082                    LocationStatusConstants.Workflow_LOCATION_STATUS, entityInstance);
083            
084            locationTransfer = new LocationTransfer(warehouseTransfer, locationName, locationTypeTransfer, locationUseTypeTransfer,
085                    velocity, inventoryLocationGroup, description, locationStatusTransfer);
086            put(userVisit, location, locationTransfer);
087            
088            if(includeCapacities) {
089                locationTransfer.setLocationCapacities(new ListWrapper<>(warehouseControl.getLocationCapacityTransfersByLocation(userVisit, location)));
090            }
091            
092            if(includeVolume) {
093                var locationVolume = warehouseControl.getLocationVolume(location);
094                
095                if(locationVolume != null) {
096                    locationTransfer.setLocationVolume(warehouseControl.getLocationVolumeTransfer(userVisit, locationVolume));
097                }
098            }
099        }
100        
101        return locationTransfer;
102    }
103    
104}