QueryManager implements IQueryManager { @Override public <T extends IMessageUnit, V extends IMessageUnitEntity> List<V> getMessageUnitsInState( Class<T> type, Direction direction, ProcessingState[] states) throws PersistenceException { List<T> jpaResult = null; final EntityManager em = EntityManagerUtil.getEntityManager(); Class jpaEntityClass = JPAEntityHelper.determineJPAClass(type); final String queryString = "SELECT mu " + "FROM " + jpaEntityClass.getSimpleName() + " mu JOIN FETCH mu.states s1 " + "WHERE mu.DIRECTION = :direction " + "AND s1.PROC_STATE_NUM = (SELECT MAX(s2.PROC_STATE_NUM) FROM mu.states s2) " + "AND s1.STATE IN :states " + "ORDER BY mu.MU_TIMESTAMP"; try { em.getTransaction().begin(); jpaResult = em.createQuery(queryString, jpaEntityClass) .setParameter("direction", direction) .setParameter("states", Arrays.asList(states)) .getResultList(); } catch (final Exception e) { throw new PersistenceException("Could not execute query \"getMessageUnitsInState\"", e); } finally { em.getTransaction().commit(); em.close(); } return JPAEntityHelper.wrapInEntity(jpaResult); }  @Override List<V> getMessageUnitsInState(
               Class<T> type, Direction direction, ProcessingState[] states); @Override Collection<IMessageUnitEntity> getMessageUnitsWithId(String messageId, 
    															Direction... direction); @Override Collection<IMessageUnitEntity> getMessageUnitsWithLastStateChangedBefore(Date maxLastChangeDate); @Override List<V> getMessageUnitsForPModesInState(Class<T> type,
                                    Collection<String> pmodeIds, ProcessingState state); @Override void ensureCompletelyLoaded(V messageUnit); static void loadCompletely(MessageUnit jpaMessageUnit); @Override int getNumberOfTransmissions(IUserMessageEntity userMessage); @Override boolean isAlreadyProcessed(final IUserMessage userMessage); List<IMessageUnitEntity> getMessageUnitHistory(final Date start, final int max);  }
@Test public void getMessageUnitsInState() throws PersistenceException { List<IUserMessageEntity> usrMsgOnlyResult = queryManager.getMessageUnitsInState(IUserMessage.class, Direction.OUT, new ProcessingState[] { ProcessingState.SUBMITTED }); assertFalse(Utils.isNullOrEmpty(usrMsgOnlyResult)); assertEquals(1 , usrMsgOnlyResult.size()); assertTrue(usrMsgOnlyResult.get(0) instanceof IUserMessageEntity); List<IMessageUnitEntity> result = queryManager.getMessageUnitsInState(IMessageUnit.class, Direction.OUT, new ProcessingState[] { ProcessingState.DELIVERED }); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(3 , result.size()); assertSorted(result); result = queryManager.getMessageUnitsInState(IMessageUnit.class, Direction.IN, new ProcessingState[] { ProcessingState.FAILURE }); assertTrue(Utils.isNullOrEmpty(result)); result = queryManager.getMessageUnitsInState(IMessageUnit.class, Direction.IN, new ProcessingState[] { ProcessingState.FAILURE, ProcessingState.RECEIVED, ProcessingState.DONE }); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(4 , result.size()); assertSorted(result); }