From 2c259d8bfc437f4a4c768ace33a15cc39e86b151 Mon Sep 17 00:00:00 2001 From: Juergen Jung Date: Sat, 10 Mar 2012 22:20:57 +0100 Subject: [PATCH 01/11] Do some code clean up --- .../java/de/ctdo/bunti/artnet/ByteUtils.java | 4 ++-- .../bunti/artnet/packets/ArtNetPacket.java | 2 +- .../ctdo/bunti/artnet/packets/PacketType.java | 9 ++++++-- .../java/de/ctdo/bunti/model/BuntiDevice.java | 8 +++---- .../de/ctdo/bunti/web/RestController.java | 21 ++++++------------- .../AtmosphereResourceArgumentResolver.java | 2 +- .../ctdo/bunti/websocket/AtmosphereUtils.java | 3 ++- .../bunti/websocket/WebSocketController.java | 16 +++++++------- 8 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/java/de/ctdo/bunti/artnet/ByteUtils.java b/src/main/java/de/ctdo/bunti/artnet/ByteUtils.java index 33cf9c1..39d81f8 100644 --- a/src/main/java/de/ctdo/bunti/artnet/ByteUtils.java +++ b/src/main/java/de/ctdo/bunti/artnet/ByteUtils.java @@ -28,7 +28,7 @@ public class ByteUtils { private final byte[] data; public ByteUtils(byte[] data) { - this.data = data; + this.data = data.clone(); } public static final int byteToUint(byte b) { @@ -40,7 +40,7 @@ public class ByteUtils { } public final byte[] getBytes() { - return data; + return data.clone(); } public final int getInt16(int offset) { diff --git a/src/main/java/de/ctdo/bunti/artnet/packets/ArtNetPacket.java b/src/main/java/de/ctdo/bunti/artnet/packets/ArtNetPacket.java index a63e753..2beaae6 100644 --- a/src/main/java/de/ctdo/bunti/artnet/packets/ArtNetPacket.java +++ b/src/main/java/de/ctdo/bunti/artnet/packets/ArtNetPacket.java @@ -37,7 +37,7 @@ public abstract class ArtNetPacket { } public final void setData(byte[] data) { - this.data = new ByteUtils(data); + this.data = new ByteUtils(data.clone()); } public final PacketType getType() { diff --git a/src/main/java/de/ctdo/bunti/artnet/packets/PacketType.java b/src/main/java/de/ctdo/bunti/artnet/packets/PacketType.java index 874e8d3..a5c3dd8 100644 --- a/src/main/java/de/ctdo/bunti/artnet/packets/PacketType.java +++ b/src/main/java/de/ctdo/bunti/artnet/packets/PacketType.java @@ -19,6 +19,9 @@ package de.ctdo.bunti.artnet.packets; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public enum PacketType { ART_POLL(0x2000, null), @@ -47,6 +50,8 @@ public enum PacketType { private final int opCode; private final Class packetClass; + private static final Logger LOGGER = LoggerFactory.getLogger(PacketType.class); + private PacketType(int code, Class clazz) { opCode = code; @@ -59,9 +64,9 @@ public enum PacketType { try { p = packetClass.newInstance(); } catch (InstantiationException e) { - e.printStackTrace(); + LOGGER.error("Could not instanciate ArtNetPacket: ",e); } catch (IllegalAccessException e) { - e.printStackTrace(); + LOGGER.error("Could not instanciate ArtNetPacket: ",e); } } return p; diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index 4d1665b..ddfa819 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -22,12 +22,12 @@ public abstract class BuntiDevice { */ @SuppressWarnings("UnusedDeclaration") public final String getType() { - String FQClassName = this.getClass().getName(); - int firstChar = FQClassName.lastIndexOf ('.') + 1; + String fqClassName = this.getClass().getName(); + int firstChar = fqClassName.lastIndexOf ('.') + 1; if ( firstChar > 0 ) { - FQClassName = FQClassName.substring ( firstChar ); + fqClassName = fqClassName.substring ( firstChar ); } - return FQClassName; + return fqClassName; } /** diff --git a/src/main/java/de/ctdo/bunti/web/RestController.java b/src/main/java/de/ctdo/bunti/web/RestController.java index bc68cb3..da6a236 100644 --- a/src/main/java/de/ctdo/bunti/web/RestController.java +++ b/src/main/java/de/ctdo/bunti/web/RestController.java @@ -8,35 +8,26 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import javax.validation.Validator; import java.util.Collection; import java.util.Map; @Controller public class RestController { private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class); - private Validator validator; + + @Autowired private BuntiController controller; - @Autowired - public void setValidator(Validator validator) { - this.validator = validator; - } - - @Autowired - public final void setController(BuntiController controller) { - this.controller = controller; - } - - @RequestMapping(value = "/devices", method = RequestMethod.GET) - public @ResponseBody Collection getAll() { + @ResponseBody + public Collection getAll() { LOGGER.info("handle GET /devices request"); return controller.getAllDevices(); } @RequestMapping(value = "/devices/{id}", method = RequestMethod.GET) - public @ResponseBody BuntiDevice getDeviceById(@PathVariable("id") int id) { + @ResponseBody + public BuntiDevice getDeviceById(@PathVariable("id") int id) { LOGGER.info("handle GET /devices/" + id + " request"); return controller.getDeviceById(id); } diff --git a/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java b/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java index fe14b33..671b4ab 100644 --- a/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java +++ b/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java @@ -17,7 +17,7 @@ public class AtmosphereResourceArgumentResolver implements WebArgumentResolver { * @see org.springframework.web.bind.support.WebArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.context.request.NativeWebRequest) */ @Override - public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception { + public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { if (AtmosphereResource.class.isAssignableFrom(methodParameter.getParameterType())) { return AtmosphereUtils.getAtmosphereResource(webRequest.getNativeRequest(HttpServletRequest.class)); diff --git a/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java b/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java index b3f56f1..502e296 100644 --- a/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java +++ b/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java @@ -9,8 +9,9 @@ import org.springframework.util.Assert; public final class AtmosphereUtils { + private AtmosphereUtils() { } - public static AtmosphereResource getAtmosphereResource( + public static AtmosphereResource getAtmosphereResource( HttpServletRequest request) { AtmosphereResource resource = diff --git a/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java b/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java index 30c51df..780e2e1 100644 --- a/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java +++ b/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java @@ -1,11 +1,5 @@ package de.ctdo.bunti.websocket; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.atmosphere.cpr.AtmosphereResource; import org.atmosphere.cpr.Broadcaster; import org.codehaus.jackson.map.ObjectMapper; @@ -16,6 +10,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + @Controller public class WebSocketController { private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketController.class); @@ -40,10 +40,8 @@ public class WebSocketController { bc.scheduleFixedBroadcast(new Callable() { - private long sinceId = 0; - @Override - public String call() throws Exception { + public String call() throws IOException { LOGGER.debug("call was called"); return mapper.writeValueAsString("blafaselblubb"); From 64f90ce10935e71ab12f5f98892fa78b771eb3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 00:44:41 +0100 Subject: [PATCH 02/11] rooms controller --- .../java/de/ctdo/bunti/control/BuntiController.java | 4 ++++ .../de/ctdo/bunti/control/BuntiControllerImpl.java | 10 ++++++++++ src/main/java/de/ctdo/bunti/web/RestController.java | 1 - 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/ctdo/bunti/control/BuntiController.java b/src/main/java/de/ctdo/bunti/control/BuntiController.java index ae9f0f1..0374d7c 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiController.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiController.java @@ -1,6 +1,7 @@ package de.ctdo.bunti.control; import de.ctdo.bunti.model.BuntiDevice; +import de.ctdo.bunti.model.Room; import java.util.Collection; import java.util.Map; @@ -12,4 +13,7 @@ public interface BuntiController { boolean updateDeviceData(int deviceId, Map options); + Collection getAllRooms(); + Room getRoomById(int roomId); + } diff --git a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java index bca6fbb..9f99185 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java @@ -54,6 +54,16 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPub return false; } + @Override + public Collection getAllRooms() { + return roomsDAO.getRooms(); + } + + @Override + public Room getRoomById(int roomId) { + return roomsDAO.getRoom(roomId); + } + @Override public Collection getAllDevices() { return devicesDAO.getAllDevices(); diff --git a/src/main/java/de/ctdo/bunti/web/RestController.java b/src/main/java/de/ctdo/bunti/web/RestController.java index a6ebce6..5e846a3 100644 --- a/src/main/java/de/ctdo/bunti/web/RestController.java +++ b/src/main/java/de/ctdo/bunti/web/RestController.java @@ -10,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import javax.validation.Validator; import java.util.*; @Controller From 8b362e1ebc931dae1f91f906bb2ecde36e7b29f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 00:44:55 +0100 Subject: [PATCH 03/11] rooms controller --- .../de/ctdo/bunti/web/RoomsController.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/de/ctdo/bunti/web/RoomsController.java diff --git a/src/main/java/de/ctdo/bunti/web/RoomsController.java b/src/main/java/de/ctdo/bunti/web/RoomsController.java new file mode 100644 index 0000000..5928cf1 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/web/RoomsController.java @@ -0,0 +1,38 @@ +package de.ctdo.bunti.web; + +import de.ctdo.bunti.control.BuntiController; +import de.ctdo.bunti.model.Room; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.Collection; + +@Controller +@RequestMapping(value = "/rooms") +public class RoomsController { + private static final Logger LOGGER = LoggerFactory.getLogger(RoomsController.class); + private BuntiController controller; + + + @Autowired + public final void setController(BuntiController controller) { + this.controller = controller; + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public @ResponseBody Collection getAll() { + 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"); + return controller.getRoomById(id); + } + +} From 4f755abce6995cfbb4425e23e64170b8f1c18dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 01:04:53 +0100 Subject: [PATCH 04/11] rooms controller --- src/main/java/de/ctdo/bunti/web/RoomsController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/ctdo/bunti/web/RoomsController.java b/src/main/java/de/ctdo/bunti/web/RoomsController.java index 5928cf1..bddcb1b 100644 --- a/src/main/java/de/ctdo/bunti/web/RoomsController.java +++ b/src/main/java/de/ctdo/bunti/web/RoomsController.java @@ -22,7 +22,7 @@ public class RoomsController { this.controller = controller; } - @RequestMapping(value = "/", method = RequestMethod.GET) + @RequestMapping(value = "", method = RequestMethod.GET) public @ResponseBody Collection getAll() { LOGGER.info("handle GET /rooms/" + " request"); return controller.getAllRooms(); From 793cb2a94060ed0f255c11ecffc4b900cd8dfe4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 21:22:53 +0100 Subject: [PATCH 05/11] renamed getDeviceID to getId --- src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java | 2 +- src/main/java/de/ctdo/bunti/model/BuntiDevice.java | 2 +- src/main/java/de/ctdo/bunti/model/Par56Spot.java | 2 +- src/main/java/de/ctdo/bunti/model/Strobe1500.java | 2 +- src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index a142599..e9c009b 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -53,7 +53,7 @@ public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO { @Override public BuntiDevice getDeviceById(int deviceId) { for (BuntiDevice dev : devices) { - if(dev.getDeviceId() == deviceId) { + if(dev.getId() == deviceId) { return dev; } } diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index d0bbba3..2922fbf 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -35,7 +35,7 @@ public abstract class BuntiDevice { * Gets the device Id * @return the device Id */ - public final int getDeviceId() { + public final int getId() { return deviceId; } diff --git a/src/main/java/de/ctdo/bunti/model/Par56Spot.java b/src/main/java/de/ctdo/bunti/model/Par56Spot.java index c59c0e2..5af682d 100644 --- a/src/main/java/de/ctdo/bunti/model/Par56Spot.java +++ b/src/main/java/de/ctdo/bunti/model/Par56Spot.java @@ -67,7 +67,7 @@ public class Par56Spot extends BuntiDMXDevice { public final String toString() { StringBuilder sb = new StringBuilder(); sb.append("Par56Spot "); - sb.append(getDeviceId()); + sb.append(getId()); sb.append(", "); sb.append(getDeviceName()); sb.append(" ["); diff --git a/src/main/java/de/ctdo/bunti/model/Strobe1500.java b/src/main/java/de/ctdo/bunti/model/Strobe1500.java index f0bd9f7..35b4e93 100644 --- a/src/main/java/de/ctdo/bunti/model/Strobe1500.java +++ b/src/main/java/de/ctdo/bunti/model/Strobe1500.java @@ -59,7 +59,7 @@ public class Strobe1500 extends BuntiDMXDevice { public final String toString() { StringBuilder sb = new StringBuilder(); sb.append("Strobe1500 "); - sb.append(getDeviceId()); + sb.append(getId()); sb.append(", "); sb.append(getDeviceName()); sb.append(" ["); diff --git a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java index c3583f9..c1388e8 100644 --- a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java +++ b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java @@ -139,7 +139,7 @@ public class BuntiDMXDeviceTest { @Test public void testGetDeviceId() throws Exception { - assertEquals(DEVICEID, dut.getDeviceId()); + assertEquals(DEVICEID, dut.getId()); } @Test From c09e08200fcc74a6a9390d7ad3876ad69a532896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 21:23:16 +0100 Subject: [PATCH 06/11] tried to make hibernate work with h2 and annotations --- pom.xml | 12 ++++++++--- src/main/java/de/ctdo/bunti/model/Room.java | 14 ++++++++++++- .../META-INF/spring/hibernate-beans.xml | 2 +- src/main/resources/bunti.hbm.xml | 20 +++++++++++++------ src/main/resources/init.sql | 15 ++++++-------- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 29a81ca..39c743e 100644 --- a/pom.xml +++ b/pom.xml @@ -90,10 +90,16 @@ 3.6.7.Final + + + + + + - org.hsqldb - hsqldb - 1.8.0.10 + com.h2database + h2 + 1.3.160 diff --git a/src/main/java/de/ctdo/bunti/model/Room.java b/src/main/java/de/ctdo/bunti/model/Room.java index 8eed3d3..9b20c40 100644 --- a/src/main/java/de/ctdo/bunti/model/Room.java +++ b/src/main/java/de/ctdo/bunti/model/Room.java @@ -1,16 +1,28 @@ 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 java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +@Entity +@Table( name = "rooms" ) public final class Room { private int id; private String name; private List devices = new ArrayList(); + @Id + @GeneratedValue(generator="increment") + @GenericGenerator(name="increment", strategy = "increment") public int getId() { return id; } @@ -37,7 +49,7 @@ public final class Room { public BuntiDevice getDevice(int id) { for (BuntiDevice device: devices) { - if( device.getDeviceId() == id) { + if( device.getId() == id) { return device; } } diff --git a/src/main/resources/META-INF/spring/hibernate-beans.xml b/src/main/resources/META-INF/spring/hibernate-beans.xml index 84e0dac..00616b7 100755 --- a/src/main/resources/META-INF/spring/hibernate-beans.xml +++ b/src/main/resources/META-INF/spring/hibernate-beans.xml @@ -6,7 +6,7 @@ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd" xmlns:jdbc="http://www.springframework.org/schema/jdbc"> - + diff --git a/src/main/resources/bunti.hbm.xml b/src/main/resources/bunti.hbm.xml index 5fedde1..4f32d5e 100755 --- a/src/main/resources/bunti.hbm.xml +++ b/src/main/resources/bunti.hbm.xml @@ -4,11 +4,19 @@ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - - - - - - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql index 00e6dc0..64c598a 100755 --- a/src/main/resources/init.sql +++ b/src/main/resources/init.sql @@ -1,12 +1,9 @@ -DROP TABLE DUAL_HIBERNATE_SEQUENCE IF EXISTS -DROP TABLE ROOMS IF EXISTS -DROP TABLE SEQUENCE IF EXISTS - - -CREATE MEMORY TABLE SEQUENCE(SEQ_NAME VARCHAR(50) NOT NULL PRIMARY KEY,SEQ_COUNT NUMERIC(38)) -CREATE MEMORY TABLE ROOMS(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,NAME VARCHAR(255)) -CREATE MEMORY TABLE DUAL_HIBERNATE_SEQUENCE(ZERO INTEGER) -INSERT INTO SEQUENCE VALUES('SEQ_GEN',0) +drop table rooms if exists; +create table rooms (id integer identity primary key, roomName varchar (255) not null); +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 From 04f77c6553ed1ec42c2be459a26a6c4be3a00a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 21 Mar 2012 22:17:40 +0100 Subject: [PATCH 07/11] renamed controller and removed duplicate function to update device data. --- ...ontroller.java => DeviceControlController.java} | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) rename src/main/java/de/ctdo/bunti/web/{RestController.java => DeviceControlController.java} (78%) diff --git a/src/main/java/de/ctdo/bunti/web/RestController.java b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java similarity index 78% rename from src/main/java/de/ctdo/bunti/web/RestController.java rename to src/main/java/de/ctdo/bunti/web/DeviceControlController.java index 5e846a3..9df036d 100644 --- a/src/main/java/de/ctdo/bunti/web/RestController.java +++ b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java @@ -14,11 +14,10 @@ import java.util.*; @Controller @RequestMapping(value = "/control") -public class RestController { - private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class); +public class DeviceControlController { + private static final Logger LOGGER = LoggerFactory.getLogger(DeviceControlController.class); private BuntiController controller; - @Autowired public final void setController(BuntiController controller) { this.controller = controller; @@ -36,15 +35,6 @@ public class RestController { return controller.getDeviceById(id); } - @RequestMapping(value = "/devices/{id}", method = RequestMethod.POST) - public @ResponseBody BuntiDevice setDeviceById(@PathVariable("id") int id, @RequestBody DeviceUpdate update) { - LOGGER.info("handle PUT /devices/" + id + " request update=" + update.toString() ); - - - controller.updateDeviceData(id, update.getOptions()); - return controller.getDeviceById(id); - } - @RequestMapping(value = "/devices", method = RequestMethod.POST) public String setDevices(@RequestBody DeviceUpdates updates) { LOGGER.info("handle PUT /devices" + " request update=" + updates.toString() ); 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 08/11] 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 @@ - + From 0cb8e585af0427486e5b4d9a4e459d3ee7a41cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sun, 25 Mar 2012 16:23:45 +0200 Subject: [PATCH 09/11] jpa+hibernate basically working --- pom.xml | 49 ++++++++++++------- .../ctdo/bunti/dao/BuntiDevicesDAOImpl.java | 24 ++++----- .../java/de/ctdo/bunti/dao/RoomsDAOImpl.java | 18 ------- .../de/ctdo/bunti/model/BuntiDMXDevice.java | 9 +--- .../java/de/ctdo/bunti/model/BuntiDevice.java | 27 +++++----- .../bunti/model/BuntiSwitchingDevice.java | 7 +-- .../java/de/ctdo/bunti/model/Par56Spot.java | 5 -- src/main/java/de/ctdo/bunti/model/Room.java | 35 ++++++------- .../java/de/ctdo/bunti/model/Strobe1500.java | 5 -- .../META-INF/spring/hibernate-beans.xml | 41 ++++++++-------- src/main/resources/bunti.hbm.xml | 22 --------- src/main/resources/db.properties | 4 +- src/main/resources/init.sql | 24 --------- src/main/resources/init_data.sql | 11 +++++ src/main/resources/init_schema.sql | 29 +++++++++++ src/main/resources/log4j.properties | 15 ------ src/main/resources/log4j.xml | 8 +-- .../de/ctdo/bunti/dao/RoomsDAOImplTest.java | 35 +++++++++++++ .../de/ctdo/bunti/dmx/DMXMixerImplTest.java | 20 ++++++-- .../ctdo/bunti/model/BuntiDMXDeviceTest.java | 5 +- .../de/ctdo/bunti/model/Par56SpotTest.java | 6 ++- .../de/ctdo/bunti/model/Strobe1500Test.java | 6 ++- .../bunti/dao/RoomsDAOImplTest-context.xml | 38 ++++++++++++++ 23 files changed, 243 insertions(+), 200 deletions(-) delete mode 100755 src/main/resources/bunti.hbm.xml delete mode 100755 src/main/resources/init.sql create mode 100755 src/main/resources/init_data.sql create mode 100755 src/main/resources/init_schema.sql delete mode 100644 src/main/resources/log4j.properties create mode 100644 src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java create mode 100644 src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml diff --git a/pom.xml b/pom.xml index 39c743e..a3d308a 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,13 @@ ${org.springframework.version} + + org.springframework + spring-test + ${org.springframework.version} + + + org.slf4j slf4j-api @@ -73,15 +80,9 @@ - javax.validation - validation-api - 1.0.0.GA - - - - org.hibernate - hibernate-validator - 4.1.0.Final + org.springframework + spring-orm + ${org.springframework.version} @@ -90,11 +91,23 @@ 3.6.7.Final - - - - - + + org.hibernate + hibernate-commons-annotations + 3.2.0.Final + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + 1.0.1.Final + + + + org.hsqldb + hsqldb + 1.8.0.10 + com.h2database @@ -103,6 +116,8 @@ + + joda-time joda-time @@ -153,11 +168,7 @@ jetty-server ${jettyVersion} - - org.springframework - spring-orm - 3.0.6.RELEASE - + diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index e9c009b..dc25b01 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -17,21 +17,21 @@ public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO { private List devices = new ArrayList(); public BuntiDevicesDAOImpl() { - addDummyDevices(); +// addDummyDevices(); } - private void addDummyDevices() { - int deviceID = 0; - - devices.add(new Par56Spot(deviceID++, 1, "Par56 Lampe 1")); - devices.add(new Par56Spot(deviceID++, 6, "Par56 Lampe 2")); - devices.add(new Par56Spot(deviceID++, 11, "Par56 Lampe 3")); - devices.add(new Par56Spot(deviceID++, 16, "Par56 Lampe 4")); - devices.add(new Strobe1500(deviceID++, 21, "Stroboskop 1")); - devices.add(new Par56Spot(deviceID, 508, "Par56 Lampe 5")); - LOGGER.debug("added dummy devices in DAO"); - } +// private void addDummyDevices() { +// int deviceID = 0; +// +// devices.add(new Par56Spot(deviceID++, 1, "Par56 Lampe 1")); +// devices.add(new Par56Spot(deviceID++, 6, "Par56 Lampe 2")); +// devices.add(new Par56Spot(deviceID++, 11, "Par56 Lampe 3")); +// devices.add(new Par56Spot(deviceID++, 16, "Par56 Lampe 4")); +// devices.add(new Strobe1500(deviceID++, 21, "Stroboskop 1")); +// devices.add(new Par56Spot(deviceID, 508, "Par56 Lampe 5")); +// LOGGER.debug("added dummy devices in DAO"); +// } @Override diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java index bf0921c..3127851 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java @@ -13,26 +13,8 @@ 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 5c7d239..cf49d11 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java @@ -22,16 +22,11 @@ public abstract class BuntiDMXDevice extends BuntiDevice { } - public BuntiDMXDevice(int deviceId, int startAddress, String name) { - super(deviceId, name); - setStartAddress(startAddress); - } - /** * Gets the DMX start address for this device * @return The address value from 1 to max 512 */ - public final int getStartAddress() { + public int getStartAddress() { return startAddress; } @@ -40,7 +35,7 @@ public abstract class BuntiDMXDevice extends BuntiDevice { * @param startAddress The address value from 1 to max 512 * @return True on success, false if start address is wrong */ - public final boolean setStartAddress(int startAddress) { + public boolean setStartAddress(int startAddress) { if(startAddress < DMX.DMX_CHANNEL_INDEX_MIN || startAddress - 1 + dmxChannels.getCount() > DMX.DMX_CHANNEL_INDEX_MAX) { return false; diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index 003a492..f9f4504 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -1,7 +1,7 @@ package de.ctdo.bunti.model; -import org.hibernate.annotations.GenericGenerator; - +import javax.persistence.*; +import java.io.Serializable; import javax.persistence.*; import java.util.Map; @@ -14,19 +14,14 @@ import java.util.Map; @Table(name = "devices") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public abstract class BuntiDevice { - private int deviceId; + private int id; private String deviceName; private String picture; -// private Room room; public BuntiDevice() { } - public BuntiDevice(int deviceId, String deviceName) { - this.deviceId = deviceId; - this.deviceName = deviceName; - } /** * Get the type of this device @@ -47,21 +42,21 @@ public abstract class BuntiDevice { * @return the device Id */ @Id - @GeneratedValue(generator="increment") - @GenericGenerator(name="increment", strategy = "increment") + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "BUNTIDEVICE_ID", updatable=false, nullable=false) public final int getId() { - return deviceId; + return id; } public final void setId(int id) { - this.deviceId = id; + this.id = id; } /** * Gets the device name * @return The name of the device */ - public final String getDeviceName() { + public String getDeviceName() { return deviceName; } @@ -69,7 +64,7 @@ public abstract class BuntiDevice { * Sets the device Name * @param deviceName a String with the device name */ - public final void setDeviceName(String deviceName) { + public void setDeviceName(String deviceName) { this.deviceName = deviceName; } @@ -77,7 +72,7 @@ public abstract class BuntiDevice { * Get the relative URL to the picture of this device * @return the relative URL to the picture */ - public final String getPicture() { + public String getPicture() { return picture; } @@ -85,7 +80,7 @@ public abstract class BuntiDevice { * Get the relative URL to the picture of this device * @param picture The relative URL to the picture */ - public final void setPicture(String picture) { + public void setPicture(String picture) { this.picture = picture; } diff --git a/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java index 3c86416..293269d 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java @@ -7,15 +7,12 @@ import java.util.Map; @Entity public abstract class BuntiSwitchingDevice extends BuntiDevice { private static final String OPTION_STATE = "state"; - private boolean state = false; - public BuntiSwitchingDevice(int deviceId, String deviceName) { - super(deviceId, deviceName); + public BuntiSwitchingDevice() { + } - - @Override public final boolean setValuesFromOptions(Map options) { diff --git a/src/main/java/de/ctdo/bunti/model/Par56Spot.java b/src/main/java/de/ctdo/bunti/model/Par56Spot.java index 69ac634..92c2d41 100644 --- a/src/main/java/de/ctdo/bunti/model/Par56Spot.java +++ b/src/main/java/de/ctdo/bunti/model/Par56Spot.java @@ -20,11 +20,6 @@ public class Par56Spot extends BuntiDMXDevice { 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)); diff --git a/src/main/java/de/ctdo/bunti/model/Room.java b/src/main/java/de/ctdo/bunti/model/Room.java index 34e5a55..7512111 100644 --- a/src/main/java/de/ctdo/bunti/model/Room.java +++ b/src/main/java/de/ctdo/bunti/model/Room.java @@ -1,12 +1,10 @@ package de.ctdo.bunti.model; - -import org.hibernate.annotations.GenericGenerator; - import javax.persistence.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + @Entity @Table( name = "rooms" ) @@ -17,11 +15,11 @@ public final class Room { private String floor; private int xCord; private int yCord; -// private List devices = new ArrayList(); + private Set devices = new HashSet(); @Id - @GeneratedValue(generator="increment") - @GenericGenerator(name="increment", strategy = "increment") + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "ROOM_ID", updatable=false, nullable=false) public int getId() { return id; } @@ -65,14 +63,17 @@ public final class Room { } -// @OneToMany(mappedBy="room") -// public List getDevices() { -// return devices; -// } -// -// public void setDevices(List devices) { -// this.devices = devices; -// } + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = BuntiDevice.class) + @JoinTable(name = "ROOM_BUNTIDEVICE", + joinColumns = { @JoinColumn(name = "ROOM_ID") }, + inverseJoinColumns = { @JoinColumn(name = "BUNTIDEVICE_ID") }) + public Set getDevices() { + return this.devices; + } + + public void setDevices(Set devices) { + this.devices = devices; + } // @Transient diff --git a/src/main/java/de/ctdo/bunti/model/Strobe1500.java b/src/main/java/de/ctdo/bunti/model/Strobe1500.java index 0db3fe6..cdd647f 100644 --- a/src/main/java/de/ctdo/bunti/model/Strobe1500.java +++ b/src/main/java/de/ctdo/bunti/model/Strobe1500.java @@ -18,11 +18,6 @@ public class Strobe1500 extends BuntiDMXDevice { 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)); diff --git a/src/main/resources/META-INF/spring/hibernate-beans.xml b/src/main/resources/META-INF/spring/hibernate-beans.xml index ccb9eb1..dd19922 100755 --- a/src/main/resources/META-INF/spring/hibernate-beans.xml +++ b/src/main/resources/META-INF/spring/hibernate-beans.xml @@ -1,44 +1,45 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" + xmlns:jdbc="http://www.springframework.org/schema/jdbc"> + + + + + + - + - - - - - - - org.hibernate.dialect.HSQLDialect - + org.hibernate.dialect.HSQLDialect true - + create - + - - - + + + + + - + + diff --git a/src/main/resources/bunti.hbm.xml b/src/main/resources/bunti.hbm.xml deleted file mode 100755 index 4f32d5e..0000000 --- a/src/main/resources/bunti.hbm.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/db.properties b/src/main/resources/db.properties index e52abc7..7a44d51 100755 --- a/src/main/resources/db.properties +++ b/src/main/resources/db.properties @@ -1,2 +1,2 @@ -db.driverClassName=org.hsqldb.jdbcDriver -db.url=jdbc:hsqldb:mem:buntiserver +#db.driverClassName=org.hsqldb.jdbcDriver +#db.url=jdbc:hsqldb:mem:buntiserver diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql deleted file mode 100755 index 067434b..0000000 --- a/src/main/resources/init.sql +++ /dev/null @@ -1,24 +0,0 @@ - -drop table rooms if exists; - -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) - - ); - - - diff --git a/src/main/resources/init_data.sql b/src/main/resources/init_data.sql new file mode 100755 index 0000000..3e189f7 --- /dev/null +++ b/src/main/resources/init_data.sql @@ -0,0 +1,11 @@ + + +insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Kueche', '0', '0'); +insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Wohnzimmer', '0', '1'); +insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Werkstatt', '1', '0'); +insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Flur', '1', '1'); + + + + + diff --git a/src/main/resources/init_schema.sql b/src/main/resources/init_schema.sql new file mode 100755 index 0000000..72ae1bc --- /dev/null +++ b/src/main/resources/init_schema.sql @@ -0,0 +1,29 @@ +drop table ROOM_BUNTIDEVICE if exists; +drop table devices if exists; +drop table rooms if exists; + +create table ROOM_BUNTIDEVICE (ROOM_ID integer not null, BUNTIDEVICE_ID int not null, + primary key (ROOM_ID, BUNTIDEVICE_ID), unique (BUNTIDEVICE_ID)); + +create table devices (DTYPE varchar(31) not null, + BUNTIDEVICE_ID integer generated by default as identity (start with 1), + deviceName varchar(255), + picture varchar(255), + startAddress integer, + primary key (BUNTIDEVICE_ID)); + +create table rooms (ROOM_ID integer generated by default as identity (start with 1), + floor varchar(255), + roomName varchar(255), + xCord integer not null, + yCord integer not null, + primary key (ROOM_ID)); + +alter table ROOM_BUNTIDEVICE add constraint FK96EF8F028BB4B62 foreign key (ROOM_ID) references rooms; +alter table ROOM_BUNTIDEVICE add constraint FK96EF8F021E9F392 foreign key (BUNTIDEVICE_ID) references devices; + + + + + + diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index 256221f..0000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,15 +0,0 @@ -#log4j.rootLogger=debug, stdout - -#log4j.appender.stdout=org.apache.log4j.ConsoleAppender -#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout - -# Print the date in ISO 8601 format -##log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n -#log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %c: %m%n - -#log4j.appender.R=org.apache.log4j.RollingFileAppender -#log4j.appender.R.File=application.log -#log4j.appender.R.MaxFileSize=100KB -#log4j.appender.R.MaxBackupIndex=1 -#log4j.appender.R.layout=org.apache.log4j.PatternLayout -#log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index c241217..85d124a 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -15,15 +15,15 @@ - - - - + + + + diff --git a/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java b/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java new file mode 100644 index 0000000..b12d053 --- /dev/null +++ b/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java @@ -0,0 +1,35 @@ +package de.ctdo.bunti.dao; + +import de.ctdo.bunti.model.Room; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +@ContextConfiguration() +@RunWith(SpringJUnit4ClassRunner.class) +public class RoomsDAOImplTest { + + @Autowired + RoomsDAO dut; + + @Test +// @Transactional + public void testGetRooms() throws Exception { + assertEquals(4, dut.getRooms().size()); + } + + @Test + public void testGetRoomKueche() throws Exception { + assertEquals("Kueche", dut.getRoom(1).getName()); + } + + @Test + public void testGetRoomKuecheError() throws Exception { + assertNull(dut.getRoom(23)); + } +} diff --git a/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java b/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java index 50bc1ef..6d9b321 100644 --- a/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java +++ b/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java @@ -36,7 +36,10 @@ public class DMXMixerImplTest { @Test public void testUpdateDevice() throws Exception { - BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Par56Spot device = new Par56Spot(); + device.setId(23); + device.setStartAddress(42); + device.setDeviceName("deviceName"); Map options = new HashMap(); options.put("red", 44); assertTrue(dut.updateDevice(device, options)); @@ -44,7 +47,10 @@ public class DMXMixerImplTest { @Test public void testUpdateDeviceWrong1() throws Exception { - BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Par56Spot device = new Par56Spot(); + device.setId(23); + device.setStartAddress(42); + device.setDeviceName("deviceName"); assertFalse(dut.updateDevice(device, null)); } @@ -57,14 +63,20 @@ public class DMXMixerImplTest { @Test public void testUpdateDeviceWrong3() throws Exception { - BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Par56Spot device = new Par56Spot(); + device.setId(23); + device.setStartAddress(42); + device.setDeviceName("deviceName"); Map options = new HashMap(); assertFalse(dut.updateDevice(device, options)); } @Test public void testUpdateDeviceWrong4() throws Exception { - BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Par56Spot device = new Par56Spot(); + device.setId(23); + device.setStartAddress(42); + device.setDeviceName("deviceName"); Map options = new HashMap(); options.put("rednonexistent", 44); assertFalse(dut.updateDevice(device, options)); diff --git a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java index c1388e8..0589455 100644 --- a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java +++ b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java @@ -16,7 +16,10 @@ public class BuntiDMXDeviceTest { @Before public void setUp() throws Exception { - dut = new Par56Spot(DEVICEID,STARTADDRESS,"device"); + dut = new Par56Spot(); + dut.setId(23); + dut.setStartAddress(42); + dut.setDeviceName("deviceName"); } @Test diff --git a/src/test/java/de/ctdo/bunti/model/Par56SpotTest.java b/src/test/java/de/ctdo/bunti/model/Par56SpotTest.java index fb1b664..5c304c3 100644 --- a/src/test/java/de/ctdo/bunti/model/Par56SpotTest.java +++ b/src/test/java/de/ctdo/bunti/model/Par56SpotTest.java @@ -9,8 +9,10 @@ public class Par56SpotTest { @Before public void setUp() throws Exception { - dut = new Par56Spot(23,42,"device"); - + dut = new Par56Spot(); + dut.setId(23); + dut.setStartAddress(42); + dut.setDeviceName("device"); } @Test diff --git a/src/test/java/de/ctdo/bunti/model/Strobe1500Test.java b/src/test/java/de/ctdo/bunti/model/Strobe1500Test.java index 91f2264..433556b 100644 --- a/src/test/java/de/ctdo/bunti/model/Strobe1500Test.java +++ b/src/test/java/de/ctdo/bunti/model/Strobe1500Test.java @@ -10,8 +10,10 @@ public class Strobe1500Test { @Before public void setUp() throws Exception { - dut = new Strobe1500(23,42,"device"); - + dut = new Strobe1500(); + dut.setId(23); + dut.setStartAddress(42); + dut.setDeviceName("device"); } @Test diff --git a/src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml b/src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml new file mode 100644 index 0000000..5816a46 --- /dev/null +++ b/src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + org.hibernate.dialect.HSQLDialect + true + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 03ed17d10df64d643f5e465d5621444eba74aae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sun, 25 Mar 2012 17:58:07 +0200 Subject: [PATCH 10/11] refactoring of the controllers and some cleanup --- .../de/ctdo/bunti/dao/BuntiDevicesDAO.java | 7 ++- .../ctdo/bunti/dao/BuntiDevicesDAOImpl.java | 61 ++++++------------- src/main/java/de/ctdo/bunti/dao/RoomsDAO.java | 7 --- .../java/de/ctdo/bunti/dao/RoomsDAOImpl.java | 7 --- .../java/de/ctdo/bunti/dmx/DMXChannels.java | 2 + .../bunti/dmx/{ => model}/DMXChannel.java | 2 +- .../de/ctdo/bunti/model/BuntiDMXDevice.java | 18 ++++-- .../java/de/ctdo/bunti/model/BuntiDevice.java | 15 ++--- .../java/de/ctdo/bunti/model/Par56Spot.java | 6 +- src/main/java/de/ctdo/bunti/model/Room.java | 54 +++++++--------- .../java/de/ctdo/bunti/model/Strobe1500.java | 6 +- .../bunti/web/DeviceControlController.java | 20 +----- .../de/ctdo/bunti/web/DevicesController.java | 38 ++++++++++++ .../de/ctdo/bunti/web/RoomsController.java | 34 +++++------ .../de/ctdo/bunti/webmodel/DeviceUpdates.java | 8 +-- .../META-INF/spring/hibernate-beans.xml | 16 +++-- src/main/resources/init_data.sql | 8 +++ .../control/BuntiControllerImplTest.java | 40 ++++++++++++ .../bunti/control/BuntiControllerTest.java | 23 ------- .../bunti/dao/BuntiDevicesDAOImplTest.java | 43 +++++++++++++ .../de/ctdo/bunti/dao/RoomsDAOImplTest.java | 3 +- .../de/ctdo/bunti/dmx/DMXChannelTest.java | 1 + .../de/ctdo/bunti/dmx/DMXChannelsTest.java | 1 + .../ctdo/bunti/model/BuntiDMXDeviceTest.java | 2 +- ...context.xml => hibernate-test-context.xml} | 13 ++-- 25 files changed, 244 insertions(+), 191 deletions(-) rename src/main/java/de/ctdo/bunti/dmx/{ => model}/DMXChannel.java (95%) create mode 100644 src/main/java/de/ctdo/bunti/web/DevicesController.java create mode 100644 src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java delete mode 100644 src/test/java/de/ctdo/bunti/control/BuntiControllerTest.java create mode 100644 src/test/java/de/ctdo/bunti/dao/BuntiDevicesDAOImplTest.java rename src/test/resources/de/ctdo/bunti/{dao/RoomsDAOImplTest-context.xml => hibernate-test-context.xml} (90%) diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java index 13bd25c..5c27899 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java @@ -1,14 +1,17 @@ package de.ctdo.bunti.dao; import java.util.Collection; +import java.util.List; import de.ctdo.bunti.model.*; public interface BuntiDevicesDAO { - Collection getAllDevices(); - Collection getAllDMXDevices(); + List getAllDevices(); + List getAllDMXDevices(); BuntiDevice getDeviceById(int deviceId); + void addDevice(BuntiDevice device); + void removeDevice(int deviceId); } diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index dc25b01..0510847 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -1,63 +1,36 @@ package de.ctdo.bunti.dao; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import de.ctdo.bunti.model.*; -import org.springframework.stereotype.Repository; - -@Repository -public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO { - private static final Logger LOGGER = LoggerFactory.getLogger(BuntiDevicesDAOImpl.class); - private List devices = new ArrayList(); - - public BuntiDevicesDAOImpl() { -// addDummyDevices(); - } +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; -// private void addDummyDevices() { -// int deviceID = 0; -// -// devices.add(new Par56Spot(deviceID++, 1, "Par56 Lampe 1")); -// devices.add(new Par56Spot(deviceID++, 6, "Par56 Lampe 2")); -// devices.add(new Par56Spot(deviceID++, 11, "Par56 Lampe 3")); -// devices.add(new Par56Spot(deviceID++, 16, "Par56 Lampe 4")); -// devices.add(new Strobe1500(deviceID++, 21, "Stroboskop 1")); -// devices.add(new Par56Spot(deviceID, 508, "Par56 Lampe 5")); -// LOGGER.debug("added dummy devices in DAO"); -// } - +public final class BuntiDevicesDAOImpl extends HibernateDaoSupport implements BuntiDevicesDAO { @Override - public Collection getAllDMXDevices() { - List list = new ArrayList(); - for (BuntiDevice device : devices) { - if( device instanceof BuntiDMXDevice ) { - list.add((BuntiDMXDevice) device); - } - } - return list; + public List getAllDMXDevices() { + return getHibernateTemplate().loadAll(BuntiDMXDevice.class); } @Override - public Collection getAllDevices() { - return Collections.unmodifiableCollection(devices); + public List getAllDevices() { + return getHibernateTemplate().loadAll(BuntiDevice.class); } @Override public BuntiDevice getDeviceById(int deviceId) { - for (BuntiDevice dev : devices) { - if(dev.getId() == deviceId) { - return dev; - } - } - return null; + return getHibernateTemplate().get(BuntiDevice.class,deviceId); } + @Override + public void addDevice(BuntiDevice device) { + getHibernateTemplate().save(device); + } + + @Override + public void removeDevice(int deviceId) { + getHibernateTemplate().delete(getDeviceById(deviceId)); + } + } diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java index f25fbb2..ecb686d 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAO.java @@ -4,16 +4,9 @@ import de.ctdo.bunti.model.Room; import java.util.List; -/** - * @author: lucas - * @date: 15.03.12 21:55 - */ public interface RoomsDAO { - List getRooms(); Room getRoom(int id); Room addRoom(Room room); void removeRoom(int id); - - } diff --git a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java index 3127851..d82e74b 100644 --- a/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/RoomsDAOImpl.java @@ -1,18 +1,11 @@ package de.ctdo.bunti.dao; -import de.ctdo.bunti.model.Par56Spot; import de.ctdo.bunti.model.Room; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; - import java.util.List; - public final class RoomsDAOImpl extends HibernateDaoSupport implements RoomsDAO { - public RoomsDAOImpl() { - - } - @Override public List getRooms() { return getHibernateTemplate().loadAll(Room.class); diff --git a/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java b/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java index 43374f8..521e4e7 100644 --- a/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java +++ b/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java @@ -1,5 +1,7 @@ package de.ctdo.bunti.dmx; +import de.ctdo.bunti.dmx.model.DMXChannel; + import java.util.Collection; import java.util.Collections; import java.util.HashMap; diff --git a/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java b/src/main/java/de/ctdo/bunti/dmx/model/DMXChannel.java similarity index 95% rename from src/main/java/de/ctdo/bunti/dmx/DMXChannel.java rename to src/main/java/de/ctdo/bunti/dmx/model/DMXChannel.java index 528cc76..9e386af 100644 --- a/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java +++ b/src/main/java/de/ctdo/bunti/dmx/model/DMXChannel.java @@ -1,4 +1,4 @@ -package de.ctdo.bunti.dmx; +package de.ctdo.bunti.dmx.model; public class DMXChannel { private int offset; diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java index cf49d11..0a281fb 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java @@ -1,13 +1,11 @@ package de.ctdo.bunti.model; import de.ctdo.bunti.dmx.DMX; -import de.ctdo.bunti.dmx.DMXChannel; +import de.ctdo.bunti.dmx.model.DMXChannel; import de.ctdo.bunti.dmx.DMXChannels; import org.codehaus.jackson.annotate.JsonIgnore; +import org.hibernate.annotations.Entity; -import javax.persistence.Column; -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; import javax.persistence.Transient; import java.util.HashMap; import java.util.Map; @@ -113,6 +111,18 @@ public abstract class BuntiDMXDevice extends BuntiDevice { return true; } + @Transient + @Override + public final Map getOptions() { + Map options = new HashMap(); + + for(DMXChannel channel: dmxChannels.getAllChannels()) { + options.put(channel.getName(), channel.getValue()); + } + + return options; + } + /** * Add a channel to this DMX Device * used internally by subclasses to define their structure diff --git a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index f9f4504..c275e12 100644 --- a/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -1,7 +1,5 @@ package de.ctdo.bunti.model; -import javax.persistence.*; -import java.io.Serializable; import javax.persistence.*; import java.util.Map; @@ -103,13 +101,8 @@ 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; -// } + @Transient + public abstract Map getOptions(); + } + diff --git a/src/main/java/de/ctdo/bunti/model/Par56Spot.java b/src/main/java/de/ctdo/bunti/model/Par56Spot.java index 92c2d41..b5b7f69 100644 --- a/src/main/java/de/ctdo/bunti/model/Par56Spot.java +++ b/src/main/java/de/ctdo/bunti/model/Par56Spot.java @@ -1,7 +1,8 @@ package de.ctdo.bunti.model; import de.ctdo.bunti.dmx.DMX; -import de.ctdo.bunti.dmx.DMXChannel; +import de.ctdo.bunti.dmx.model.DMXChannel; +import org.codehaus.jackson.annotate.JsonIgnore; import javax.persistence.Entity; import javax.persistence.Transient; @@ -41,16 +42,19 @@ public class Par56Spot extends BuntiDMXDevice { setChannelValueByName(CHANNEL_BLUE, value); } + @JsonIgnore @Transient public final int getRed() { return getChannelValueByName(CHANNEL_RED); } + @JsonIgnore @Transient public final int getGreen() { return getChannelValueByName(CHANNEL_GREEN); } + @JsonIgnore @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 7512111..ebb2b06 100644 --- a/src/main/java/de/ctdo/bunti/model/Room.java +++ b/src/main/java/de/ctdo/bunti/model/Room.java @@ -1,9 +1,7 @@ package de.ctdo.bunti.model; import javax.persistence.*; -import java.io.Serializable; -import java.util.HashSet; -import java.util.Set; +import java.util.*; @Entity @@ -15,7 +13,7 @@ public final class Room { private String floor; private int xCord; private int yCord; - private Set devices = new HashSet(); + private List devices = new ArrayList(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -67,38 +65,32 @@ public final class Room { @JoinTable(name = "ROOM_BUNTIDEVICE", joinColumns = { @JoinColumn(name = "ROOM_ID") }, inverseJoinColumns = { @JoinColumn(name = "BUNTIDEVICE_ID") }) - public Set getDevices() { - return this.devices; + public List getDevices() { + return Collections.unmodifiableList(this.devices); } - public void setDevices(Set 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()); -// } + @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; + } } diff --git a/src/main/java/de/ctdo/bunti/model/Strobe1500.java b/src/main/java/de/ctdo/bunti/model/Strobe1500.java index cdd647f..4cbb134 100644 --- a/src/main/java/de/ctdo/bunti/model/Strobe1500.java +++ b/src/main/java/de/ctdo/bunti/model/Strobe1500.java @@ -1,7 +1,8 @@ package de.ctdo.bunti.model; import de.ctdo.bunti.dmx.DMX; -import de.ctdo.bunti.dmx.DMXChannel; +import de.ctdo.bunti.dmx.model.DMXChannel; +import org.codehaus.jackson.annotate.JsonIgnore; import javax.persistence.Entity; import javax.persistence.Transient; @@ -36,16 +37,19 @@ public class Strobe1500 extends BuntiDMXDevice { return setChannelValueByName(CHANNEL_MODE, value); } + @JsonIgnore @Transient public final int getSpeed() { return getChannelValueByName(CHANNEL_SPEED); } + @JsonIgnore @Transient public final int getIntensity() { return getChannelValueByName(CHANNEL_INTENSITY); } + @JsonIgnore @Transient public final int getMode() { return getChannelValueByName(CHANNEL_MODE); diff --git a/src/main/java/de/ctdo/bunti/web/DeviceControlController.java b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java index 9df036d..6428089 100644 --- a/src/main/java/de/ctdo/bunti/web/DeviceControlController.java +++ b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java @@ -1,7 +1,6 @@ package de.ctdo.bunti.web; import de.ctdo.bunti.control.BuntiController; -import de.ctdo.bunti.model.BuntiDevice; import de.ctdo.bunti.webmodel.DeviceUpdate; import de.ctdo.bunti.webmodel.DeviceUpdates; import org.slf4j.Logger; @@ -10,8 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.util.*; - @Controller @RequestMapping(value = "/control") public class DeviceControlController { @@ -23,29 +20,14 @@ public class DeviceControlController { this.controller = controller; } - @RequestMapping(value = "/devices", method = RequestMethod.GET) - public @ResponseBody Collection getAll() { - LOGGER.info("handle GET /devices request"); - return controller.getAllDevices(); - } - - @RequestMapping(value = "/devices/{id}", method = RequestMethod.GET) - public @ResponseBody BuntiDevice getDeviceById(@PathVariable("id") int id) { - LOGGER.info("handle GET /devices/" + id + " request"); - return controller.getDeviceById(id); - } - @RequestMapping(value = "/devices", method = RequestMethod.POST) - public String setDevices(@RequestBody DeviceUpdates updates) { + public void setDevices(@RequestBody DeviceUpdates updates) { LOGGER.info("handle PUT /devices" + " request update=" + updates.toString() ); for (DeviceUpdate update: updates.getUpdates()) { LOGGER.info("Update deviceId=" + update.getDeviceId()); - controller.updateDeviceData(update.getDeviceId(), update.getOptions()); } - - return "bla"; } } diff --git a/src/main/java/de/ctdo/bunti/web/DevicesController.java b/src/main/java/de/ctdo/bunti/web/DevicesController.java new file mode 100644 index 0000000..5e7dfa2 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/web/DevicesController.java @@ -0,0 +1,38 @@ +package de.ctdo.bunti.web; + +import de.ctdo.bunti.dao.BuntiDevicesDAO; +import de.ctdo.bunti.model.BuntiDevice; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.Collection; + +@Controller +@RequestMapping(value = "/devices") +public class DevicesController { + private BuntiDevicesDAO devicesDAO; + + @Autowired + public void setDevicesDAO(BuntiDevicesDAO devicesDAO) { + this.devicesDAO = devicesDAO; + } + + @RequestMapping(value = "", method = RequestMethod.GET) + public @ResponseBody Collection getAll() { + return devicesDAO.getAllDevices(); + } + + @RequestMapping(value = "/{id}", method = RequestMethod.GET) + public @ResponseBody BuntiDevice getDeviceById(@PathVariable("id") int id) { + return devicesDAO.getDeviceById(id); + } + + @RequestMapping(value = "", method = RequestMethod.POST) + public @ResponseBody BuntiDevice setDevices(@RequestBody BuntiDevice device) { + devicesDAO.addDevice(device); + return device; + } + + +} diff --git a/src/main/java/de/ctdo/bunti/web/RoomsController.java b/src/main/java/de/ctdo/bunti/web/RoomsController.java index 180172a..dcd4c00 100644 --- a/src/main/java/de/ctdo/bunti/web/RoomsController.java +++ b/src/main/java/de/ctdo/bunti/web/RoomsController.java @@ -1,47 +1,45 @@ package de.ctdo.bunti.web; -import de.ctdo.bunti.control.BuntiController; +import de.ctdo.bunti.dao.RoomsDAO; +import de.ctdo.bunti.model.BuntiDevice; import de.ctdo.bunti.model.Room; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.Collection; +import java.util.List; @Controller @RequestMapping(value = "/rooms") public class RoomsController { - private static final Logger LOGGER = LoggerFactory.getLogger(RoomsController.class); - private BuntiController controller; - + private RoomsDAO roomsDAO; @Autowired - public final void setController(BuntiController controller) { - this.controller = controller; + public void setRoomsDAO(RoomsDAO roomsDAO) { + this.roomsDAO = roomsDAO; } @RequestMapping(value = "", method = RequestMethod.GET) public @ResponseBody Collection getAll() { - LOGGER.info("handle GET /rooms" + " request"); - return controller.getAllRooms(); + return roomsDAO.getRooms(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public @ResponseBody Room getRoomById(@PathVariable("id") int id) { - LOGGER.info("handle GET /rooms/" + id + " request"); - return controller.getRoomById(id); + return roomsDAO.getRoom(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"); + public @ResponseBody + List getDevicesFromRoom(@PathVariable("id") int id) { + Room room = roomsDAO.getRoom(id); + + if(room != null) { + return room.getDevices(); + } - Room r = controller.getRoomById(id); - - - return controller.getRoomById(id); + return null; } } diff --git a/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java index 55a4b84..11635a6 100644 --- a/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java +++ b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java @@ -1,16 +1,16 @@ package de.ctdo.bunti.webmodel; import java.util.ArrayList; -import java.util.Collection; import java.util.List; +/** + * Diese Klasse gibts nur, weil ich es nicht hinbekomme mit Jackson + * ein ArrayList als Parameter im Controller zu verarbeiten. + */ public class DeviceUpdates { - private long timeStamp = 0; - private List updates = new ArrayList(); - public List getUpdates() { return updates; } diff --git a/src/main/resources/META-INF/spring/hibernate-beans.xml b/src/main/resources/META-INF/spring/hibernate-beans.xml index dd19922..ccc9010 100755 --- a/src/main/resources/META-INF/spring/hibernate-beans.xml +++ b/src/main/resources/META-INF/spring/hibernate-beans.xml @@ -1,19 +1,13 @@ - - - - - - - - + + @@ -22,7 +16,7 @@ org.hibernate.dialect.HSQLDialect true - create + @@ -32,6 +26,10 @@ + + + + diff --git a/src/main/resources/init_data.sql b/src/main/resources/init_data.sql index 3e189f7..0b1197e 100755 --- a/src/main/resources/init_data.sql +++ b/src/main/resources/init_data.sql @@ -6,6 +6,14 @@ insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Eta insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Flur', '1', '1'); +insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress) + values ('Par56Spot',null,'Lampe1',null, 1); +insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress) + values ('Par56Spot',null,'Lampe2',null, 6); +insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress) + values ('Par56Spot',null,'Lampe3',null, 11); +insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress) + values ('Par56Spot',null,'Lampe4',null, 16); diff --git a/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java b/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java new file mode 100644 index 0000000..a474047 --- /dev/null +++ b/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java @@ -0,0 +1,40 @@ +package de.ctdo.bunti.control; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +//@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml","classpath:/de/ctdo/bunti/hibernate-test-context.xml" }) +//@RunWith(SpringJUnit4ClassRunner.class) +//public class BuntiControllerImplTest { + +// @Autowired +// BuntiController dut; +// +// @Test +// public void testUpdateDeviceData() throws Exception { +// +// } +// +// @Test +// public void testGetAllRooms() throws Exception { +// +// } +// +// @Test +// public void testGetRoomById() throws Exception { +// +// } +// +// @Test +// public void testGetAllDevices() throws Exception { +// +// } +// +// @Test +// public void testGetDeviceById() throws Exception { +// +// } +//} diff --git a/src/test/java/de/ctdo/bunti/control/BuntiControllerTest.java b/src/test/java/de/ctdo/bunti/control/BuntiControllerTest.java deleted file mode 100644 index 46d34ce..0000000 --- a/src/test/java/de/ctdo/bunti/control/BuntiControllerTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.ctdo.bunti.control; - -import org.junit.Before; - -public class BuntiControllerTest { - - BuntiControllerImpl dut; - - @Before - public void setUp() throws Exception { - dut = new BuntiControllerImpl(); -// dut.setDevicesDAO(daoMock); - } - -// @Test -// public void testSetDevice() throws Exception { -// Map options = new HashMap(); -// options.put("optionA", "valueA"); -// options.put("optionB", 42); -// assertTrue(dut.updateDeviceData(12, options)); -// } - -} diff --git a/src/test/java/de/ctdo/bunti/dao/BuntiDevicesDAOImplTest.java b/src/test/java/de/ctdo/bunti/dao/BuntiDevicesDAOImplTest.java new file mode 100644 index 0000000..f926a49 --- /dev/null +++ b/src/test/java/de/ctdo/bunti/dao/BuntiDevicesDAOImplTest.java @@ -0,0 +1,43 @@ +package de.ctdo.bunti.dao; + +import de.ctdo.bunti.model.BuntiDMXDevice; +import de.ctdo.bunti.model.BuntiDevice; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Collection; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +@ContextConfiguration(locations = { "classpath:/de/ctdo/bunti/hibernate-test-context.xml" }) +@RunWith(SpringJUnit4ClassRunner.class) +public class BuntiDevicesDAOImplTest { + + @Autowired + BuntiDevicesDAO dut; + + @Test + public void testGetAllDMXDevices() throws Exception { + List deviceList = dut.getAllDMXDevices(); + assertEquals(4, deviceList.size()); + } + + @Test + public void testGetAllDevices() throws Exception { + List deviceList = dut.getAllDevices(); + assertEquals(4, deviceList.size()); + } + + @Test + public void testGetDeviceById() throws Exception { + BuntiDevice device = dut.getDeviceById(2); + + assertEquals("Lampe2", device.getDeviceName()); + + } +} diff --git a/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java b/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java index b12d053..881057c 100644 --- a/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java +++ b/src/test/java/de/ctdo/bunti/dao/RoomsDAOImplTest.java @@ -1,6 +1,5 @@ package de.ctdo.bunti.dao; -import de.ctdo.bunti.model.Room; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -10,7 +9,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNull; -@ContextConfiguration() +@ContextConfiguration(locations = { "classpath:/de/ctdo/bunti/hibernate-test-context.xml" }) @RunWith(SpringJUnit4ClassRunner.class) public class RoomsDAOImplTest { diff --git a/src/test/java/de/ctdo/bunti/dmx/DMXChannelTest.java b/src/test/java/de/ctdo/bunti/dmx/DMXChannelTest.java index df71b7f..6bb8375 100644 --- a/src/test/java/de/ctdo/bunti/dmx/DMXChannelTest.java +++ b/src/test/java/de/ctdo/bunti/dmx/DMXChannelTest.java @@ -1,5 +1,6 @@ package de.ctdo.bunti.dmx; +import de.ctdo.bunti.dmx.model.DMXChannel; import org.junit.Before; import org.junit.Test; diff --git a/src/test/java/de/ctdo/bunti/dmx/DMXChannelsTest.java b/src/test/java/de/ctdo/bunti/dmx/DMXChannelsTest.java index cbc6922..d24c8b8 100644 --- a/src/test/java/de/ctdo/bunti/dmx/DMXChannelsTest.java +++ b/src/test/java/de/ctdo/bunti/dmx/DMXChannelsTest.java @@ -1,5 +1,6 @@ package de.ctdo.bunti.dmx; +import de.ctdo.bunti.dmx.model.DMXChannel; import org.junit.Before; import org.junit.Test; diff --git a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java index 0589455..8e69918 100644 --- a/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java +++ b/src/test/java/de/ctdo/bunti/model/BuntiDMXDeviceTest.java @@ -1,6 +1,6 @@ package de.ctdo.bunti.model; -import de.ctdo.bunti.dmx.DMXChannel; +import de.ctdo.bunti.dmx.model.DMXChannel; import org.junit.Before; import org.junit.Test; diff --git a/src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml b/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml similarity index 90% rename from src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml rename to src/test/resources/de/ctdo/bunti/hibernate-test-context.xml index 5816a46..3def185 100644 --- a/src/test/resources/de/ctdo/bunti/dao/RoomsDAOImplTest-context.xml +++ b/src/test/resources/de/ctdo/bunti/hibernate-test-context.xml @@ -26,13 +26,14 @@ + + + + + + + - - - - - - \ No newline at end of file From 8248804ac77c1da6923e27018c8a7d726b7b2306 Mon Sep 17 00:00:00 2001 From: Hendrik Fellerhoff Date: Thu, 10 May 2012 17:03:33 +0200 Subject: [PATCH 11/11] =?UTF-8?q?diverse=20fortschritte,=20page=20f=C3=BCr?= =?UTF-8?q?=20crashtest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/webapp/WEB-INF/jsp/index.jsp | 65 ++++++--------- src/main/webapp/WEB-INF/jsp/index2.jsp | 77 ++++++++++++++++++ src/main/webapp/resources/js/events.class.js | 51 ++++++++++++ src/main/webapp/resources/js/main.js | 22 ++++- src/main/webapp/resources/js/models.js | 4 + src/main/webapp/resources/js/views.js | 42 +++++++++- src/main/webapp/resources/json/room0.json | 11 +++ src/main/webapp/resources/json/room1.json | 85 ++++++++++++++++++++ 8 files changed, 312 insertions(+), 45 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jsp/index2.jsp create mode 100644 src/main/webapp/resources/js/events.class.js create mode 100644 src/main/webapp/resources/json/room0.json create mode 100644 src/main/webapp/resources/json/room1.json diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp index 3e109d2..18a6402 100644 --- a/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -12,6 +12,7 @@ " rel="stylesheet" /> + @@ -26,45 +27,8 @@

CTDO Raumsteuerung

- -
- -
-
-
-
- -
-
- -
-
-
-
-
-
- - - - -
- -
-
- -
- Raumlicht:
- An Aus -
+
+
@@ -80,6 +44,20 @@
{{/each}} + + diff --git a/src/main/webapp/WEB-INF/jsp/index2.jsp b/src/main/webapp/WEB-INF/jsp/index2.jsp new file mode 100644 index 0000000..9740e38 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/index2.jsp @@ -0,0 +1,77 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + +<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + WebSockets + + " rel="stylesheet"/> + " rel="stylesheet"/> + + + + + + +

CTDO Raumsteuerung

+ +

Par56:
+ + + +

+

Lampel:
+ + + + + + +

+ + + diff --git a/src/main/webapp/resources/js/events.class.js b/src/main/webapp/resources/js/events.class.js new file mode 100644 index 0000000..6801ccf --- /dev/null +++ b/src/main/webapp/resources/js/events.class.js @@ -0,0 +1,51 @@ +//Copyright (c) 2010 Nicholas C. Zakas. All rights reserved. +//MIT License + +function EventTarget(){ + this._listeners = {}; +} + +EventTarget.prototype = { + + constructor: EventTarget, + + addListener: function(type, listener){ + if (typeof this._listeners[type] == "undefined"){ + this._listeners[type] = []; + } + + this._listeners[type].push(listener); + }, + + fire: function(event){ + if (typeof event == "string"){ + event = { type: event }; + } + if (!event.target){ + event.target = this; + } + + if (!event.type){ //falsy + throw new Error("Event object missing 'type' property."); + } + + if (this._listeners[event.type] instanceof Array){ + var listeners = this._listeners[event.type]; + for (var i=0, len=listeners.length; i < len; i++){ + listeners[i].call(this, event); + } + } + }, + + removeListener: function(type, listener){ + if (this._listeners[type] instanceof Array){ + var listeners = this._listeners[type]; + for (var i=0, len=listeners.length; i < len; i++){ + if (listeners[i] === listener){ + listeners.splice(i, 1); + break; + } + } + } + } +}; \ No newline at end of file diff --git a/src/main/webapp/resources/js/main.js b/src/main/webapp/resources/js/main.js index 9fb3d14..2e099a1 100644 --- a/src/main/webapp/resources/js/main.js +++ b/src/main/webapp/resources/js/main.js @@ -5,7 +5,9 @@ * Time: 00:51 * Main Javascript file */ - +var events = new EventTarget(); +var roomDevices = []; +var roomDeviceViews = []; Workspace = Backbone.Router.extend({ routes:{ "":"home", @@ -13,11 +15,27 @@ Workspace = Backbone.Router.extend({ }, home: function() { + function roomsLoadedHandler() { + rooms.each(function(room) { + var id = room.get('roomId'); + roomDevices[id] = new RoomDevices(); + roomDevices[id].url = '/resources/json/room' + id + '.json'; + roomDevices[id].fetch(); + }); + events.removeListener("roomsLoaded", roomsLoadedHandler); + } + events.addListener("roomsLoaded", roomsLoadedHandler); rooms.fetch(); + }, show_room: function(id) { this.home(); - window.roomTabsId = id; + function roomsLoadedHandlerTabSelect() { + $('#room-tabs').tabs('select', id); + events.removeListener("roomsLoaded", roomsLoadedHandlerTabSelect) + } + events.addListener("roomsLoaded", roomsLoadedHandlerTabSelect); + } }); $(document).ready(function() { diff --git a/src/main/webapp/resources/js/models.js b/src/main/webapp/resources/js/models.js index d29a530..9ea62da 100644 --- a/src/main/webapp/resources/js/models.js +++ b/src/main/webapp/resources/js/models.js @@ -4,4 +4,8 @@ model: Room, url: '/resources/json/rooms.json' }); + window.Device = Backbone.Model.extend({}); + window.RoomDevices = Backbone.Collection.extend({ + model: Device + }); })(jQuery); \ No newline at end of file diff --git a/src/main/webapp/resources/js/views.js b/src/main/webapp/resources/js/views.js index ce01f0b..ffe1eaa 100644 --- a/src/main/webapp/resources/js/views.js +++ b/src/main/webapp/resources/js/views.js @@ -3,12 +3,46 @@ window.RoomsView = Backbone.View.extend({ this.collection.bind('reset', this.render, this); }, render: function() { + $('#room-tabs').tabs('destroy'); this.template = Handlebars.compile($("#room-tabs-template").html()); var rooms = {'rooms': this.collection.toJSON()}; - $('#room-tabs').html(this.template(rooms)).tabs(); - if(typeof(window.roomTabsId) != "undefined") { - $('#room-tabs').tabs('select', window.roomTabsId); - } + $('#room-tabs').html(this.template(rooms)).tabs({ + select: function(event, ui) { + //set hashtag? + } + }); + events.fire("roomsLoaded"); + + return this; + } +}); +window.LampelView = Backbone.View.extend({ + initialize: function() { + this.collection.bind('reset', this.render, this); + }, + render: function() { + events.fire("lampelLoaded"); + + return this; + } +}); +window.BuzzerView = Backbone.View.extend({ + initialize: function() { + this.collection.bind('reset', this.render, this); + }, + render: function() { + events.fire("buzzerLoaded"); + + return this; + } +}); +window.Par56View = Backbone.View.extend({ + initialize: function() { + this.collection.bind('reset', this.render, this); + }, + render: function() { + events.fire("par56Loaded"); + return this; } }); diff --git a/src/main/webapp/resources/json/room0.json b/src/main/webapp/resources/json/room0.json new file mode 100644 index 0000000..b9911b0 --- /dev/null +++ b/src/main/webapp/resources/json/room0.json @@ -0,0 +1,11 @@ +[ + { + "type":"Buzzer", + "deviceId":0, + "deviceName":"Buzzer", + "picture":null, + "options": { + "state":0 + } + } +] \ No newline at end of file diff --git a/src/main/webapp/resources/json/room1.json b/src/main/webapp/resources/json/room1.json new file mode 100644 index 0000000..7f52483 --- /dev/null +++ b/src/main/webapp/resources/json/room1.json @@ -0,0 +1,85 @@ +[ + { + "startAddress":1, + "type":"Par56Spot", + "deviceId":0, + "deviceName":"Par56 Lampe 1", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, + { + "startAddress":6, + "type":"Par56Spot", + "deviceId":1, + "deviceName":"Par56 Lampe 2", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, + { + "startAddress":11, + "type":"Par56Spot", + "deviceId":2, + "deviceName":"Par56 Lampe 3", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, + { + "startAddress":16, + "type":"Par56Spot", + "deviceId":3, + "deviceName":"Par56 Lampe 5", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, + { + "startAddress":21, + "type":"Strobe1500", + "deviceId":4, + "deviceName":"Stroboskop 1", + "picture":null, + "options": { + "mode":0, + "speed":0, + "intensity":0 + } + }, + { + "startAddress":508, + "type":"Par56Spot", + "deviceId":5, + "deviceName":"Par56 Lampe 5", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, + { + "type":"Lampel", + "deviceId":6, + "deviceName":"Lampel", + "picture":null, + "options": { + "red":0, + "green":0, + "blue":0 + } + }, +] \ No newline at end of file