/**
* 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.entities;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cbus.CGateSession;
import com.daveoxley.cbus.Group;
import com.daveoxley.cnery.dao.SceneActionConditionDAO;
import com.workplacesystems.queuj.Process;
import com.workplacesystems.queuj.QueueFactory;
import com.workplacesystems.queuj.process.ProcessImplServer;
import com.workplacesystems.queuj.process.ProcessWrapper;
import com.workplacesystems.queuj.process.QueujFactory;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@Entity
@Name("sceneActionEntity")
@Table(name="scene_action")
public class SceneAction extends BaseEntity implements ConditionProvider<SceneActionCondition>, Comparable<SceneAction>, Serializable {
public enum Type {
ON("cn.enum.SceneAction.Type.On"),
OFF("cn.enum.SceneAction.Type.Off"),
RAMP("cn.enum.SceneAction.Type.Ramp"),
BELL_PRESS("cn.enum.SceneAction.Type.BellPress");
private final String description;
Type(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
};
private Scene scene;
private String address;
private Type type;
private int delay;
private int duration;
private int level;
private int originalLevel;
private int processId;
private boolean firstRun;
private boolean active;
private Set<SceneActionCondition> sceneActionConditions;
@ManyToOne
@JoinColumn(name="scene_id")
public Scene getScene() { return scene; }
public void setScene(Scene scene) { this.scene = scene; }
@Column(name="address")
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
@Column(name="type")
@Enumerated(EnumType.STRING)
public Type getType() { return type; }
public void setType(Type type) { this.type = type; }
@Column(name="delay")
public int getDelay() { return delay; }
public void setDelay(int delay) { this.delay = delay; }
@Column(name="duration")
public int getDuration() { return duration; }
public void setDuration(int duration) { this.duration = duration; }
@Column(name="level")
public int getLevel() { return level; }
public void setLevel(int level) { this.level = level; }
@Column(name="original_level")
public int getOriginalLevel() { return originalLevel; }
public void setOriginalLevel(int originalLevel) { this.originalLevel = originalLevel; }
@Column(name = "process_id")
public int getProcessId() { return processId; }
public void setProcessId(int processId) { this.processId = processId; }
@Column(name = "first_run")
public boolean isFirstRun() { return firstRun; }
public void setFirstRun(boolean firstRun) { this.firstRun = firstRun; }
@Column(name = "active")
public boolean isActive() { return active; }
public void setActive(boolean active) { this.active = active; }
@OneToMany(mappedBy="sceneAction",cascade = CascadeType.ALL)
public Set<SceneActionCondition> getSceneActionConditions() { return sceneActionConditions; }
public void setSceneActionConditions(Set<SceneActionCondition> sceneActionConditions) { this.sceneActionConditions = sceneActionConditions; }
@Override
public int compareTo(SceneAction o) {
int cmp = getScene().compareTo(o.getScene());
if (cmp != 0)
return cmp;
cmp = (getDelay()<o.getDelay() ? -1 : (getDelay()==o.getDelay() ? 0 : 1));
if (cmp != 0)
return cmp;
Group g1 = null;
Group g2 = null;
try {
g1 = getCBusGroup();
} catch (Exception e) {}
try {
g2 = o.getCBusGroup();
} catch (Exception e) {}
if (g1 == null || g2 == null)
return (g1 != null ? -1 : (g2 == null ? 0 : 1));
else
return g1.compareTo(g2);
}
@Transient
public Group getCBusGroup() throws CGateException {
CGateSession cGateSession = (CGateSession)Contexts.getApplicationContext().get("cGateSession");
return (Group)cGateSession.getCGateObject(getAddress());
}
@Transient
public String getGroupName() {
try {
return getCBusGroup().getName();
} catch(Exception e) {}
return "";
}
@Transient
public Process<Integer> getProcess() {
ProcessWrapper<Integer> pw = ((ProcessImplServer)QueujFactory.getProcessServer((String)null, null)).get(getProcessId());
if (pw == null)
return null;
return new Process<Integer>(pw);
}
@Override
@Transient
public Collection<SceneActionCondition> getConditions() {
SceneActionConditionDAO sacdao = (SceneActionConditionDAO)Component.getInstance(SceneActionConditionDAO.class);
return sacdao.findSceneActionConditions(this, null);
}
}