/**
* 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 java.io.Serializable;
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.Table;
import javax.persistence.Transient;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@Entity
@Name("sceneActivationEntity")
@Table(name="scene_activation")
public class SceneActivation extends BaseEntity implements Comparable<SceneActivation>, Serializable {
public enum Action {
ACTIVATE_SCENE("cn.enum.SceneActivation.Action.ActivateScene"),
RESET_SCENE("cn.enum.SceneActivation.Action.ResetScene"),
DO_NOTHING("cn.enum.SceneActivation.Action.DoNothing");
private final String description;
Action(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
};
private Scene scene;
private String groupAddress;
private Action groupOnAction;
private Action groupOffAction;
@ManyToOne
@JoinColumn(name="scene_id")
public Scene getScene() { return scene; }
public void setScene(Scene scene) { this.scene = scene; }
@Column(name="group_address")
public String getGroupAddress() { return groupAddress; }
public void setGroupAddress(String groupAddress) { this.groupAddress = groupAddress; }
@Column(name="group_on_action")
@Enumerated(EnumType.STRING)
public Action getGroupOnAction() { return groupOnAction; }
public void setGroupOnAction(Action groupOnAction) { this.groupOnAction = groupOnAction; }
@Column(name="group_off_action")
@Enumerated(EnumType.STRING)
public Action getGroupOffAction() { return groupOffAction; }
public void setGroupOffAction(Action groupOffAction) { this.groupOffAction = groupOffAction; }
@Override
public int compareTo(SceneActivation o) {
Group g1 = null;
Group g2 = null;
try {
g1 = getGroup();
} catch (Exception e) {}
try {
g2 = o.getGroup();
} catch (Exception e) {}
if (g1 == null || g2 == null)
return (g1 != null ? -1 : (g2 == null ? 0 : 1));
else
return g1.compareTo(g2);
}
@Transient
private Group getGroup() throws Exception {
String address = getGroupAddress();
if (address == null)
return null;
CGateSession cGateSession = (CGateSession)Contexts.getApplicationContext().get("cGateSession");
return (Group)cGateSession.getCGateObject(address);
}
@Transient
public String getGroupName() {
try {
Group group = getGroup();
if (group != null)
return group.getName();
} catch(Exception e) {
new CGateException(e);
}
return "";
}
}