PackagingUnitCreationController.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.pu;

import jakarta.validation.Valid;
import org.ameba.http.MeasuredRestController;
import org.openwms.core.http.AbstractWebController;
import org.openwms.wms.CycleAvoidingMappingContext;
import org.openwms.wms.inventory.PackagingUnitCreator;
import org.openwms.wms.inventory.PackagingUnitMapper;
import org.openwms.wms.inventory.api.CreatePURequestVO;
import org.openwms.wms.inventory.api.PackagingUnitVO;
import org.openwms.wms.inventory.api.ValidationGroups;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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_PACKAGING_UNITS;
import static org.openwms.wms.inventory.api.PackagingUnitApi.ACCEPT_HEADER_PU;
import static org.openwms.wms.transport.api.TransportUnitApi.API_TRANSPORT_UNITS;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

/**
 * A PackagingUnitCreationController.
 *
 * @author Heiko Scherrer
 */
@Validated
@MeasuredRestController
public class PackagingUnitCreationController extends AbstractWebController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PackagingUnitCreationController.class);
    protected final PackagingUnitCreator creator;
    protected final PackagingUnitMapper packagingUnitMapper;

    protected PackagingUnitCreationController(PackagingUnitCreator creator, PackagingUnitMapper packagingUnitMapper) {
        this.creator = creator;
        this.packagingUnitMapper = packagingUnitMapper;
    }

    @PostMapping(value = API_TRANSPORT_UNITS + "/{transportUnitBK}/load-units/{luPos}/packaging-units")
    public ResponseEntity<Void> create(
            @PathVariable("transportUnitBK") String transportUnitBK,
            @PathVariable("luPos") String luPos,
            @RequestParam(value = "loadUnitType", required = false) String loadUnitType,
            @Valid @RequestBody CreatePURequestVO request
    ) {
        creator.createMultipleInContainer(transportUnitBK, luPos, packagingUnitMapper.convertFromVO(request.getPackagingUnits(), new CycleAvoidingMappingContext()), loadUnitType);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Validated(ValidationGroups.CreatePackagingUnit.class)
    @PostMapping(API_PACKAGING_UNITS)
    public ResponseEntity<Void> createOnLocation(
            @Valid @RequestBody List<PackagingUnitVO> pus
    ) {
        if (pus.size() == 1) {
            LOGGER.debug("Create new PackagingUnit on Location");
            var eo = packagingUnitMapper.convertVO(pus.get(0), new CycleAvoidingMappingContext());
            var pKey = creator.create(eo).getPersistentKey();
            return ResponseEntity.created(linkTo(methodOn(PackagingUnitFinderController.class).findByPKey(pKey, ACCEPT_HEADER_PU)).toUri()).build();
        }
        LOGGER.debug("Create new PackagingUnits on Location");
        creator.create(packagingUnitMapper.convertFromVO(pus, new CycleAvoidingMappingContext()));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}