PackagingUnitMoveController.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.inventory.PackagingUnitMover;
import org.openwms.wms.inventory.PackagingUnitService;
import org.openwms.wms.inventory.api.MovePURequestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

import static org.openwms.wms.inventory.api.InventoryConstants.API_PACKAGING_UNITS;
import static org.openwms.wms.transport.api.TransportUnitApi.API_TRANSPORT_UNITS;

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

    private static final Logger LOGGER = LoggerFactory.getLogger(PackagingUnitMoveController.class);
    protected final PackagingUnitMover mover;
    protected final PackagingUnitService service;

    protected PackagingUnitMoveController(PackagingUnitMover mover, PackagingUnitService service) {
        this.mover = mover;
        this.service = service;
    }

    @PutMapping(value = API_TRANSPORT_UNITS + "/{transportUnitBK}/load-units/{luPos}/packaging-units")
    public ResponseEntity<Void> movePUQuantity(
            @PathVariable("transportUnitBK") String transportUnitBK,
            @PathVariable("luPos") String luPos,
            @Valid @RequestBody MovePURequestVO request
    ) {
        if (request.hasActualLocation()) {
            if (request.hasQuantity()) {
                LOGGER.debug("Move PackagingUnits of quantity [{}] from the TransportUnit [{}] to Location [{}]",
                        request.getQuantity(), transportUnitBK, request.getActualLocation());
                mover.moveQuantityToLocation(transportUnitBK, luPos, request.getActualLocation(),
                        request.getQuantity(), request.getProduct());
            } else {
                LOGGER.debug("Move all PackagingUnits from the TransportUnit [{}] to Location [{}]",
                        transportUnitBK, request.getActualLocation());
                mover.moveAllToLocation(transportUnitBK, luPos, request.getActualLocation(),
                        request.getProduct());
            }

        } else {
            if (request.hasQuantity()) {
                LOGGER.debug("Move PackagingUnits of quantity [{}] from the TransportUnit [{}] to TransportUnit [{}]",
                        request.getQuantity(), transportUnitBK, request.getTargetTransportUnitBK());
                mover.moveQuantity(transportUnitBK, luPos, request.getTargetTransportUnitBK(),
                        request.getTargetLoadUnitPosition(), request.getQuantity(), request.getProduct());
            } else {
                LOGGER.debug("Move all PackagingUnits from the TransportUnit [{}] to TransportUnit [{}]",
                        transportUnitBK, request.getTargetTransportUnitBK());
                mover.moveAll(transportUnitBK, luPos, request.getTargetTransportUnitBK(),
                        request.getTargetLoadUnitPosition(), request.getProduct());
            }
        }
        return ResponseEntity.noContent().build();
    }

    @PutMapping(API_PACKAGING_UNITS + "/move-between-locations")
    public ResponseEntity<Void> moveBetweenLocations(@Valid @RequestBody List<MovePURequestVO> requests) {
        LOGGER.debug("Move PackagingUnits between Locations for [{}] requests", requests.size());
        mover.moveBetweenLocations(requests);
        return ResponseEntity.noContent().build();
    }

    @PutMapping(API_PACKAGING_UNITS + "/move-to-load-unit")
    public ResponseEntity<Void> moveFromLocationToLoadUnit(@Valid @RequestBody MovePURequestVO request) {
        LOGGER.debug("Move PackagingUnits from Location to LoadUnit, request is [{}]", request);
        mover.moveToLoadUnit(request);
        return ResponseEntity.noContent().build();
    }
}