/**
* C-Nery - A home automation web application for C-Bus.
* Copyright (C) 2008,2009,2012 Dave Oxley <dave@daveoxley.co.uk>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.daveoxley.cnery.scenes;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cnery.actions.SceneActionHome;
import com.daveoxley.cnery.actions.SceneHome;
import com.daveoxley.cnery.entities.Scene;
import com.daveoxley.cnery.entities.SceneAction;
import com.workplacesystems.queuj.Process;
import com.workplacesystems.queuj.process.java.JavaProcessRunner;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@Name("conditionalSceneActionRunner")
public class ConditionalSceneActionRunner {
@In
private JavaProcessRunner PROCESS_RUNNER;
@In
private SceneAction sceneAction;
@In(create=true)
private SceneHome sceneHome;
@In(create=true)
private SceneActionHome sceneActionHome;
public void run() throws CGateException {
sceneActionHome.clearInstance();
sceneActionHome.setId(sceneAction.getId());
sceneAction = sceneActionHome.getInstance();
Scene parentScene = sceneAction.getScene();
Process<Integer> process = sceneAction.getProcess();
sceneHome.clearInstance();
sceneHome.setId(parentScene.getId());
parentScene = sceneHome.getInstance();
if (!parentScene.isActive() &&
!parentScene.getActivateProcess().isRunning() &&
!parentScene.getActivateProcess().isWaitingToRun())
return;
if (sceneAction.isFirstRun() &&
!process.isRunning())
return;
if (process.isRunning()) {
process.attach();
if (process.isFailed())
throw new CGateException("Process failed.");
}
process.startNow();
}
}