diff --git a/src/main/java/org/olat/core/dispatcher/mapper/manager/MapperDAO.java b/src/main/java/org/olat/core/dispatcher/mapper/manager/MapperDAO.java index fd0433f5b26392970e4b947811409e94ee87be1b..160f96d8e14ea524044d3ad4bb6fb01ffbaf6fc6 100644 --- a/src/main/java/org/olat/core/dispatcher/mapper/manager/MapperDAO.java +++ b/src/main/java/org/olat/core/dispatcher/mapper/manager/MapperDAO.java @@ -44,6 +44,15 @@ public class MapperDAO { @Autowired private DB dbInstance; + /** + * Persist a mapper on the database. + * + * @param sessionId The HTTP session id + * @param mapperId The unique id of the mapper + * @param mapper The mapper (serializable) + * @param expirationTime The expiration time in seconds + * @return + */ public PersistedMapper persistMapper(String sessionId, String mapperId, Serializable mapper, int expirationTime) { PersistedMapper m = new PersistedMapper(); m.setMapperId(mapperId); @@ -64,33 +73,33 @@ public class MapperDAO { return m; } + /** + * Update a persisted mapper. + * + * @param mapperId The mapper unique id (uuid) + * @param mapper The mapper itself (serializable) + * @param expirationTime The expiration time in seconds + * @return + */ public boolean updateConfiguration(String mapperId, Serializable mapper, int expirationTime) { - PersistedMapper m = loadForUpdate(mapperId); - if(m != null) { - String configuration = XStreamHelper.createXStreamInstance().toXML(mapper); - m.setXmlConfiguration(configuration); - Date currentDate = new Date(); - m.setLastModified(currentDate); - if(expirationTime > 0) { - Calendar cal = Calendar.getInstance(); - cal.setTime(currentDate); - cal.add(Calendar.SECOND, expirationTime); - m.setExpirationDate(cal.getTime()); - } - dbInstance.getCurrentEntityManager().merge(m); + String configuration = XStreamHelper.createXStreamInstance().toXML(mapper); + Date currentDate = new Date(); + Date expirationDate = null; + if(expirationTime > 0) { + Calendar cal = Calendar.getInstance(); + cal.setTime(currentDate); + cal.add(Calendar.SECOND, expirationTime); + expirationDate = cal.getTime(); } + int row = dbInstance.getCurrentEntityManager().createNamedQuery("updateMapperByMapperId") + .setParameter("now", currentDate) + .setParameter("expirationDate", expirationDate) + .setParameter("config", configuration) + .setParameter("mapperId", mapperId) + .executeUpdate(); + dbInstance.commit(); - return true; - } - - private PersistedMapper loadForUpdate(String mapperId) { - List<PersistedMapper> mappers = dbInstance.getCurrentEntityManager() - .createNamedQuery("loadMapperByKeyOrdered", PersistedMapper.class) - .setParameter("mapperId", mapperId) - .setFirstResult(0) - .setMaxResults(1) - .getResultList(); - return mappers.isEmpty() ? null : mappers.get(0); + return row > 0; } public PersistedMapper loadByMapperId(String mapperId) { diff --git a/src/main/java/org/olat/core/dispatcher/mapper/model/PersistedMapper.java b/src/main/java/org/olat/core/dispatcher/mapper/model/PersistedMapper.java index 851e79b49baaf4c55d33b46975920c9d6d06f416..1599305df5f4d4a627c6ba0d66b2962333bcccb8 100644 --- a/src/main/java/org/olat/core/dispatcher/mapper/model/PersistedMapper.java +++ b/src/main/java/org/olat/core/dispatcher/mapper/model/PersistedMapper.java @@ -45,7 +45,8 @@ import org.olat.core.id.Persistable; @Table(name="o_mapper") @NamedQueries({ @NamedQuery(name="loadMapperByKeyOrdered", query="select mapper from pmapper as mapper where mapper.mapperId=:mapperId order by mapper.key"), - @NamedQuery(name="loadMapperByKey", query="select mapper from pmapper as mapper where mapper.mapperId=:mapperId") + @NamedQuery(name="loadMapperByKey", query="select mapper from pmapper as mapper where mapper.mapperId=:mapperId"), + @NamedQuery(name="updateMapperByMapperId", query="update pmapper set lastModified=:now, expirationDate=:expirationDate, xmlConfiguration=:config where mapperId=:mapperId") }) public class PersistedMapper implements CreateInfo, ModifiedInfo, Persistable { diff --git a/src/test/java/org/olat/core/dispatcher/mapper/MapperDAOTest.java b/src/test/java/org/olat/core/dispatcher/mapper/MapperDAOTest.java index c2aa60d8eed8c707d80238b9b7361d993c54f04d..675ad74d65ce62d5730ad5f7daefa0b7ea4e11c4 100644 --- a/src/test/java/org/olat/core/dispatcher/mapper/MapperDAOTest.java +++ b/src/test/java/org/olat/core/dispatcher/mapper/MapperDAOTest.java @@ -131,6 +131,41 @@ public class MapperDAOTest extends OlatTestCase { Assert.assertEquals("mapper-to-update", sMapperReloaded2.getKey()); } + @Test + public void testUpdateMapper_serializade_withExpirationDate() { + //create a mapper + String mapperId = UUID.randomUUID().toString(); + String sessionId = UUID.randomUUID().toString().substring(0, 32); + PersistentMapper sMapper = new PersistentMapper("mapper-to-persist-until"); + PersistedMapper pMapper = mapperDao.persistMapper(sessionId, mapperId, sMapper, 60000); + Assert.assertNotNull(pMapper); + dbInstance.commitAndCloseSession(); + + //load the mapper + PersistedMapper loadedMapper = mapperDao.loadByMapperId(mapperId); + Assert.assertNotNull(loadedMapper); + Object objReloaded = XStreamHelper.createXStreamInstance().fromXML(pMapper.getXmlConfiguration()); + Assert.assertTrue(objReloaded instanceof PersistentMapper); + PersistentMapper sMapperReloaded = (PersistentMapper)objReloaded; + Assert.assertEquals("mapper-to-persist-until", sMapperReloaded.getKey()); + Assert.assertNotNull(loadedMapper.getExpirationDate()); + + //update + PersistentMapper sMapper2 = new PersistentMapper("mapper-to-update-until"); + boolean updated = mapperDao.updateConfiguration(mapperId, sMapper2, 120000); + Assert.assertTrue(updated); + dbInstance.commitAndCloseSession(); + + //load the updated mapper + PersistedMapper loadedMapper2 = mapperDao.loadByMapperId(mapperId); + Assert.assertNotNull(loadedMapper2); + Object objReloaded2 = XStreamHelper.createXStreamInstance().fromXML(loadedMapper2.getXmlConfiguration()); + Assert.assertTrue(objReloaded2 instanceof PersistentMapper); + PersistentMapper sMapperReloaded2 = (PersistentMapper)objReloaded2; + Assert.assertEquals("mapper-to-update-until", sMapperReloaded2.getKey()); + Assert.assertNotNull(loadedMapper2.getExpirationDate()); + } + @Test public void testDeleteMapperByMapper() throws Exception { //create mappers