refactoring of the controllers and some cleanup

This commit is contained in:
Lucas Pleß 2012-03-25 17:58:07 +02:00
parent 0cb8e585af
commit 03ed17d10d
25 changed files with 244 additions and 191 deletions

View File

@ -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<BuntiDevice> getAllDevices();
Collection<BuntiDMXDevice> getAllDMXDevices();
List<BuntiDevice> getAllDevices();
List<BuntiDMXDevice> getAllDMXDevices();
BuntiDevice getDeviceById(int deviceId);
void addDevice(BuntiDevice device);
void removeDevice(int deviceId);
}

View File

@ -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<BuntiDevice> devices = new ArrayList<BuntiDevice>();
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<BuntiDMXDevice> getAllDMXDevices() {
List<BuntiDMXDevice> list = new ArrayList<BuntiDMXDevice>();
for (BuntiDevice device : devices) {
if( device instanceof BuntiDMXDevice ) {
list.add((BuntiDMXDevice) device);
}
}
return list;
public List<BuntiDMXDevice> getAllDMXDevices() {
return getHibernateTemplate().loadAll(BuntiDMXDevice.class);
}
@Override
public Collection<BuntiDevice> getAllDevices() {
return Collections.unmodifiableCollection(devices);
public List<BuntiDevice> getAllDevices() {
return getHibernateTemplate().loadAll(BuntiDevice.class);
}
@Override
public BuntiDevice getDeviceById(int deviceId) {
for (BuntiDevice dev : devices) {
if(dev.getId() == deviceId) {
return dev;
return getHibernateTemplate().get(BuntiDevice.class,deviceId);
}
@Override
public void addDevice(BuntiDevice device) {
getHibernateTemplate().save(device);
}
return null;
@Override
public void removeDevice(int deviceId) {
getHibernateTemplate().delete(getDeviceById(deviceId));
}
}

View File

@ -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<Room> getRooms();
Room getRoom(int id);
Room addRoom(Room room);
void removeRoom(int id);
}

View File

@ -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<Room> getRooms() {
return getHibernateTemplate().loadAll(Room.class);

View File

@ -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;

View File

@ -1,4 +1,4 @@
package de.ctdo.bunti.dmx;
package de.ctdo.bunti.dmx.model;
public class DMXChannel {
private int offset;

View File

@ -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<String, Object> getOptions() {
Map<String, Object> options = new HashMap<String, Object>();
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

View File

@ -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<String, Object> options);
// @ManyToOne
// @JoinColumn(name="ROOM_ID")
// public Room getRoom() {
// return room;
// }
//
// public void setRoom(Room room) {
// this.room = room;
// }
@Transient
public abstract Map<String, Object> getOptions();
}

View File

@ -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);

View File

@ -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<BuntiDevice> devices = new HashSet<BuntiDevice>();
private List<BuntiDevice> devices = new ArrayList<BuntiDevice>();
@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<BuntiDevice> getDevices() {
return this.devices;
public List<BuntiDevice> getDevices() {
return Collections.unmodifiableList(this.devices);
}
public void setDevices(Set<BuntiDevice> devices) {
public void setDevices(List<BuntiDevice> devices) {
this.devices = devices;
}
// @Transient
// public boolean addDevice(BuntiDevice device) {
// return getDevices().add(device);
// }
//
// @Transient
// public boolean removeDevice(BuntiDevice device) {
// return getDevices().remove(device);
// }
//
// @Transient
// public BuntiDevice getDevice(int id) {
// for (BuntiDevice device: getDevices()) {
// if( device.getId() == id) {
// return device;
// }
// }
// return null;
// }
//
// @Transient
// public List<BuntiDevice> getDeviceList() {
// return Collections.unmodifiableList(getDevices());
// }
@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;
}
}

View File

@ -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);

View File

@ -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<BuntiDevice> 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";
}
}

View File

@ -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<BuntiDevice> 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;
}
}

View File

@ -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<Room> 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<BuntiDevice> getDevicesFromRoom(@PathVariable("id") int id) {
Room room = roomsDAO.getRoom(id);
Room r = controller.getRoomById(id);
if(room != null) {
return room.getDevices();
}
return controller.getRoomById(id);
return null;
}
}

View File

@ -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<DeviceUpdate> als Parameter im Controller zu verarbeiten.
*/
public class DeviceUpdates {
private long timeStamp = 0;
private List<DeviceUpdate> updates = new ArrayList<DeviceUpdate>();
public List<DeviceUpdate> getUpdates() {
return updates;
}

View File

@ -1,19 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--<property name="location">-->
<!--<value>db.properties</value>-->
<!--</property>-->
<!--</bean>-->
<jdbc:embedded-database id="dataSource" type="H2">
<!--<jdbc:script location="classpath:init_data.sql" />-->
<jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
@ -22,7 +16,7 @@
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource" />
@ -32,6 +26,10 @@
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

View File

@ -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);

View File

@ -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 {
//
// }
//}

View File

@ -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<String,Object> options = new HashMap<String, Object>();
// options.put("optionA", "valueA");
// options.put("optionB", 42);
// assertTrue(dut.updateDeviceData(12, options));
// }
}

View File

@ -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<BuntiDMXDevice> deviceList = dut.getAllDMXDevices();
assertEquals(4, deviceList.size());
}
@Test
public void testGetAllDevices() throws Exception {
List<BuntiDevice> deviceList = dut.getAllDevices();
assertEquals(4, deviceList.size());
}
@Test
public void testGetDeviceById() throws Exception {
BuntiDevice device = dut.getDeviceById(2);
assertEquals("Lampe2", device.getDeviceName());
}
}

View File

@ -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 {

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.dmx.model.DMXChannel;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.dmx.model.DMXChannel;
import org.junit.Before;
import org.junit.Test;

View File

@ -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;

View File

@ -26,13 +26,14 @@
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
</beans>