LoadUnitController.java
/*
* Copyright 2005-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openwms.wms.inventory.rest;
import org.ameba.http.MeasuredRestController;
import org.openwms.core.http.AbstractWebController;
import org.openwms.core.http.Index;
import org.openwms.wms.CycleAvoidingMappingContext;
import org.openwms.wms.inventory.LoadUnitMapper;
import org.openwms.wms.inventory.LoadUnitService;
import org.openwms.wms.inventory.PackagingUnitMapper;
import org.openwms.wms.inventory.api.LoadUnitVO;
import org.openwms.wms.inventory.api.PackagingUnitVO;
import org.openwms.wms.inventory.api.ProductVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import static org.openwms.wms.inventory.api.InventoryConstants.API_LOAD_UNITS;
import static org.openwms.wms.transport.api.TransportUnitApi.API_TRANSPORT_UNIT;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
/**
* A LoadUnitController.
*
* @author Heiko Scherrer
*/
@Validated
@MeasuredRestController
public class LoadUnitController extends AbstractWebController {
private final LoadUnitService service;
private final LoadUnitMapper loadUnitMapper;
private final PackagingUnitMapper packagingUnitMapper;
LoadUnitController(LoadUnitService service, LoadUnitMapper loadUnitMapper, PackagingUnitMapper packagingUnitMapper) {
this.service = service;
this.loadUnitMapper = loadUnitMapper;
this.packagingUnitMapper = packagingUnitMapper;
}
@GetMapping(API_LOAD_UNITS + "/index")
public ResponseEntity<Index> index() {
return ResponseEntity.ok(
new Index(
linkTo(methodOn(LoadUnitController.class).divide("{transportUnitBK}", "{loadUnitType}", 4)).withRel("lu-divide").withType("POST"),
linkTo(methodOn(LoadUnitController.class).findByPKey("{pKey}")).withRel("lu-findbypkey").withType("GET"),
linkTo(methodOn(LoadUnitController.class).findPUinLU("{pKey}")).withRel("lu-findpuinlu").withType("GET"),
linkTo(methodOn(LoadUnitController.class).putProductInto("{pKey}", new ProductVO())).withRel("lu-putproductinto").withType("PUT")
)
);
}
@Transactional(readOnly = true)
@GetMapping(API_LOAD_UNITS + "/{pKey}")
public ResponseEntity<LoadUnitVO> findByPKey(@PathVariable("pKey") String pKey) {
var lu = service.findByPKey(pKey);
// normalize the LoadUnit
lu.removePackagingUnits();
var vo = loadUnitMapper.convert(lu, new CycleAvoidingMappingContext());
if (vo.getProduct() != null && vo.getProduct().getUnits() != null) {
vo.getProduct().getUnits().clear();
}
vo.add(linkTo(methodOn(LoadUnitController.class).findPUinLU(pKey)).withRel("packaging-units"));
return ResponseEntity.ok(vo);
}
@Transactional(readOnly = true)
@GetMapping(API_LOAD_UNITS + "/{pKey}/packaging-units")
public ResponseEntity<List<PackagingUnitVO>> findPUinLU(@PathVariable("pKey") String pKey) {
var lu = service.findByPKey(pKey);
var pus = packagingUnitMapper.convert(lu.getPackagingUnits(), new CycleAvoidingMappingContext());
return ResponseEntity.ok(pus);
}
@PostMapping(value = API_TRANSPORT_UNIT, params = {"transportUnitBK", "loadUnitType", "parts"})
public ResponseEntity<Void> divide(
@RequestParam("transportUnitBK") String transportUnitBK,
@RequestParam("loadUnitType") String loadUnitType,
@RequestParam("parts") int parts
) {
service.divide(transportUnitBK, loadUnitType, parts);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@PutMapping(API_LOAD_UNITS + "/{pKey}")
public ResponseEntity<Void> putProductInto(
@PathVariable("pKey") String pKey,
@RequestBody ProductVO productVO
) {
service.load(pKey, productVO);
return ResponseEntity.ok().build();
}
}