From 55fe1dca1751a7eb999f09fb20f069a72bdd9dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 23:43:00 +0100 Subject: [PATCH] annotation based hibernate. but relations are still broken and the inheritance (Device) is not working correctly --- .../bunti/control/BuntiControllerImpl.java | 2 +- .../java/de/ctdo/bunti/dao/RoomsDAOImpl.java | 18 ++++ .../de/ctdo/bunti/model/BuntiDMXDevice.java | 12 +++ .../java/de/ctdo/bunti/model/BuntiDevice.java | 29 ++++++- .../bunti/model/BuntiSwitchingDevice.java | 4 + .../java/de/ctdo/bunti/model/Par56Spot.java | 16 ++++ src/main/java/de/ctdo/bunti/model/Room.java | 83 ++++++++++++++----- .../java/de/ctdo/bunti/model/Strobe1500.java | 15 ++++ .../de/ctdo/bunti/web/RoomsController.java | 17 +++- .../META-INF/spring/hibernate-beans.xml | 14 ++-- src/main/resources/init.sql | 25 ++++-- src/main/resources/log4j.xml | 2 +- 12 files changed, 196 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java index 9f99185..95cb875 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java @@ -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; diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java index b3359a2..bf0921c 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java @@ -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 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); } diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java index 13f1d4c..5c7d239 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java @@ -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 getChannelData() { Map map = new HashMap(); diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index 2922fbf..003a492 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -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 options); +// @ManyToOne +// @JoinColumn(name="ROOM_ID") +// public Room getRoom() { +// return room; +// } +// +// public void setRoom(Room room) { +// this.room = room; +// } } diff --git a/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java index 949c0f2..3c86416 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java @@ -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; } diff --git a/src/main/java/de/ctdo/bunti/model/Par56Spot.java b/src/main/java/de/ctdo/bunti/model/Par56Spot.java index 5af682d..69ac634 100644 --- a/src/main/java/de/ctdo/bunti/model/Par56Spot.java +++ b/src/main/java/de/ctdo/bunti/model/Par56Spot.java @@ -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); } diff --git a/src/main/java/de/ctdo/bunti/model/Room.java b/src/main/java/de/ctdo/bunti/model/Room.java index 9b20c40..34e5a55 100644 --- a/src/main/java/de/ctdo/bunti/model/Room.java +++ b/src/main/java/de/ctdo/bunti/model/Room.java @@ -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 devices = new ArrayList(); + private String floor; + private int xCord; + private int yCord; +// private List devices = new ArrayList(); @Id @GeneratedValue(generator="increment") @@ -31,6 +30,7 @@ public final class Room { this.id = id; } + @Column( name = "roomName") public String getName() { return name; } @@ -38,27 +38,66 @@ public final class Room { public void setName(String name) { this.name = name; } - - public boolean addDevice(BuntiDevice device) { - return devices.add(device); - } - - public boolean removeDevice(BuntiDevice device) { - return devices.remove(device); + + public int getxCord() { + return xCord; } - public BuntiDevice getDevice(int id) { - for (BuntiDevice device: devices) { - if( device.getId() == id) { - return device; - } - } - return null; + public void setxCord(int xCord) { + this.xCord = xCord; } - public Collection getDeviceList() { - return Collections.unmodifiableList(devices); + public int getyCord() { + return yCord; } + 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 getDevices() { +// return devices; +// } +// +// public void setDevices(List 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 getDeviceList() { +// return Collections.unmodifiableList(getDevices()); +// } } diff --git a/src/main/java/de/ctdo/bunti/model/Strobe1500.java b/src/main/java/de/ctdo/bunti/model/Strobe1500.java index 35b4e93..0db3fe6 100644 --- a/src/main/java/de/ctdo/bunti/model/Strobe1500.java +++ b/src/main/java/de/ctdo/bunti/model/Strobe1500.java @@ -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); } diff --git a/src/main/java/de/ctdo/bunti/web/RoomsController.java b/src/main/java/de/ctdo/bunti/web/RoomsController.java index bddcb1b..180172a 100644 --- a/src/main/java/de/ctdo/bunti/web/RoomsController.java +++ b/src/main/java/de/ctdo/bunti/web/RoomsController.java @@ -24,14 +24,23 @@ public class RoomsController { @RequestMapping(value = "", method = RequestMethod.GET) public @ResponseBody Collection 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); } diff --git a/src/main/resources/META-INF/spring/hibernate-beans.xml b/src/main/resources/META-INF/spring/hibernate-beans.xml index 00616b7..ccb9eb1 100755 --- a/src/main/resources/META-INF/spring/hibernate-beans.xml +++ b/src/main/resources/META-INF/spring/hibernate-beans.xml @@ -10,13 +10,13 @@ - - - - /bunti.hbm.xml - - + + + + + + + diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql index 64c598a..067434b 100755 --- a/src/main/resources/init.sql +++ b/src/main/resources/init.sql @@ -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'); \ No newline at end of file diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index 2494fe4..c241217 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -26,7 +26,7 @@ - +