001// Generated by delombok at Mon Oct 12 22:59:35 BST 2020
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one or more
004 *  contributor license agreements.  See the NOTICE file distributed with
005 *  this work for additional information regarding copyright ownership.
006 *  The ASF licenses this file to You under the Apache License, Version 2.0
007 *  (the "License"); you may not use this file except in compliance with
008 *  the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing, software
013 *  distributed under the License is distributed on an "AS IS" BASIS,
014 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 *  See the License for the specific language governing permissions and
016 *  limitations under the License.
017 */
018package org.apache.isis.extensions.commandreplay.secondary.executor;
019
020import java.util.function.Supplier;
021import javax.inject.Named;
022import org.springframework.beans.factory.annotation.Qualifier;
023import org.springframework.core.annotation.Order;
024import org.springframework.stereotype.Service;
025import org.apache.isis.applib.annotation.OrderPrecedence;
026import org.apache.isis.applib.jaxb.JavaSqlXMLGregorianCalendarMarshalling;
027import org.apache.isis.applib.services.bookmark.Bookmark;
028import org.apache.isis.applib.services.command.Command;
029import org.apache.isis.applib.services.command.CommandExecutorService;
030import org.apache.isis.applib.services.command.CommandOutcomeHandler;
031import org.apache.isis.extensions.commandreplay.secondary.clock.TickingClockService;
032import org.apache.isis.schema.cmd.v2.CommandDto;
033// before CommandExecutorServiceDefault
034/**
035 * Override of {@link CommandExecutorService} that also sets the time (using the {@link TickingClockService}) to that
036
037 * of the {@link Command}'s {@link Command#getTimestamp() timestamp} before executing the command.
038
039 *
040
041 * <p>
042
043 *     It then delegates down to the default implementation.
044
045 * </p>
046 */
047@Service
048@Named("isisExtensionsCommandReplaySecondary.CommandExecutorServiceWithTime")
049@Order(OrderPrecedence.MIDPOINT - 10)
050@Qualifier("WithTime")
051public class CommandExecutorServiceWithTime implements CommandExecutorService {
052    @java.lang.SuppressWarnings("all")
053    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(CommandExecutorServiceWithTime.class);
054    final CommandExecutorService delegate;
055    final TickingClockService tickingClockService;
056
057    public CommandExecutorServiceWithTime(@Qualifier("Default") final CommandExecutorService delegate, final TickingClockService tickingClockService) {
058        this.delegate = delegate;
059        this.tickingClockService = tickingClockService;
060    }
061
062    @Override
063    public Bookmark executeCommand(final Command command) {
064        final Supplier<Bookmark> executeCommand = () -> delegate.executeCommand(command);
065        return tickingClockService.isInitialized() ? tickingClockService.at(command.getTimestamp(), executeCommand) : executeCommand.get();
066    }
067
068    @Override
069    public Bookmark executeCommand(final SudoPolicy sudoPolicy, final Command command) {
070        final Supplier<Bookmark> executeCommand = () -> delegate.executeCommand(sudoPolicy, command);
071        return tickingClockService.isInitialized() ? tickingClockService.at(command.getTimestamp(), executeCommand) : executeCommand.get();
072    }
073
074    @Override
075    public Bookmark executeCommand(final CommandDto dto, final CommandOutcomeHandler outcomeHandler) {
076        final Supplier<Bookmark> executeCommand = () -> delegate.executeCommand(dto, outcomeHandler);
077        return tickingClockService.isInitialized() ? tickingClockService.at(JavaSqlXMLGregorianCalendarMarshalling.toTimestamp(dto.getTimestamp()), executeCommand) : executeCommand.get();
078    }
079
080    @Override
081    public Bookmark executeCommand(final SudoPolicy sudoPolicy, final CommandDto dto, final CommandOutcomeHandler outcomeHandler) {
082        final Supplier<Bookmark> executeCommand = () -> delegate.executeCommand(sudoPolicy, dto, outcomeHandler);
083        return tickingClockService.isInitialized() ? tickingClockService.at(JavaSqlXMLGregorianCalendarMarshalling.toTimestamp(dto.getTimestamp()), executeCommand) : executeCommand.get();
084    }
085}