TransportUnitController.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.transport;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.ameba.exception.NotFoundException;
import org.ameba.http.MeasuredRestController;
import org.ameba.i18n.Translator;
import org.openwms.core.http.AbstractWebController;
import org.openwms.wms.transport.api.TransportUnitVO;
import org.openwms.wms.transport.api.ValidationGroups;
import org.openwms.wms.transport.barcode.Barcode;
import org.openwms.wms.transport.spi.CommonTransportUnitVO;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.net.URI;
import java.net.URISyntaxException;

import static org.openwms.wms.InventoryMessageCodes.TRANSPORT_UNIT_NOT_FOUND_BY_BK;
import static org.openwms.wms.transport.api.TransportUnitApi.API_TRANSPORT_UNITS;
import static org.openwms.wms.transport.api.TransportUnitVO.MEDIA_TYPE;

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

    private final Translator translator;
    private final TransportUnitService service;
    private final TransportUnitMapper transportUnitMapper;

    TransportUnitController(Translator translator, TransportUnitService service,
                            TransportUnitMapper transportUnitMapper) {
        this.translator = translator;
        this.service = service;
        this.transportUnitMapper = transportUnitMapper;
    }

    @GetMapping(value = API_TRANSPORT_UNITS + "/{pKey}", produces = MEDIA_TYPE)
    public ResponseEntity<TransportUnitVO> findByPKey(@PathVariable("pKey") String pKey) {
        var transportUnit = service.findByPKey(pKey);
        return ResponseEntity.ok(transportUnitMapper.convertVO(transportUnit));
    }

    @GetMapping(value = API_TRANSPORT_UNITS, params = {"bk"}, produces = MEDIA_TYPE)
    public ResponseEntity<TransportUnitVO> findTransportUnit(
            @RequestParam("bk") String transportUnitBK
    ) {
        var optTransportUnit = service.findOneBy(transportUnitBK);
        if (optTransportUnit.isEmpty()) {
            throw new NotFoundException(translator, TRANSPORT_UNIT_NOT_FOUND_BY_BK, new String[]{transportUnitBK}, transportUnitBK);
        }
        var result = transportUnitMapper.convertVO(optTransportUnit.get());
        return ResponseEntity.ok(result);
    }

    @Validated(ValidationGroups.CreateTransportUnit.class)
    @PostMapping(API_TRANSPORT_UNITS)
    public ResponseEntity<Void> createTU(
            @Valid @RequestBody TransportUnitVO tu,
            HttpServletRequest req) throws URISyntaxException {
        var transportUnit = service.create(transportUnitMapper.convertVO(tu));
        return ResponseEntity.created(new URI(getLocationForCreatedResource(req, transportUnit.getPersistentKey()))).build();
    }

    @PostMapping(value = API_TRANSPORT_UNITS, params = {"actualErpCode", "tut"})
    public ResponseEntity<Object> createTU(
            @RequestParam(value = "bk", required = false) String transportUnitBK,
            @RequestParam("actualErpCode") String actualErpCode,
            @RequestParam("tut") String tut,
            @RequestParam(value = "loadUnitType", required = false) String loadUnitType,
            @RequestParam(value = "parts", required = false, defaultValue = "0") int parts,
            @RequestParam(value = "customerOrderId", required = false) String customerOrderId,
            HttpServletRequest req
    ) {
        if (transportUnitBK == null) {
            CommonTransportUnitVO created = service.createWithoutBK(tut, actualErpCode, loadUnitType, parts, customerOrderId);
            return ResponseEntity.created(getLocationURIForCreatedResource(req, created.getpKey())).body(created);
        } else {
            TransportUnit tu = service.createWithBK(transportUnitBK, tut, actualErpCode, loadUnitType, parts, customerOrderId);
            return ResponseEntity.created(getLocationURIForCreatedResource(req, tu.getPersistentKey())).body(tu);
        }
    }

    @PatchMapping(value = API_TRANSPORT_UNITS, params = {"bk", "newLocation"})
    public ResponseEntity<TransportUnitVO> moveTU(
            @RequestParam("bk") String transportUnitBK,
            @RequestParam("newLocation") String newLocation
    ) {
        var tu = service.moveTransportUnit(Barcode.of(transportUnitBK), newLocation);
        return ResponseEntity.ok(
                transportUnitMapper.convertVO(tu)
        );
    }
}