LocationMapper.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 org.ameba.exception.NotFoundException;
import org.ameba.i18n.Translator;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.openwms.common.location.api.messages.LocationMO;
import org.openwms.wms.location.Location;
import org.openwms.wms.location.LocationPK;
import org.openwms.wms.location.api.LocationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * A LocationMapper.
 *
 * @author Heiko Scherrer
 */
@Validated
@Mapper
public abstract class LocationMapper {

    private LocationRepository locationRepository;
    private Translator translator;

    @Autowired
    public void setLocationRepository(LocationRepository locationRepository) {
        this.locationRepository = locationRepository;
    }

    @Autowired
    public void setTranslator(Translator translator) {
        this.translator = translator;
    }

    public Location convertFromId(@NotBlank String locationId) {
        if (locationId == null) {
            throw new IllegalArgumentException("Can't resolve Location for NULL argument {locationId}");
        }
        return locationRepository.findByLocationId(LocationPK.fromString(locationId))
                .orElseGet(() -> locationRepository.findByErpCode(locationId)
                        .orElseThrow(() -> new NotFoundException(translator, "owms.wms.inv.loc.erpCodeNotExists", locationId))
                );
    }

    @Mapping(target = "pKey", source = "persistentKey")
    @Mapping(target = "locationId", expression = "java( eo.getLocationId().toString() )" )
    @Mapping(target = "locationGroupName", source = "locationGroup")
    public abstract LocationVO convertToVO(Location eo);

    @Mapping(target = "foreignPKey", ignore = true)
    @Mapping(target = "locationGroup", ignore = true)
    @Mapping(target = "markForDeletion", ignore = true)
    public abstract Location convertFromVO(LocationVO vo);

    public String convertToId(LocationPK location) {
        if (location == null) {
            return null;
        }
        return location.toString();
    }

    @Mapping(target = "locationId", source = "id")
    @Mapping(target = "foreignPKey", source = "mo.pKey")
    @Mapping(target = "locationGroup", source = "locationGroupName")
    @Mapping(target = "description", ignore = true)
    @Mapping(target = "noMaxTransportUnits", ignore = true)
    @Mapping(target = "lastInventoryDate", ignore = true)
    @Mapping(target = "markForDeletion", ignore = true)
    public abstract Location convertFromMO(LocationMO mo);

    public LocationPK convert(String locationId) {
        return LocationPK.fromString(locationId);
    }
}