LocationServiceImpl.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.impl;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.ameba.annotation.Measured;
import org.ameba.annotation.TxService;
import org.ameba.exception.NotFoundException;
import org.ameba.i18n.Translator;
import org.openwms.wms.location.Location;
import org.openwms.wms.location.LocationPK;
import org.openwms.wms.location.LocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Optional;

import static org.openwms.wms.InventoryMessageCodes.LOCATION_NOT_FOUND_BY_ERP_CODE;
import static org.openwms.wms.InventoryMessageCodes.LOCATION_NOT_FOUND_BY_FOREIGN_PKEY;
import static org.openwms.wms.InventoryMessageCodes.LOCATION_NOT_FOUND_BY_ID;

/**
 * A LocationServiceImpl is a Spring managed transaction service that cares about Location management.
 *
 * @author Heiko Scherrer
 */
@Validated
@TxService
class LocationServiceImpl implements LocationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LocationServiceImpl.class);
    private final Translator translator;
    private final LocationRepository repository;

    LocationServiceImpl(Translator translator, LocationRepository repository) {
        this.translator = translator;
        this.repository = repository;
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public @NotNull Location findByForeignPKey(@NotBlank String foreignPKey) {
        return findByForeignPKeyInternal(foreignPKey);
    }

    private Location findByForeignPKeyInternal(String foreignPKey) {
        return repository.findByForeignPKey(foreignPKey).orElseThrow(() ->
                new NotFoundException(translator, LOCATION_NOT_FOUND_BY_FOREIGN_PKEY, new String[]{foreignPKey}, foreignPKey));
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public Optional<Location> findOptionalByID(@NotNull LocationPK locationId) {
        return repository.findByLocationId(locationId);
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public @NotNull Location findByBK(@NotNull LocationPK locationId) {
        return repository.findByLocationId(locationId)
                .orElseThrow(() -> new NotFoundException(translator, LOCATION_NOT_FOUND_BY_ID,
                        new String[]{locationId.toString()}, locationId.toString()));
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public @NotNull Location findByErpCode(@NotBlank String erpCode) {
        return repository.findByErpCode(erpCode).orElseThrow(
                () -> new NotFoundException(translator, LOCATION_NOT_FOUND_BY_ERP_CODE,
                        new String[]{erpCode}, erpCode));
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public Optional<Location> findOptionalByErpCode(@NotBlank String erpCode) {
        return repository.findByErpCode(erpCode);
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public @NotNull Location save(@NotNull Location location) {
        if (location.getForeignPKey() == null || location.getForeignPKey().isBlank()) {
            throw new NotFoundException(translator, LOCATION_NOT_FOUND_BY_FOREIGN_PKEY,
                    new String[]{location.getForeignPKey()}, location.getForeignPKey());
        }
        var existing = repository.findByForeignPKey(location.getForeignPKey());
        if (existing.isEmpty()) {
            throw new NotFoundException(translator, LOCATION_NOT_FOUND_BY_FOREIGN_PKEY,
                    new String[]{location.getForeignPKey()}, location.getForeignPKey());
        }
        return repository.saveAndFlush(location);
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public boolean isRemovalAllowed(@NotNull List<String> foreignPKeys) {
        boolean result = true;
        for (var foreignPKey : foreignPKeys) {
            var existing = repository.findByForeignPKey(foreignPKey);
            if (existing.isEmpty()) {
                LOGGER.warn("Location to delete does not exist, foreignPKey [{}]", foreignPKey);
                continue;
            }
            var puExists = repository.doesPUonLocationExists(existing.get().getPk());
            if (puExists) {
                LOGGER.warn("Not allowed to delete Location with foreignPKey [{}], because PackagingUnits are still booked on this Location", foreignPKey);
                result = false;
                break;
            }
            var tuExists = repository.doesTUonLocationExists(existing.get().getPk());
            if (tuExists) {
                LOGGER.warn("Not allowed to delete Location with foreignPKey [{}], because TransportUnits are still booked on this Location", foreignPKey);
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Measured
    @Override
    public void markForRemoval(@NotNull List<String> foreignPKeys) {
        for (var foreignPKey : foreignPKeys) {
            var existing = repository.findByForeignPKey(foreignPKey);
            if (existing.isEmpty()) {
                LOGGER.warn("Location to delete does not exist, foreignPKey [{}]", foreignPKey);
                continue;
            }
            existing.get().setMarkForDeletion(true);
            repository.saveAndFlush(existing.get());
            LOGGER.info("Marked Location [{}] for removal", foreignPKey);
        }
    }
}