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

@ -16,7 +16,7 @@ import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.*;
@Component
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerImpl.class);
private ApplicationEventPublisher applicationEventPublisher = null;
private BuntiDevicesDAO devicesDAO;

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.dao;
import de.ctdo.bunti.model.Par56Spot;
import de.ctdo.bunti.model.Room;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
@ -15,6 +16,23 @@ public final class RoomsDAOImpl extends HibernateDaoSupport implements RoomsDAO
@Override
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);
}

View File

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

View File

@ -1,5 +1,8 @@
package de.ctdo.bunti.model;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
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, ...
* @author lucas
*/
@Entity
@Table(name = "devices")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class BuntiDevice {
private int deviceId;
private String deviceName;
private String picture;
// private Room room;
public BuntiDevice() {
}
public BuntiDevice(int deviceId, String deviceName) {
this.deviceId = deviceId;
@ -21,7 +32,7 @@ public abstract class BuntiDevice {
* Get the type of this device
* @return a string with the class name (=the Type)
*/
@SuppressWarnings("UnusedDeclaration")
@Transient
public final String getType() {
String FQClassName = this.getClass().getName();
int firstChar = FQClassName.lastIndexOf ('.') + 1;
@ -35,10 +46,17 @@ public abstract class BuntiDevice {
* Gets the device Id
* @return the device Id
*/
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public final int getId() {
return deviceId;
}
public final void setId(int id) {
this.deviceId = id;
}
/**
* Gets the device name
* @return The name of the device
@ -90,4 +108,13 @@ public abstract class BuntiDevice {
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;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.Map;
@Entity
public abstract class BuntiSwitchingDevice extends BuntiDevice {
private static final String OPTION_STATE = "state";
@ -30,6 +33,7 @@ public abstract class BuntiSwitchingDevice extends BuntiDevice {
return false;
}
@Transient
public boolean isState() {
return state;
}

View File

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

View File

@ -3,12 +3,8 @@ package de.ctdo.bunti.model;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -18,7 +14,10 @@ public final class Room {
private int id;
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
@GeneratedValue(generator="increment")
@ -31,6 +30,7 @@ public final class Room {
this.id = id;
}
@Column( name = "roomName")
public String getName() {
return name;
}
@ -39,26 +39,65 @@ public final class Room {
this.name = name;
}
public boolean addDevice(BuntiDevice device) {
return devices.add(device);
public int getxCord() {
return xCord;
}
public boolean removeDevice(BuntiDevice device) {
return devices.remove(device);
public void setxCord(int xCord) {
this.xCord = xCord;
}
public BuntiDevice getDevice(int id) {
for (BuntiDevice device: devices) {
if( device.getId() == id) {
return device;
}
}
return null;
public int getyCord() {
return yCord;
}
public Collection<BuntiDevice> getDeviceList() {
return Collections.unmodifiableList(devices);
public void setyCord(int yCord) {
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.DMXChannel;
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class Strobe1500 extends BuntiDMXDevice {
private static final String CHANNEL_SPEED = "speed";
private static final String CHANNEL_INTENSITY = "intensity";
private static final String CHANNEL_MODE = "mode";
public Strobe1500() {
super();
addChannels();
}
public Strobe1500(int deviceId, int startAddress, String deviceName) {
super(deviceId, startAddress, deviceName);
addChannels();
}
private void addChannels() {
addChannel(new DMXChannel(0, CHANNEL_SPEED));
addChannel(new DMXChannel(1, CHANNEL_INTENSITY));
addChannel(new DMXChannel(2, CHANNEL_MODE));
@ -29,14 +41,17 @@ public class Strobe1500 extends BuntiDMXDevice {
return setChannelValueByName(CHANNEL_MODE, value);
}
@Transient
public final int getSpeed() {
return getChannelValueByName(CHANNEL_SPEED);
}
@Transient
public final int getIntensity() {
return getChannelValueByName(CHANNEL_INTENSITY);
}
@Transient
public final int getMode() {
return getChannelValueByName(CHANNEL_MODE);
}

View File

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

View File

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

View File

@ -1,9 +1,24 @@
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>
<priority value="debug" />
<priority value="info" />
<appender-ref ref="console" />
</root>