LocationController.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.location;
import org.ameba.exception.NotFoundException;
import org.ameba.http.MeasuredRestController;
import org.openwms.core.http.AbstractWebController;
import org.openwms.wms.location.api.LocationVO;
import org.openwms.wms.location.impl.LocationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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 java.util.Optional;
import static java.lang.String.format;
import static org.openwms.wms.location.api.LocationApi.API_LOCATIONS;
/**
* A LocationController.
*
* @author Heiko Scherrer
*/
@Profile("!INMEM")
@MeasuredRestController
public class LocationController extends AbstractWebController {
private static final Logger LOGGER = LoggerFactory.getLogger(LocationController.class);
private final LocationMapper mapper;
private final LocationService locationService;
LocationController(LocationService locationService, LocationMapper mapper) {
this.locationService = locationService;
this.mapper = mapper;
}
@GetMapping(value = API_LOCATIONS, params = {"locationId"})
public ResponseEntity<Optional<LocationVO>> findByIdOpt(@RequestParam("locationId") String locationId) {
if (!LocationPK.isValid(locationId)) {
// here we need to throw an NFE because Feign needs to cast it into an Optional. IAE won't work!
throw new NotFoundException(format("Invalid location [%s]", locationId));
}
var locationOpt = locationService.findOptionalByID(LocationPK.fromString(locationId));
return locationOpt.map(location -> ResponseEntity.ok(Optional.of(mapper.convertToVO(location)))).orElseGet(() -> ResponseEntity.ok(Optional.empty()));
}
@GetMapping(value = API_LOCATIONS, params = {"erpCode"})
public ResponseEntity<Optional<LocationVO>> findByErpCodeOpt(@RequestParam("erpCode") String erpCode) {
try {
var location = locationService.findByErpCode(erpCode);
return ResponseEntity.ok(Optional.ofNullable(mapper.convertToVO(location)));
} catch (NotFoundException e) {
return ResponseEntity.ok(Optional.empty());
}
}
@PostMapping(API_LOCATIONS + "/remove")
public ResponseEntity<Boolean> requestDeleteLocation(@RequestBody List<String> pKeys) {
LOGGER.debug("Request deleting Locations with pKeys [{}]", pKeys);
return ResponseEntity.ok(locationService.isRemovalAllowed(pKeys));
}
@DeleteMapping(API_LOCATIONS)
public ResponseEntity<Void> deleteLocation(@RequestBody List<String> pKeys) {
LOGGER.debug("Deleting Locations with pKeys [{}]", pKeys);
locationService.markForRemoval(pKeys);
return ResponseEntity.noContent().build();
}
}