/**
* 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.pages;
import com.daveoxley.cnery.actions.SceneConditionHome;
import com.daveoxley.cnery.dao.SceneConditionDAO;
import com.daveoxley.cnery.entities.AbstractCondition;
import com.daveoxley.cnery.entities.AbstractCondition.Logic;
import com.daveoxley.cnery.entities.Scene;
import com.daveoxley.cnery.entities.SceneCondition;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.jboss.seam.Component;
import org.jboss.seam.core.Interpolator;
/**
*
* @author dave
*/
public class SceneConditionNode extends TreeNode<SceneCondition> implements Serializable {
public enum Type {
LOGIC,
CONDITION;
};
private Type type;
private SceneCondition condition;
private Scene scene;
private Map<Long,TreeNode> logicNodes;
private Map<Long,TreeNode> conditionNodes;
public SceneConditionNode(Scene scene, Type type) {
this.condition = null;
this.scene = scene;
this.type = type;
}
private SceneConditionDAO getSceneConditionDAO() {
return (SceneConditionDAO)Component.getInstance(SceneConditionDAO.class);
}
public SceneConditionNode(SceneCondition condition) {
this.condition = condition;
this.scene = condition.getScene();
if (condition.getActionType() == SceneCondition.ActionType.LOGIC)
type = Type.LOGIC;
else
type = Type.CONDITION;
}
public List<SceneConditionNode> getLogicNodes() {
if (logicNodes == null)
logicNodes = new TreeMap<Long,TreeNode>();
mergeNodes(logicNodes, getSceneConditionDAO().findLogicSceneConditions(scene, condition));
return new ArrayList(logicNodes.values());
}
public List<SceneConditionNode> getConditionNodes() {
if (conditionNodes == null)
conditionNodes = new TreeMap<Long,TreeNode>();
mergeNodes(conditionNodes, getSceneConditionDAO().findNonLogicSceneConditions(scene, condition));
return new ArrayList(conditionNodes.values());
}
@Override
protected SceneCondition getEntity() {
return condition;
}
@Override
protected TreeNode getNewTreeNode(SceneCondition sac) {
return new SceneConditionNode(sac);
}
public String getDescription() {
if (condition != null) {
SceneConditionHome home = (SceneConditionHome)Component.getInstance(SceneConditionHome.class, true);
home.setId(condition.getId());
condition = home.getInstance();
}
if (type == Type.LOGIC) {
if (condition == null)
return "And";
else if (condition.getLogic() == Logic.AND)
return "And";
else if (condition.getLogic() == Logic.OR)
return "Or";
else
throw new IllegalStateException();
}
String conditionDesc = getEnumDesc(condition.getAction().getDescription()) + " scene to action ";
if (condition.getActionType() == AbstractCondition.ActionType.TIME) {
conditionDesc = conditionDesc + "if time is " + getEnumDesc(condition.getTimeAfterBefore().getDescription()) + " " +
getEnumDesc(condition.getTimeWhen().getDescription()) + " " + getEnumDesc(condition.getTimePlusMinus().getDescription()) + " " +
condition.getTimeMinutes() + " minutes";
}
else {
if (condition.getActionType() == AbstractCondition.ActionType.GROUP) {
conditionDesc = conditionDesc + "if group " + condition.getDependGroupName();
}
else if (condition.getActionType() == AbstractCondition.ActionType.SCENE) {
conditionDesc = conditionDesc + "if scene " + condition.getDependSceneName();
}
conditionDesc = conditionDesc + " is " + getEnumDesc(condition.getGroupOnOff().getDescription());
}
return conditionDesc;
}
private String getEnumDesc(String value) {
return Interpolator.instance().interpolate("#{messages['" + value + "']}");
}
}