@Test public void testFindEventsByFromAndToAndTypeAndVesselAndArea() { 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(); }}); Calendar calendar = Calendar.getInstance(); calendar.set(2014, 03, 27, 0, 0, 0); Date from = calendar.getTime(); calendar.set(2014, 03, 27, 14, 12, 10); Date to = calendar.getTime(); eventRepository.findEventsByFromAndToAndTypeAndVesselAndArea(from, to, "SpeedOverGroundEvent", "vessel", 56.1, 12.1, 56.0, 12.0); assertTrue(queryString.getCapturedObject().toString().matches(".*suppressed=false.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*latitude<:north.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*latitude>:south.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*longitude<:east.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*longitude>:west.*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*[(]e.startTime >= :from OR e.endTime >= :from[)] AND [(]e.startTime <= :to OR e.endTime <= :to[)].*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*TYPE[(]e[)] IN [(].*[)].*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*b.vessel.callsign LIKE :vessel OR b.vessel.name LIKE :vessel.*")); context.assertIsSatisfied(); }
@Override public List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west) { Session session = getSession(); boolean usesFrom = false, usesTo = false, usesType = false, usesVessel = false, usesArea = false; List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT DISTINCT e FROM Event e "); if (! StringUtils.isBlank(vessel)) { hql.append("LEFT JOIN e.behaviours AS b "); } if (north != null && east != null && south != null && west != null) { hql.append("LEFT JOIN e.behaviours AS b LEFT JOIN b.trackingPoints AS tp WHERE e.suppressed=false AND latitude<:north AND latitude>:south AND longitude<:east AND longitude>:west AND "); usesArea = true; } else { hql.append("WHERE e.suppressed=false AND "); } if (from != null) { hql.append("(e.startTime >= :from OR e.endTime >= :from) AND "); usesFrom = true; } if (to != null) { hql.append("(e.startTime <= :to OR e.endTime <= :to) AND "); usesTo = true; } if (! StringUtils.isBlank(type)) { hql.append("TYPE(e) IN (:classes) AND "); usesType = true; } if (! StringUtils.isBlank(vessel)) { hql.append("("); hql.append("b.vessel.callsign LIKE :vessel OR "); hql.append("b.vessel.name LIKE :vessel OR "); try { Long vesselAsLong = Long.valueOf(vessel); hql.append("b.vessel.mmsi = :vessel OR "); hql.append("b.vessel.imo = :vessel OR "); } catch (NumberFormatException e) { } hql.replace(hql.length()-3, hql.length(), ")"); if (! vessel.startsWith("%")) { vessel = "%" + vessel; } if (! vessel.endsWith("%")) { vessel = vessel + "%"; } usesVessel = true; } String hqlAsString = hql.toString().trim(); if (hqlAsString.endsWith("AND")) { hqlAsString = hqlAsString.substring(0, hqlAsString.lastIndexOf("AND")); } Query query = session.createQuery(hqlAsString); if (usesArea) { query.setParameter("north", north); query.setParameter("east", east); query.setParameter("south", south); query.setParameter("west", west); } if (usesFrom) { query.setParameter("from", from); } if (usesTo) { query.setParameter("to", to); } if (usesType) { String className = "dk.dma.ais.abnormal.event.db.domain." + type; try { Class clazz = Class.forName(className); query.setParameter("classes", clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class " + className + " not found."); } } if (usesVessel) { query.setParameter("vessel", vessel); } LOG.debug("Query: " + query.toString()); final long t0 = currentTimeMillis(); events = query.list(); final long t1 = currentTimeMillis(); LOG.debug("Found " + events.size() + " matching events in " + (t1-t0) + " msecs."); } finally { session.close(); LOG.debug("Database session closed: " + session); } return events; }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west) { Session session = getSession(); boolean usesFrom = false, usesTo = false, usesType = false, usesVessel = false, usesArea = false; List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT DISTINCT e FROM Event e "); if (! StringUtils.isBlank(vessel)) { hql.append("LEFT JOIN e.behaviours AS b "); } if (north != null && east != null && south != null && west != null) { hql.append("LEFT JOIN e.behaviours AS b LEFT JOIN b.trackingPoints AS tp WHERE e.suppressed=false AND latitude<:north AND latitude>:south AND longitude<:east AND longitude>:west AND "); usesArea = true; } else { hql.append("WHERE e.suppressed=false AND "); } if (from != null) { hql.append("(e.startTime >= :from OR e.endTime >= :from) AND "); usesFrom = true; } if (to != null) { hql.append("(e.startTime <= :to OR e.endTime <= :to) AND "); usesTo = true; } if (! StringUtils.isBlank(type)) { hql.append("TYPE(e) IN (:classes) AND "); usesType = true; } if (! StringUtils.isBlank(vessel)) { hql.append("("); hql.append("b.vessel.callsign LIKE :vessel OR "); hql.append("b.vessel.name LIKE :vessel OR "); try { Long vesselAsLong = Long.valueOf(vessel); hql.append("b.vessel.mmsi = :vessel OR "); hql.append("b.vessel.imo = :vessel OR "); } catch (NumberFormatException e) { } hql.replace(hql.length()-3, hql.length(), ")"); if (! vessel.startsWith("%")) { vessel = "%" + vessel; } if (! vessel.endsWith("%")) { vessel = vessel + "%"; } usesVessel = true; } String hqlAsString = hql.toString().trim(); if (hqlAsString.endsWith("AND")) { hqlAsString = hqlAsString.substring(0, hqlAsString.lastIndexOf("AND")); } Query query = session.createQuery(hqlAsString); if (usesArea) { query.setParameter("north", north); query.setParameter("east", east); query.setParameter("south", south); query.setParameter("west", west); } if (usesFrom) { query.setParameter("from", from); } if (usesTo) { query.setParameter("to", to); } if (usesType) { String className = "dk.dma.ais.abnormal.event.db.domain." + type; try { Class clazz = Class.forName(className); query.setParameter("classes", clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class " + className + " not found."); } } if (usesVessel) { query.setParameter("vessel", vessel); } LOG.debug("Query: " + query.toString()); final long t0 = currentTimeMillis(); events = query.list(); final long t1 = currentTimeMillis(); LOG.debug("Found " + events.size() + " matching events in " + (t1-t0) + " msecs."); } finally { session.close(); LOG.debug("Database session closed: " + session); } return events; } }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west) { Session session = getSession(); boolean usesFrom = false, usesTo = false, usesType = false, usesVessel = false, usesArea = false; List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT DISTINCT e FROM Event e "); if (! StringUtils.isBlank(vessel)) { hql.append("LEFT JOIN e.behaviours AS b "); } if (north != null && east != null && south != null && west != null) { hql.append("LEFT JOIN e.behaviours AS b LEFT JOIN b.trackingPoints AS tp WHERE e.suppressed=false AND latitude<:north AND latitude>:south AND longitude<:east AND longitude>:west AND "); usesArea = true; } else { hql.append("WHERE e.suppressed=false AND "); } if (from != null) { hql.append("(e.startTime >= :from OR e.endTime >= :from) AND "); usesFrom = true; } if (to != null) { hql.append("(e.startTime <= :to OR e.endTime <= :to) AND "); usesTo = true; } if (! StringUtils.isBlank(type)) { hql.append("TYPE(e) IN (:classes) AND "); usesType = true; } if (! StringUtils.isBlank(vessel)) { hql.append("("); hql.append("b.vessel.callsign LIKE :vessel OR "); hql.append("b.vessel.name LIKE :vessel OR "); try { Long vesselAsLong = Long.valueOf(vessel); hql.append("b.vessel.mmsi = :vessel OR "); hql.append("b.vessel.imo = :vessel OR "); } catch (NumberFormatException e) { } hql.replace(hql.length()-3, hql.length(), ")"); if (! vessel.startsWith("%")) { vessel = "%" + vessel; } if (! vessel.endsWith("%")) { vessel = vessel + "%"; } usesVessel = true; } String hqlAsString = hql.toString().trim(); if (hqlAsString.endsWith("AND")) { hqlAsString = hqlAsString.substring(0, hqlAsString.lastIndexOf("AND")); } Query query = session.createQuery(hqlAsString); if (usesArea) { query.setParameter("north", north); query.setParameter("east", east); query.setParameter("south", south); query.setParameter("west", west); } if (usesFrom) { query.setParameter("from", from); } if (usesTo) { query.setParameter("to", to); } if (usesType) { String className = "dk.dma.ais.abnormal.event.db.domain." + type; try { Class clazz = Class.forName(className); query.setParameter("classes", clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class " + className + " not found."); } } if (usesVessel) { query.setParameter("vessel", vessel); } LOG.debug("Query: " + query.toString()); final long t0 = currentTimeMillis(); events = query.list(); final long t1 = currentTimeMillis(); LOG.debug("Found " + events.size() + " matching events in " + (t1-t0) + " msecs."); } finally { session.close(); LOG.debug("Database session closed: " + session); } return events; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west) { Session session = getSession(); boolean usesFrom = false, usesTo = false, usesType = false, usesVessel = false, usesArea = false; List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT DISTINCT e FROM Event e "); if (! StringUtils.isBlank(vessel)) { hql.append("LEFT JOIN e.behaviours AS b "); } if (north != null && east != null && south != null && west != null) { hql.append("LEFT JOIN e.behaviours AS b LEFT JOIN b.trackingPoints AS tp WHERE e.suppressed=false AND latitude<:north AND latitude>:south AND longitude<:east AND longitude>:west AND "); usesArea = true; } else { hql.append("WHERE e.suppressed=false AND "); } if (from != null) { hql.append("(e.startTime >= :from OR e.endTime >= :from) AND "); usesFrom = true; } if (to != null) { hql.append("(e.startTime <= :to OR e.endTime <= :to) AND "); usesTo = true; } if (! StringUtils.isBlank(type)) { hql.append("TYPE(e) IN (:classes) AND "); usesType = true; } if (! StringUtils.isBlank(vessel)) { hql.append("("); hql.append("b.vessel.callsign LIKE :vessel OR "); hql.append("b.vessel.name LIKE :vessel OR "); try { Long vesselAsLong = Long.valueOf(vessel); hql.append("b.vessel.mmsi = :vessel OR "); hql.append("b.vessel.imo = :vessel OR "); } catch (NumberFormatException e) { } hql.replace(hql.length()-3, hql.length(), ")"); if (! vessel.startsWith("%")) { vessel = "%" + vessel; } if (! vessel.endsWith("%")) { vessel = vessel + "%"; } usesVessel = true; } String hqlAsString = hql.toString().trim(); if (hqlAsString.endsWith("AND")) { hqlAsString = hqlAsString.substring(0, hqlAsString.lastIndexOf("AND")); } Query query = session.createQuery(hqlAsString); if (usesArea) { query.setParameter("north", north); query.setParameter("east", east); query.setParameter("south", south); query.setParameter("west", west); } if (usesFrom) { query.setParameter("from", from); } if (usesTo) { query.setParameter("to", to); } if (usesType) { String className = "dk.dma.ais.abnormal.event.db.domain." + type; try { Class clazz = Class.forName(className); query.setParameter("classes", clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class " + className + " not found."); } } if (usesVessel) { query.setParameter("vessel", vessel); } LOG.debug("Query: " + query.toString()); final long t0 = currentTimeMillis(); events = query.list(); final long t1 = currentTimeMillis(); LOG.debug("Found " + events.size() + " matching events in " + (t1-t0) + " msecs."); } finally { session.close(); LOG.debug("Database session closed: " + session); } return events; } @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 List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west) { Session session = getSession(); boolean usesFrom = false, usesTo = false, usesType = false, usesVessel = false, usesArea = false; List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT DISTINCT e FROM Event e "); if (! StringUtils.isBlank(vessel)) { hql.append("LEFT JOIN e.behaviours AS b "); } if (north != null && east != null && south != null && west != null) { hql.append("LEFT JOIN e.behaviours AS b LEFT JOIN b.trackingPoints AS tp WHERE e.suppressed=false AND latitude<:north AND latitude>:south AND longitude<:east AND longitude>:west AND "); usesArea = true; } else { hql.append("WHERE e.suppressed=false AND "); } if (from != null) { hql.append("(e.startTime >= :from OR e.endTime >= :from) AND "); usesFrom = true; } if (to != null) { hql.append("(e.startTime <= :to OR e.endTime <= :to) AND "); usesTo = true; } if (! StringUtils.isBlank(type)) { hql.append("TYPE(e) IN (:classes) AND "); usesType = true; } if (! StringUtils.isBlank(vessel)) { hql.append("("); hql.append("b.vessel.callsign LIKE :vessel OR "); hql.append("b.vessel.name LIKE :vessel OR "); try { Long vesselAsLong = Long.valueOf(vessel); hql.append("b.vessel.mmsi = :vessel OR "); hql.append("b.vessel.imo = :vessel OR "); } catch (NumberFormatException e) { } hql.replace(hql.length()-3, hql.length(), ")"); if (! vessel.startsWith("%")) { vessel = "%" + vessel; } if (! vessel.endsWith("%")) { vessel = vessel + "%"; } usesVessel = true; } String hqlAsString = hql.toString().trim(); if (hqlAsString.endsWith("AND")) { hqlAsString = hqlAsString.substring(0, hqlAsString.lastIndexOf("AND")); } Query query = session.createQuery(hqlAsString); if (usesArea) { query.setParameter("north", north); query.setParameter("east", east); query.setParameter("south", south); query.setParameter("west", west); } if (usesFrom) { query.setParameter("from", from); } if (usesTo) { query.setParameter("to", to); } if (usesType) { String className = "dk.dma.ais.abnormal.event.db.domain." + type; try { Class clazz = Class.forName(className); query.setParameter("classes", clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class " + className + " not found."); } } if (usesVessel) { query.setParameter("vessel", vessel); } LOG.debug("Query: " + query.toString()); final long t0 = currentTimeMillis(); events = query.list(); final long t1 = currentTimeMillis(); LOG.debug("Found " + events.size() + " matching events in " + (t1-t0) + " msecs."); } finally { session.close(); LOG.debug("Database session closed: " + session); } return events; } @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);  }