@Test public void testFindOngoingEventByVessel() { ShipSizeOrTypeEvent event = new ShipSizeOrTypeEvent(); final ArgumentCaptor<String> queryString = ArgumentCaptor.forClass(String.class); context.checking(new Expectations() {{ oneOf(sessionFactory).openSession(); will(returnValue(session)); oneOf(session).createQuery(with(queryString.getMatcher())); will(returnValue(query)); allowing(query).setCacheable(with(any(Boolean.class))); allowing(query).setParameter(with(aNonNull(String.class)), with(aNonNull(Object.class))); allowing(query).setString(with(aNonNull(String.class)), with(aNonNull(String.class))); allowing(query).setInteger(with(aNonNull(String.class)), with(aNonNull(Integer.class))); oneOf(query).list(); oneOf(session).close(); }}); eventRepository.findOngoingEventByVessel(219886000, ShipSizeOrTypeEvent.class); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*TYPE[(]e[)] = [:]clazz.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*vessel.mmsi.*=.*")); context.assertIsSatisfied(); }
@Override public <T extends Event> T findOngoingEventByVessel(int mmsi, Class<T> eventClass) { Session session = getSession(); T event = null; try { Query query = session.createQuery("SELECT e FROM Event e LEFT JOIN e.behaviours b WHERE TYPE(e) = :clazz AND e.state = :state AND b.vessel.mmsi = :mmsi AND e.suppressed=false"); query.setCacheable(true); query.setParameter("clazz", eventClass); query.setString("state", "ONGOING"); query.setInteger("mmsi", mmsi); List events = query.list(); if (events.size() > 0) { if (events.size() > 1) { LOG.warn("More than one (" + events.size() + ") ongoing event of type " + eventClass + "; expected max. 1. Using first."); } event = (T) events.get(0); } } finally { session.close(); LOG.debug("Database session closed: " + session); } return event; }
JpaEventRepository implements EventRepository { @Override public <T extends Event> T findOngoingEventByVessel(int mmsi, Class<T> eventClass) { Session session = getSession(); T event = null; try { Query query = session.createQuery("SELECT e FROM Event e LEFT JOIN e.behaviours b WHERE TYPE(e) = :clazz AND e.state = :state AND b.vessel.mmsi = :mmsi AND e.suppressed=false"); query.setCacheable(true); query.setParameter("clazz", eventClass); query.setString("state", "ONGOING"); query.setInteger("mmsi", mmsi); List events = query.list(); if (events.size() > 0) { if (events.size() > 1) { LOG.warn("More than one (" + events.size() + ") ongoing event of type " + eventClass + "; expected max. 1. Using first."); } event = (T) events.get(0); } } finally { session.close(); LOG.debug("Database session closed: " + session); } return event; } }
JpaEventRepository implements EventRepository { @Override public <T extends Event> T findOngoingEventByVessel(int mmsi, Class<T> eventClass) { Session session = getSession(); T event = null; try { Query query = session.createQuery("SELECT e FROM Event e LEFT JOIN e.behaviours b WHERE TYPE(e) = :clazz AND e.state = :state AND b.vessel.mmsi = :mmsi AND e.suppressed=false"); query.setCacheable(true); query.setParameter("clazz", eventClass); query.setString("state", "ONGOING"); query.setInteger("mmsi", mmsi); List events = query.list(); if (events.size() > 0) { if (events.size() > 1) { LOG.warn("More than one (" + events.size() + ") ongoing event of type " + eventClass + "; expected max. 1. Using first."); } event = (T) events.get(0); } } finally { session.close(); LOG.debug("Database session closed: " + session); } return event; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); }
JpaEventRepository implements EventRepository { @Override public <T extends Event> T findOngoingEventByVessel(int mmsi, Class<T> eventClass) { Session session = getSession(); T event = null; try { Query query = session.createQuery("SELECT e FROM Event e LEFT JOIN e.behaviours b WHERE TYPE(e) = :clazz AND e.state = :state AND b.vessel.mmsi = :mmsi AND e.suppressed=false"); query.setCacheable(true); query.setParameter("clazz", eventClass); query.setString("state", "ONGOING"); query.setInteger("mmsi", mmsi); List events = query.list(); if (events.size() > 0) { if (events.size() > 1) { LOG.warn("More than one (" + events.size() + ") ongoing event of type " + eventClass + "; expected max. 1. Using first."); } event = (T) events.get(0); } } finally { session.close(); LOG.debug("Database session closed: " + session); } return event; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); @Override List<String> getEventTypes(); @Override void save(Event event); @Override Event getEvent(long eventId); @Override List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west); @Override List<Event> findEventsByFromAndTo(Date from, Date to); @Override List<Event> findRecentEvents(int numberOfEvents); @Override T findOngoingEventByVessel(int mmsi, Class<T> eventClass); }
JpaEventRepository implements EventRepository { @Override public <T extends Event> T findOngoingEventByVessel(int mmsi, Class<T> eventClass) { Session session = getSession(); T event = null; try { Query query = session.createQuery("SELECT e FROM Event e LEFT JOIN e.behaviours b WHERE TYPE(e) = :clazz AND e.state = :state AND b.vessel.mmsi = :mmsi AND e.suppressed=false"); query.setCacheable(true); query.setParameter("clazz", eventClass); query.setString("state", "ONGOING"); query.setInteger("mmsi", mmsi); List events = query.list(); if (events.size() > 0) { if (events.size() > 1) { LOG.warn("More than one (" + events.size() + ") ongoing event of type " + eventClass + "; expected max. 1. Using first."); } event = (T) events.get(0); } } finally { session.close(); LOG.debug("Database session closed: " + session); } return event; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); @Override List<String> getEventTypes(); @Override void save(Event event); @Override Event getEvent(long eventId); @Override List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west); @Override List<Event> findEventsByFromAndTo(Date from, Date to); @Override List<Event> findRecentEvents(int numberOfEvents); @Override T findOngoingEventByVessel(int mmsi, Class<T> eventClass);  }