annotation based hibernate. but relations are still broken and the inheritance (Device) is not working correctly

This commit is contained in:
Lucas Pleß 2012-03-21 23:43:00 +01:00
parent 04f77c6553
commit 55fe1dca17
12 changed files with 196 additions and 41 deletions

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.dao; package de.ctdo.bunti.dao;
import de.ctdo.bunti.model.Par56Spot;
import de.ctdo.bunti.model.Room; import de.ctdo.bunti.model.Room;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
@ -15,6 +16,23 @@ public final class RoomsDAOImpl extends HibernateDaoSupport implements RoomsDAO
@Override @Override
public List<Room> getRooms() { public List<Room> getRooms() {
//if(getHibernateTemplate().loadAll(Room.class).size() == 0) {
Room r = new Room();
r.setId(1);
r.setFloor("Floor 1");
r.setName("Kueche");
Par56Spot spot = new Par56Spot();
spot.setDeviceName("Spot 1");
spot.setStartAddress(1);
// r.addDevice(spot);
getHibernateTemplate().save(spot);
getHibernateTemplate().save(r);
//}
return getHibernateTemplate().loadAll(Room.class); return getHibernateTemplate().loadAll(Room.class);
} }

View File

@ -5,14 +5,23 @@ import de.ctdo.bunti.dmx.DMXChannel;
import de.ctdo.bunti.dmx.DMXChannels; import de.ctdo.bunti.dmx.DMXChannels;
import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnore;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@Entity
public abstract class BuntiDMXDevice extends BuntiDevice { public abstract class BuntiDMXDevice extends BuntiDevice {
private int startAddress; private int startAddress;
private final DMXChannels dmxChannels = new DMXChannels(); private final DMXChannels dmxChannels = new DMXChannels();
public BuntiDMXDevice() {
}
public BuntiDMXDevice(int deviceId, int startAddress, String name) { public BuntiDMXDevice(int deviceId, int startAddress, String name) {
super(deviceId, name); super(deviceId, name);
setStartAddress(startAddress); setStartAddress(startAddress);
@ -62,6 +71,8 @@ public abstract class BuntiDMXDevice extends BuntiDevice {
* @param name The channel name to get the value from. * @param name The channel name to get the value from.
* @return The desired channel value. * @return The desired channel value.
*/ */
@JsonIgnore
@Transient
protected final int getChannelValueByName(String name) { protected final int getChannelValueByName(String name) {
DMXChannel dx = dmxChannels.getChannelByName(name); DMXChannel dx = dmxChannels.getChannelByName(name);
if (dx != null) { if (dx != null) {
@ -75,6 +86,7 @@ public abstract class BuntiDMXDevice extends BuntiDevice {
* @return The channel data with startaddress+offset of every channel * @return The channel data with startaddress+offset of every channel
*/ */
@JsonIgnore @JsonIgnore
@Transient
public Map<Integer, Integer> getChannelData() { public Map<Integer, Integer> getChannelData() {
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Map<Integer, Integer> map = new HashMap<Integer, Integer>();

View File

@ -1,5 +1,8 @@
package de.ctdo.bunti.model; package de.ctdo.bunti.model;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Map; import java.util.Map;
/** /**
@ -7,10 +10,18 @@ import java.util.Map;
* Maybe this is a lamp, or a switchable power source, or a strobe, ... * Maybe this is a lamp, or a switchable power source, or a strobe, ...
* @author lucas * @author lucas
*/ */
@Entity
@Table(name = "devices")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class BuntiDevice { public abstract class BuntiDevice {
private int deviceId; private int deviceId;
private String deviceName; private String deviceName;
private String picture; private String picture;
// private Room room;
public BuntiDevice() {
}
public BuntiDevice(int deviceId, String deviceName) { public BuntiDevice(int deviceId, String deviceName) {
this.deviceId = deviceId; this.deviceId = deviceId;
@ -21,7 +32,7 @@ public abstract class BuntiDevice {
* Get the type of this device * Get the type of this device
* @return a string with the class name (=the Type) * @return a string with the class name (=the Type)
*/ */
@SuppressWarnings("UnusedDeclaration") @Transient
public final String getType() { public final String getType() {
String FQClassName = this.getClass().getName(); String FQClassName = this.getClass().getName();
int firstChar = FQClassName.lastIndexOf ('.') + 1; int firstChar = FQClassName.lastIndexOf ('.') + 1;
@ -35,10 +46,17 @@ public abstract class BuntiDevice {
* Gets the device Id * Gets the device Id
* @return the device Id * @return the device Id
*/ */
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public final int getId() { public final int getId() {
return deviceId; return deviceId;
} }
public final void setId(int id) {
this.deviceId = id;
}
/** /**
* Gets the device name * Gets the device name
* @return The name of the device * @return The name of the device
@ -90,4 +108,13 @@ public abstract class BuntiDevice {
public abstract boolean setValuesFromOptions(Map<String, Object> options); public abstract boolean setValuesFromOptions(Map<String, Object> options);
// @ManyToOne
// @JoinColumn(name="ROOM_ID")
// public Room getRoom() {
// return room;
// }
//
// public void setRoom(Room room) {
// this.room = room;
// }
} }

View File

@ -1,7 +1,10 @@
package de.ctdo.bunti.model; package de.ctdo.bunti.model;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.Map; import java.util.Map;
@Entity
public abstract class BuntiSwitchingDevice extends BuntiDevice { public abstract class BuntiSwitchingDevice extends BuntiDevice {
private static final String OPTION_STATE = "state"; private static final String OPTION_STATE = "state";
@ -30,6 +33,7 @@ public abstract class BuntiSwitchingDevice extends BuntiDevice {
return false; return false;
} }
@Transient
public boolean isState() { public boolean isState() {
return state; return state;
} }

View File

@ -3,6 +3,10 @@ package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX; import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.DMXChannel; import de.ctdo.bunti.dmx.DMXChannel;
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class Par56Spot extends BuntiDMXDevice { public class Par56Spot extends BuntiDMXDevice {
private static final String CHANNEL_MODE = "mode"; private static final String CHANNEL_MODE = "mode";
@ -11,8 +15,17 @@ public class Par56Spot extends BuntiDMXDevice {
private static final String CHANNEL_BLUE = "blue"; private static final String CHANNEL_BLUE = "blue";
private static final String CHANNEL_SPEED = "speed"; private static final String CHANNEL_SPEED = "speed";
public Par56Spot() {
super();
addChannels();
}
public Par56Spot(int deviceId, int startAddress, String deviceName) { public Par56Spot(int deviceId, int startAddress, String deviceName) {
super(deviceId, startAddress, deviceName); super(deviceId, startAddress, deviceName);
addChannels();
}
private void addChannels() {
int offset = 0; int offset = 0;
addChannel(new DMXChannel(offset++, CHANNEL_MODE)); addChannel(new DMXChannel(offset++, CHANNEL_MODE));
addChannel(new DMXChannel(offset++, CHANNEL_RED)); addChannel(new DMXChannel(offset++, CHANNEL_RED));
@ -33,14 +46,17 @@ public class Par56Spot extends BuntiDMXDevice {
setChannelValueByName(CHANNEL_BLUE, value); setChannelValueByName(CHANNEL_BLUE, value);
} }
@Transient
public final int getRed() { public final int getRed() {
return getChannelValueByName(CHANNEL_RED); return getChannelValueByName(CHANNEL_RED);
} }
@Transient
public final int getGreen() { public final int getGreen() {
return getChannelValueByName(CHANNEL_GREEN); return getChannelValueByName(CHANNEL_GREEN);
} }
@Transient
public final int getBlue() { public final int getBlue() {
return getChannelValueByName(CHANNEL_BLUE); return getChannelValueByName(CHANNEL_BLUE);
} }

View File

@ -3,12 +3,8 @@ package de.ctdo.bunti.model;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -18,7 +14,10 @@ public final class Room {
private int id; private int id;
private String name; private String name;
private List<BuntiDevice> devices = new ArrayList<BuntiDevice>(); private String floor;
private int xCord;
private int yCord;
// private List<BuntiDevice> devices = new ArrayList<BuntiDevice>();
@Id @Id
@GeneratedValue(generator="increment") @GeneratedValue(generator="increment")
@ -31,6 +30,7 @@ public final class Room {
this.id = id; this.id = id;
} }
@Column( name = "roomName")
public String getName() { public String getName() {
return name; return name;
} }
@ -39,26 +39,65 @@ public final class Room {
this.name = name; this.name = name;
} }
public boolean addDevice(BuntiDevice device) { public int getxCord() {
return devices.add(device); return xCord;
} }
public boolean removeDevice(BuntiDevice device) { public void setxCord(int xCord) {
return devices.remove(device); this.xCord = xCord;
} }
public BuntiDevice getDevice(int id) { public int getyCord() {
for (BuntiDevice device: devices) { return yCord;
if( device.getId() == id) {
return device;
}
}
return null;
} }
public Collection<BuntiDevice> getDeviceList() { public void setyCord(int yCord) {
return Collections.unmodifiableList(devices); this.yCord = yCord;
} }
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
// @OneToMany(mappedBy="room")
// public List<BuntiDevice> getDevices() {
// return devices;
// }
//
// public void setDevices(List<BuntiDevice> devices) {
// this.devices = devices;
// }
// @Transient
// public boolean addDevice(BuntiDevice device) {
// return getDevices().add(device);
// }
//
// @Transient
// public boolean removeDevice(BuntiDevice device) {
// return getDevices().remove(device);
// }
//
// @Transient
// public BuntiDevice getDevice(int id) {
// for (BuntiDevice device: getDevices()) {
// if( device.getId() == id) {
// return device;
// }
// }
// return null;
// }
//
// @Transient
// public List<BuntiDevice> getDeviceList() {
// return Collections.unmodifiableList(getDevices());
// }
} }

View File

@ -3,15 +3,27 @@ package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX; import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.DMXChannel; import de.ctdo.bunti.dmx.DMXChannel;
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class Strobe1500 extends BuntiDMXDevice { public class Strobe1500 extends BuntiDMXDevice {
private static final String CHANNEL_SPEED = "speed"; private static final String CHANNEL_SPEED = "speed";
private static final String CHANNEL_INTENSITY = "intensity"; private static final String CHANNEL_INTENSITY = "intensity";
private static final String CHANNEL_MODE = "mode"; private static final String CHANNEL_MODE = "mode";
public Strobe1500() {
super();
addChannels();
}
public Strobe1500(int deviceId, int startAddress, String deviceName) { public Strobe1500(int deviceId, int startAddress, String deviceName) {
super(deviceId, startAddress, deviceName); super(deviceId, startAddress, deviceName);
addChannels();
}
private void addChannels() {
addChannel(new DMXChannel(0, CHANNEL_SPEED)); addChannel(new DMXChannel(0, CHANNEL_SPEED));
addChannel(new DMXChannel(1, CHANNEL_INTENSITY)); addChannel(new DMXChannel(1, CHANNEL_INTENSITY));
addChannel(new DMXChannel(2, CHANNEL_MODE)); addChannel(new DMXChannel(2, CHANNEL_MODE));
@ -29,14 +41,17 @@ public class Strobe1500 extends BuntiDMXDevice {
return setChannelValueByName(CHANNEL_MODE, value); return setChannelValueByName(CHANNEL_MODE, value);
} }
@Transient
public final int getSpeed() { public final int getSpeed() {
return getChannelValueByName(CHANNEL_SPEED); return getChannelValueByName(CHANNEL_SPEED);
} }
@Transient
public final int getIntensity() { public final int getIntensity() {
return getChannelValueByName(CHANNEL_INTENSITY); return getChannelValueByName(CHANNEL_INTENSITY);
} }
@Transient
public final int getMode() { public final int getMode() {
return getChannelValueByName(CHANNEL_MODE); return getChannelValueByName(CHANNEL_MODE);
} }

View File

@ -24,14 +24,23 @@ public class RoomsController {
@RequestMapping(value = "", method = RequestMethod.GET) @RequestMapping(value = "", method = RequestMethod.GET)
public @ResponseBody Collection<Room> getAll() { public @ResponseBody Collection<Room> getAll() {
LOGGER.info("handle GET /rooms/" + " request"); LOGGER.info("handle GET /rooms" + " request");
return controller.getAllRooms(); return controller.getAllRooms();
} }
@RequestMapping(value = "/{id}", method = RequestMethod.GET) @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody public @ResponseBody Room getRoomById(@PathVariable("id") int id) {
Room getDeviceById(@PathVariable("id") int id) { LOGGER.info("handle GET /rooms/" + id + " request");
LOGGER.info("handle GET /rooms/id" + id + " request"); return controller.getRoomById(id);
}
@RequestMapping(value = "/{id}/devices", method = RequestMethod.GET)
public @ResponseBody Room getDevicesFromRoom(@PathVariable("id") int id) {
LOGGER.info("handle GET /rooms/id/devices " + id + " request");
Room r = controller.getRoomById(id);
return controller.getRoomById(id); return controller.getRoomById(id);
} }

View File

@ -10,13 +10,13 @@
<jdbc:script location="classpath:init.sql" /> <jdbc:script location="classpath:init.sql" />
</jdbc:embedded-database> </jdbc:embedded-database>
<bean id="hibernateSessionFactory" <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="packagesToScan" value="de.ctdo.bunti.model"/>
<property name="mappingResources"> <!--<property name="mappingResources">-->
<list> <!--<list>-->
<value>/bunti.hbm.xml</value> <!--<value>/bunti.hbm.xml</value>-->
</list> <!--</list>-->
</property> <!--</property>-->
<property name="hibernateProperties"> <property name="hibernateProperties">
<props> <props>
<prop key="hibernate.dialect"> <prop key="hibernate.dialect">

View File

@ -1,9 +1,24 @@
drop table rooms if exists; drop table rooms if exists;
create table rooms (id integer identity primary key, roomName varchar (255) not null); create table rooms (id integer identity primary key,
roomName varchar (255) not null,
floor varchar (255),
yCord integer,
xCord integer
);
create table devices (id integer primary key,
ROOM_ID integer,
deviceName varchar (255) not null,
picture varchar (255),
startAddress integer,
DTYPE varchar (255)
);
insert into rooms (roomName) values ('Raum 1');
insert into rooms (roomName) values ('Raum 2');
insert into rooms (roomName) values ('Raum 3');
insert into rooms (roomName) values ('Raum 4');

View File

@ -26,7 +26,7 @@
<root> <root>
<priority value="debug" /> <priority value="info" />
<appender-ref ref="console" /> <appender-ref ref="console" />
</root> </root>