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