PackagingUnitIndexController.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 org.ameba.annotation.Public;
import org.ameba.http.MeasuredRestController;
import org.openwms.core.http.AbstractWebController;
import org.openwms.core.http.Index;
import org.openwms.wms.inventory.PackagingUnitCreator;
import org.openwms.wms.inventory.PackagingUnitFinder;
import org.openwms.wms.inventory.PackagingUnitMapper;
import org.openwms.wms.inventory.PackagingUnitMover;
import org.openwms.wms.inventory.PackagingUnitService;
import org.openwms.wms.inventory.api.CreatePURequestVO;
import org.openwms.wms.inventory.api.DeletePURequestVO;
import org.openwms.wms.inventory.api.MovePURequestVO;
import org.openwms.wms.inventory.api.PackagingUnitVO;
import org.openwms.wms.inventory.api.ReportProblemVO;
import org.openwms.wms.transport.TransportUnitService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import static java.util.Arrays.asList;
import static org.openwms.wms.inventory.api.InventoryConstants.API_PACKAGING_UNITS;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

/**
 * A PackagingUnitController.
 *
 * @author Heiko Scherrer
 */
@Public
@Validated
@MeasuredRestController
public class PackagingUnitIndexController extends AbstractWebController {

    public static final String P_KEY = "{pKey}";
    protected final PackagingUnitCreator creator;
    protected final PackagingUnitFinder finder;
    protected final PackagingUnitMover mover;
    protected final PackagingUnitService service;
    protected final TransportUnitService transportUnitService;
    protected final PackagingUnitMapper packagingUnitMapper;

    protected PackagingUnitIndexController(PackagingUnitCreator creator, PackagingUnitFinder finder, PackagingUnitMover mover,
            PackagingUnitService service, TransportUnitService transportUnitService, PackagingUnitMapper packagingUnitMapper) {
        this.creator = creator;
        this.finder = finder;
        this.mover = mover;
        this.service = service;
        this.transportUnitService = transportUnitService;
        this.packagingUnitMapper = packagingUnitMapper;
    }

    @GetMapping(API_PACKAGING_UNITS + "/index")
    public ResponseEntity<Index> index() {
        return ResponseEntity.ok(
                new Index(
                        linkTo(methodOn(PackagingUnitCreationController.class).create("{transportUnitBK}", "{luPos}", "{loadUnitType}", new CreatePURequestVO(asList(new PackagingUnitVO())))).withRel("packaging-units-create").withType("POST"),
                        linkTo(methodOn(PackagingUnitCreationController.class).createOnLocation(asList(new PackagingUnitVO()))).withRel("packaging-units-createonlocation").withType("POST"),

                        linkTo(methodOn(PackagingUnitDeletionController.class).deleteOnLocation(new DeletePURequestVO())).withRel("packaging-units-deleteonlocation").withType("DELETE"),
                        linkTo(methodOn(PackagingUnitDeletionController.class).delete("{pKey")).withRel("packaging-units-deletebypkey").withType("DELETE"),

                        linkTo(methodOn(PackagingUnitFinderController.class).findByPKey(P_KEY, MediaType.APPLICATION_JSON_VALUE)).withRel("packaging-units-findbypkey").withType("GET"),
                        linkTo(methodOn(PackagingUnitFinderController.class).findForProduct("{sku}", "{amount}", "{asc}", "{createDt}", MediaType.APPLICATION_JSON_VALUE)).withRel("packaging-units-findforproduct").withType("GET"),
                        linkTo(methodOn(PackagingUnitFinderController.class).findForProductInLG("{productPKey}", "{locationGroupName}", "{asc}", "{createDt}", MediaType.APPLICATION_JSON_VALUE)).withRel("packaging-units-findforproductinlg").withType("GET"),
                        linkTo(methodOn(PackagingUnitFinderController.class).findOnTU("{transportUnitBK}", MediaType.APPLICATION_JSON_VALUE)).withRel("packaging-units-findontu").withType("GET"),
                        linkTo(methodOn(PackagingUnitFinderController.class).findOnTUandLU("{transportUnitBK}", "{luPos}", MediaType.APPLICATION_JSON_VALUE)).withRel("packaging-units-findontuandlu").withType("GET"),

                        linkTo(methodOn(PackagingUnitMoveController.class).moveBetweenLocations(asList(new MovePURequestVO()))).withRel("packaging-units-movebetweenlocations").withType("PUT"),
                        linkTo(methodOn(PackagingUnitMoveController.class).moveFromLocationToLoadUnit(new MovePURequestVO())).withRel("packaging-units-movefromlocationtoloadunit").withType("PUT"),
                        linkTo(methodOn(PackagingUnitMoveController.class).movePUQuantity("{transportUnitBK}", "{luPos}", new MovePURequestVO())).withRel("packaging-units-movepuquantity").withType("PUT"),

                        linkTo(methodOn(PackagingUnitController.class).releasePU(P_KEY)).withRel("packaging-units-releasepu").withType("POST"),
                        linkTo(methodOn(PackagingUnitController.class).reportProblemForPUinLU("{transportUnitBK}", "{luPos}", new ReportProblemVO())).withRel("packaging-units-reportproblemforpuinlu").withType("POST"),

                        linkTo(methodOn(PackagingUnitUpdateController.class).update(P_KEY, new PackagingUnitVO())).withRel("packaging-units-update").withType("PUT")
                )
        );
    }
}