LocationCommandSender.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.commands;

import org.ameba.annotation.Measured;
import org.ameba.app.SpringProfiles;
import org.openwms.common.location.api.messages.LocationMO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * A LocationCommandSender sends out commands to request the modification of {@code Location}s.
 *
 * @author Heiko Scherrer
 */
@Profile(SpringProfiles.ASYNCHRONOUS_PROFILE)
@Component
class LocationCommandSender {

    private final String exchangeName;
    private final AmqpTemplate amqpTemplate;

    public LocationCommandSender(
            @Value("${owms.commands.common.loc.exchange-name}") String exchangeName,
            AmqpTemplate amqpTemplate) {
        this.exchangeName = exchangeName;
        this.amqpTemplate = amqpTemplate;
    }

    @Measured
    @TransactionalEventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void onLocationCommand(LocationCommand command) {
        if (command.getType() == LocationCommand.Type.SET_LOCATION_EMPTY) {
            var mo = LocationMO.ofPKey(command.getSource().getForeignPKey());
            var locationCommand = new org.openwms.common.location.api.commands.LocationCommand(
                    org.openwms.common.location.api.commands.LocationCommand.Type.SET_LOCATION_EMPTY, mo);
            amqpTemplate.convertAndSend(exchangeName, "common.loc.command.in.change-state", locationCommand);
        }
    }
}