Overview
The Product Distribution Dashboard loads data from JSON sources, validates it, persists it to a PostgreSQL database, and automatically refreshes when changes are detected. This document explains the data models, loading process, and refresh mechanisms.Data Sources
The system loads three types of data from JSON files or URLs:Products
Product catalog with available sizes
Stores
Retail locations with demand and capacity
Warehouses
Distribution centers with inventory
Configuration
Data source URLs are configured inapplication.properties:
- External HTTP/HTTPS endpoints
- Local file system paths (file://)
- Cloud storage URLs
Data Models
Product Model
Represents a product in the catalog:Unique product identifier
Brand identifier for grouping products
Available sizes for this product (e.g., [“S”, “M”, “L”, “XL”])
Store Model
Represents a retail location with demand:Unique store identifier
Geographic latitude coordinate
Geographic longitude coordinate
Country code or name
Maximum number of units the store can hold
Expected return rate as decimal (0.0 - 1.0). Used to adjust demand calculations.
List of products and quantities the store needs
Warehouse Model
Represents a distribution center with inventory:Unique warehouse identifier
Geographic latitude coordinate
Geographic longitude coordinate
Country code or name
List of products and quantities available in inventory
Data Loading Process
TheJsonDataLoaderService handles loading data from configured URLs:
Loading Implementation
- Throws
DataLoadingExceptionfor HTTP errors - Throws
DataLoadingExceptionfor JSON parsing errors - Validates non-empty response
- Uses blocking WebClient for synchronous loading
Database Persistence
Data is persisted to PostgreSQL using Spring Data JPA:- Repositories
- Schema Management
- Products: Standalone entities
- Stores: Have one-to-many relationship with ProductItem (demand)
- Warehouses: Have one-to-many relationship with ProductItem (stock)
- StockAssignments: Created by distribution algorithm
- UnfulfilledDemands: Created when demand cannot be satisfied
Scheduled Refresh
The system automatically checks for data changes and triggers redistribution:- Application starts
- Initial distribution runs automatically
- Content hashes are stored for change detection
Scheduled Check
- Compares content hashes (not timestamps)
- Only triggers redistribution if actual content changed
- Efficient for large files
- Avoids unnecessary processing
Data Validation
Validation occurs at multiple levels:JSON Schema Validation
JSON Schema Validation
Jackson ObjectMapper validates JSON structure matches entity annotations:
- Required fields must be present
- Data types must match
- Collections must be properly formatted
JPA Validation
JPA Validation
Hibernate validates entities before persistence:
- ID fields must be unique
- Foreign key constraints enforced
- Data types and lengths validated
Business Logic Validation
Business Logic Validation
Distribution service validates business rules:
- Capacity cannot be negative
- Quantities must be positive
- Geographic coordinates must be valid
- Return rates must be between 0 and 1
Data Flow
Best Practices
Data Consistency
- Keep JSON files synchronized
- Use transactions for multi-entity updates
- Validate data before committing
- Monitor error logs for parsing issues
Performance
- Schedule refreshes during low-traffic periods
- Use content hashing to avoid unnecessary processing
- Index frequently queried fields
- Monitor database query performance
Reliability
- Implement retry logic for failed loads
- Log all data loading operations
- Set up alerts for loading failures
- Maintain backup data sources
Scalability
- Use lazy loading for entity relationships
- Implement pagination for large datasets
- Consider data partitioning for massive scale
- Monitor memory usage during loads
Troubleshooting
Data Loading Fails
Data Loading Fails
Symptoms: DataLoadingException in logsSolutions:
- Check URL accessibility
- Verify JSON format validity
- Review network connectivity
- Check authentication/authorization
- Validate SSL certificates for HTTPS
JSON Parsing Errors
JSON Parsing Errors
Symptoms: IOException or mapping errorsSolutions:
- Validate JSON against schema
- Check field name case sensitivity
- Verify data types match entity definitions
- Look for missing required fields
- Check for special characters in strings
Database Constraint Violations
Database Constraint Violations
Symptoms: ConstraintViolationExceptionSolutions:
- Verify unique IDs across entities
- Check foreign key references
- Validate data ranges (e.g., capacity > 0)
- Review entity relationships
- Check for duplicate entries
Distribution Not Triggering
Distribution Not Triggering
Symptoms: No redistribution despite data changesSolutions:
- Verify cron expression is valid
- Check scheduler is enabled (not in test profile)
- Review JsonChangeWatcher hash comparison
- Ensure content actually changed (not just timestamp)
- Check application logs for scheduler execution
API Access
Access raw data via REST endpoints:Next Steps
Distribution Algorithm
Learn how loaded data is processed
Metrics
Understand metrics calculated from persisted data