001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.isis.objectstore.jdo.applib.service.background;
018
019import java.util.UUID;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022import org.apache.isis.applib.AbstractService;
023import org.apache.isis.applib.annotation.Command.ExecuteIn;
024import org.apache.isis.applib.annotation.DomainService;
025import org.apache.isis.applib.annotation.Programmatic;
026import org.apache.isis.applib.clock.Clock;
027import org.apache.isis.applib.services.background.ActionInvocationMemento;
028import org.apache.isis.applib.services.background.BackgroundCommandService;
029import org.apache.isis.applib.services.command.Command;
030import org.apache.isis.objectstore.jdo.applib.service.command.CommandJdo;
031
032/**
033 * Persists a {@link ActionInvocationMemento memento-ized} action such that it can be executed asynchronously,
034 * for example through a Quartz scheduler (using
035 * {@link org.apache.isis.objectstore.jdo.service.BackgroundCommandExecutionFromBackgroundCommandServiceJdo}).
036 *
037 * <p>
038 * This implementation has no UI and there are no other implementations of the service API, and so it annotated
039 * with {@link org.apache.isis.applib.annotation.DomainService}.  This class is implemented in the
040 * <tt>o.a.i.module:isis-module-command-jdo</tt> module.  If that module is included in the classpath, it this means
041 * that this service is automatically registered; no further configuration is required.
042 *
043 * <p>
044 * (That said, do note that other services in the <tt>o.a.i.module:isis-module-command-jdo</tt> do require explicit
045 * registration as services, eg in <tt>isis.properties</tt>).
046 */
047@DomainService
048public class BackgroundCommandServiceJdo extends AbstractService implements BackgroundCommandService {
049
050    @SuppressWarnings("unused")
051    private static final Logger LOG = LoggerFactory.getLogger(BackgroundCommandServiceJdo.class);
052    
053    @Programmatic
054    @Override
055    public void schedule(
056            final ActionInvocationMemento aim, 
057            final Command parentCommand, 
058            final String targetClassName, 
059            final String targetActionName, 
060            final String targetArgs) {
061        
062        final UUID transactionId = UUID.randomUUID();
063        final String user = parentCommand.getUser();
064
065        final CommandJdo backgroundCommand = newTransientInstance(CommandJdo.class);
066
067        backgroundCommand.setParent(parentCommand);
068        
069        backgroundCommand.setTransactionId(transactionId);
070
071        backgroundCommand.setUser(user);
072        backgroundCommand.setTimestamp(Clock.getTimeAsJavaSqlTimestamp());
073
074        backgroundCommand.setExecuteIn(ExecuteIn.BACKGROUND);
075
076        backgroundCommand.setTargetClass(targetClassName);
077        backgroundCommand.setTargetAction(targetActionName);
078        backgroundCommand.setTargetStr(aim.getTarget().toString());
079        backgroundCommand.setMemberIdentifier(aim.getActionId());
080
081        backgroundCommand.setArguments(targetArgs);
082        backgroundCommand.setMemento(aim.asMementoString());
083        
084        parentCommand.setPersistHint(true);
085        
086        persist(backgroundCommand);
087    }
088
089}