Location Utils
Intended Audience:
Technical User
Author:
Holger Lierse
Changed on:
17 Sept 2025
Overview
The `LocationUtils`
class in the `util-sourcing`
is a utility class that provides utilities for location-based sourcing decisions. It handles location-specific sourcing logic including distance calculations, location availability checks, and provide location-based caching optimization.
Key points
- Location-Based Decisions: Handles location-specific sourcing logic.
- Distance Calculations: Calculates distances between locations for proximity-based sourcing.
- Location Availability: Checks if locations are active and available for sourcing.
- Caching Optimization: Provides location-based caching optimization.
Core Methods
`getLocationByRef()`
Loads a single Location by provided reference, with caching for performance.
1Location location = LocationUtils.getLocationByRef(context, "store-123");
`getLocationsInNetwork()`
Load all the locations in a network with caching for performance.
1List<Location> networkLocations = LocationUtils.getLocationsInNetwork(
2 context,
3 "network-001"
4);
`getLocationsInNetworks()`
Loads all locations that belong to the provided networks.
1List<String> networkRefs = Arrays.asList("network-001", "network-002", "network-003");
2List<Location> allLocations = LocationUtils.getLocationsInNetworks(context, networkRefs);
`distanceInMetres()`
Calculate distance between two points in latitude and longitude using the Haversine formula.
1Location storeLocation = LocationUtils.getLocationByRef(context, "store-123");
2Location customerLocation = LocationUtils.getLocationByRef(context, "customer-location");
3
4if (storeLocation != null && customerLocation != null &&
5 storeLocation.getPrimaryAddress() != null && customerLocation.getPrimaryAddress() != null) {
6
7 double distance = LocationUtils.distanceInMetres(
8 storeLocation.getPrimaryAddress().getLatitude(),
9 storeLocation.getPrimaryAddress().getLongitude(),
10 customerLocation.getPrimaryAddress().getLatitude(),
11 customerLocation.getPrimaryAddress().getLongitude()
12 );
13
14 // Check if within delivery radius (e.g., 50km)
15 if (distance <= 50000) {
16 context.action().log("Location is within delivery radius");
17 }
18}