ScriptDao extends BaseDao<Script> { @Override public Script saveOrUpdate(Script script) { MethodTimer mt = new MethodTimer(LOG, getClass(), "saveOrUpdate").start(); int size = script.getScriptSteps().size(); ScriptUtil.setScriptStepLabels(script); LOG.info("persisting script " + script.getName() + " with id " + script.getId() + " into database"); EntityManager em = getEntityManager(); try { begin(); SerializedScriptStep serializedScriptStep = serialize(script.getScriptSteps()); serializedScriptStep.setSerialzedData( Hibernate.getLobCreator(getHibernateSession()).createBlob(serializedScriptStep.getBytes())); SerializedScriptStep serializedSteps = new SerializedScriptStepDao().saveOrUpdate(serializedScriptStep); script.setSerializedScriptStepId(serializedScriptStep.getId()); if (script.getId() == 0) { em.persist(script); } else { script = em.merge(script); } LOG.debug("Saved Script Steps with id " + serializedSteps.getId() + " for script " + script.getId()); commit(); } catch (Exception e) { rollback(); e.printStackTrace(); throw new RuntimeException(e); } finally { cleanup(); } mt.markAndLog("Store script with " + size + " steps to database."); mt.endAndLog(); return script; } ScriptDao(); @Override Script findById(Integer id); Script getScript(Integer id); @Override void delete(Integer id); List<Script> getScriptsForProductId(int productId); Script loadScriptSteps(@Nonnull Script script); @Override Script saveOrUpdate(Script script); SerializedScriptStep serialize(List<ScriptStep> steps);  }
@Test @Tag(TestGroups.FUNCTIONAL) public void testFilter() throws Exception { Calendar calendar = Calendar.getInstance(); Script first = DaoTestUtil.createScript(); first = dao.saveOrUpdate(first); Script third = DaoTestUtil.createScript(); calendar.add(Calendar.DAY_OF_YEAR, -10); third.setForceCreateDate(calendar.getTime()); third = dao.saveOrUpdate(third); Script second = DaoTestUtil.createScript(); calendar.add(Calendar.DAY_OF_YEAR, 7); second.setForceCreateDate(calendar.getTime()); second = dao.saveOrUpdate(second); Script fourth = DaoTestUtil.createScript(); calendar.add(Calendar.MONTH, -1); fourth.setForceCreateDate(calendar.getTime()); fourth = dao.saveOrUpdate(fourth); List<Script> list = dao.findAll(); assertEquals(4, list.size()); assertEquals(first.getId(), list.get(0).getId()); assertEquals(second.getId(), list.get(2).getId()); assertEquals(third.getId(), list.get(1).getId()); assertEquals(fourth.getId(), list.get(3).getId()); list = dao.findFiltered(ViewFilterType.LAST_TWO_WEEKS); assertEquals(3, list.size()); assertEquals(first.getId(), list.get(0).getId()); assertEquals(second.getId(), list.get(1).getId()); list = dao.findFiltered(ViewFilterType.ALL); assertEquals(4, list.size()); assertEquals(first.getId(), list.get(0).getId()); assertEquals(second.getId(), list.get(1).getId()); assertEquals(third.getId(), list.get(2).getId()); assertEquals(fourth.getId(), list.get(3).getId()); }